【题目】
#include <cstdlib>
#include <iostream>
using namespace std;
char encoder[26] = {'C','S','P',0};
char decoder[26];
string st;
int main() {
int k = 0;
for (int i = 0; i < 26; ++i)
if (encoder[i] != 0) ++k;
for (char x ='A'; x <= 'Z'; ++x) {
bool flag = true;
for (int i = 0; i < 26; ++i)
if (encoder[i] ==x) {
flag = false;
break;
}
if (flag) {
encoder[k]= x;
++k;
}
}
for (int i = 0; i < 26; ++i)
decoder[encoder[i]- 'A'] = i + 'A';
cin >> st;
for (int i = 0; i < st.length( ); ++i)
st[i] = decoder[st[i] -'A'];
cout << st;
return 0;
}
判断题
1) 输入的字符串应当只由大写字母组成,否则在访问数组时可能越界。( √ )
2) 若输入的字符串不是空串,则输入的字符串与输出的字符串一定不一样。( × )
3) 将第 12 行的“i < 26”改为“i < 16”,程序运行结果不会改变。( √ )
4) 将第 26 行的”i < 26”改为“i < 16”,程序运行结果不会改变。( × )
单选题
5) 若输出的字符串为”ABCABCABCA”,则下列说法正确的是( A )。
A. 输入的字符串中既有S又有P
B. 输入的字符串中既有S又有B
C. 输入的字符串中既有A又有P
D. 输入的字符串中既有A又有B
6)若输出的字符串为”CSPCSPCSPCSP”,则下列说法正确的是( D )。
A. 输入的字符串中既有P又有K
B. 输入的字符串中既有J又有R
C. 输入的字符串中既有J又有K
D. 输入的字符串中既有P又有R
【解析】
这样的题目你想要直接读懂程序太难了,所以直接按照程序模拟一遍,带入部分简单数据尝试运行一下,能理解程序的主要原理即可。但是这样的题目,建议多做笔记,多写草稿,将过程记录着,以防有用。
如下程序中就是我所做笔记。
#include <cstdlib>//P16
#include <iostream>
using namespace std;
char encoder[26] = {'C','S','P',0};
char decoder[26];
string st;
int main() {
int k = 0;
for (int i = 0; i < 26; ++i)
if (encoder[i] != 0) ++k;// 字符个数计数 k=3
for (char x ='A'; x <= 'Z'; ++x) {
bool flag = true;
for (int i = 0; i < 26; ++i)
if (encoder[i] ==x) {
flag = false;
break;
}
if (flag) {//如果 x未出现,则存入
//encoder=CSPAB DEFGH IJKLM NOQRT UVWXYZ
encoder[k]= x;
++k;
}
}
for (int i = 0; i < 26; ++i)
decoder[encoder[i]- 'A'] = i + 'A';
//decoder[C-A]=A
//decoder[S-A]=B
//decoder[P-A]=C
//decoder[A-A]=D
//decoder[B-A]=E
//decoder[D-A]=F
//decoder[E-A]=G
//decoder[F-A]=H
//decoder[G-A]=I
//decoder[H-A]=J
//decoder[I-A]=K
//decoder[J-A]=L
//decoder[K-A]=M
//decoder[L-A]=N
//decoder[M-A]=O
//decoder[N-A]=P
//decoder[O-A]=Q
//decoder[Q-A]=R
//decoder[R-A]=S
//decoder[T-A]=T
//decoder[U-A]=U
//decoder[V-A]=V
//decoder[W-A]=W
//decoder[X-A]=X
//decoder[Y-A]=Y
//decoder[Z-A]=Z
cin >> st;//假设:CSP
for (int i = 0; i < st.length( ); ++i)
st[i] = decoder[st[i] -'A'];
//st=ABC
//str=ABCABCABCA
//str=CSPCSPCSPCSP
cout << st;
return 0;
}