[C++] 实验二 程序控制(选择+循环)


实验一

【问题描述】
利用循环结构,编制程序显示出如下“图形”。
1
131
13531
1357531
135797531

【输入形式】打印图形的行数
【输出形式】打印图形
【样例输入】
3
【样例输出】
1
131
13531
【ps】
注意行末的换行

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int N;
	cin>>N;
	for (int i=1;i<=N;i++)
	{
		for (int j=1;j<=2*i-1;j+=2)
			cout<<j;
		for (int j=2*i-3;j>=1;j-=2)
			cout<<j;
		cout<<endl;
	}
}

实验二

【问题描述】
某商店出售四种商品: A商品每公斤2.75元;B商品每个12.5 元;C商品每米26.8 元;D商品每台512元,超过3台优惠10%,超过8台优惠15%。设计一个计算价格的程序,通过输入购买四种商品的数量,计算并显示每种商品应付金额以及总金额。

【输入形式】
输入每种商品的数量。

【输出形式】
输出每种商品的应付金额和总金额。

【样例输入】
1 2 3 2

【样例输出】
A:2.75
B:25
C:80.4
D:1024
total:1132.15

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	double a,b,c,d,Sum=0;
	cin>>a>>b>>c>>d;
	cout<<"A:"<<a*2.75<<endl;
	cout<<"B:"<<b*12.5<<endl;
	cout<<"C:"<<c*26.8<<endl;
	Sum+=a*2.75+b*12.5+c*26.8;
	if (d>3&&d<=8) d*=512*0.9;
	else if (d>8) d*=512*0.85;
		else d*=512;
	Sum+=d;		
	cout<<"D:"<<d<<endl;
	cout<<"total:"<<Sum<<endl;
}

实验三

【问题描述】
求n以内被3除余1且个位数为6的所有整数(如16、46、…、286等)并显示在屏幕上。

【输入形式】
输入某个数

【输出形式】
输出所有结果,空格隔开

【样例输入】
300

【样例输出】
16 46 76 106 136 166 196 226 256 286

【ps】
注意判断输出空格的时机

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int N,i,t;
	cin>>N;
	for (i=6;i<=N;i+=10)
		if (i%3==1) 
		{
			if (i>16) cout<<" ";
			cout<<i;
		}
}

实验四

【问题描述】
编写一程序统计参赛选手的得分,计分标准为去掉一个最高分和一个最低分后,对剩余得分求平均值。要求首先从键盘输入评委的个数num,然后输入num个分数(分数为小于等于10的一个正实数),输出最终得分。

【输入形式】
输入评委个数和各自分数。

【输出形式】
输出得分。

【样例输入】
5
9.2 9.6 9.5 9.7 9.7

【样例输出】
9.6

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	double N,Sum,a,max_a=0,min_a=30;
	cin>>N;
	for (int i=1;i<=N;i++)
	{
		cin>>a;
		min_a = min(min_a,a);
		max_a = max(max_a,a);
		Sum+=a;
	}
	cout<<(Sum-min_a-max_a)/(N-2)<<endl;
}

实验五

【问题描述】
设计一个程序,对于用户输入的任意正整数a(a≥1)和b(b≥2),求出满足bn≤a的最大整数n。

【输入形式】
两个数。

【输出形式】
一个数据。

【样例输入】
30 5

【样例输出】
2

做法一:
(计数器累乘)

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int x,a,b,t=0;
	cin>>a>>b;
	x=1;
	while (x<=a)
	{
		x*=b;
		t++;
	}
	if (x>a) t--;
	cout<<t<<endl;
}

做法二:
(先求对数,作商并取整)

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int a,b,ans;
	cin >> a >> b;
	ans = log(a)/log(b);
	cout << ans;
    return 0;
}
`
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值