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;
}
这辈子不可能刷oj的

include

include

char str[100][100]={“zero”,”one”,”two”,”three”,”four”,”five”,”six”,”seven”,”eight”,”nine”};
int fw(char s[])
{
int j;
for(j=0;j<10;j++){
if(strcmp(str[j],s)==0) return j;
}
}
int main()
{
int x,y;
scanf(“%d”,&x);
getchar();
for(y=0;y<x;y++){
int fw(char s[]);
int k=0,m=0,n=0,w=0;
int i,a=0,b=0;
char aa[100][100]={},bb[100][100]={};
while(1)
{
char c=getchar();
if(c!=’+’){
if(c!=’ ‘){
aa[k][m]=c;
m++;
}
else if(c==’ ‘){
aa[k][m]=’\0’;
k++;m=0;
}
}
else if(c==’+’){
aa[k][m]=’\0’;
break;
}
}
getchar();
for(i=0;i<k;i++){
//printf(“%s”,aa[i]);
a=a10+fw(aa[i]);
//printf(“%d”,a);
}
while(1)
{
char p=getchar();
if(p!=’=’){
if(p!=’ ‘){
bb[n][w]=p;
w++;
}
else if(p==’ ‘){
bb[n][w]=’\0’;
w=0;n++;
}
}
else if(p==’=’){
//bb[n][w]=’\0’;
break;
}
}
getchar();
for(i=0;i<n;i++){
//printf(“%s”,bb[i]);
b=b
10+fw(bb[i]);
//printf(“%d”,b);
}
printf(“case #%d:\n”,y);
printf(“%d\n”,a+b);
}
return 0;
}

茶沫茶沫

include

using namespace std;

int main() {
int n;

cin &gt;&gt; n;
for (int i = 0; i &lt; n; i++) {
    cout &lt;&lt; "case #" &lt;&lt; i &lt;&lt; ':' &lt;&lt; endl;

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

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

    while (cin &gt;&gt; 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 &lt;&lt; res &lt;&lt; endl;

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

}
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;
}
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&lt;2;i++)
{
   for(k=0;k&lt;10;k++)
   {
       if(strcmp(code[i],a[k])==0)
        p=p*10+k;
   }
}

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

for(i=cnt-3;i&lt;cnt-1;i++)
{
    for(k=0;k&lt;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;
}

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