实践题1

【问题描述】

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

打印图形的行数

【输出形式】

打印图形
【样例输入】

  3

【样例输出】

1
131
13531

【题解】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
int main()
{
int i,m,n,j;
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
cout<<2*j-1;
for(m=i-1;m>=1;m--)
cout<<2*m-1;
cout<<endl;
}
}

实践题2

【问题描述】

  某商店出售四种商品: 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

【题解】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;
int main()
{
double a,b,c,d,pd;
cin>>a>>b>>c>>d;
if(d>8)
{
pd=d*512*0.85;
}
else if(d>3)
{
pd=d*512*0.9;
}
else
{
pd=d*512;
}
cout<<"A:"<<2.75*a<<endl<<"B:"<<12.5*b<<endl<<"C:"<<26.8*c<<endl<<"D:"<<pd<<endl<<"total:"<<2.75*a+12.5*b+26.8*c+pd;
}

实践题3

【问题描述】

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

【输入形式】

输入某个数

【输出形式】

输出所有结果,空格隔开

【样例输入】

300

【样例输出】

16 46 76 106 136 166 196 226 256 286

【题解】

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;
int main()
{
int n,i;
cin>>n;
for(i=1;i<=n;i++)
{
if(i%3==1&&i%10==6)
cout<<i<<" ";
}
}

实践题4

【问题描述】

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

【输入形式】

输入评委个数和各自分数。

【输出形式】

输出得分。

【样例输入】

5
9.2 9.6 9.5 9.7 9.7

【样例输出】

9.6

【题解】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;
int main()
{
int num,i;
double max,min,sum,avg;
cin>>num;
double score[num];
for(i=0;i<num;i++)
{
cin>>score[i];
}
min=score[0];
max=score[0];
for(i=1;i<num;i++)
{
if(score[i]<min)
min=score[i];
if(score[i]>max)
max=score[i];
}
for(i=0,sum=0;i<num;i++)
{
sum+=score[i];
}
avg=(sum-min-max)/(num-2);
cout<<avg;
}

实践题5

【问题描述】

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

【输入形式】

两个数。

【输出形式】

一个数据。

【样例输入】

30 5

【样例输出】

2

【题解】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
int main()
{
int i,a,b,result;
cin>>a>>b;
for(i=1,result=1;;i++)
{
result*=b;
if(result>a)
{
cout<<i-1;
break;
}
}
}