跳到正文

个人记录

暑期班课后练习解析(第七课)

sky1994约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年5月28日
更新
2026年5月28日
篇幅
约 7 分钟

版本

  1. v1当前

第一题:P1423 小玉在游泳

题目链接:https://www.luogu.com.cn/problem/P1423

定义一个变量表示小玉一次的游泳距离,每游一次就把她游一次的距离乘98%,并把游泳次数加一,当游过的距离大于输入的距离时,跳出循环并输出游泳次数。注意输入的距离可能为小数,所以它要定义成浮点数:

C++
#include<iostream>
using namespace std;
int main(){
	double s,x=0,d=2;
	int ans = 0;
	cin >> s;
	while(x<s){
		x+=d;
		ans++;
		d *=0.98;
	}
	cout << ans;
	return 0;
}

第二题:P5721 【深基4.例6】数字直角三角形

题目链接:https://www.luogu.com.cn/problem/P5721

可以看出要输出a(a+1)a(a+1)个数

每行输出a,a-1……,1个数

所以用i记录现在要输出什么

j记录在第几列

j>a之后就换行

输出是注意所有数字都是 2 位组成的即可:

C++
#include<cstdio>
int main(){
	int n,t=1;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=(n+1-i);j++){
			printf("%02d",t);
			t++;
		}
		printf("\n");
	}
	return 0;
}

第三题:T241590 奇数求和

题目链接:https://www.luogu.com.cn/problem/T241590

从m到n遍历,一边遍历一边判断,如果是奇数就加上,如果是偶数就不进行操作:

C++
#include<iostream>
using namespace std;
int main(){
	int m,n,sum=0;
	cin >> m>>n;
	for(m;m<=n;m++){
		if(m%2!=0) sum+=m;
	}
	cout<<sum;
	return 0;
}

第四题:B2054 求平均年龄

题目链接:https://www.luogu.com.cn/problem/B2054

将所有年龄累加起来,最后再除以总人数,得到的就是平均年龄。

需要注意的点: 因为我们最后输出的是浮点数,所以我们需要把累加年龄的变量设为 double 类型,或者在最后输出时将累加年龄的变量乘以 1.0:

C++
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	int n, tmp ,sum=0;
	cin >> n;
	for(int i = 1; i<=n;i++){
		cin >> tmp;
		sum += tmp;
	}
	printf("%.2f",1.0*sum/n);
	return 0;
}

第五题:T241593 求整数的和与均值

题目链接:https://www.luogu.com.cn/problem/T241593

和上一题解法相同:

C++
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	int n, tmp ,sum=0;
	cin >> n;
	for(int i = 1; i<=n;i++){
		cin >> tmp;
		sum += tmp;
	}
	printf("%d %.5f",sum,1.0*sum/n);
	return 0;
}

第六题:P1980 [NOIP2013 普及组] 计数问题

题目链接:https://www.luogu.com.cn/problem/P1980

做两层循环嵌套,外层循环遍历数字,内层循环把数字的每一位依次拆分出来进行比较:

C++
#include<iostream>
using namespace std;
int main(){
	int n, x, tmp, ans=0;
	cin >> n >> x;
	for(int i=1;i<=n;i++){
		tmp = i;
		while(tmp!=0){
			 if(tmp%10==x) ans++;
			 tmp /= 10;
		}
	}
	cout <<ans;
	return 0;
}

第七题:P2669 [NOIP2015 普及组] 金币

题目链接:https://www.luogu.com.cn/problem/P2669 模拟法,做两层循环嵌套,外层循环遍历每天发几枚金币,从1开始递增,内层循环把当天的金币加入总数中,并且记录现在的天数:

C++
#include<iostream>
using namespace std;
int main(){
	int k,c=0,d=0;
	cin>>k;
	for(int i=1; ; i++){
		for(int j=1;j<=i;j++){
			c+=i;
			d++;
			if(d==k){
				cout << c;
				return 0;
			}
		}
    }
}

第八题:P5722 【深基4.例11】数列求和

题目链接:https://www.luogu.com.cn/problem/P5722 本题上课讲过原题,不做赘述:

C++
#include<iostream>
using namespace std;
int main(){
	int n,s=0;
	cin >>n;
	for(int i=1;i<=n;i++){
		s+=i;
	}
	cout << s;
	return 0;
}

第九题:P1085 [NOIP2004 普及组] 不高兴的津津

题目链接:https://www.luogu.com.cn/problem/P1085 本题上课讲过原题,不做赘述:

C++
#include<iostream>
using namespace std;
int main(){
	int s,k,maxd,maxt;
	maxd = 0,maxt = 8;
	cin >> s >> k;
	if(s+k>maxt)
		maxt = s+k,maxd = 1;
	cin >> s >> k;
	if(s+k>maxt)
		maxt = s+k,maxd = 2;
	cin >> s >> k;
	if(s+k>maxt)
		maxt = s+k,maxd = 3;
	cin >> s >> k;
	if(s+k>maxt)
		maxt = s+k,maxd = 4;
	cin >> s >> k;
	if(s+k>maxt)
		maxt = s+k,maxd = 5;
	cin >> s >> k;
	if(s+k>maxt)
		maxt = s+k,maxd = 6;
	cin >> s >> k;
	if(s+k>maxt)
		maxt = s+k,maxd = 7;
	cout << maxd;
	return 0;
}

第十题:P4956 [COCI2017-2018#6] Davor

题目链接:https://www.luogu.com.cn/problem/P4956

遍历法解题,按照题目要求,k尽可能小,x尽可能大,所以外层循环k从1开始增加,内层循环x从100开始减小,满足条件时停止(return 0):

C++
#include<iostream>
using namespace std;
int main(){
	int x,k,n;
	cin>>n;
	for(k=1; ;k++){
		for(x=100;x>=1;x--){
			if(52*(7*x+21*k)==n){
				cout<<x<<endl;
				cout<<k<<endl;
				return 0;
			}
		}
	}
}
原文