用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

咕噜船长    -  云代码空间

——

PAT 1001

2018-02-24|549阅||

摘要:PAT 1001

代码如下所示,满分代码,由于这道题目限制了输入在-1000000~1000000之间,因此可以采用手动判断的方式来加入,以实现三级的输出

题目如下:

1001. A+B Format (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.


Output

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input
-1000000 9
Sample Output
-999,991

解答代码如下:
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
string change(long a);
int main()
{
    long a,b;
    while(cin>>a>>b){
        long c=a+b;
        if(c==0){
            cout<<c<<endl;
        }
        else{
            string s=change(c);
            cout<<s<<endl;
        }
    }
    return 0;
}

string change(long a){
    string s;
    long b=a;
    int flag=0;
    while(a!=0){
        char b=(char)(abs(a%10)+48);
        s+=b;
        a=a/10;
        flag++;
        if(flag%3==0&&a!=0){
            s+=',';
        }
    }
    if(b<0){
        s+='-';
    }
    reverse(s.begin(),s.end());
    return s;
}

顶 0踩 0收藏
文章评论
    发表评论

    个人资料

    • 昵称: 咕噜船长
    • 等级: 中级程序员
    • 积分: 215
    • 代码: 0 个
    • 文章: 4 篇
    • 随想: 0 条
    • 访问: 5 次
    • 关注

    人气代码

      最新提问

        站长推荐