hdu杭电c++实践报告二

  • 实践要求

2013.蟠桃记

输入数据有多组,每组占一行,包含一个正整数n(1<n<30),表示只剩下一个桃子的时候是在第n天发生的。

Output

对于每组输入数据,输出第一天开始吃的时候桃子的总数,每个测试实例占一行。

Sample Input

2

4

Sample Output

4

22

  1. 青年歌手大奖赛_评委会打分

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

Output

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

Sample Input

3 99 98 97

4 100 99 98 97

Sample Output

98.00

98.50

2015.偶数求和

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

Input

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

Output

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

Sample Input

3 2

4 2

Sample Output

3 6

3 7

2016.数据的交换输出

输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数。

Input

输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,不做处理。

Output

对于每组输入数据,输出交换后的数列,每组输出占一行。

Sample Input

4 2 1 3 4

5 5 4 3 2 1

0

Sample Output

1 2 3 4

1 4 3 2 5

2017.字符串统计

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

Input

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

Output

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

Sample Input

2

asdfasdf123123asdfasdf

asdf111111111asdfasdfasdf

Sample Output

6

9

  1. 数列有序!

有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

  1. Snooker

background:
Philip likes to play the QQ game of Snooker when he wants a relax, though he was just a little vegetable-bird. Maybe you hadn't played that game yet, no matter, I'll introduce the rule for you first.
There are 21 object balls on board, including 15 red balls and 6 color balls: yellow, green, brown, blue, pink, black.
The player should use a white main ball to make the object balls roll into the hole, the sum of the ball's fixed value he made in the hole is the player's score. The player should firstly made a red ball into the hole, after that he gains red-ball's value(1 points), then he gets the chance to make a color ball, then alternately. The color ball should be took out until all the red-ball are in the hole. In other word, if there are only color balls left on board, the player should hit the object balls in this order: yellow(2 point), green(3 point), brown(4 point), blue(5 point), pink(6 point), black(7 point), after the ball being hit into the hole, they are not get out of the hole, after no ball left on board, the game ends, the player who has
the higher score wins the game. PS: red object balls never get out of the hole.
I just illustrate the rules that maybe used, if you want to contact more details, visit http://sports.tom.com/snooker/ after
the contest.

for example, if there are 12 red balls on board(if there are still red ball left on board, it can be sure that all the color
balls must be on board either). So suppose Philp can continuesly hit the ball into the hole, he can get the maximun score is
12 * 1 (12 red-ball in one shoot) + 7 * 12(after hit a red ball, a black ball which was the most valuable ball should be the target) + 2 + 3 + 4 + 5 + 6 + 7(when no red ball left, make all the color ball in hole).
Now, your task is to judge whether Philip should make the decision to give up when telling you the condition on board(How many object balls still left not in the hole and the other player's score). If Philp still gets the chance to win, just print "Yes", otherwise print "No". (PS: if the max score he could get on board add his current score is equal to the opponent's current score, still output "Yes")

Input

The first line contains a numble N indicating the total conditions. Then followed by N lines, each line is made of three integers:
Ball_Left P_Score O_Score represeting the ball number left on board, Philp's current score, and the opponent's current score.
All the input value are in 32 bit integer value range.

Output

You should caculate the max score left Philp can gain, and judge whether he has the possiblity to win.

Sample Input

2

12 1 1

1 30 39

Sample Output

Yes

No

  1. Treasure the new start, freshmen!

background:
A new semester comes , and the HDU also meets its 50th birthday. No matter what's your major, the only thing I want to tell you is:"Treasure the college life and seize the time." Most people thought that the college life should be colorful, less presure.But in actual, the college life is also busy and rough. If you want to master the knowledge learned from the book, a great deal of leisure time should be spend on individual study and practise, especially on the latter one. I think the every one of you should take the learning attitude just as you have in senior school.
"No pain, No Gain", HDU also has scholarship, who can win it? That's mainly rely on the GPA(grade-point average) of the student had got. Now, I gonna tell you the rule, and your task is to program to caculate the GPA.
If there are K(K > 0) courses, the i-th course has the credit Ci, your score Si, then the result GPA is
GPA = (C1 * S1 + C2 * S2 +……+Ci * Si……) / (C1 + C2 + ……+ Ci……) (1 <= i <= K, Ci != 0)
If there is a 0 <= Si < 60, The GPA is always not existed.

 Input

The first number N indicate that there are N test cases(N <= 50). In each case, there is a number K (the total courses number), then K lines followed, each line would obey the format: Course-Name (Length <= 30) , Credits(<= 10), Score(<= 100).
Notice: There is no blank in the Course Name. All the Inputs are legal

Output

Output the GPA of each case as discribed above, if the GPA is not existed, ouput:"Sorry!", else just output the GPA value which is rounded to the 2 digits after the decimal point. There is a blank line between two test cases.

Sample Input

2

3

Algorithm 3 97

DataStruct 3 90

softwareProject 4 85

2

Database 4 59

English 4 81

Sample Output

90.10

Sorry!

  1. 汉诺塔III

约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下、由小到大顺序串着由64个圆盘构成的塔。目的是将最左边杆上的盘全部移到右边的杆上,条件是一次只能移动一个盘,且不允许大盘放在小盘的上面。
现在我们改变游戏的玩法,不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允许大盘放到下盘的上面。
Daisy已经做过原来的汉诺塔问题和汉诺塔II,但碰到这个问题时,她想了很久都不能解决,现在请你帮助她。现在有N个圆盘,她至少多少次移动才能把这些圆盘从最左边移到最右边?

 Input

包含多组数据,每次输入一个N值(1<=N=35)。

 Output

对于每组数据,输出移动最小的次数。

 Sample Input

1

3

12

 Sample Output

2

26

531440

  1. Coin Change

Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.

For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.

Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 100 coins.

 Input

The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.

 Output

For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.

 Sample Input

11

26

 Sample Output

4

13

  • 实践代码

2013.#include<iostream> 

#include<iomanip>

using namespace std;

int main()

{

int n, a, b;

while (cin >> n)

{

a = 1;

for (int i = 1; i < n; i++)

{

a = (a + 1) * 2;

b = a;

}

cout << b << endl;

}

return 0;

}

2014.#include<iostream> 

#include<iomanip>

using namespace std;

int main()

{

int n; int a[200];

while (cin >> n)

{

for (int i = 0; i < n; i++)

{

cin >> a[i];//输入评分

}

int z;

z = 0;

for (int j = 0; j < n; j++)

{

z += a[j];//总分

}

int max = a[0], min = a[0];

for (int i = 0; i < n; i++)

{

if (max < a[i])

max = a[i];

if (min > a[i])

min = a[i];

}

double w = (z-max-min) *1.0/ (n-2);//排除最大与最小后得到数值

cout<<fixed << setprecision(2) << w << endl;

}

return 0;

}

2015.

#include<iostream>

using namespace std;

int main()

{

int m, n;

while (cin >> n >> m)

{

int sum = 0;

int a = 2;//首项为2

for (int i = 1; i <= n; i++)

{

sum += a;

a += 2;

if (i % m == 0)

{

cout << sum / m ;

sum = 0;

if (i != n) cout << " ";

}

}

if (n % m != 0)//如果最后不足m个,则以实际数量求平均值

{

cout << sum / (n % m);

}

cout << endl;

}

return 0;

}

2016.

#include<iostream> 

#include<iomanip>

using namespace std;

int main()

{

int n, addr, min, w;

while (cin >> n && n != 0)

{

int a[105];

for (int i = 0; i < n; i++)

{

cin >> a[i];//输入

}

min = a[0];

addr = 0;

for (int i = 0; i < n; i++)

{

if (min > a[i])

{

min = a[i];//最小值

addr = i;//最小数字的下标

}

}

int z;

z = a[0];

a[0] = a[addr];

a[addr] = z;

for (int i = 0; i < n-1; i++)//注意避免多输出空格,否则会格式错误

{

cout << a[i] << " ";

}

cout << a[n - 1];

cout << endl;

}

return 0;

}

2017.

#include<iostream> 

#include<string>

#include<iomanip>

using namespace std;

int main()

{

int n;

string str1;

cin >>n; //测试实例的个数

for (int i = 0; i < n; i++)

{

cin >> str1;

int w = str1.length();//字符串长度

int sum = 0;//数字数

for (int j = 0; j < w; j++)

{

if (str1[j] >= '1' && str1[j] <= '9')

sum++;

}

cout << sum << endl;

str1.clear();//清除字符串内所存数据

}

return 0;

}

2019.

#include<iostream> 

#include<string>

#include<iomanip>

using namespace std;

int main()

{

int m, n;

while (cin >> n >> m && n != 0 && m != 0) //输入n和m

{

int s1[130],s2[105];

for (int i = 0; i < n; i++)

{

cin >> s1[i];

}

int addr=0;

for (int i = 0; i < n; i++)

{

if (s1[i] < m)//将插入数字与原数列中数字进行对比

addr = i;

}

for (int i = 0; i <= addr; i++)

{

s2[i] = s1[i];

}

s2[addr + 1] = m;

for (int i = addr + 2; i < n + 1; i++)

{

s2[i] = s1[i-1];//存入s2

}

for (int i = 0; i < n; i++)

{

cout << s2[i] << " ";

}

cout << s2[n] << endl;

}

return 0;

}

2064.

将n-1个圆盘移动到目标盘

将最大盘移动到中间盘

将n-1个圆盘移动到初始盘

将最大盘移动到目标盘

将n-1个圆盘移动到目标盘

第一步,第三步,第五步,都需要移动n-1个圆盘

第二步,第四步都需要移动一次

抽象出函数f(n)=3*f(n-1)+2;

#include<iostream>

using namespace std;

long long change(int n)

{

if (n == 1)

return 2;

else

return 3 * change(n - 1) + 2;

}

int main()

{

int n;

while (cin >> n)

{

cout << change(n)<<endl;

}

}

  • 实践体会

成4个题目的前提下,继续提高编程

汉诺塔问题的在于递归函数的计,这尤为重要。数列的问题使用冒泡排序或者循环比较求解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值