您的位置:首页 > 编程语言 > C语言/C++

C++学习笔记(刷题ing ) 航电OJ(日常水题)

2019-07-08 19:47 531 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_42012437/article/details/95090913

航电oj2014:
青年歌手大奖赛中,评委会给参赛选手打分。选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分。

Input

输入数据有多组,每组占一行,每行的第一个数是n(2<n<=100),表示评委的人数,然后是n个评委的打分。

Output

对于每组输入数据,输出选手的得分,结果保留2位小数,每组输出占一行。

Sample Input

3 99 98 97
4 100 99 98 97

Sample Output

98.00
98.50

#include<iostream>
#include <iomanip>
#include<algorithm>
using namespace std;
int main()
{
int  n;
while(cin >> n) {
double  a[100] = { 0 };
double avg = 0;
for(int i =0;i<n;i++){
cin >> a[i];
}
sort(a, a+n);
for (int i = 1; i < n-1; i++) {
avg += a[i];
}
avg = avg / (n - 2);
cout << setiosflags(ios::fixed) << setprecision(2);
cout << avg << endl;
}
}

航电oj2015:
Problem Description

有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值序列。

Input

输入数据有多组,每组占一行,包含两个正整数n和m,n和m的含义如上所述。

Output

对于每组输入数据,输出一个平均值序列,每组输出占一行。

Sample Input

3 2
4 2

Sample Output

3 6
3 7

#include<iostream>
using namespace std;
int main()
{
int  n,m,x,y;
while(cin >> n >> m) {
int  a[100] = { 0 };
for (int i = 0; i < 100; i++) {
a[i] = 2 + 2 * i;
}
x = n / m;//商和余数
y = n % m;
for (int i = 0; i < n - y; i += m) {
double avg = 0;
for (int j = i; j < i + m; j++) {
avg += a[j];
}
avg = avg / m;
cout << avg ;
if (i + m != n - y) { cout << ' '; }
}
if (n%m != 0) {
double avg = 0;
for (int k = x * m; k < n; k++) {
avg += a[k];
}
avg = avg / y;
cout<<' ' << avg;
}
cout << endl;
}
}

航电oj2017:
Problem Description

对于给定的一个字符串,统计其中数字字符出现的次数。

Input

输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

Output

对于每个测试实例,输出该串中数值的个数,每个输出占一行。

Sample Input

2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf

Sample Output

6
9

#include<iostream>
#include<string>
using namespace std;
int main()
{
int  n,length;
cin >> n;
string str;
while (n--) {
cin >> str;
int count = 0;
length = str.length();
for (int i = 0; i < length; i++) {
if (str[i] >= 48 && str[i] <= 57) count++;
}
cout << count << endl;
}
}

航电oj2018:
Problem Description

有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?

Input

输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。

Output

对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。

Sample Input

2
4
5
0

Sample Output

2
4
6

#include<iostream>
using namespace std;
int main()
{
int  n, years = 1, sum = 1;
int a[10000] = { 3,0,0,0 };
while (cin >> n) {
if (n == 0) break;
while (years != n) {
for (int i = 0; i < sum; i++) {
a[i]++;
if (a[i] >= 4)sum++;
}
years++;
}

cout << sum << endl;
}
}

航电oj2019
Problem Description

有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。

Input

输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。

Output

对于每个测试实例,输出插入新的元素后的数列。

Sample Input

3 3
1 2 4
0 0

Sample Output

1 2 3 4

#include<iostream>
using namespace std;
int main()
{
int  n,m;
int a[101] = { 0 };
while (cin >> n >> m) {
if (n == 0 && m == 0) break;
int flag = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
if (a[i] > m) {
for (int j = n; j > i; j--) {
a[j] = a[j - 1];
}
a[i] = m;
break;
}
}
for (int i = 0; i <= n; i++) {
if (flag > 0) { cout << ' '; }
if (i == n) { cout << a[i] << endl; }
else{ cout << a[i]; }
flag++;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: