codeup之A+B
Description
给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。
现在请计算A+B的结果,并以正常形式输出。
Input
输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。
Output
请计算A+B的结果,并以正常形式输出,每组数据占一行。
Sample Input Copy
-234,567,890 123,456,789
1,234 2,345,678
Sample Output Copy
-111111101
2346912
idea
- 关键:字符串转数字,借用sscanf()
char str[100];
int n;
sscanf(str, "%d", &n);//字符串str中的内容以"%d"的格式写到n中
sprintf(str, "%d", &n);//整数n中的内容以"%d"的格式写到字符串str中
solution
#include <cstdio>
#include <cstring>
void change(char s[]){
int j = 0;
char s1[strlen(s)] = {0};
for(int i = 0; i < strlen(s); i++){
if(s[i] != ','){
s1[j++] = s[i];
}
}
for(int i = 0; i < strlen(s); i++)
s[i] = s1[i];
}
int main(){
long long a, b;
char s1[20], s2[20];
int temp;
while(scanf("%s %s", s1, s2) != EOF){
change(s1);
change(s2);
sscanf(s1, "%lld", &a);
sscanf(s2, "%lld", &b);
printf("%lld\n", a + b);
}
return 0;
}