本文主要是介绍usaco 1.2 Name That Number(数字字母转化),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
巧妙的利用code[b[0]-'A'] 将字符ABC...Z转换为数字
需要注意的是重新开一个数组 c [ ] 存储字符串 应人为的在末尾附上 ‘ \ 0 ’
详见代码:
/*
ID: who jay
LANG: C++
TASK: namenum
*/#include<stdio.h>
#include<string.h>int main()
{FILE *fin = fopen ("namenum.in", "r");FILE *fout = fopen ("namenum.out", "w");FILE *fsearch = fopen("dict.txt","r");char code[]= {'2','2','2','3','3','3','4','4','4','5','5','5','6','6','6','7','0','7','7','8','8','8','9','9','9','0'};char a[13],b[13],c[13];int i,len;bool flag=0;while(fscanf(fin,"%s",a)!=EOF){while(fscanf(fsearch,"%s",b)!=EOF){len=strlen(b);if (code[b[0]-'A'] != a[0])continue;for (i = 0; i<len; i++)c[i] = code[b[i]-'A'];c[i]='\0';if (!strcmp(a,c)){flag = 1;fprintf(fout,"%s\n",b);}}if(!flag)fprintf(fout,"NONE\n");}return 0;
}
这篇关于usaco 1.2 Name That Number(数字字母转化)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!