一、课内实验题(共10小题,100分)
题型得分 | 100 |
-
【描述】
判断输入的一个字符串是否为回文串,若是输出“Yes”,否则输出“No”。回文串是指正读和反读都一样的字符串,如level。
【输入】
输入一个字符串。
【输出】
输出“Yes”或“No”。
【输入示例】
【输出示例】abcddcba
【来源】Yes
《程序设计基础——以C++为例》第4章实验8。 (10分)我的答案:#include<iostream> #include<string> using namespace std; int main() { string s; cin >> s; int ret = 1; for (int i = 0;i < s.length();i++) { if (s[i] != s[s.length() - i - 1]) { ret = 0; break; } } if (ret)cout << "Yes"; else cout << "No"; }
题目得分 10 参考答案:#include <iostream> #include <string> using namespace std; int main() { string str; cin >> str; string::size_type loc1 = 0; string::size_type loc2 = str.size() - 1; bool flag = true; while(loc1 < loc2) { if(str[loc1++] != str[loc2--]) { flag = false; break; } } cout << (flag ? "Yes" : "No") << endl; return 0; }
-
【描述】
输入一个字符串,统计并输出该字符串中26个英文字母(不区分大小写)出现的次数。
【输入】
输入一个字符串。
【输出】
分行输出26个英文字母(不区分大小写)出现的次数。
【输入示例】
【输出示例】I am a student.
【来源】a:2 d:1 e:1 i:1 m:1 n:1 s:1 t:2 u:1
《程序设计基础——以C++为例》第4章实验9。 (10分)我的答案:#include<iostream> #include<string> using namespace std; int main() { int a[30] = { 0 }; string x; getline(cin, x); for (int i = 0;i < x.length();i++) { if (x[i] >= 'a' && x[i] <= 'z') { a[x[i] - 'a']++; } if (x[i] >= 'A' && x[i] <= 'Z') { a[x[i] - 'A']++; } } for (int i = 0;i < 26;i++) { if (a[i]) { cout << (char)(i + 97) << ":" ; cout << a[i] << endl; } } }
题目得分 10 参考答案:#include <iostream> #include <string> #include <cctype> using namespace std; int main() { string str; getline(cin, str); const int ARRAY_SIZE = 26; int count[ARRAY_SIZE] = {0}; for(string::size_type i = 0; i < str.size(); ++i) { if(isalpha(str[i])) { if(isupper(str[i])) str[i] = tolower(str[i]); ++count[str[i] - 'a']; } } for(int i = 0; i < ARRAY_SIZE; ++i) if(count[i] != 0) cout << static_cast<char>('a' + i) << ":" << count[i] << endl; return 0; }
-
【描述】
输入5个字符串,输出其中最大的字符串(按照字典顺序)。
【输入】
输入5个字符串。
【输出】
输出5个字符串中最大的字符串。
【输入示例】
【输出示例】red blue yellow green purple
【来源】yellow
《程序设计基础——以C++为例》第4章实验10。 (10分)我的答案:#include<iostream> #include<string> using namespace std; int main() { string x; int n = 5-1; cin >> x; string maxstr = x; while (n--) { string x1; cin >> x1; if (x1.compare(maxstr)==1) { maxstr = x1; } } cout << maxstr; }
题目得分 10 参考答案:#include <iostream> #include <string> using namespace std; int main() { string str[5]; // 存放5个字符串 for(int i = 0; i < 5; ++i) { getline(cin, str[i]); } string max = str[0]; // 存放最大的字符串 for(int i = 1; i < 5; ++i) if(max.compare(str[i]) < 0) max = str[i]; cout << max << endl; return 0; }
-
【描述】
定义和调用函数:bool isAnagram(string str1, string str2),检查两个单词是否是字母易位词,如果是,返回true;否则返回false。两个单词如果包含相同的字母,次序不同,则称为字母易位词(anagram)。例如,“silent”和“listen”是字母易位词。
【输入】
输入有两行,分别对应两个单词。
【输出】
若两个单词是字母易位词,输出true,否则输出false。
【输入示例】
【输出示例】silent listen
【来源】true
《程序设计基础——以C++为例》第4章实验题11。 (10分)我的答案:bool isAnagram(string str1,string str2){ int a[30]={0}; if(str1.length()!=str2.length()){ return false; }else{ for(int i=0;i<str1.length();i++){ a[str1[i]-'a']++; a[str2[i]-'a']--; } for(int i=0;i<30;i++){ if(a[i]!=0)return false; } return true; } }
题目得分 10 参考答案:#include <iostream> #include <algorithm> #include <string> using namespace std; bool isAnagram(string str1, string str2) { sort(str1.begin(), str1.end()); sort(str2.begin(), str2.end()); if(str1.size() != str2.size()) return false; if(str1.compare(str2) != 0) return false; return true; } int main() { string str1, str2; getline(cin, str1); getline(cin, str2); cout << boolalpha << isAnagram(str1, str2) << endl; return 0; }
-
【描述】
输入一个字符串,求出其中最长的英文单词的长度,并输出。单词之间只能用空格间隔。
【输入】
输入一个字符串。
【输出】
输出字符串中最长的英文单词的长度。
【输入示例】
【输出示例】Nice to meet you
【来源】4
《程序设计基础——以C++为例》第4章实验题12。 (10分)我的答案:#include<iostream> #include<string> #include<sstream> using namespace std; int main() { string str,a; getline(cin, str); stringstream ss(str); int maxlen = 0; while (ss >> a) { if (a.length() > maxlen) { maxlen = a.length(); } } cout << maxlen; }
题目得分 10 参考答案:#include <iostream> #include <sstream> #include <string> using namespace std; int main() { string line; getline(cin, line); stringstream strm(line); string word; int length = 0; while(strm >> word) { if(length < word.size()) length = word.size(); } cout << length << endl; return 0; }
-
【描述】
输入5个字符串,按字典顺序升序排序后输出。
【输入】
输入5个字符串。
【输出】
输出升序排序后的5个字符串。
【输入示例】
【输出示例】red blue yellow green purple
【来源】blue green purple red yellow
《程序设计基础——以C++为例》第4章实验10强化练习。 (10分)我的答案:#include<bits/stdc++.h> #include<iostream> #include<string> #include<sstream> #include<algorithm> #include<vector> using namespace std; bool cmp(string a, string b){ return strcmp(a.c_str(), b.c_str()) < 0; } int main(){ vector<string> v; for (int i = 0;i < 5;i++) { string str; cin >> str; v.push_back(str); } sort(v.begin(), v.end(), cmp); for (vector<string>::iterator p = v.begin();p < v.end();++p) { cout << *p << endl; } }
题目得分 10 参考答案:#include <iostream> #include <algorithm> #include <string> using namespace std; int main() { string str[5]; for(int i = 0; i < 5; ++i) { getline(cin, str[i]); } sort(str, str + 5); for(int i = 0; i < 5; ++i) cout << str[i] << endl; return 0; }
-
【描述】
输入一段英文,计算并输出该段英文中元音字母的个数,不区分大小写。
【输入】
一行中输入一段英文。
【输出】
一行中输出5个数字,分别为字母a、e、i、o、u的个数,数字以空格间隔。
【输入示例】The C++ programming language is a general-purpose computer programming language originally developed in 1983.
(10分)【输出示例】 9 10 6 6 4
我的答案:#include<iostream> #include<string> #include<sstream> #include<algorithm> #include<vector> using namespace std; bool f1(char n){ if (n == 'a')return true; else return false; } bool f2(char n) { if (n == 'e')return true; else return false; } bool f3(char n) { if (n == 'i')return true; else return false; } bool f4(char n) { if (n == 'o')return true; else return false; } bool f5(char n) { if (n == 'u')return true; else return false; } int main() { string str; getline(cin, str); transform(str.begin(), str.end(), str.begin(), ::tolower); cout << count_if(str.begin(), str.end(), f1) << " "; cout << count_if(str.begin(), str.end(), f2) << " "; cout << count_if(str.begin(), str.end(), f3) << " "; cout << count_if(str.begin(), str.end(), f4) << " "; cout << count_if(str.begin(), str.end(), f5) << " "; }
题目得分 10 参考答案:#include <iostream> #include <cctype> #include <string> using namespace std; int main() { int count[5] = {0}; string str; getline(cin, str); for(int i = 0; i < str.size(); ++i) { switch(tolower(str[i])) { case 'a' : count[0]++; break; case 'e' : count[1]++; break; case 'i' : count[2]++; break; case 'o' : count[3]++; break; case 'u' : count[4]++; break; } } cout << count[0] << " " << count[1] << " " << count[2] << " " << count[3] << " " << count[4] << endl; return 0; }
-
【描述】
编写程序,提取一个字符串中的所有数字字符('0'…'9'),将其转换为一个整数输出。
【输入】
在一行中,输入一个字符串。
【输出】
在一行中输出转换后的整数。题目保证输出不超过长长整型范围。
【输入示例】
【输出示例】free82jeep5
(10分)825
我的答案:#include<iostream> #include<string> #include<sstream> #include<algorithm> #include<vector> using namespace std; int main() { string str; getline(cin,str); long long int ans=0; for (int i = 0;i < str.length();i++) { if (str[i] >= '0' && str[i] <= '9') { ans = ans * 10 + (str[i] - '0'); } } cout << ans; }
题目得分 10 参考答案:#include <iostream> #include <string> #include <cctype> using namespace std; int main() { string line; getline(cin, line); long long number = 0; for(int i = 0; i < line.length(); ++i) { if(isdigit(line[i])) number = number * 10 + (line[i] - '0'); } cout << number << endl; return 0; }
-
【描述】
每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字、1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”就是分隔符(键盘上的减号),最后一位是识别码,例如0-670-82162-4就是一个标准的ISBN码。ISBN码的首位数字表示书籍的出版语言,例如0代表英语;第一个分隔符“-”之后的三位数字代表出版社,例如670代表维京出版社;第二个分隔符后的五位数字代表该书在该出版社的编号;最后一位为识别码。
识别码的计算方法如下:
首位数字乘以1加上次位数字乘以2……以此类推,用所得的结果mod 11,所得的余数即为识别码,如果余数为10,则识别码为大写字母X。例如ISBN号码0-670-82162-4中的识别码4是这样得到的:对067082162这9个数字,从左至右,分别乘以1,2,...,9,再求和,即0×1+6×2+……+2×9=158,然后取158 mod 11的结果4作为识别码。
你的任务是编写程序判断输入的ISBN号码中识别码是否正确,如果正确,则仅输出“Right”;如果错误,则输出你认为是正确的ISBN号码。
【输入】
输入只有一行,是一个字符序列,表示一本书的ISBN号码(保证输入符合ISBN的格式要求)。
【输出】
输出只有一行,假如输入的ISBN号码的识别码正确,那么输出“Right”,否则,按照规定的格式,输出正确的ISBN号码(包括分隔符“-”)。
【输入示例】0-123-41562-4
【输出示例】
(10分)Right
我的答案:#include<iostream> #include<string> #include<sstream> #include<algorithm> #include<vector> using namespace std; int main() { string str; cin >> str; int count = 1,sum=0; for (int i = 0;i < str.length()-1;i++) { if (str[i] >= '0' && str[i] <= '9') { sum += (str[i]-'0') * count; count++; } } if (sum % 11 == (str[str.length() - 1]-'0')) { cout << "Right"; } else { for (int i = 0;i < str.length();i++) { if (i == 1 || i == 5 || i == 11) { cout << "-"; } else { if (i != str.length() - 1) { for (int i = 0;i < str.length();i++) { if (str[i] >= '0' && str[i] <= '9') { cout << str[i]; str[i] = '*'; break; } } } else { cout << sum % 11; } } } } }
题目得分 10 参考答案:#include <iostream> #include <string> using namespace std; int main() { string line; getline(cin, line); int sum = 0; sum += (line[0] - '0'); for(int i = 2; i <= 4; ++i) sum += (line[i] - '0') * i; for(int i = 6; i <= 10; ++i) sum += (line[i] - '0') * (i - 1); if(sum % 11 == line[12] - '0') cout << "Right" << endl; else { line[12] = (char)((sum % 11) + '0'); cout << line << endl; } return 0; }
-
【描述】
输入一段英文,将该段英文中的每一个单词翻转后输出。
【输入】
一行中输入一段英文。单词之间以空格隔开。
【输出】
一行中输出翻转每一个单词后的字符串
【输入示例】
hello world
【输出示例】
olleh dlrow
(10分)我的答案:#include<iostream> #include<string> #include<sstream> #include<algorithm> #include<vector> using namespace std; int main() { string str; string ans; int fp=0; getline(cin, str); for(int i=0;i<str.length();i++){ if(str[i]==' '){ for(int j=i-1;j>=fp;j--){ cout<<str[j]; } fp=i+1; cout<<" "; } } if(fp!=str.length()){ for(int j=str.length()-1;j>=fp;j--){ cout<<str[j]; } } }
题目得分 10 参考答案:#include <iostream> #include <sstream> #include <string> #include <algorithm> using namespace std; int main() { string str, s = "", word; getline(cin, str); istringstream istrm(str); // 分离单词 bool first = true; while(istrm >> word) { // 反转单词 reverse(word.begin(), word.end()); if(first) { s += word; first = false; } else s += " " + word; } cout << s << endl; return 0; }