1. 統計不同類型字符出現次數
【問題描述】
輸入一個字符串(假設長度不超過1000個字符),統計其中大寫,小寫,數字,其他字符出現的次數。
【樣例輸入】Hello,what are you doing 123?
【樣例輸出】1 19 3 6
#include<iostream> #include<cstring> using namespace std; int main() { char a[1001]; int dx = 0,xx = 0,sz = 0,qt = 0; int l, i; //輸入字符串 cin.getline(a,1001); l = strlen(a); for(i = 0; i < l; i++) { if(a[i] >='A' && a[i] <='Z') { dx++; } else if(a[i] >='a' && a[i] <='z') { xx++; } else if(a[i] >='0' && a[i] <='9') { sz++; } else { qt++; } } cout << dx <<" " << xx <<" "<< sz<<" " << qt; return 0; }
2. 刪除數字字符,並統計刪除的數字的個數
【問題描述】
從鍵盤輸入一個由大、小寫字母和數字組成的任意一個字符串(不需判斷),其長度不小於 8,不大於 30。現要求將字符串
中的所有數字字符刪除,其他字符依照原有順序保持不變,並統計刪除的數字的個數。
【輸入文件]】
只有一行,包含只由大、小寫字母和數字組成的一個字符串(其長度 8≤L≤30)。
【輸出文件]】
有兩行:
第一行:為刪除數字字符后的字符串;
第二行:為統計刪除的數字的個數。
【要求】每行的輸出數據從第一列輸出
【樣例輸入】
ABCD123efg678
【樣例輸出】
ABCDefg
6
#include<iostream> #include<cstring> using namespace std; int main() { char a[31]; int i,l,sum = 0; cin >> a; l=strlen(a); for(i=0; i<l; i++) { if(a[i]>='0' && a[i]<='9') { sum++; } else { cout << a[i]; } } cout << endl; cout << sum; return 0; }
1. 判斷字符串是否為回文
【問題描述】
輸入一個字符串,輸出該字符串是否回文。回文是指順讀和倒讀都一樣的字符串。
輸入為一行字符串(字符串中沒有空白字符,字符串長度不超過100)。
如果字符串是回文,輸出yes;否則,輸出no。
【樣例輸入】
abcdedcba
【樣例輸出】
Yes
#include<iostream> #include<cstring> using namespace std; int main() { char str1[101],str2[101]; int i; long len; cin >> str1; len = strlen(str1); for(i=0; i<len; i++) { str2[i]=str1[len-1-i]; } str2[i]='\0'; if(strcmp(str1,str2)==0) { cout << "yes"; } else { cout << "no"; } return 0; }
2. 石頭剪子布
【問題描述】
石頭剪子布,是一種猜拳游戲。起源於中國,然后傳到日本、朝鮮等地,隨着亞歐貿易的不斷發展它傳到了歐洲,到了近現代逐漸風靡世界。簡單明了的規則,使得石頭剪子布沒有任何規則漏洞可鑽,單次玩法比拼運氣,多回合玩法比拼心理博弈,使得石頭剪子布這個古老的游戲同時用於“意外”與“技術”兩種特性,深受世界人民喜愛。
游戲規則:石頭打剪刀,布包石頭,剪刀剪布。
現在,需要你寫一個程序來判斷石頭剪子布游戲的結果。
輸入:輸入包括N+1行:
第一行是一個整數N,表示一共進行了N次游戲。1 <= N <= 100。
接下來N行的每一行包括兩個字符串,表示游戲參與者Player1,Player2的選擇(石頭、剪子或者是布):S1 S2
字符串之間以空格隔開S1,S2只可能取值在{"Rock", "Scissors", "Paper"}(大小寫敏感)中。
輸出:輸出包括N行,每一行對應一個勝利者(Player1或者Player2),或者游戲出現平局,則輸出Tie。
【樣例輸入】
3
Rock Scissors
Paper Paper
Rock Paper
【樣例輸出】
Player1
Tie
Player2
#include <iostream> #include <string> #include <cstring> using namespace std; int main() { int N; string a, b; cin >> N; for (int i=0; i<N; i++) { cin >> a; cin >> b; if (a.compare("Rock") == 0) { if (b.compare("Rock") == 0) { cout << "Tie" << endl; } else if (b.compare("Scissors") == 0) { cout << "Player1" << endl; } else { cout << "Player2" << endl; } } else if (a.compare("Scissors") == 0) { if (b.compare("Rock") == 0) { cout << "Player2" << endl; } else if (b.compare("Scissors") == 0) { cout << "Tie" << endl; } else { cout << "Player1" << endl; } } else { if (b.compare("Rock") == 0) { cout << "Player1" << endl; } else if (b.compare("Scissors") == 0) { cout << "Player2" << endl; } else { cout << "Tie" << endl; } } } return 0; }
3. 找第一個只出現一次的字符
【問題描述】
給定一個只包含小寫字母的字符串,請你找到第一個僅出現一次的字符。如果沒有,輸出no。
輸入:一個字符串,長度小於100000。
輸出:輸出第一個僅出現一次的字符,若沒有則輸出no。
【樣例輸入】
abcabd
【樣例輸出】
c
#include<iostream> #include<cstring> using namespace std; int main() { //定義字符數組 char str[100001]; //定義一個長度 定義存儲每一個字符出現的次數的數組 int len,a[26]= {0}; //輸入字符串 cin >> str; //求字符串長度 len = strlen(str); //abacad a存每個字符的個數 a0++ 2 a++ 1 for(int i=0; i<len; i++) { a[str[i]-97]++; } for(int i=0; i<len; i++) { if(a[str[i]-97]==1) { cout << str[i]; return 0; } } cout << "no"; return 0; }