代码如下(正确代码)
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n, t;
string a, area;
cin >> n;
while(n--)
{
cin >> a;
t = (a[0]-'0')*10+a[1]-'0';
switch(t)
{
case 11: area = "Beijing";break;
case 33: area = "Zhejiang";break;
case 71: area = "Taiwan";break;
case 81: area = "Hong Kong";break;
case 82: area = "Macao";break;
case 54: area = "Tibet";break;
case 21: area = "Liaoning";break;
case 31: area = "Shanghai";break;
default:break;
}
cout<<"He/She is from "<<area<<",and his/her birthday is on "<<a[10]<<a[11]<<","<<a[12]<<a[13]<<","<<a[6]<<a[7]<<a[8]<<a[9]<<" based on the table."<<endl;
}
return 0;
}
错误代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int n, i;
char ccity[10], ID[11], ncity[2];
scanf("%d\n",&n);
while (n--)
{
for (i = 0; i < 11; i++)
scanf(" %s ", ID);
for (i = 0; i < 2; i++)
ncity[i] = ID[i];
if( ncity[0] == '3' and ncity[0] == '3')
strcpy(ccity, "Zhejiang");
if( ncity[0] == '1' and ncity[0] == '1')
strcpy(ccity, "Beijing!");
if( ncity[0] == '7' and ncity[0] == '1')
strcpy(ccity, "Taiwan");
if( ncity[0] == '8' and ncity[0] == '1')
strcpy(ccity, "Hong Kong");
if( ncity[0] == '8' and ncity[0] == '2')
strcpy(ccity, "Macao");
if( ncity[0] == '5' and ncity[0] == '4')
strcpy(ccity, "Tibet");
if( ncity[0] == '2' and ncity[0] == '1')
strcpy(ccity, "Liaoning");
if( ncity[0] == '3' and ncity[0] == '1')
strcpy(ccity, "Shanghai");
printf("He/She is from %s,and his/her birthday is on %c%c,%c%c,%c%c%c%c based on the table.",ccity,ID[10],ID[11],ID[12],ID[13],ID[6],ID[7],ID[8],ID[9]);
}
return 0;
}
至于为什么错目前还没找出来,但是切记此类型的题的可以通过设定string来简化操作,如果通过数组,按理说应该也是可以完成的,但是。。。。。
还有几个常见的字符数组的输入要注意:
/*#include<stdio.h>
int main()
{
int sum,i;
int a[9];
for(i=0;i<9;i++)
scanf("%d",&a[i]);
//scanf("%d",a);
printf("%d%d",a[7],a[8]);
return 0;
}*/
/*#include<stdio.h>
int main()
{
int sum,i;
char a[12];
scanf("%s",a);
printf("%c%c",a[0],a[1]);
return 0;
}*/
//对于字符0,1,2,3,4......转化为数字1,2,3,4......
//有两种转化方法,其一为该字符数字减去数字48,其二为该字符减去字符0;
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
char s[100];
string b;
cin >> s;
cin >> b;
cout << s[0]-0 << endl;
cout << s[0]-'0' << endl;
cout << s[0]-48 << endl;
cout << b[0]-'0' << endl;
cout << b[0]-0 << endl;
cout << b[0]-48 << endl;
return 0;
}
**加油,加油**