中学生C++练习题
- C++编程比赛题目:编写一个程序,接受一组数字的输入,然后统计并输出不超过这组数字的平均值的数字的个数
#include <iostream>
using namespace std;
int main() {
int n;
cout << "请输入数字的数量: ";
cin >> n;
// 声明一个数组来存储输入的数字
int numbers[n];
int sum = 0;
// 输入数字
for (int i = 0; i < n; i++) {
cout << "请输入第 " << i + 1 << " 个数字: ";
cin >> numbers[i];
sum += numbers[i];
}
// 计算平均值
double average = static_cast<double>(sum) / n;
// 统计不超过平均值的数字的个数
int count = 0;
for (int i = 0; i < n; i++) {
if (numbers[i] <= average) {
count++;
}
}
// 输出结果
cout << "平均值为: " << average << endl;
cout << "不超过平均值的数字个数为: " << count << endl;
return 0;
}
- 输入一些数,求第二大的值
#include <iostream>
using namespace std;
int main() {
int n;
cout << "请输入数字的数量: ";
cin >> n;
if (n < 2) {
cout << "至少需要输入两个数字才能找到第二大值。" << endl;
return 1;
}
int first_max = INT_MIN; // 初始最大值为负无穷
int second_max = INT_MIN; // 初始第二大值为负无穷
for (int i = 0; i < n; i++) {
int num;
cout << "请输入第 " << i + 1 << " 个数字: ";
cin >> num;
if (num > first_max) {
second_max = first_max;
first_max = num;
} else if (num > second_max && num != first_max) {
second_max = num;
}
}
if (second_max == INT_MIN) {
cout << "没有找到第二大值。" << endl;
} else {
cout << "第二大值为: " << second_max << endl;
}
return 0;
}
- 输入一些数,哪两个数最接近
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
int n;
cout << "请输入数字的数量: ";
cin >> n;
if (n < 2) {
cout << "至少需要输入两个数字才能找到最接近的两个数字。" << endl;
return 1;
}
vector<int> numbers;
for (int i = 0; i < n; i++) {
int num;
cout << "请输入第 " << i + 1 << " 个数字: ";
cin >> num;
numbers.push_back(num);
}
int minDiff = abs(numbers[0] - numbers[1]);
int closest1 = numbers[0];
int closest2 = numbers[1];
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int diff = abs(numbers[i] - numbers[j]);
if (diff < minDiff) {
minDiff = diff;
closest1 = numbers[i];
closest2 = numbers[j];
}
}
}
cout << "最接近的两个数字是: " << closest1 << " 和 " << closest2 << endl;
return 0;
}
效率更高一点算法?
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
int main() {
int n;
cout << "请输入数字的数量: ";
cin >> n;
if (n < 2) {
cout << "至少需要输入两个数字才能找到最接近的两个数字。" << endl;
return 1;
}
vector<int> numbers;
for (int i = 0; i < n; i++) {
int num;
cout << "请输入第 " << i + 1 << " 个数字: ";
cin >> num;
numbers.push_back(num);
}
sort(numbers.begin(), numbers.end()); // 排序数组
int minDiff = INT_MAX;
int closest1, closest2;
for (int i = 0; i < n - 1; i++) {
int diff = numbers[i + 1] - numbers[i];
if (diff < minDiff) {
minDiff = diff;
closest1 = numbers[i];
closest2 = numbers[i + 1];
}
}
cout << "最接近的两个数字是: " << closest1 << " 和 " << closest2 << endl;
return 0;
}
- 统计字符1的个数
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cout << "请输入一个字符串: ";
cin >> input;
int count = 0;
for (char c : input) {
if (c == '1') {
count++;
}
}
cout << "字符 '1' 的个数是: " << count << endl;
return 0;
}
- 前n个整数顺序写在以其,数一数0~9的数字各出现多少次?
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cout << "请输入一个正整数 n: ";
cin >> n;
if (n <= 0) {
cout << "请输入一个有效的正整数。" << endl;
return 1;
}
vector<int> counts(10, 0); // 初始化一个长度为10,每个元素都为0的向量,用于统计0~9的出现次数
for (int i = 1; i <= n; i++) {
int num = i;
while (num > 0) {
int digit = num % 10; // 获取最低位的数字
counts[digit]++; // 增加对应数字的计数
num /= 10; // 去掉最低位
}
}
// 输出各数字出现次数
for (int i = 0; i < 10; i++) {
cout << "数字 " << i << " 出现 " << counts[i] << " 次" << endl;
}
return 0;
}