codeup之A+B
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【HUST】网安|操作系统实验|实验一
- 下一篇: C#程序的内存缓存