天空之城
爱上这里~
posts - 0,comments - 0,trackbacks - 0
#include <iostream>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
这道题我提交了三次,第一次提交之后显示部分正确,检查了代码发现输出负号之后没有对数进行处理,即后来判断时-100000也小于1000啊。
改了之后提交第二次,测试结果正确的次数多了,还是部分错误,我又检查了一下,用了几个特殊的数来试,发现100000输出的结果是100,0,因为没有考虑后面的数字可能会发生没有三位但是却没有自动补全0的情况。如果这个时候仍然使用使用C++可能麻烦很多,就想着要利用C语言的printf的输出固定格式,没有达到自动补全0,修改之后提交通过。
下面贴我的代码:
#include <iostream>
using namespace std;
int main(void){
    
int a,b;
    
int sum=0;
    
while(cin>>a>>b){
        sum
=a+b;
        
if(sum<0){
            sum
=-sum;      
            cout
<<"-";
        }
        
if(sum<1000)
        cout
<<sum;
        
else if(sum>=1000&&sum<1000000)
            printf(
"%d,%03d",(sum/1000),sum%1000);
        
else if(sum>=1000000)
            printf(
"%d,%03d,%03d",sum/1000000,((sum%1000000)/1000),sum%1000);
    }
    
return 0;
}

posted on 2014-07-26 10:40 兔兔的天空之城 阅读(99) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理