zoukankan      html  css  js  c++  java
  • PAT甲级1001.A+B Format(20)

    ---恢复内容开始---

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400

    对于该题目,主要在于其输出格式方面,因此考虑到字符数组存储结果,再输出

    #include<iostream>
    #include<cstdlib>
    using namespace std;
    int main()
    {
        int A, B, C;
        while (cin >> A >> B)
        {
            int flag = 1;//标志结果的正负
            C = A + B;
            if (C < 0)
            {
                flag = 0;
                C = -C;//将其转换为正数,方便后面计算
            }
            int length = -1;
            char a[10];
            int i = 0;
            while (true)
            {
                a[++length] = C % 10 + '0';
                i++;
                C = C / 10;
                if (C == 0)
                    break;
                if (i % 3 == 0)
                    a[++length] = ',';
            }
            if (flag == 0)
                cout << "-";
            while (length >= 0)
            {
                cout << a[length--];
            }
            cout << endl;
        }
        system("pause");
        return 0;
    }

    由于计算出的结果转换成的形式属于栈的特点,所以使用栈存储输出结果会比较简单些

    网上代码:

    #include<cstdio>
    #include<queue>
    #include<stack>
    #define QWERTY
    using namespace std;
    int main()
    {
        long a,b,sum;
        scanf("%ld%ld",&a,&b);
        sum=a+b;
        long st=sum;
        int i=0,c1,c2;
        
        if(st<1000&&st>-1000)
        {
            printf("%ld
    ",st);
            return 0;
        }
        if(st<0)
        {
            printf("-");
            st=-st;
        }
        stack<int> sta;
        while(st>0)
        {
            i++;
            sta.push(st%10);
            st=st/10;
        }
    #ifdef QWERTY
        while(!sta.empty())
        {
            i--;
            printf("%d",sta.top());
            if(i!=0&&i%3==0)
                printf(",");
            sta.pop();
     
        }
    #endif
        printf("
    ");
        return 0;
    }
  • 相关阅读:
    009-Python-面向对象
    008-Python-模块
    007-Python函数-装饰器
    006-Python函数
    005-Python字典
    003-python列表
    PyCharm之python书写规范--消去提示波浪线
    001-python基础
    Java基础总结(一)
    High ASCII字符从bat文件到dos控制台的转化问题
  • 原文地址:https://www.cnblogs.com/denghui666/p/9368231.html
Copyright © 2011-2022 走看看