P1603 斯诺登密码
https://www.luogu.org/problemnew/show/P1603
思路:题目里写的非正规是真的坑啊,我还以为是非正规输入输入这些是不能算数字的,其实是要算的,而且 another 是什么鬼居然算1.
1 #include <iostream> 2 #include <set> 3 #include <cmath> 4 #include <stdio.h> 5 #include <cstring> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 using namespace std; 11 typedef long long LL; 12 #define inf 0x7f7f7f7f 13 14 string number[21] = {"", "one", "two", "three", "four", "five", "six", "seven", 15 "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", 16 "sixteen", "seventeen", "eighteen", "nineteen", "twenty"}; 17 string number1[3] = {"", "a", "both"}; 18 string number2[4] = {"", "first", "second", "third"}; 19 20 bool cmp(int i, int j) 21 { 22 if(i / 10 == j / 10) return (i % 10) < (j % 10); 23 else return (i / 10) < (j / 10); 24 } 25 26 int main() 27 { 28 string s; 29 int nums[6]; 30 int cnt = 0; 31 for(int i = 0; i < 6; i++){ 32 cin>>s; 33 for(int j = 0; j < 21; j++){ 34 if(s == number[j]){ 35 //cout<<number[j]<<" "<<s<<endl; 36 nums[cnt++] = j * j % 100; 37 break; 38 } 39 } 40 for(int j = 0; j < 3; j++){ 41 if(s == number1[j]){ 42 nums[cnt++] = j * j % 100; 43 break; 44 } 45 } 46 if(s == "another"){ 47 nums[cnt++] = 1; 48 } 49 for(int j = 0; j < 4; j++){ 50 if(s == number2[j]){ 51 nums[cnt++] = j * j % 100; 52 break; 53 } 54 } 55 } 56 57 sort(nums, nums + cnt, cmp); 58 printf("%d", nums[0]); 59 for(int i = 1; i < cnt; i++){ 60 if(nums[i] < 10){ 61 printf("0"); 62 } 63 printf("%d", nums[i]); 64 } 65 printf("\n"); 66 return 0; 67 }
P1071 潜伏者
https://www.luogu.org/problemnew/show/P1071
思路:要求的是一个字母对应的是一个密文,一个密文对应的是一个字母,要用两个map。而且要求每个字母都有出现。
1 #include <iostream> 2 #include <set> 3 #include <cmath> 4 #include <stdio.h> 5 #include <cstring> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 using namespace std; 11 typedef long long LL; 12 #define inf 0x7f7f7f7f 13 14 char sec[105], str[105]; 15 map<char, char>mp, revmp; 16 17 int main() 18 { 19 scanf("%s", sec); 20 scanf("%s", str); 21 int len = strlen(sec); 22 for(int i = 0; i < len; i++){ 23 if(mp.find(sec[i]) == mp.end()){ 24 mp[sec[i]] = str[i]; 25 } 26 else if(mp[sec[i]] != str[i]){ 27 printf("Failed\n"); 28 return 0; 29 } 30 31 if(revmp.find(str[i]) == revmp.end()){ 32 revmp[str[i]] = sec[i]; 33 } 34 else if(revmp[str[i]] != sec[i]){ 35 printf("Failed\n"); 36 return 0; 37 } 38 } 39 for(int i = 0; i < 26; i++){ 40 if(mp.find(‘A‘ + i) == mp.end()){ 41 printf("Failed\n"); 42 return 0; 43 } 44 } 45 scanf("%s", sec); 46 len = strlen(sec); 47 for(int i = 0; i < len; i++){ 48 if(mp.find(sec[i]) == mp.end()){ 49 printf("Failed"); 50 } 51 else{ 52 printf("%c", mp[sec[i]]); 53 } 54 } 55 printf("\n"); 56 return 0; 57 }
P1012 拼数
https://www.luogu.org/problemnew/show/P1012
思路:把输入的整数当成是string,对他们排序,大的放前面。有一个需要注意的点,比如两个数30,300.如果只是单纯比较字符串的大小,会得到30030,但显然答案应该是30300.因此不能单纯比较字符串的大小,而是比较a+b>b+a,意思是两个字符串拼接后较大的放前面。
1 #include <iostream> 2 #include <set> 3 #include <cmath> 4 #include <stdio.h> 5 #include <cstring> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 using namespace std; 11 typedef long long LL; 12 #define inf 0x7f7f7f7f 13 14 string num[21]; 15 int n; 16 17 bool cmp(string a, string b) 18 { 19 /*if(a.length() == b.length()){ 20 return a > b; 21 } 22 else return a.length() > b.length();*/ 23 return a + b > b + a; 24 } 25 26 int main() 27 { 28 scanf("%d", &n); 29 for(int i = 0; i < n; i++){ 30 cin>>num[i]; 31 } 32 sort(num, num + n, cmp); 33 for(int i = 0; i < n; i++){ 34 cout<<num[i]; 35 } 36 cout<<endl; 37 return 0; 38 }
原文:https://www.cnblogs.com/wyboooo/p/9846622.html