码迷,mamicode.com
首页 > 其他好文 > 详细

PAT-甲级-1001-A+B Format

时间:2018-10-12 01:21:55      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:form   where   contains   std   put   ace   math   sample   nbsp   

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

标签:form   where   contains   std   put   ace   math   sample   nbsp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!