HDOJ 2061-2070
2061Treasure the new start, freshmen!
Problem Description
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.
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
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!
#include <bits/stdc++.h> using namespace std; int main() { int n, num; string lesson; double c, s; int i, j; int flag; cin >> n; for (i = 0; i < n; i++) { flag=0; double sumc = 0; double sums = 0; cin >> num; while (num--) { cin >> lesson >> c >> s; if (s < 60) flag = 1; sumc += c; sums += s * c; } if (flag = 1) cout << "Sorry" << endl; else printf("%.2lf\n", sums / sumc); } }
2062Subset Squence
Problem Description
Consider the aggregate An= { 1, 2, …, n }. For example, A1={1}, A3={1,2,3}. A subset sequence is defined as a array of a non-empty subset. Sort all the subset sequece of An in lexicography order. Your task is to find the m-th one.
Input
The input contains several test cases. Each test case consists of two numbers n and m ( 0< n<= 20, 0< m<= the total number of the subset sequence of An ).
Output
For each test case, you should output the m-th subset sequence of An in one line.
Sample Input
1 1
2 1
2 2
2 3
2 4
3 10
Sample Output
1
1
1 2
2
2 1
2 3 1
翻译:给定一个集合An= { 1, 2, …, n };按词典顺序对此集合的子集进行排列,找出第m个子集并输出。
A3={1,2,3}的子序列: {1} {1, 2} {1, 2, 3} {1, 3} {1, 3, 2} {2} {2, 1} {2, 1, 3} {2, 3} {2, 3, 1} {3} {3, 1} {3, 1, 2} {3, 2} {3, 2, 1}
①去除首元素,剩下的就是A2的排列规则和空集。那么对于An来说也是如此,去除首元素来说,剩下的就是An-1的排列规则和n个空集。
f(n)=n*[f(n-1)+1]
②根据首元素分组,则g(n)就为An中每一个分组的子集序列数,首元素为n。
f(n)=n*[f(n-1)+1]
②根据首元素分组,则g(n)就为An中每一个分组的子集序列数,首元素为n。
f(n)/n=g(n),f(n-1)/(n-1)=g(n-1);
f(n)= n*g(n),f(n-1)=(n-1)g(n-1);
联立上式,有:
ng(n)=n*[(n-1)g(n-1)+1]
则g(n)=(n-1)*g(n-1)+1
ng(n)=n*[(n-1)g(n-1)+1]
则g(n)=(n-1)*g(n-1)+1
#include <bits/stdc++.h> using namespace std; const int maxn = 25; int main() { int n, m; int i, j; int g[maxn]; int num[maxn]; int temp; for (i = 1; i < maxn; i++) g[i] = (i - 1) * g[i - 1] + 1; while (cin >> n >> m) { for (i = 0; i <= maxn; i++) num[i] = i; while (n > 0 && m > 0) { temp = (m - 1) / g[n] + 1; if (temp > 0) { cout << num[temp]; for (i = temp; i <= n; i++) num[i] = num[i + 1]; m = m - ((temp - 1) * g[n] + 1); if (m != 0) cout << " "; else cout << endl; } n--; } } }
2063过山车
Problem Description
RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了。可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐。但是,每个女孩都有各自的想法,举个例子把,Rabbit只愿意和XHD或PQK做partner,Grass只愿意和linle或LL做partner,PrincessSnow愿意和水域浪子或伪酷儿做partner。考虑到经费问题,boss刘决定只让找到partner的人去坐过山车,其他的人,嘿嘿,就站在下面看着吧。聪明的Acmer,你可以帮忙算算最多有多少对组合可以坐上过山车吗?
Input
输入数据的第一行是三个整数K , M , N,分别表示可能的组合数目,女生的人数,男生的人数。0<K<=1000
1<=N 和M<=500.接下来的K行,每行有两个数,分别表示女生Ai愿意和男生Bj做partner。最后一个0结束输入。
1<=N 和M<=500.接下来的K行,每行有两个数,分别表示女生Ai愿意和男生Bj做partner。最后一个0结束输入。
Output
对于每组数据,输出一个整数,表示可以坐上过山车的最多组合数。
Sample Input
6 3 3
1 1
1 2
1 3
2 1
2 3
3 1
0
Sample Output
3
2064汉诺塔III
Problem Description
约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下、由小到大顺序串着由64个圆盘构成的塔。目的是将最左边杆上的盘全部移到右边的杆上,条件是一次只能移动一个盘,且不允许大盘放在小盘的上面。
现在我们改变游戏的玩法,不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允许大盘放到下盘的上面。
Daisy已经做过原来的汉诺塔问题和汉诺塔II,但碰到这个问题时,她想了很久都不能解决,现在请你帮助她。现在有N个圆盘,她至少多少次移动才能把这些圆盘从最左边移到最右边?
现在我们改变游戏的玩法,不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允许大盘放到下盘的上面。
Daisy已经做过原来的汉诺塔问题和汉诺塔II,但碰到这个问题时,她想了很久都不能解决,现在请你帮助她。现在有N个圆盘,她至少多少次移动才能把这些圆盘从最左边移到最右边?
Input
包含多组数据,每次输入一个N值(1<=N=35)。
Output
对于每组数据,输出移动最小的次数。
Sample Input
1
3
12
Sample Output
2
26
531440
首先初始化:对于1个盘,需要移动2次,对于2个盘子需要移动8次。
那么对于i个盘子呢,步骤:
1.需要把i-1个盘子移动到c柱的位置,
2.将最底下的一个盘子移动到b
3.之后将i-1个盘移动到b再移动到a
4.将b的那一个最大的盘,移动到c
5.将a柱上的i-1个盘子移动到b再到c
f[x]:将x个盘子,三个柱,从a到c或者从c到a的最少步数
动归方程f[i]=3*f[i-1]+2;
参考:https://blog.csdn.net/with_wine/article/details/121576250
那么对于i个盘子呢,步骤:
1.需要把i-1个盘子移动到c柱的位置,
2.将最底下的一个盘子移动到b
3.之后将i-1个盘移动到b再移动到a
4.将b的那一个最大的盘,移动到c
5.将a柱上的i-1个盘子移动到b再到c
f[x]:将x个盘子,三个柱,从a到c或者从c到a的最少步数
动归方程f[i]=3*f[i-1]+2;
参考:https://blog.csdn.net/with_wine/article/details/121576250
#include<bits/stdc++.h> using namespace std; int n; long long f[40]; int main() { f[1]=2; f[2]=8; int i; for(i=3;i<36;i++) { f[i]=3*f[i-1]+2; } while(cin>>n) { cout<<f[n]<<endl; } }
2065“红色病毒”问题
Problem Description
医学界发现的新病毒因其蔓延速度和Internet上传播的"红色病毒"不相上下,被称为"红色病毒",经研究发现,该病毒及其变种的DNA的一条单链中,胞嘧啶,腺嘧啶均是成对出现的。
现在有一长度为N的字符串,满足一下条件:
(1) 字符串仅由A,B,C,D四个字母组成;
(2) A出现偶数次(也可以不出现);
(3) C出现偶数次(也可以不出现);
计算满足条件的字符串个数.
当N=2时,所有满足条件的字符串有如下6个:BB,BD,DB,DD,AA,CC.
由于这个数据肯能非常庞大,你只要给出最后两位数字即可.
现在有一长度为N的字符串,满足一下条件:
(1) 字符串仅由A,B,C,D四个字母组成;
(2) A出现偶数次(也可以不出现);
(3) C出现偶数次(也可以不出现);
计算满足条件的字符串个数.
当N=2时,所有满足条件的字符串有如下6个:BB,BD,DB,DD,AA,CC.
由于这个数据肯能非常庞大,你只要给出最后两位数字即可.
Input
每组输入的第一行是一个整数T,表示测试实例的个数,下面是T行数据,每行一个整数N(1<=N<2^64),当T=0时结束.
Output
对于每个测试实例,输出字符串个数的最后两位,每组输出后跟一个空行.
Sample Input
4
1
4
20
11
3
14
24
6
0
Sample Output
Case 1: 2
Case 2: 72
Case 3: 32
Case 4: 0
Case 1: 56
Case 2: 72
Case 3: 56
1 =2 = 1*2
2 =6 = 2*3
3 =20 = 4*5
4 =72 = 8*9
推出 s=2(n-1)*[2(n-1)+1],利用快速幂运算mod100
#include <bits/stdc++.h> typedef long long ll; using namespace std; ll fastPower(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) { ans = (ans * a) % 100; } b >>= 1; a = (a * a) % 100; } return ans; } int main() { ll n; ll i; ll count; while (cin >> n && n) { count = 0; for (i = 0; i < n; ++i) { ll s; cin >> s; count++; ll x = fastPower(2, s - 1); ll y = (x + 1) % 100; cout << "Case" << count << ":" << x * y % 100 << endl; } cout<<endl; } }
2066一个人的旅行
Problem Description
虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景……草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女……眼看寒假就快到了,这么一大段时间,可不能浪费啊,一定要给自己好好的放个假,可是也不能荒废了训练啊,所以草儿决定在要在最短的时间去一个自己想去的地方!因为草儿的家在一个小镇上,没有火车经过,所以她只能去邻近的城市坐火车(好可怜啊~)。
Input
输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个,草儿想去的地方有D个;
接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路)
接着的第T+1行有S个数,表示和草儿家相连的城市;
接着的第T+2行有D个数,表示草儿想去地方。
接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路)
接着的第T+1行有S个数,表示和草儿家相连的城市;
接着的第T+2行有D个数,表示草儿想去地方。
Output
输出草儿能去某个喜欢的城市的最短时间。
Sample Input
6 2 3
1 3 5
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2
8 9 10
Sample Output
9
草儿的家=相临的点(第T+1行),比较这些点到D个点的距离,输出其中的最小值,即弗洛伊德求多源路径问题
设全局变量全局变量g[i][j]表示结点i到j间的边距离
①初始化均为极大值
②输入T行数据到g矩阵
③更新g的数值,使得g[i][j]最小
#include <bits/stdc++.h> using namespace std; /* Floyd-Warshall算法:计算图中任意2点之间的最短距离 * 复杂度:O(N×N×N) * 输入:n 全局变量,图结点数 * g 全局变量,邻接矩阵,g[i][j]表示结点i到j间的边距离 * 输出:g 全局变量 */ const int INF = 0x3f3f3f3f; const int N = 1000 + 1; int g[N][N], n; void floyd() { for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) if(g[i][k] != INF) for(int j = 1; j <= n; j++) if(g[k][j] != INF) g[i][j] = min(g[i][j], g[i][k] + g[k][j]); } int start[N]; int end2[N]; int main() { int t, s, d; while(~scanf("%d%d%d", &t, &s, &d)) { memset(g, INF, sizeof(g)); n = 0; for(int i = 0; i < t; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); g[u][v] = g[v][u] = min(g[u][v], w); n = max(n, u); n = max(n, v); } for(int i = 1; i <= s; i++) scanf("%d", &start[i]); for(int i = 1; i <= d; i++) scanf("%d", &end2[i]); floyd(); int mind = INF; for(int i = 1; i <= s; i++) for(int j = 1; j <= d; j++) mind = min(mind, g[start[i]][end2[j]]); printf("%d\n", mind); } return 0; }
2067小兔的棋盘
Problem Description
小兔的叔叔从外面旅游回来给她带来了一个礼物,小兔高兴地跑回自己的房间,拆开一看是一个棋盘,小兔有所失望。不过没过几天发现了棋盘的好玩之处。从起点(0,0)走到终点(n,n)的最短路径数是C(2n,n),现在小兔又想如果不穿越对角线(但可接触对角线上的格点),这样的路径数有多少?小兔想了很长时间都没想出来,现在想请你帮助小兔解决这个问题,对于你来说应该不难吧!
Input
每次输入一个数n(1<=n<=35),当n等于-1时结束输入。
Output
对于每个输入数据输出路径数,具体格式看Sample。
Sample Input
1
3
12
-1
Sample Output
1 1 2
2 3 10
3 12 416024
寻找从起点(0,0)走到终点(n,n)且不穿过对角线的最短路径数。
题者并没有说清楚输出规则,应该是“序号,n,路径数”
1.动态规划
#include<iostream> #include<cstdio> #include<cstring> using namespace std; long long dp[40][40]; int main() { int n, cnt = 1; while(~scanf("%d", &n) && n != -1) { memset(dp, 0, sizeof(dp));//初始化 for(int j = 1; j <= n+1; j++) //初始化 dp[n+1][j] = 1; for(int i = n; i >= 1; i--) { for(int j = n+2-i; j <= n+1; j++) { dp[i][j] = dp[i+1][j] + dp[i][j-1]; } } printf("%d %d %lld\n", cnt++, n, dp[1][n+1]*2);//2倍 } }
https://www.cnblogs.com/ACMerszl/p/9572958.html
2.卡特兰数
2068RPG的错排
Problem Description
今年暑假杭电ACM集训队第一次组成女生队,其中有一队叫RPG,但做为集训队成员之一的野骆驼竟然不知道RPG三个人具体是谁谁。RPG给他机会让他猜猜,第一次猜:R是公主,P是草儿,G是月野兔;第二次猜:R是草儿,P是月野兔,G是公主;第三次猜:R是草儿,P是公主,G是月野兔;......可怜的野骆驼第六次终于把RPG分清楚了。由于RPG的带动,做ACM的女生越来越多,我们的野骆驼想都知道她们,可现在有N多人,他要猜的次数可就多了,为了不为难野骆驼,女生们只要求他答对一半或以上就算过关,请问有多少组答案能使他顺利过关。
Input
输入的数据里有多个case,每个case包括一个n,代表有几个女生,(n<=25), n = 0输入结束。
Sample Input
1
2
0
Sample Output
1
1
2069Coin Change
Problem Description
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.
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
换硬币:假设有5种硬币:50分、25分、10分、5分和1分。将≥0的输入n换成一定数量的硬币,有几种换法,n=0是默认为1
#include<iostream> #define ll long long using namespace std; int dp[251][101]; int type[5]={1,5,10,25,50}; void solve() { dp[0][0]=1; for(int i=0;i<5;i++)//5种类型的面值 { for(int j=1;j<101;j++)//硬币的个数 { for(int k=type[i];k<251;k++) { dp[k][j]+=dp[k-type[i]][j-1]; } } } } int main() { int ans[251]={0}; solve(); for(int i=0;i<251;i++)//0元~251元 { for(int j=0;j<101;j++)//0个硬币~100个硬币 { ans[i]+=dp[i][j]; } } int s; while(cin>>s) { cout<<ans[s]<<endl; } return 0; }
2070Fibbonaci Number
Problem Description
Your objective for this question is to develop a program which will generate a fibbonacci number. The fibbonacci function is defined as such:
f(0) = 0
f(1) = 1
f(n) = f(n-1) + f(n-2)
Your program should be able to handle values of n in the range 0 to 50.
f(0) = 0
f(1) = 1
f(n) = f(n-1) + f(n-2)
Your program should be able to handle values of n in the range 0 to 50.
Input
Each test case consists of one integer n in a single line where 0≤n≤50. The input is terminated by -1.
Output
Print out the answer in a single line for each test case.
Sample Input
3
4
5
-1
Sample Output
2
3
5