项目2:数组选择
从键盘中输入10个数放在数组A中,将该数组中不重复的数放到数组B中
#include<iostream> using namespace std; int main() { bool m; int a[10], b[10], i,j, k=0,n=0; cout << "请输入10个数:"; for (i = 0; i < 10; i++) cin >> a[i]; { for (i = 0; i < 10; i++) { m = 1; for (j = 0; j < 10; j++) { if (i == j)continue; if (a[i] == a[j]) m = 0; } if (m) { b[k] = a[i]; k++; } } cout << "A:"; for (i = 0; i < 10; i++) { cout << a[i] << " "; } cout << endl; cout << "B:"; for (n = 0; n < k; n++) { cout << b[n] << " "; } cout << endl; return 0; } }
项目3:成绩
在数组score中将要存储某小组程序设计的成绩(设有10人),编程实现下列功能:
(1)输入小组人数及成绩,要保证成绩在0-100之间;
(2)输出该小组的最高成绩、最低成绩、平均成绩;
(3)输出考得最高成绩和最低成绩的同学的人数;
(4)输出考得最高成绩和最低成绩的同学的学号(设数组下标即学号,可能有相同的成绩)。
(5)(选做)求出所有同学成绩的标准偏差,标准偏差公式为,其中为xi样本(即某同学成绩),x(上带一横)为均值(前面已经求出),N为样本数目;
#include<iostream> using namespace std; int main() { int N, i = 0, j = 0, k = 0,m, n; double a[10], max = 0, min = 0, sum = 0, average = 0; cout<< "请输入10个同学的成绩(0到100之间):"; for (i = 0; i < 10; i++) { cin >> a[i]; } max = a[0]; min = a[0]; for (i = 1; i<10; i++) { if (a[i]>max) max = a[i]; if (a[i]<min) min = a[i]; } cout << "最高分为" << max << "," << "最低分为" << min << ","; for (i = 0; i<10; i++) { sum = sum + a[i]; } average = sum / 10.0; cout << "平均分为" << average << endl; cout << "最高分同学的学号为"; for (i = 0; i<10; i++) { if (max == a[i]) { j++; m= i+1; cout << m << " "; } } cout << endl; cout << "最低分同学的学号为"; for (i = 0; i < 10; i++) { if (min == a[i]) { k++; n = i + 1; cout << n <<" "; } } cout << endl; cout << "分数最高的同学有" << j << "个" << endl; cout << "分数最低的同学有" << k << "个" << endl; return 0; }
项目4:字符串
统计输出字符串中(大/小写)字母个数,数字个数及其它字符个数。
#include<iostream> #include<cstdio> using namespace std; int main() { char str[50]; int i = 0, m = 0, n = 0, j = 0, k = 0; cout << "请输入字符串:"; gets(str); while (str[i] != '\0') { if (str[i] >= '0'&&str[i] <= '9') m++; else if (str[i] >= 'a'&&str[i] <= 'z') n++; else if (str[i] >= 'A'&&str[i] <= 'Z') j++; else k++; i++; } cout << "其中数字个数: " << m << endl; cout << "小写字母个数:" << n << endl; cout << "大写字母个数: " << j << endl; cout << "其他字符个数: " << k << endl; return 0; }
时间: 2024-10-10 05:29:33