欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

codeup之A+B

发布时间:2025/5/22 编程问答 36 如意码农
生活随笔 收集整理的这篇文章主要介绍了 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;
}

总结

以上是生活随笔为你收集整理的codeup之A+B的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。