連載一

/*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;

}


圖片發(fā)自簡書App

/*荷蘭國旗問題

樣例輸入

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;

}


圖片發(fā)自簡書App

/*空格字符與非空格字符統(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;

}?


圖片發(fā)自簡書App

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容