DES:给出一个字符串。连续空格的个数代表一个新的字符。奇数个表示0。偶数个表示1。然后根据这个码作为ASCII码。写出对应的字符。就是统计空格个数。二进制转换成十进制的小模拟。但是比赛的时候敲得很不顺。
1 #include<stdio.h> 2 #include<string.h> 3 #include<map> 4 #include<vector> 5 #include<math.h> 6 #include<iostream> 7 #include<map> 8 using namespace std; 9 10 char str[100]; 11 int num[400]; 12 char anss[70]; 13 int cou; 14 int tt; 15 16 void change() 17 { 18 int temp = (cou % 5); 19 int ans = 0; 20 tt = 0; 21 char la; 22 for (int i=0; i<temp; ++i) 23 { 24 num[cou++] = 0; 25 } 26 for (int i=0; i<cou; i+=5) 27 { 28 int t = 4; 29 ans = 0; 30 for (int j=i; j<i+5; ++j) 31 { 32 ans += pow(2, t) * num[j]; 33 t--; 34 } 35 if (ans >=1 && ans <= 26) 36 { 37 int hh = ans + int(‘A‘); 38 hh -= 1; 39 la = char(hh); 40 } 41 else if (ans == 30) la = ‘.‘; 42 else if (ans == 29) la = ‘-‘; 43 else if (ans == 28) la = ‘,‘; 44 else if (ans == 27) la = ‘\‘‘; 45 else if (ans == 0) la = ‘ ‘; 46 else if (ans == 31) la = ‘?‘; 47 anss[tt++] = la; 48 } 49 } 50 51 52 int main() 53 { 54 cou = 0; 55 while(gets(str)) 56 { 57 int len = strlen(str); 58 int cnt = 0; 59 if (str[0] == ‘*‘) 60 { 61 change(); 62 int st = 0; 63 while(anss[st] == ‘ ‘) 64 st++; 65 int ed = tt-1; 66 while(anss[ed] == ‘ ‘) 67 ed--; 68 for (int i=st; i<=ed; ++i) 69 { 70 cout << anss[i]; 71 } 72 cout << endl; 73 memset(num, 0, sizeof(num)); 74 memset(anss, 0, sizeof(anss)); 75 cou = 0; 76 continue; 77 } 78 if (str[0] == ‘#‘) 79 break; 80 81 for (int i=0; i<len; ++i) 82 { 83 cnt = 0; 84 if (str[i] == ‘ ‘) 85 { 86 int j; 87 for (j=i; ; j++) 88 { 89 if (str[j] != ‘ ‘) 90 break; 91 cnt++; 92 } 93 if (cnt % 2 == 1) num[cou++] = 0; 94 else num[cou++] = 1; 95 cnt = 0; 96 i = j; 97 } 98 } 99 } 100 return 0; 101 }
LOoK
时间: 2024-12-25 04:49:28