实验4-1(A)
【题目描述】
编程序,实现如下功能:
(1)定义两个一维数组x,y,不超过50个元素。
(2)从键盘输入k个整数到数组x中。
(3)计算x中数据的平均值ave及大于平均值的元素个数n并输出。
(4)将数组x中数据复制到数组y中,重复的数据只存储一次,最后输出y中的数据。
【输入形式】
输入两行,第一行为输入的数据个数,第二行若干整数,每个整数后面有一个空格用于分隔;
【输出形式】
第一行若干整数,每个整数后面有一个空格,最后一个空格后换行;第二行若干整数,每个整数后面有一个空格,最后一个空格后不需要换行;
【样例输入】
6
6 3 4 3 2 9
【样例输出】
4.5 2
6 3 4 2 9
#include<string>
#include<iostream>
using namespace std;
int main()
{
int res=0,N,x[60],y[60],len_y=0;
bool flag;
double Sum=0;
cin>>N;
for (int i=1;i<=N;i++) //读入,求和
{
cin>>x[i];
Sum+=x[i];
}
for (int i=1;i<=N;i++) //记录大于平均值的数,并将未出现的数放入数组y
{
if (x[i]>Sum/N) res++;
flag = true;
for (int j=1;j<=len_y;j++) if (x[i]==y[j]) flag = false;
if (flag) {len_y++;y[len_y]=x[i];}
}
cout<<Sum/N<<" "<<res<<endl<<y[1];
for (int i=2;i<=len_y;i++) cout<<' '<<y[i];
}
实验4-2(B)
【题目描述】
有 12 人围坐成一圈玩报数游戏,从1号人员开始顺时针报数,报到k的人员被淘汰出局;接着仍沿顺时针方向从被淘汰出局者的下一人员又重新从 1 开始报数,报到 k的人被淘汰;如此继续,直到最后只剩下一个人时停止。请编写程序输出最后所剩那个人的编号。
注意:
- 假设参加游戏的人的编号沿顺时针方向依次为 1 到 12,可以使用数组来存放各数据;
- k>1,由用户通过 cin 输入指定。
【输入形式】
输入一个整数,代表报数值;
【输出形式】
输出一个整数,即最后剩下的人的编号;
【样例输入】
3
【样例输出】
10
方法一:循环模拟报数过程
#include<string>
#include<iostream>
using namespace std;
int main()
{
int N,i=0,peo_num=12,t;
bool circle[13];
cin>>N;
while (peo_num>0)
{
t=0;
while (t<N) //开始一轮报数
{
if (!circle[i]) t++; //若未被淘汰,则报数成功
i=(i+1)%12;
}
i=(i+11)%12; //让i回到被淘汰的人
circle[i]=true;
peo_num--;
}
cout<<i+1<<endl;
}
方法二:链表模拟报数过程
#include<string>
#include<iostream>
using namespace std;
int main()
{
int N,circle[13],i=0,peo_num=12,peo_now=1,peo_last;
cin>>N;
for (int i=1;i<=12;i++) circle[i]=i+1; //初始化链表,使每个人指向顺时针下一个人
circle[12]=1;
while (peo_now!=circle[peo_now]) //(peo_num>0)
{
i++;
if (i==N){ //报到K次被淘汰
circle[peo_last]=circle[peo_now]; //将淘汰者移出链表
peo_now=circle[peo_now];
//peo_num--;
i=0;
}
else{
peo_last=peo_now; //未到K,顺次报数
peo_now=circle[peo_now];
}
}
cout<<peo_now<<endl;
}
方法三:数学方法降维打击
#include <iostream>
using namespace std;
const int peo_total = 12;
int main()
{
int N,res=0;
cin>>N;
for (int i=1;i<=peo_total;i++) res=(res+N)%i;
cout<<res+1<<endl;
}
实验4-3(C)
【题目描述】
小宗想知道两个日期之间所间隔的天数,他希望有一个日期计算器,输入两个日期后能够自动计算之间的天数。要求:设计相应的函数完成天数的计算,在主函数中验证正确性。
【输入形式】
按照年月日的顺序输入两个日期,年月日之间用一个空格分隔;
【输出形式】
输出两个日期之间的天数,即一个整数,整数后不需要换行;
【样例输入】
2016 3 6
2017 1 1
【样例输出】
301
【ps】
可以将整个问题分为三个部分:
- 计算起始年已过天数d1
- 计算终末年已过天数d2
- 最后统计
(终末年 − 起始年) ∗ 365 + 终末年天数 d 2 − 起始年天数 d 1 + 闰年天数 (终末年-起始年)*365+终末年天数d2-起始年天数d1+闰年天数 (终末年−起始年)∗365+终末年天数d2−起始年天数d1+闰年天数
#include<string>
#include<iostream>
using namespace std;
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool checkrun(int y) //判断是否为闰年
{
if (y%400==0||(y%4==0&&y%100!=0)) return true;
return false;
}
int ca_day(int m,bool flag) //统计本年已经度过的日子
{
int days=0;
for (int i=1;i<m;i++)
{
if (i==2&&flag) days++;
days+=month[i];
}
return days;
}
int main()
{
int y1,m1,d1,y2,m2,d2,days=0;
cin>>y1>>m1>>d1;
cin>>y2>>m2>>d2;
days+=d2-d1+ca_day(m2,checkrun(y2))-ca_day(m1,checkrun(y1));
for (int i=y1;i<y2;i++) //统计中间年份日子总数
if (checkrun(i)) days+=366;
else days+=365;
cout<<days<<endl;
}
实验4-4(D)
【题目描述】
对于整型数组a[10]和b[10],编制程序完成下列任务:
(1)由用户从键盘为两个数组输入值;
(2)求出两个数组的最大值和最小值;
(3)把数组a和b中的整数分别从小到大和从大到小排序;
(4)把两个有序的数组a和b组成一个长度为20的有序数组c[20],使数组c的顺序为从小到大。
【输入形式】
输入两行整数,每行10个,第一行是数组a里的数组,第二行是数组b里的数值;
【输出形式】
输出五行:
- 第一行有两个整数,分别是数组a的最大值和最小值,两个整数之间用一个空格分隔;
- 第二行有两个整数,分别是数组b的最大值和最小值,两个整数之间用一个空格分隔;
- 第三行按照从小到大的顺序输出数组a里的数值,每个数字后面有一个空格,最后一个数字后面也有空格;
- 第四行按照从大到小的顺序输出数组b里的数值,每个数字后面有一个空格,最后一个数字后面也有空格;
- 第五行按照从小到大的顺序输出合并后数组c里的数值,每个数字后面有一个空格,最后一个数字后面也有空格。
【样例输入】
2 5 9 1 3 4 0 6 7 8
10 5 25 9 6 3 7 1 2 13
【样例输出】
9 0
25 1
0 1 2 3 4 5 6 7 8 9
25 13 10 9 7 6 5 3 2 1
0 1 1 2 2 3 3 4 5 5 6 6 7 7 8 9 9 10 13 25
#include<string>
#include<iostream>
using namespace std;
void getin(int a[]) //读入,并求出最大、最小值
{
int max_a=0,min_a=0;
cin>>a[1];
min_a=max_a=a[1];
for (int i=2;i<=10;i++)
{
cin>>a[i];
if (min_a>a[i]) min_a=a[i];
if (max_a<a[i]) max_a=a[i];
}
cout<<max_a<<" "<<min_a<<endl;
}
void sort(int a[],int len) //冒泡排序
{
int temp;
for (int i=1;i<=len-1;i++)
for (int j=1;j<=len-i;j++)
if (a[j]>a[j+1]) {
temp = a[j];
a[j] =a[j+1];
a[j+1] = temp;
}
}
int main()
{
int a[15],b[15],c[25];
getin(a);getin(b);
for (int i=1;i<=10;i++)
{
c[i]=a[i];
c[i+10]=b[i];
}
sort(a,10);sort(b,10);sort(c,20);
for (int i=1;i<=10;i++) cout<<a[i]<<" "; cout<<endl;
for (int i=10;i>=1;i--) cout<<b[i]<<" "; cout<<endl;
for (int i=1;i<=20;i++) cout<<c[i]<<" "; cout<<endl;
}
实验4-5(E)
【题目描述】
利用cin.getline()函数从键盘录入一句英文,其中每个单词之间用一个空格隔开,最后用’.'结束。统计该句话中单词的个数,并依次输出每个单词。输出个数后换行,输出每个单词后也换行。注意:在本平台下,cin.getline()函数的使用方式如下:
char s[50];
cin.getline(s,50); //最多存储49个字符
【输入形式】
输入一句英文,其中每个单词之间用一个空格隔开,最后一个单词后面用英文的’.'作为结束;
【输出形式】
输出每个单词后换行,最后一行输出单词的数量。
【样例输入】
I like juice.
【样例输出】
I
like
juice
3
【ps】
由于首末均无空格,且要求以‘.’结尾,因此一旦出现" _ " 与 ’ . ’ 即可记为一个单词。
#include<string>
#include<iostream>
using namespace std;
int main()
{
char s[50];
cin.getline(s,50);
int i=-1,res=0;
while(s[++i]!='\0')
{
if (s[i]==' '||s[i]=='.') //判断单词是否结束
{
cout<<endl;
res++;
}
else cout<<s[i];
}
cout<<res<<endl;
}