问题 E: Jack的A+B
问题 E: Jack的A+B
时间限制: 1 Sec 内存限制: 128 MB提交: 1996 解决: 601
[提交] [状态] [命题人:jsu_admin]
题目描述
现在有整数a,b,请按西方数字数量级方式输出a+b
输入
题目有多组测试数据
每组输入两个整数a,b
(0<=a,b<=10000000)
每组输入两个整数a,b
(0<=a,b<=10000000)
输出
输出西方数字数量级的a+b
样例输入 Copy
999 1
36 30
100000 100
样例输出 Copy
1,000
66
100,100
提示
输出的数从最低位起,每三位用逗号隔开
倒着模拟题意即可,每三位输出一个逗号,注意和为 0 的情况
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 void int2str(int n, char *str) { 5 char buf[10] = ""; 6 int i = 0; 7 int len = 0; 8 int temp = n < 0 ? -n: n; // temp为n的绝对值 9 10 if (str == NULL) 11 { 12 return; 13 } 14 while(temp) 15 { 16 buf[i++] = (temp % 10) + '0'; //把temp的每一位上的数存入buf 17 temp = temp / 10; 18 } 19 20 len = n < 0 ? ++i: i; //如果n是负数,则多需要一位来存储负号 21 str[i] = 0; //末尾是结束符0 22 while(1) 23 { 24 i--; 25 if (buf[len-i-1] ==0) 26 { 27 break; 28 } 29 str[i] = buf[len-i-1]; //把buf数组里的字符拷到字符串 30 } 31 if (i == 0 ) 32 { 33 str[i] = '-'; //如果是负数,添加一个负号 34 } 35 } 36 37 int main(){ 38 int a,b; 39 int sum; 40 char num[1000]; 41 while(scanf("%d%d",&a,&b)!=EOF){ 42 memset(num,0,sizeof(num)); 43 sum = a+b; 44 if(sum ==0) 45 { 46 printf("0\n"); 47 continue; 48 } 49 int k =0; 50 int2str(sum,num); 51 //printf("%s",num); 52 int h = strlen(num)%3; 53 if(h!=0){ 54 55 for(int i= 0;i<h;i++) 56 { 57 printf("%c",num[i]); 58 } 59 if(strlen(num)>3) 60 printf(","); 61 } 62 // int k = 0; 63 for(int i = h;i<strlen(num);i++) 64 { 65 66 printf("%c",num[i]); 67 k++; 68 if(k%3==0&&i!=strlen(num)-1&&k!=strlen(num)){ 69 printf(","); 70 } 71 } 72 printf("\n"); 73 } 74 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 *@Author: STZG 3 *@Language: C++ 4 */ 5 #include <bits/stdc++.h> 6 #include<iostream> 7 #include<algorithm> 8 #include<cstdlib> 9 #include<cstring> 10 #include<cstdio> 11 #include<string> 12 #include<vector> 13 #include<bitset> 14 #include<queue> 15 #include<deque> 16 #include<stack> 17 #include<cmath> 18 #include<list> 19 #include<map> 20 #include<set> 21 //#define DEBUG 22 #define RI register int 23 using namespace std; 24 typedef long long ll; 25 //typedef __int128 lll; 26 const int N=10000; 27 const int MOD=1e9+7; 28 const double PI = acos(-1.0); 29 const double EXP = 1E-8; 30 const int INF = 0x3f3f3f3f; 31 int t,n,m,k,q; 32 33 int main() 34 { 35 #ifdef DEBUG 36 freopen("input.in", "r", stdin); 37 //freopen("output.out", "w", stdout); 38 #endif 39 while(scanf("%d%d",&n,&m)!=EOF){ 40 t=n+m; 41 string ans; 42 k=0; 43 while(t){ 44 k++; 45 q=t%10; 46 ans=(char)('0'+q)+ans; 47 t/=10; 48 if(t&&k>=3){ 49 k=0; 50 ans=','+ans; 51 } 52 } 53 if(n+m<0) 54 ans='-'+ans; 55 if(n+m==0) 56 ans='0'+ans; 57 cout<<ans<<endl; 58 } 59 60 //cout << "Hello world!" << endl; 61 return 0; 62 }