目录
字符统计
【问题描述】输入字符串s,编程统计出s中共出现了多少个数字字符。进一步考虑,如何统计出10个数字字符各自出现的次数。
【输入形式】输入带数字字符的字符串
【输出形式】两行,第一行输出字符串中数字字符的总个数;第二行依次输出从数字字符‘0’到数字字符‘9’的个数
【样例输入】abcdefk12345
【样例输出】
Number's amount is:5
'0...9' amount is:0 1 1 1 1 1 0 0 0 0
【样例说明】请严格按照【样例输出】的要求输出结果,程序的输入及输出暂时不支持中文字符。
方法一:
#include<iostream>
#include<string>
#include<vector>
#include<cctype>
using namespace std;
int main() {
string s;
int count = 0;
getline(cin, s);
vector<char>num;
for (char c : s) {
if (isdigit(c)) {
count++;
num.push_back(c);
}
}
cout << "Number's amount is:" << count << endl;
int a[10] = { 0 };
for (char digit : num) {
switch (digit) {
case '0':a[0] += 1; break;
case '1':a[1] += 1; break;
case '2':a[2] += 1; break;
case '3':a[3] += 1; break;
case '4':a[4] += 1; break;
case '5':a[5] += 1; break;
case '6':a[6] += 1; break;
case '7':a[7] += 1; break;
case '8':a[8] += 1; break;
case '9':a[9] += 1; break;
}
}
cout << " '0...9' amount is:";
for (int i = 0; i < 10; i++) {
cout << a[i] << " ";
}
return 0;
}
方法二:(更简洁直观,无需先将数字字符存储在向量中,这样可以减少内存使用)
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main() {
string s;
int count = 0;
getline(cin, s);
int countDigits[10] = { 0 };
for (char c : s) {
if (isdigit(c)) {
count++;
countDigits[c - '0']++;
}
}
cout << "Number's amount is:" << count << endl;
cout << " '0...9' amount is:";
for (int i = 0; i < 10; i++) {
cout << countDigits[i] << " ";
}
return 0;
}
学生成绩统计
【问题描述】定义数组存储5位学生的姓名和4科考试的成绩,计算并输出每个学生的总成绩和平均成绩,找出平均成绩最高的学生姓名。
【输入形式】依次输入每位学生的姓名和4科考试的成绩。
【输出形式】1)输出每个学生的总成绩和平均成绩;2)输出平均成绩最高的学生姓名。
【样例输入】输入的数据之间间隔一个空格,如“zhangsan 87 76 91 79”中,“zhangsan”与“87”之间为一个空格
zhangsan 87 76 91 79
lisi 90 89 78 69
wangwu 80 88 91 85
zhaoqian 77 79 78 75
sunli 89 87 88 87
【样例输出】下列输出内容格式如下:
其中"Name"、"TotalScore"、"AverageScore"用具体的数值替代,如下:
Name TotalScore AverageScore
zhangsan 333 83
lisi 326 81
wangwu 344 86
zhaoqian 309 77
sunli 351 87
Student with the highest AverageScore is sunli,AverageScore is 87
【样例说明】此例共7行,平均成绩最高是sunli,但其他用例数据得到的结果会不同。程序的输入及输出暂时不支持中文字符。
方法一
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main() {
string name[5];
int score[5][4] = {0};
int TotalScore[5] = { 0 };
int ave[5] = { 0 };
for (int i = 0; i < 5; i++) {
cin >> name[i];
for (int j = 0; j < 4; j++) {
cin >> score[i][j];
TotalScore[i] += score[i][j];
}
ave[i] = TotalScore[i] / 4;
}
cout << "Name TotalScore AverageScore" << endl;
int highAve = ave[0];
int p = 0;
for (int i = 0; i < 5; i++) {
cout << setiosflags(ios::left) << setw(10) << name[i] << setw(9) << TotalScore[i] << ave[i] << endl;
if (ave[i] > highAve)highAve = ave[i];
p = i;
}
cout << "Student with the highest AverageScore is " << name[p] << ",AverageScore is " << ave[p];
return 0;
}
方法二(引入类的概念)
#include<iostream>
#include <string>
#include<vector>
#include<iomanip>
using namespace std;
class Student {
protected:
string name;
int scores[4];
public:
Student(string n, int s1, int s2, int s3, int s4) : name(n), scores{ s1, s2, s3, s4 } {}
int TotalScore() {
int total = 0;
for (int i = 0; i < 4; ++i) {
total += scores[i];
}
return total;
}
int AverageScore() {
return TotalScore() / 4;
}
string get_name() {
return name;
}
};
int main() {
vector<Student>students;
string name;
double s1, s2, s3, s4;
for (int i = 0; i < 5; i++) {
cin >> name >> s1 >> s2 >> s3 >> s4;
students.push_back(Student(name, s1, s2, s3, s4));
}
cout << setiosflags(ios::left) << "Name TotalScore AverageScore" << endl;
for (Student student : students) {
cout << setiosflags(ios::left) << setw(10)<< student.get_name() << setw(9) << student.TotalScore() << student.AverageScore()<<endl;
}
int high_ave = 0;
string stu;
for (Student high : students) {
if (high.AverageScore() > high_ave)
{
high_ave = high.AverageScore();
stu = high.get_name();
}
}
cout << "Student with the highest AverageScore is " << stu << ",AverageScore is " << high_ave << endl;
return 0;
}
求二维数组的鞍点
【问题描述】找出一个二维数组(以三行四列的数组为例)中的鞍点,即该位置上的元素在该行上值最大,在该列上值最小(也可能没有鞍点)。
【输入形式】二维数组(以三行四列的数组为例)
【输出形式】如果有鞍点,输出鞍点的值及所在行列(例如[1][2]=96 is Saddle Point),如果没有,输出没有鞍点(例如 No Saddle Point)
【样例输入】
9 80 205 40
90 60 96 1
210 3 101 89
【样例输出】
[1][2]=96 is Saddle Point
【样例说明】请正确输入二维数组,并严格按照【样例输出】的要求输出结果。程序的输入及输出暂时不支持中文字符。
#include<iostream>
using namespace std;
int judge_maxRows(int x, int a[3][4]) {
int max_in_rows = a[x][0];
int p = 0;
for (int j = 1; j < 4; j++) {
if (a[x][j] > max_in_rows) {
max_in_rows = a[x][j];
p = j;
}
}
return p;
}
int judge_minCols(int y, int a[3][4]) {
int min_in_columns = a[0][y];
int n = 0;
for (int i = 1; i < 3; i++) {
if (a[i][y] < min_in_columns) {
min_in_columns = a[i][y];
n = i;
}
}
return n;
}
int main() {
int a[3][4];
bool SaddlePoint = false;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cin >> a[i][j];
}
}
for (int i = 0; i < 3; i++) {
int y = judge_maxRows(i, a);
int z = judge_minCols(y, a);
if (i == z && a[i][y] == a[z][y]) {
SaddlePoint = true;
cout << "[" << i << "][" << y << "]" << "=" << a[i][y] << " is a Saddle Point" << endl;
}
}
if (!SaddlePoint) {
cout << "No Saddle Point" << endl;
}
return 0;
}