3186. A+B

CCXXXI_

补个更简单(懒)的写法

#include <bits/stdc++.h>
using namespace std;

vector<string>fuck{ "zero","one","two","three","four","five","six","seven","eight","nine" };
int trans(string s)
{
    return find(fuck.begin(), fuck.end(), s) - fuck.begin();
}
int main()
{
    int T; cin >> T;
    for (int I = 0; I < T; I++)
    {
        string s;
        int a = 0, b = 0;
        while (1)
        {
            cin >> s;
            if (s == "+")break;
            a = a * 10 + trans(s);
        }
        while (1)
        {
            cin >> s;
            if (s == "=")break;
            b = b * 10 + trans(s);
        }
        printf("case #%d:\n%d\n", I, a + b);
    }
    return 0;
}
徐摆渡

转换为大数加法

#include <bits/stdc++.h>
using namespace std;

map<string, int> mm;
int t, cnt;
char ch;

void ini()
{
    mm["zero"]=0, mm["one"]=1, mm["two"]=2;
    mm["three"]=3, mm["four"]=4, mm["five"]=5;
    mm["six"]=6, mm["seven"]=7, mm["eight"]=8, mm["nine"]=9;
}

string add(string s1, string s2)
{
    if(s1.length()<s2.length()) swap(s1, s2);
    int l1=s1.length(), l2=s2.length();
    int a[200]={0}, b[200]={0};
    for(int i=1;i<=l1;++i) a[i]=s1[l1-i]-'0';
    for(int i=1;i<=l2;++i) b[i]=s2[l2-i]-'0';
    for(int i=1;i<=l1;++i) {
        a[i]+=b[i];
        //cout<<a[i]<<endl;
        a[i+1]+=a[i]/10;a[i]%=10;
    }
    if(a[l1+1]) l1++;
    string ans;
    int l=l1;
    for(int i=l1;i>=1;--i) {
        if(!a[i]) l--;
        else break;
    }
    for(int i=l;i>=1;--i) ans+=a[i]+'0';
    return ans;
}
int main()
{
    ini();
    cin>>t;getchar();
    while(t--){
        string s1, s2, temp;
        int flag=0;
        char t;
        while((ch=getchar())!='='){
            if(ch>='a'&&ch<='z') temp+=ch;
            else if(ch==' '&&!flag) {
                s1+=mm[temp]+'0';
                temp="";
            }
            else if(ch=='+') flag=1;
            else if(ch==' '&&flag){
                if(t=='+') {t=ch;continue;}
                s2+=mm[temp]+'0';
                temp="";
            }
            t=ch;
         }
         //cout<<s1<<' '<<s2<<endl;
         printf("case #%d:\n",cnt++);
         cout<<add(s1, s2)<<endl;
    }
    return 0;
}
茶沫茶沫

include

using namespace std;

int main() {
int n;

cin >> n;
for (int i = 0; i < n; i++) {
    cout << "case #" << i << ':' << endl;

    /*--------------------------------------------*/

    int k = 0;
    int res = 0;
    int cal[2] = { 0 };
    char a[100];

    while (cin >> a) {

        string temp = "";temp = a;
        if (a[0] == '+') { k++;continue; }
        if (a[0] == '=') break;
        cal[k] *= 10;

        if (temp == "zero") cal[k] += 0;
        else if (temp == "one") cal[k] += 1;
        else if (temp == "two") cal[k] += 2;
        else if (temp == "three") cal[k] += 3;
        else if (temp == "four") cal[k] += 4;
        else if (temp == "five") cal[k] += 5;
        else if (temp == "six") cal[k] += 6;
        else if (temp == "seven") cal[k] += 7;
        else if (temp == "eight") cal[k] += 8;
        else if (temp == "nine") cal[k] += 9;
        else cal[k] += 100;

    }

    /*--------------------------------------------*/
    res = cal[0] + cal[1];
    //Output
    cout << res << endl;

    /*--------------------------------------------*/

}
return 0;

}

Luckywei233

include

int strlen(char p){
int i=0;
while(p[i]!=’\0’){
i++;
}
return i;
}
int equals(char
str1,char str2){
if(strlen(str1)!=strlen(str2))return 0;
else{
int len=strlen(str1);
for(int i=0;i<=len-1;i++){
if(str1[i]!=str2[i])return 0;
}
return 1;
}
}
int getnum(char
p){
int len=strlen(p);
int bigger=0;
int i;
for(i=0;i<=len-1;i++){
if(p[i]==’ ‘){
bigger=1;
break;
}
}
if(!bigger){
if(equals(p,”zero”))return 0;
if(equals(p,”one”))return 1;
if(equals(p,”two”))return 2;
if(equals(p,”three”))return 3;
if(equals(p,”four”))return 4;
if(equals(p,”five”))return 5;
if(equals(p,”six”))return 6;
if(equals(p,”seven”))return 7;
if(equals(p,”eight”))return 8;
if(equals(p,”nine”))return 9;
}else{
char substr1[10],substr2[10];
int j;
for(j=0;j<i;j++){
substr1[j]=p[j];
}
substr1[j]=’\0’;
for(j=i+1;j<=len-1;j++){
substr2[j-i-1]=p[j];
}
substr2[j-i-1]=’\0’;
return getnum(substr1)10+getnum(substr2);
}
}
void getstrnum(char
num1,char *num2){
int i=0;
char temp;
while((temp=getchar())!=’+’){
num1[i]=temp;
i++;
}
num1[i-1]=’\0’;
i=0;
getchar();
while((temp=getchar())!=’=’){
num2[i]=temp;
i++;
}
num2[i-1]=’\0’;
getchar();
}
int main(){
char num1[20],num2[20];
int N;
scanf(“%d”,&N);
getchar();
for(int i=0;i<=N-1;i++){
getstrnum(num1,num2);
printf(“case #%d:\n%d\n”,i,getnum(num1)+getnum(num2));
}
return 0;
}

三七茧茧

先把字符串中所有的单词分离出来,放在二维数组中。
前两个单词放在二维数组的第一个和第二个,后两个单词放在二维数组的倒数第三个和倒数第二个里面;
然后再分别计算出所代表的数字相加即可。
、、、

include

include

include

include

include

void solve()
{
char a[10][10]={“zero”,”one”,”two”,”three”,”four”,”five”,”six”,”seven”,”eight”,”nine”};
int p=0,q=0;
char str[35];
char code[7][7];
memset(code,0,sizeof(code));
gets(str);
int cnt=0, i, j=0,k;
while(str[j])
{
k=0;
while(str[j]>=’a’&&str[j]<=’z’)
code[cnt][k++]=str[j++];
code[cnt][k]=’\0’;
cnt++;

    j++;
}

for(i=0;i<2;i++)
{
   for(k=0;k<10;k++)
   {
       if(strcmp(code[i],a[k])==0)
        p=p*10+k;
   }
}

//printf("%d\n",p);

for(i=cnt-3;i<cnt-1;i++)
{
    for(k=0;k<10;k++)
   {
       if(strcmp(code[i],a[k])==0)
        q=q*10+k;
   }
}

//printf("%d\n",q);

printf("%d\n",p+q);

}

int main()
{
int j, t;
//init();
scanf(“%d”,&t);
getchar();
for(j=0;j<t;j++)
{
printf(“case #%d:\n”,j);
solve();
}
return 0;
}

10152130114

include

include

include

int main()
{
char a[10][10] = {“zero”, “one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”, “nine”};
char str[100];
int i,T=0,j, y = 0, z = 0, sum = 0, b = 0, d = 0, e = 0;
scanf(“%d”,&T);
for(i=0;i<T;i++)
{
y = 0;
z = 0;
sum = 0;
b = 0;
d = 0;
e = 0;

    while(b == 0)
    {
        scanf("%s", str);

        for(j = 0; j < 10; j++)
        {
            if(strcmp(str, a[j]) == 0)
            {
                d = j;
                y = y * 10 + d;
            }

            if(strcmp(str, "+") == 0)
            {
                b++;
                break;
            }
        }
    }

    while(b == 1)
    {
        scanf("%s", str);

        for(j = 0; j < 10; j++)
        {
            if(strcmp(str, a[j]) == 0)
            {
                e = j;
                z = z * 10 + e;
            }

            if(strcmp(str, "=") == 0)
                break;
        }

        if(strcmp(str, "=") == 0)
            break;
    }

    if(y == 0 && z == 0)
        sum=0;
    else sum = y + z;
    printf("case #%d:\n%d\n",i,sum);
}
return 0;

}

你当前正在回复 博客/题目
存在问题!