PAT (Advanced Level) Practise 1001 A+B Format (20)
2014-04-08 00:10
489 查看
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
#include<stdio.h>#include<stdlib.h>int main(){ long a,b,c; char *num,*num1; int flag=0; int i=0,j=0; num=calloc(7,sizeof(char)); num1=calloc(7,sizeof(char));
scanf("%ld%ld",&a,&b); c=a+b; if(c<0) { c=-c; flag=1; } do { num[i++]=c%10+'0'; c=c/10; j++; if(j%3==0 && c!=0) num[i++]=','; }while(c!=0); j=0; if(flag==1) num1[j++]='-'; do { num1[j++]=num[--i]; }while(i!=0); printf("%s\n",num1); return 0;}
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
code:
#include<stdio.h>#include<stdlib.h>int main(){ long a,b,c; char *num,*num1; int flag=0; int i=0,j=0; num=calloc(7,sizeof(char)); num1=calloc(7,sizeof(char));
scanf("%ld%ld",&a,&b); c=a+b; if(c<0) { c=-c; flag=1; } do { num[i++]=c%10+'0'; c=c/10; j++; if(j%3==0 && c!=0) num[i++]=','; }while(c!=0); j=0; if(flag==1) num1[j++]='-'; do { num1[j++]=num[--i]; }while(i!=0); printf("%s\n",num1); return 0;}
相关文章推荐
- PAT (Advanced Level) Practise 1001. A+B Format (20)
- PAT (Advanced Level) Practise 1001. A+B Format (20)
- PAT (Advanced Level) Practise - 1001 A+B Format (20)
- PAT-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】
- PAT (Advanced Level) Practise 1001. A+B Format (20)
- PAT (Advanced Level) Practise 1001. A+B Format (20)
- PAT (Advanced Level) Practise 1001. A+B Format (20)
- 浙大PAT (Advanced Level) Practise 1001 A+B Format (20)
- PAT (Advanced Level) Practise 1001 A+B Format (20)
- PAT (Advanced Level) Practise 1001 A+B Format (20)
- PAT-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】
- PAT (Advanced Level) Practise 1001. A+B Format (20)
- PAT (Advanced Level) Practise 1001 A+B Format
- PAT (Advanced Level) Practise:1001. A+B Format
- PAT(Advanced level) 1001 A+B Format(20)
- PAT (Advanced Level) Practise —1001:A+B format
- 【PAT】【Advanced Level】1001. A+B Format (20)
- PAT (Advanced Level) Practise 1001. A+B Format
- 【PAT Advanced Level】1001. A+B Format (20)
- 编程题目:PAT(Advanced Level) Practice 1001. A+B Format (20)