3 条题解
-
1
用字典来解决
#include <iostream> #include <cstring> #include <map> using namespace std; int main() { string s; getline(cin, s); map<string, string> dict; dict["shi xian"] = "li bai"; dict["shi sheng"] = "du fu"; dict["shi kuang"] = "he zhi zhang"; dict["shi mo"] = "bai ju yi"; dict["shi fo"] = "wang wei"; dict["shi gui"] = "li he"; dict["shi shen"] = "su shi"; dict["shi hao"] = "liu yu xi"; cout << dict[s] << endl; return 0; } -
0
分享一个有意思的解法。因为雅号前面全是"shi",所以直接忽略掉,匹配后面就行了。
#include <stdio.h> #include <string.h> int main() { char nickname[10]; scanf("%*s %s", nickname); if (strncmp(nickname, "xian", 4) == 0) { printf("li bai"); } else if (strncmp(nickname, "sheng", 5) == 0) { printf("du fu"); } else if (strncmp(nickname, "kuang", 5) == 0) { printf("he zhi zhang"); } else if (strncmp(nickname, "mo", 2) == 0) { printf("bai ju yi"); } else if (strncmp(nickname, "fo", 2) == 0) { printf("wang wei"); } else if (strncmp(nickname, "gui", 3) == 0) { printf("li he"); } else if (strncmp(nickname, "shen", 4) == 0) { printf("su shi"); } else if (strncmp(nickname, "hao", 3) == 0) { printf("liu yu xi"); } return 0; } -
0
可以用二维数组定义a和b两个,分别对应雅号和名字。
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> int main(){ char a[8][20]={"shi xian","shi sheng","shi kuang","shi mo","shi fo","shi gui","shi shen","shi hao"}; char b[8][20]={"li bai","du fu","he zhi zhang","bai ju yi","wang wei","li he","su shi","liu yu xi"}; char s[50]; fgets(s,50,stdin); s[strcspn(s,"\n")]=0; int i; for(i=0;i<8;i++) if(!strcmp(s,a[i])){printf("%s",b[i]);return 0;} return 0; }
- 1
信息
- ID
- 32
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- 5
- 标签
- 递交数
- 320
- 已通过
- 123
- 上传者