pat 1001 A+B Format
2014-10-24 09:51
204 查看
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
Sample Output
-999,991
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 <cstring> #include <stdio.h> #include <stdlib.h> using namespace std; int main() { int a,b; scanf("%d%d",&a,&b); int sum=a+b; char result[15]; int p=0; if(sum<0) { //strcat(result,"-"); result[p]='-'; p++; sum=sum*(-1); } char str[15]; //itoa(sum,str,10); sprintf(str,"%d",sum); int len=strlen(str); int total=len+len/3; int re=len%3; for(int i=0;i<re;i++) { result[p]=str[i]; p++; } for(int i=re;i<len;i++) { if(i>0 && (i-re)%3==0) { result[p]=','; p++; } result[p]=str[i]; p++; } result[p]='\0'; //strcat(result,str); printf("%s\n",result); return 0; }
相关文章推荐
- ( PAT )1001. A+B Format (20)
- pat甲级1001. A+B Format (20)
- [PAT-甲级]1001.A+B Format
- PAT 1001. A+B Format (20)
- Pat 1001. A+B format
- PAT-A 1001. A+B Format (20)
- 【PAT甲级】1001. A+B Format (20)
- PAT-A 1001. A+B Format (20)
- [PAT]1001.A+B Format (20)
- PAT Advanced Level 1001:A+B Format
- PAT - 甲级 - 1001. A+B Format (20)(字符串处理)
- PAT (Advanced Level) Practise —1001:A+B format
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT-A 1001. A+B Format (20)
- PAT1001 A+B Format
- PAT1001---A+B Formatb
- PAT (Advanced Level) Practise 1001 A+B Format
- PAT甲级-1001. A+B Format (20)逗号分隔
- PAT 1001. A+B Format (20)