算法250327题目

1114: 4006 A+B问题

题目描述

给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号,隔开。
现在请计算A+B的结果,并以正常形式输出。

输入

输入包含多组数据,每组数据占一行,由两个整数A和B组成(-109 < A,B < 109)。

输出

请计算A+B的结果,并以正常形式输出,每组数据占一行。

样例输入 复制
-234,567,890 123,456,789
1,234 2,345,678
样例输出 复制
-111111101
2346912
#include 
#include
#include
char str1[15];
char str2[15];
using namespace std;
int main(){
    while(scanf("%s%s",str1,str2) != EOF){
        int a = 0;
        int b = 0;
        for(int i = 0;str1[i] != '\0';i++){
            if(str1[i] >= '0' && str1[i] <= '9'){
                a = a * 10 + str1[i] - '0';
            }
        }
        for(int i = 0;str2[i] != '\0';i++){
            if(str2[i] >= '0' && str2[i] <= '9'){
                b = b * 10 + str2[i] - '0';
            }
        }
        if(str1[0] == '-')a = -a;
        if(str2[0] == '-')b = -b;
        cout << a + b << endl;

    }

    return 0;
}

看和说

#include 
#include
#include
char str[1001];
using namespace std;
int main(){
    int num = 0,times = 1;
    int c;
    scanf("%d",&c);
    while(c--){
        scanf("%s",str);
        for(int i = 0;str[i] != '\0';i++){
            if(str[i] == str[i + 1]){
                times++;
            }
            if(str[i] != str[i + 1]){
                num = str[i] - '0';
                cout << times << num;
                times = 1;
            }
        }
        cout << endl;

    }
    return 0;
}

你可能感兴趣的:(算法,c++,数据结构)