求助

P1603 斯诺登的密码

sxy714729460 @ 2019-02-11 18:10:09

我在自己的编译器上输入样例Black Obama is two five zero . 得到的答案就是425 但是在下载错误数据后发现这个样例的答案就是错的,想不明白

以下代码

include <iostream>

include <cmath>

include <algorithm>

using namespace std; int main(int argc, const char argv[]) { char alpha[27][20]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","a","both","another","first","second","third"}; int num[27]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,1,1,2,3}; for(int i=0;i<27;i++) num[i]=(num[i]num[i])%100;//把原数字转为平方再模 //输入单词 char a[6][20]; int m=0; while(m<6){ cin>>a[m]; m++; } //判断是否有数字,并将对应数字存入数组ans int ans[6],n=0,flag=1;//ans用来存数字,n表示句子中有几个数字,flag判断一个单词是不是数字 long int x=0;//x表示最后答案的和 for(int i=0;i<6;i++){ for(int j=0;j<27;j++){//单词和字母表逐一比较 flag=1;//判断是否为数字,若是,flag仍为1;若不是,flag=0 for(int k=0;k<20;k++){ if(k==0&&i==0){//第一个字母如果为大写的特判 if(a[0][0]==alpha[j][0]-'a'+'A'||a[0][0]==alpha[j][0]) continue; else { flag=0;//如果第一个字母不对,跳出循环 break; } } else if(a[i][k]!=alpha[j][k]){//找到一个字母不一样就跳出循环 flag=0; break; } } if(flag==1){//查完一个单词后发现该单词代表数字,就把对应数字传到数组ans ans[n++]=num[j]; } } } if(n==0) cout<<"0"<<endl;//如果没有数字,输出0 else { sort(ans,ans+n);//k数字排序 for(int i=0;i<n;i++){ x=x*100+ans[i]; } cout<<x<<endl; }

return 0;

}


by t162 @ 2019-02-11 18:13:05

希望更丰富的展现?使用Markdown


by sxy714729460 @ 2019-02-11 21:54:51

include <iostream>

include <cmath>

include <algorithm>

using namespace std; int main(int argc, const char * argv[]) {

char alpha[27][20]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","a","both","another","first","second","third"};
int num[27]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,1,1,2,3};
for(int i=0;i<27;i++)
    num[i]=(num[i]*num[i])%100;//把原数字转为平方再模
//输入单词
char a[6][20];
int m=0;
while(m<6){
    cin>>a[m];
    m++;
}
//判断是否有数字,并将对应数字存入数组ans
int ans[6],n=0,flag=1;//ans用来存数字,n表示句子中有几个数字,flag判断一个单词是不是数字
long int x=0;//x表示最后答案的和
for(int i=0;i<6;i++){
    for(int j=0;j<27;j++){//单词和字母表逐一比较
        flag=1;//判断是否为数字,若是,flag仍为1;若不是,flag=0
        for(int k=0;k<20;k++){
            if(k==0&&i==0){//第一个字母如果为大写的特判
                if(a[0][0]==alpha[j][0]-'a'+'A'||a[0][0]==alpha[j][0]) continue;
                else {
                    flag=0;//如果第一个字母不对,跳出循环
                    break;
                }
            }
            else if(a[i][k]!=alpha[j][k]){//找到一个字母不一样就跳出循环
                flag=0;
                break;
            }
        }
        if(flag==1){//查完一个单词后发现该单词代表数字,就把对应数字传到数组ans
            ans[n++]=num[j];
        }
    }
}
if(n==0) cout<<"0"<<endl;//如果没有数字,输出0
else {
    sort(ans,ans+n);//k数字排序
    for(int i=0;i<n;i++){
        x=x*100+ans[i];
    }
    cout<<x<<endl;
}

return 0;

}


|