首页 > 其他 > 详细

PAT-甲级-1001-A+B Format

时间:2018-10-12 00:35:23      阅读:175      评论:0      收藏:0      [点我收藏+]

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 Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where ?. The numbers are separated by a space.

Output Specification:

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



分析:将a+b存为字符数组,然后每三位分隔。



 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define N 10
 5 
 6 
 7 int main(){
 8   int a,b,c;
 9   char s[N];
10   scanf("%d%d",&a,&b);
11   c=a+b;
12   if(c<0){
13     c=-c;
14     printf("-");
15   }
16   sprintf(s,"%d",c);
17   for(int i=0; s[i]!=\0; i++){
18     printf("%c",s[i]);
19     if ((strlen(s)-i-1)%3 == 0 && i!=(strlen(s)-1))
20       printf(",");
21   }
22   return 0;
23 }

 

 



PAT-甲级-1001-A+B Format

原文:https://www.cnblogs.com/tenjl-exv/p/9775853.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!