求解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-11 21:54:34

相信我,strcmp就是个坑


by tylon2006 @ 2018-10-11 21:55:24

我以前一次膜你赛就是用strcmp爆零的


by tylon2006 @ 2018-10-11 21:56:05

你不能写a==c[j]吗


by tylon2006 @ 2018-10-11 21:56:32

字符串可以直接比较


by tylon2006 @ 2018-10-11 21:56:52

比函数稳多了


by tylon2006 @ 2018-10-11 21:57:07

@许凉城


by 镉八君 @ 2018-10-11 21:57:28

总有一些奇怪的字符出现在字符串里面
而且strcmp本来就是暴力枚举
还不如自己写一个


by Prurite @ 2018-10-11 22:03:08

@许凉城 string 类型可以直接用==,不要用strcmpchar 数组才需要strcmp.

比如这样

string a, b;
cin>>a>>b;
if ( a==b )
    cout<<"ok\n";
char c[20], d[20];
cin>>c>>d;
if ( strcmp( c, d )==0 )
    cout<<"ok\n";

by __Hacheylight__ @ 2018-10-11 22:26:35

哇,神仙们不复习初赛么QAQ


by 许凉城 @ 2018-10-12 18:49:52

@tylon2006 ojbk!!!感谢你!!!但是它不知道为什么输不出来QAQ


| 下一页