程序设计与算法 第七周测验
2016-09-19 15:00
429 查看
程序设计与算法 第七周测验
-eof-
1:统计数字字符个数
#include <iostream> #include <cstring> using namespace std; #define LEN 255 int main(void) { char s[LEN+1] = {'\0'}; int cnt = 0; cin.getline(s, sizeof(s)); for (int i=0; s[i]!='\0'; i++) if ('0' <= s[i] && s[i] <= '9') cnt++; cout << cnt << endl; return 0; }
2:找第一个只出现一次的字符
#include <iostream> #include <cstring> using namespace std; #define ML 100000 #define AL 26 int main(void) { char s[ML+1] = {'\0'}; int cnt[AL] = {0}; cin.getline(s, sizeof(s)); for (int i=0; s[i]!='\0'; i++) cnt[s[i]-'a']++; for (int i=0; s[i]!='\0'; i++) { if(cnt[s[i]-'a']==1) { cout << s[i] << endl; return 0; } } cout << "no" << endl; return 0; }
3:石头剪子布
#include <iostream> #include <cstring> using namespace std; #define L 10 #define ROCK 2 #define SCISSORS 1 #define PAPER 0 int s2i(char s[]) { if (strcmp(s,"Rock")==0) return ROCK; else if (strcmp(s,"Scissors")==0) return SCISSORS; else return PAPER; } int cmp(char s1[], char s2[]) { int i1 = s2i(s1); int i2 = s2i(s2); if (i1==i2) return 0; else if (i1<i2) { if (i1==PAPER && i2==ROCK) return 1; else return -1; } else { if (i1==ROCK && i2==PAPER) return -1; else return 1; } } int main(void) { int N; char s1[L], s2[L]; cin >> N; while(N--) { cin >> s1 >> s2; int n = cmp(s1, s2); if (n<0) cout << "Player2" << endl; else if (n==0) cout << "Tie" << endl; else cout << "Player1" << endl; } return 0; }
4:最长最短单词
#include <iostream> #include <cstring> using namespace std; int main(void) { char word[101], min_word[101]={'\0'}, max_word[101]={'\0'}; char c, *w = word; while(cin.get(c)) { if(c==' ' || c==',' || c=='\n') { *w = '\0'; int len = w - word; if (len>0) { if(len<strlen(min_word)||strlen(min_word)==0) strcpy(min_word,word); if (len>strlen(max_word)) strcpy(max_word,word); } if (c=='\n') break; w = word; } else { *w++ = c; } } cout << max_word << endl; cout << min_word << endl; return 0; }
5:密码翻译
#include <iostream> #include <cstring> using namespace std; #define ML 80 int main(void) { char s[ML+1] = {'\0'}; cin.getline(s, sizeof(s)); for (int i=0; s[i]!='\0'; i++) { if (s[i]=='z') cout << 'a'; else if (s[i]=='Z') cout << 'A'; else if ( ('a'<=s[i] && s[i]<='y') || ('A'<=s[i] && s[i]<='Y') ) cout << (char)(s[i]+1); else cout << s[i]; } cout << endl; return 0; }
-eof-
相关文章推荐
- 6:数字统计( 程序设计与算法(一)第四周测验(2017夏季)
- 慕课-程序设计与算法(大学先修课)-郭炜-第七周练习题
- 程序设计实习MOOC / 程序设计与算法(一)第三周测验(2017冬季)8:奥运奖牌计数
- 程序设计与算法 第十周测验
- 程序设计与算法 第八、九周测验
- 程序设计与算法(一)第一周测验(2017夏季) 解题报告
- 程序设计实习MOOC - 程序设计与算法(三)第一周测验 4-神秘的数组初始化
- 1:角谷猜想(程序设计与算法(一)第四周测验(2017夏季))
- 4:求特殊自然数(程序设计与算法(一)第四周测验(2017夏季))
- 程序设计与算法(二)测验汇总011:最佳加法表达式(DP、高精度)
- 程序设计实习MOOC / 程序设计与算法(一)第三周测验(2017冬季)2:求一元二次方程的根
- 程序设计实习MOOC / 程序设计与算法(一)第三周测验(2017冬季)3:点和正方形的关系
- 程序设计实习MOOC / 程序设计与算法(一)第三周测验(2017冬季)6:求整数的和与均值
- 2:正常血压(程序设计与算法(一)第四周测验(2017夏季)
- 程序设计实习MOOC / 程序设计与算法(一)第三周测验(2017冬季)4:苹果和虫子2
- 程序设计与算法 第六周测验
- 程序设计实习MOOC / 程序设计与算法(一)第三周测验(2017冬季)5:简单计算器
- 程序设计与算法 第三周测验
- 程序设计与算法 第一周测验
- 算法----计算机程序设计之魂