Homework:
有一个国家的外交信件使用如下方法写成密码:首先颠倒所有的非元音字母段的序列(包括空格和标点符号),然后再把全部信件倒着写。该国总理发出密信如下:
rn.urtbae hes mevi ginoreppe. pesee chaxtret a thekam
手算一下:
rn.urtbae hes mevi ginoreppe. pesee chaxtret a thekam
mkhut aetretxheci sipo e.peprnegee vma she batr.enarrane.rtab ehs amv eegenrpep.e opis icehxtertea tuhkm
这是什么鸟语,怎么看不懂。
莫非题目题理解错了?
以下程序,解出来如上,@%#*#&@……*#@


#include <iostream>
#include <string>
using namespace std;
bool isVowel(char c)
{
if(c == 'a' || c == 'A' || c == 'e' || c == 'E' ||
c == 'i' || c == 'I' || c == 'o' || c == 'O' ||
c == 'u' || c == 'U')
return true;
else
return false;
}
inline void exchange(char& a, char& b)
{
char temp = a;
a = b;
b = temp;
}
void decipher(string& code)
{
int n = code.length();
int i = 0, j = n-1;
while(i < j)
{
while(isVowel(code[i]))
++i;
while(isVowel(code[j]))
--j;
if(i < j)
exchange(code[i],code[j]);
++i;
--j;
}
i = 0;
j = n-1;
while(i < j)
{
exchange(code[i],code[j]);
++i;
--j;
}
}
int main()
{
string s = "rn.urtbae hes mevi ginoreppe. pesee chaxtret a thekam";
decipher(s);
cout<<s<<endl;
return 0;
}
理解错了,正确如下:


1 #include <iostream>
2 #include <string>
3
4 using namespace std;
5
6 bool isVowel(char c)
7 {
8 if(c == 'a' || c == 'A' || c == 'e' || c == 'E' ||
9 c == 'i' || c == 'I' || c == 'o' || c == 'O' ||
10 c == 'u' || c == 'U')
11 return true;
12 else
13 return false;
14 }
15 inline void exchange(char& a, char& b)
16 {
17 char temp = a;
18 a = b;
19 b = temp;
20 }
21 void reverse(string& code, int begin, int end)
22 {
23 while(begin < end)
24 {
25 exchange(code[begin],code[end]);
26 ++begin;
27 --end;
28 }
29 }
30 void decipher(string& code)
31 {
32 int n = code.length();
33 int i = 0, j = n-1;
34 int begin=0, end=0;
35 while(begin < n && end < n)
36 {
37 while(isVowel(code[begin]) && begin < n)
38 ++begin;
39 end=begin;
40 while(!isVowel(code[end]) && end < n)
41 ++end;
42 --end;
43 reverse(code,begin,end);
44 begin=end+1;
45 }
46 while(i < j)
47 {
48 exchange(code[i],code[j]);
49 ++i;
50 --j;
51 }
52 }
53 int main()
54 {
55 string s = "rn.urtbae hes mevi ginoreppe. pesee chaxtret a thekam";
56 decipher(s);
57 cout<<s<<endl;
58 return 0;
59 }
60