/*IBM減一
樣例輸入
2
HAL
SWERC
樣例輸出
String #1
IBM
String #2
TXFSD
*/
思路分析:觀察案例可知,輸出字母是輸入字母的后一個(gè),而Z對(duì)應(yīng)的則是A。通過if―else語句即可完成要求。
//方法一:
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int num;
vector<string>v;
vector<string>::iterator it;
cin>>num;
for(int i=0;i<num;i++)
{
string temp;
cin>>temp;
v.push_back(temp);
}
int n = 1;
for(it=v.begin();it!=v.end();it++)
{
string temp;
temp = *it;
cout<<"string #"<<n++<<endl;
for(int j=0;j<temp.length();j++)
{
if(temp[j]=='Z')
cout<<'A';
cout<<char(temp[j]+1);
}
cout<<endl;
}
return 0;
}? ?
*/
//方法二:
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
string s;
while(cin>>s)
{
for(int j=0;j<s.length();j++)
{
if(s[j]=='Z')
{
cout<<'A';
}
cout<<char(s[j]+1);
}
cout<<endl;
}
}
return 0;
}

/*荷蘭國旗問題
樣例輸入
3
BBRRWBWRRR
RRRWWRWRB
RBRW
樣例輸出
RRRRRWWBBB
RRRRRWWWB
RRWB
*/
思路分析:本題關(guān)鍵是如何輸入一行案例,計(jì)算出三種字母的個(gè)數(shù),按循序輸出。
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n;
cin>>n;
cin.get();//吸收尾部標(biāo)記
for(int i=0;i<n;i++)
{
char s[100];
cin.getline(s,100,'\n');//讀取一行
int len = strlen(s);
int r = 0,w = 0,b = 0;
for(int i=0;i<len;i++)
{
if(s[i]=='R')
r++;
else
{
if(s[i]=='W')
w++;
else
b++;
}
}
for(int i=0;i<r;i++)
cout<<'R';
for(int j=0;j<w;j++)
cout<<'W';
for(int k=0;k<b;k++)
cout<<'B';
cout<<endl;
}
return 0;
}

/*空格字符與非空格字符統(tǒng)計(jì)
樣例輸入
123fe*&54 0934j
df *A? S
樣例輸出
14 1
5 3
*/
思路分析:關(guān)鍵在于如何判斷已經(jīng)輸完字符串。
#include<iostream>
using namespace std;
int main()
{
char ch;
int m = 0,n = 0;
while(!cin.eof())
{
if((ch=cin.get())!=' ')
{
if((ch!='\n')&&(ch!=-1))//-1表示文件尾部標(biāo)志
m++;
else
{
cout<<m<<" "<<n<<endl;
m = 0,n = 0;
}
}
else
n++;
}
return 0;
}?
