算法刷题记录

http://acm.hdu.edu.cn/showproblem.php?pid=1094

#include <iostream>
#include <vector>

int main()
{
    using namespace std;
    
    vector<int> vecrow;
    vector<int> vecout;
    
    while (char c = getchar())
    {
        if (c == '\n')  /*行末 或者 空行回车结束*/
        {
            if (vecrow.size() == 0)
                break;
            else
            {
                int sum = 0;
                for (auto it = vecrow.begin(); it != vecrow.end(); it++)
                {
                    sum += *it;
                }
                vecout.push_back(sum);
                vecrow.clear();
            }
        }
        else
        {
            ungetc(c,stdin);  /*回退读取*/
            int fornum;
            cin >> fornum;
            int rowele;
            for (int i = 0; i < fornum; i++)
            {
                cin >> rowele;  /*最后一次 for 循环结束 ,仍剩余行末的换行符在 stdin */
                vecrow.push_back(rowele);
            }
        }
    }
    
    if (vecout.size() != 0)
    {
        for (auto it = vecout.begin(); it != vecout.end(); it++)
        {
            cout << *it << endl;
        }

    }
    
    return 0;
}

 https://acm.hdu.edu.cn/showproblem.php?pid=1095

 只修改上面的 if 对应的 else :

else
        {
            if (c != ' ')
            {
                ungetc(c, stdin);
                int rowele;
                cin >> rowele;
                vecrow.push_back(rowele);
            }
            /*ungetc(c,stdin);
            int fornum;
            cin >> fornum;
            int rowele;
            for (int i = 0; i < fornum; i++)
            {
                cin >> rowele;
                vecrow.push_back(rowele);
            }*/
        }

https://acm.hdu.edu.cn/showproblem.php?pid=1096

    int rownum;
    vector<int> vecrow;
    vector<int> vecout;
    char c = getchar();

    if ( c ==  '\n')
        return 0;
    else
    {
        ungetc(c, stdin);
        cin >> rownum;
        if (rownum > 0)
        {
            for (int i = 0; i < rownum; i++)
            {
                int rowhead;
                cin >> rowhead;
                if (rowhead > 0)
                {
                    for (int j = 0; j < rowhead; j++)
                    {
                        int rowele;
                        cin >> rowele;
                        vecrow.push_back(rowele);
                    }
                    int sum = 0;
                    for (auto it = vecrow.begin(); it != vecrow.end(); it++)
                    {
                        sum += *it;
                    }
                    vecout.push_back(sum);
                    vecrow.clear();
                }
                else
                {
                    vecout.push_back(0);
                }


            }
            /*打印*/
            for (auto it = vecout.begin(); it != vecout.end(); it++)
            {
                cout << *it << endl;
            }
            
        }
    }

https://acm.hdu.edu.cn/showproblem.php?pid=2000

#include <iostream>
#include <list>
#include <vector>
#include <algorithm>

int main()
{
    using namespace std;

    list<char> lcRow;
    vector<char> vcOut;

    while (char c = getchar())
    {
        if (c == '\n')
        {
            if (lcRow.size() == 0)
                break;
            else
            {
                lcRow.sort();  /*排序*/
                for (list<char>::iterator it = lcRow.begin(); it != lcRow.end(); it++)
                {
                    vcOut.push_back(*it);
                    vcOut.push_back(' ');
                }
                vcOut[vcOut.size() - 1] = '\n';
                lcRow.clear();
            }
        }
        else
        {
            lcRow.push_back(c);
        }
    }

    for (vector<char>::iterator it = vcOut.begin(); it != vcOut.end(); it++)
    {
        cout << *it;
    }

}

https://acm.hdu.edu.cn/showproblem.php?pid=2001

输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。 对于每组输入数据,输出一行,结果保留两位小数。

#include <iostream>
#include <vector>
#include <math.h>

int main( )
{
    using namespace std;

    vector<float> vcLocate;
    vector<float> vcOut;

    while (char c = getchar())
    {
        if (c == '\n')
        {
            if (vcLocate.size() == 0)
                break;
            else //计算一组坐标
            {
                float span = sqrt( pow( (vcLocate[0] - vcLocate[2] ), 2) + pow((vcLocate[1] - vcLocate[3]),2) );
                vcOut.push_back(span);
                vcLocate.clear();
            }
        }
        else
        {
            if (c != ' ')
            {
                ungetc(c, stdin);
                float element;
                cin >> element;
                vcLocate.push_back(element);
            } 
        }
    }

    cout.setf(ios::fixed);
    cout.precision(2);

    for (auto it = vcOut.begin(); it != vcOut.end(); it++)
    {
        cout  << *it << endl;
    }

    return 0;
}

https://acm.hdu.edu.cn/showproblem.php?pid=2003

#include <iostream>
#include <list>
#include <vector>
#include <math.h>
#include <algorithm>

int main()
{
    using namespace std;

    vector<float> vcout;
    float num;
    char c = getchar();
    if (c == '\n')
        return 0;
    else
        ungetc(c , stdin);

    while (cin >> num)
    {
        vcout.push_back(num);
        c = getchar(); //读取每行末尾的 换行符

        c = getchar();
        if (c == '\n')  // 连续两个换行符 表示结束, 打印结果。
        {
            for (auto it = vcout.begin(); it != vcout.end(); it++)
            {
                cout << fabs(*it) << endl;
            }
        }
        else
            ungetc(c, stdin);

    }

    return 0;
}

https://acm.hdu.edu.cn/showproblem.php?pid=2004

int main()
{
    using namespace std;

    vector<int> vcscore;
    char c = getchar();
    if (c == '\n')
        return 0;
    else
        ungetc(c , stdin);

    int num;
    while (cin >> num)
    {
        vcscore.push_back(num);
        c = getchar(); //读取每行末尾的 换行符

        c = getchar();
        if (c == '\n')  // 连续两个换行符 表示结束, 打印结果。
        {
            for (auto it = vcscore.begin(); it != vcscore.end(); it++)
            {

                if (90 <= *it && *it <= 100)
                    cout << 'A' << endl;
                else if (80 <= *it && *it <= 89)
                    cout << 'B' << endl;
                else if (70 <= *it && *it <= 79)
                    cout << 'C' << endl;
                else if (60 <= *it && *it <= 69)
                    cout << 'D' << endl;
                else if (0 <= *it && *it <= 59)
                    cout << 'E' << endl;
                else
                    cout << "Score is error!" << endl;
            }
        }
        else
            ungetc(c, stdin);

    }

    return 0;
}

 输入 年月日 yy/mm/dd , 计算 dd 是当年第几天 https://acm.hdu.edu.cn/showproblem.php?pid=2005

#include <iostream>
#include <vector>

int main()
{
    using namespace std;

    vector<int> vcdate;
    vector<int> vcout;
    int yy, mm, dd;
    int msd[12] = { 31,28, 31,30, 31,30,31,31,30,31,30,31};
    char c = getchar();
    if (c == '\n')
        return 0;
    else
        ungetc(c, stdin);

    while (cin >> yy>>c>>mm>>c>>dd)
    {
        vcdate.push_back(yy);
        vcdate.push_back(mm);
        vcdate.push_back(dd);
        
        yy % 4 == 0 ? msd[1] = 29 : msd[1] = 28;

        int daysum = 0;
        if (mm >= 2)
        {
            for (int i = 0; i < mm - 1; i++)
            {
                daysum += msd[i];
            }
        }
        daysum += dd;
        vcout.push_back(daysum);
        vcdate.clear();

        c = getchar(); //行末换行符
        c = getchar();
        if (c == '\n') //打印
        {
            for (auto it = vcout.begin(); it != vcout.end(); it++)
            {
                cout << *it << endl;
            }
            //return 0;
        }
        else
            ungetc(c, stdin);

    }

    return 0;
}

https://acm.hdu.edu.cn/showproblem.php?pid=2006

 

相比 http://acm.hdu.edu.cn/showproblem.php?pid=1094 只改:

            if (vecrow.size() == 0)
                break;
            else
            {
                int sum = 1;
                int flag = 0;
                for (auto it = vecrow.begin(); it != vecrow.end(); it++)
                {
                    if (*it % 2 != 0) // 奇数
                    {
                        sum *= *it;
                        flag = 1; // 标记有 奇数
                    }
                }
                flag ? vecout.push_back(sum) : vecout.push_back(0); // 无奇数行 记为 0
                vecrow.clear();
            }

的 2010 水仙花数 ( 由2009题代码修改得到)

#include <iostream>
#include <vector>
#include <list>

int main()
{
    using namespace std;

    vector<int> vecrow;
    vector<int> vecout;
    list<vector<int>> lviList;

    while (char c = getchar())
    {
        if (c == '\n')  /*行末 或者 空行回车结束*/
        {
            if (vecrow.size() != 2) //限定每行两个 数
                break;
            else
            {
                if (vecrow[0] <= vecrow[1] && 100<=vecrow[0] && vecrow[1]<=999)
                {
                    for (int cerinum = vecrow[0]; cerinum <= vecrow[1]; cerinum++)
                    {
                        int a = cerinum / 100;
                        int b = cerinum / 10 % 10;
                        int c = cerinum % 10;
                        //cout << "a= "<<a << "b= "<<b <<"c= "<< c << endl;

                        if (cerinum == (a * a * a + b * b * b + c * c * c))
                            vecout.push_back(cerinum);
                    }
                    if (vecout.size() == 0)
                    {
                        vecout.push_back(0);
                    }
                        
                }
                else
                {
                    cout << "输入不规范,记为0"<<endl;
                    vecout.push_back(0);
                }
                lviList.push_back(vecout);
                vecout.clear();
                vecrow.clear();
                /*int sum = vecrow[0];
                int tmpsqrt = vecrow[0];
                for (int i = 0; i < vecrow[1] -1; i++)
                {
                    tmpsqrt = sqrt(tmpsqrt);
                    sum += tmpsqrt;
                }
                vecout.push_back(sum);*/
                //vecrow.clear();
            }
        }
        else
        {
            if (c != ' ')
            {
                ungetc(c, stdin);
                int rowele;
                cin >> rowele;
                vecrow.push_back(rowele);
            }
        }
    }

    if (lviList.size() != 0)
    {    
        //cout.setf(ios::fixed);
        //cout.precision(2);
        for (auto oit = lviList.begin(); oit != lviList.end(); oit++ )
        {
            for (auto it = (*oit).begin(); it != (*oit).end(); it++)
            {
                if ( *it < 100 || *it > 999)
                    cout << "no " ;
                else
                    cout << *it << ' ';
            }
            cout << endl;
        }

    }

    return 0;
}
View Code

多项式求和  1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ......

#include <iostream>
#include <vector>
#include <list>
using namespace std;

int main()
{
    int elenum;
    cout << "input elements number:\n";
    cin >> elenum;
    if (elenum <= 0)
        return 0;

    float element;
    vector<float> vecout;

    for (int i = 0; i < elenum; i++)
    {
        cin >> element;
        if (element > 0)
        {
            float sum = 0;
            int pre=1;
            for (float j = 1; j <= element; j++)
            {
                sum += pre*1 / j;
                pre *= -1;
            }
            vecout.push_back(sum);
        }
        else
        {
            vecout.push_back(0);
        }

    }
    for (auto it = vecout.begin(); it != vecout.end(); it++)
    {
        cout << *it << endl;
    }

    return 0;
}
View Code

 

posted @ 2023-03-18 18:48  星云体  阅读(17)  评论(0编辑  收藏  举报