求解strcmp的用法?这样不对吗?!!!

P1603 斯诺登的密码

许凉城 @ 2018-10-11 21:53:40

#include <bits/stdc++.h>
using namespace std;
string c[26]={"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 d[30]={0,1,4,9,16,25,36,49,64,81,00,21,44,69,96,25,56,89,24,61,0,1,4,1,1,4,9};
int main()
{
    string a;
    int b[10]={0},k=0;
    for (int i=1;i<=6;i++)
    {
        scanf("%s",&a);
        for(int j=1;j<=26;j++)
        {
            if(strcmp(a,c[j])==0)//strcmp(s1,s2);如果他们相同,返回0
            {
                b[++k]=d[j];//用数组存储
                break;//立即停止寻找
            }
        }
    }
    for (int i=1;i<=k;i++)
    {
        b[i]=b[i]*b[i]%100;
    }
    sort(b+1,b+1+k);
    cout<<b[1];
    for (int i=2;i<=k;i++)
    {
        if (b[i]<10)
        {
            cout<<"0"<<b[i];
        }
        else
        {
            cout<<b[i];
        }
    }
    return 0;
}

by tylon2006 @ 2018-10-12 21:05:19

@许凉城 请用cin

字符串用scanf会凉

XP杀


by 许凉城 @ 2018-10-12 21:15:10

@tylon2006可是还是不行诶?(超级感谢你!!!)


by tylon2006 @ 2018-10-12 21:23:12

数组下标越界


by tylon2006 @ 2018-10-12 21:23:24

@许凉城


by tylon2006 @ 2018-10-12 21:23:51

c[26]指c[0]~c[25]


by tylon2006 @ 2018-10-12 21:24:35

for(int j=0;j<=25;j++)
        {
            if(a==c[j])
            {
                b[++k]=d[j];//用数组存储
                break;//立即停止寻找
            }
        }

by tylon2006 @ 2018-10-12 21:27:28

而且算法似乎有问题


by 许凉城 @ 2018-10-12 21:44:29

@tylon2006 感谢你感谢你感谢你!!!终于过了!!!

#include <bits/stdc++.h>
using namespace std;
string c[26]={"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 d[30]={1,4,9,16,25,36,49,64,81,0,21,44,69,96,25,56,89,24,61,0,1,4,1,1,4,9};
int main()
{
    string a;
    int b[10]={0},k=0;
    for (int i=1;i<=6;i++)
    {
        cin>>a;
        for(int j=0;j<=25;j++)
        {
            if(a==c[j])
            {
                b[++k]=d[j];
                break;
            }
        }
    }
    sort(b+1,b+1+k);
    cout<<b[1];
    for (int i=2;i<=k;i++)
    {
        if (b[i]<10)
        {
            cout<<"0"<<b[i];
        }
        else
        {
            cout<<b[i];
        }
    }
    return 0;
}

by _UUQ_ @ 2018-11-02 20:34:05

弱弱问一句...这不是题解...吗?


上一页 |