HDOJ 2051-2060
2050Bitset
#include <bits/stdc++.h> using namespace std; int main() { int n; int a[16]; int i, j; while (scanf("%d", &n) != EOF) { i = 0; while (n > 0) { a[i] = n % 2; n = n / 2; i++; } for (j = i - 1; j >= 0; j--) printf("%d", a[j]); printf("\n"); } }
2052Picture
after each case, you should a blank line.
#include <bits/stdc++.h> using namespace std; int main() { int w, h; int i, j; while (scanf("%d %d", &w, &h) != EOF) { cout << "+"; for (i = 0; i < w; i++) printf("-"); cout << "+" << endl; for (i = 0; i < h; i++) { cout << "|"; for (j = 0; j < w; j++) printf(" "); cout << "|" << endl; } cout << "+"; for (i = 0; i < w; i++) printf("-"); cout << "+" << endl; } }
2053Switch game
翻译:有n盏灯,所有灯开始是关着的。i从1数到n,每当灯的序号是i的倍数的时候就对灯进行一次操作(开->关,关->开),求最后第n盏灯是关着还是开着。
完全平方数:①平方数的个位数字只能是 0, 1,4,5,6,9 。
②任何偶数的平方一定能被 4 整除;任何奇数的平方被 4(或 8)除余 1,即被4 除余 2 或 3 的数一定不是完全平方数。
③完全平方数的个位数字是奇数时,其十位上的数字必为偶数。完全平方数的个位数字是 6 时,其十位数字必为奇数。
其实就是判断n的约数数目的奇偶性,奇数就开着,偶数就关着。
#include <bits/stdc++.h> using namespace std; int main() { int n; int i; int count; while (scanf("%d", &n) != EOF) { count = 0; for (i = 1; i <= sqrt(n); i++) { if (n % i == 0) count += 2; if (i * i == n) count--; } if (count % 2 == 1) printf("1\n"); else printf("0\n"); } }
更简易的写法:
#include <bits/stdc++.h> using namespace std; int main() { int n; int i; int count; while (scanf("%d", &n) != EOF) { int x = sqrt(n * 1.0); if (x * x == n) puts("1"); else puts("0"); } }
2054A==B?
#include <bits/stdc++.h> using namespace std; int main() { int a, b; while (~scanf("%d %d", &a, &b)) { if (a == b) printf("YES\n"); else printf("NO\n"); } }
2055An easy problem
Give you a letter x and a number y , you should output the result of y+f(x).
#include<bits/stdc++.h> using namespace std; int Ffun(char a,int n) { if(a>='A'&&a<='Z') return a-64+n; else return (a-96)*(-1)+n; } int main() { int n; int i,j; char s; int num; scanf("%d",&n); for(i=0;i<n;i++) { cin>>s>>num; printf("%d\n",Ffun(s,num)); } }
2056Rectangles

#include <bits/stdc++.h> using namespace std; int main() { double x1, y1, x2, y2, x3, y3, x4, y4; double x[4]; double y[4]; double length, width; while (cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4) { x[0] = x1; x[1] = x2; x[2] = x3; x[3] = x4; y[0] = y1; y[1] = y2; y[2] = y3; y[3] = y4; sort(x, x + 4); sort(y, y + 4); length = fabs(x1 - x2) + fabs(x4 - x3) - (x[3] - x[0]); width = fabs(y1 - y2) + abs(y4 - y3) - (y[3] - y[0]); if (length == 0 || width == 0) cout << "0.00" << endl; else printf("%.2lf\n", length * width); } }
2057A+B Again
Give you two hexadecimal integers , your task is to calculate the sum of them,and print it in hexadecimal too.
Easy ? AC it !
Each case consists of two hexadecimal integers A and B in a line seperated by a blank.
The length of A and B is less than 15.
#include<stdio.h> int main() { long long n,m,v; while(scanf("%llx%llx",&n,&m)==2)//llx是长整形的16进制 { v=n+m; if(v<0) { v=-v; printf("-%llX\n",v); } else printf("%llX\n",v); } return 0; }
2058 The sum problem
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int i,j; int a,b; int count; while (scanf("%d %d", &n, &m) != EOF) { if(n==0&&m==0) { return 0; } for(i=1;i<=n;i++) { if(i==m) { printf("[%d,%d]\n",i,i); continue; } count=0; a=i; for(j=i;j<=n;j++) { count+=j; if(count==m) { b=j; printf("[%d,%d]\n",a,b); break; } } } } }
2059龟兔赛跑
最近正值HDU举办50周年校庆,社会各大名流齐聚下沙,兔子也趁此机会向乌龟发起挑战。虽然乌龟深知获胜希望不大,不过迫于舆论压力,只能接受挑战。
比赛是设在一条笔直的道路上,长度为L米,规则很简单,谁先到达终点谁就算获胜。
无奈乌龟自从上次获胜以后,成了名龟,被一些八卦杂志称为“动物界的刘翔”,广告不断,手头也有了不少积蓄。为了能够再赢兔子,乌龟不惜花下血本买了最先进的武器——“"小飞鸽"牌电动车。这辆车在有电的情况下能够以VT1 m/s的速度“飞驰”,可惜电池容量有限,每次充满电最多只能行驶C米的距离,以后就只能用脚来蹬了,乌龟用脚蹬时的速度为VT2 m/s。更过分的是,乌龟竟然在跑道上修建了很多很多(N个)的供电站,供自己给电动车充电。其中,每次充电需要花费T秒钟的时间。当然,乌龟经过一个充电站的时候可以选择去或不去充电。
比赛马上开始了,兔子和带着充满电的电动车的乌龟并列站在起跑线上。你的任务就是写个程序,判断乌龟用最佳的方案进军时,能不能赢了一直以恒定速度奔跑的兔子。
第一行是一个整数L代表跑道的总长度
第二行包含三个整数N,C,T,分别表示充电站的个数,电动车冲满电以后能行驶的距离以及每次充电所需要的时间
第三行也是三个整数VR,VT1,VT2,分别表示兔子跑步的速度,乌龟开电动车的速度,乌龟脚蹬电动车的速度
第四行包含了N(N<=100)个整数p1,p2...pn,分别表示各个充电站离跑道起点的距离,其中0<p1<p2<...<pn<L
其中每个数都在32位整型范围之内。
题目数据保证不会出现乌龟和兔子同时到达的情况。
动态规划算法(DP问题)
保存已解决的子问题的答案,而在需要时再找出已求得的答案。
特征:1.最优子结构:当问题的最优解包含了其子问题的最优解时,称该问题具有最优子结构性质。
2.重叠子问题:在用递归算法自顶向下解问题时,每次产生的子问题并不总是新问题,有些子问题被反复计算多次。动态规划算法正是利用了这种子问题的重叠性质,对每一个子问题只解一次,而后将其解保存在一个表格中,在以后尽可能多地利用这些子问题的解。

1.要使到达终点的时间最短,则到达倒数第一站的时间也必是最短的。
设dp[i]表示从起点到第i充电站所花费的最短时间,dp[N+1]的值是由dp[N]的值决定的
2.寻找递归定义式
设a[i]是i站到起点的距离;m[i:j]是由i站到j站所用时间(i、j相邻)
m[i:j]=C/VT1+(a[i]-a[j]-C)/VT2;...........a[j]-a[i]>C
m[i:j]=(a[i]-a[j])/VT1;...................a[j]-a[i]≤C
dp[j]=dp[i]+m[i:j]
3.dp[n+1]为待求值
0x3f3f3f3f的十进制是1061109567,也就是10^9级别的(和0x7fffffff(32-bit int的最大值)一个数量级),而一般场合下的数据都是小于10^9的,所以它可以作为无穷大使用而不致出现数据大于无穷大的情形。

#include <bits/stdc++.h> #define MAX 0x3f3f3f3f using namespace std; int main() { double a[105], dp[105]; double L; int N; double C, T; double VR, VT1, VT2; int i, j; double small; while (cin >> L) { cin >> N >> C >> T >> VR >> VT1 >> VT2; a[0] = 0; a[N + 1] = L; dp[0] = 0; for (i = 1; i<=N; i++) { cin>>a[i]; } double rabbit = L / VR; double turtle; for (i = 1; i <= N + 1; i++) { small = MAX; for (j = 0; j < i; j++) { if (a[i] - a[j] > C) turtle = C / VT1 + (a[i] - a[j] - C) / VT2; else turtle = (a[i] - a[j]) / VT1; turtle += dp[j]; if (j != 0) turtle += T; if (small > turtle) small = turtle; } dp[i] = small; } if (rabbit > dp[N + 1]) cout << "What a pity rabbit!" << endl; else cout << "Good job,rabbit!" << endl; } }
2060Snooker
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")
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.
斯诺克规则是总共有15个红球和6个彩球,打球顺序为打一个红球,进球得1分,再打一个彩球
①如一红球被击进袋,该运动员可继续进行下一击球,且下一个活球应是该运动员所选的一个彩球。如该彩球被击进袋,可得分。然后再将彩球放回置球点。
②红球全部离台前,轮流交替地将红球与彩球击进袋,才能一杆继续下去。直到台面上最后一只红球被击落后,随之一个彩球也被击进袋,一杆球仍可继续进行。
③进球的彩球颜色相应分值(黄2 绿3 棕4 蓝5 粉6 黑7)。
桌上有n个球,一人得分为a,另一人为b,问如果第一个人将n个球都打进洞后,得分能否超过(或等于)第二个人。
思路:分有无红球,可分为6个球以上(有红球)的情况和6个球以下(无红球)的情况
① :当 m > 6时 ,应该将有颜色的球都取了,有色球得分为2 + 3 + 4 + 5 + 6 + 7 ,有色球总得分为27 ;然后再取红球 m - 6 ,本来得分应该是 ( m - 6 ) * 1 ,但是由于有色球全部打进洞后,每个球需要额外增加黑球(最高得分)的得分;所以红球总得分为( m - 6 ) * 1 + ( m- 6 ) * 7 ; 总得分为( m - 6 ) * 8 + 27
②:当 m <= 6 时 ,应该由价值最高的黑球( 7 分) 向前依次增加求和,又因为有色球满足等差数列(Sn=n*(a1+an)/2),由前6项减去前 6 - m项和,所以求得为( 7 - m + 1 + 7 ) * m / 2 ( 这里直接通过得分来计算的)。因此,第二种情况的得分为( 15 - m ) *m/ 2 ;
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int i; int x, a, b; for (i = 0; i < n; i++) { cin >> x >> a >> b; if (x > 6) a += (x - 6) * 8 + 2 + 3 + 4 + 5 + 6 + 7; if (x <= 6) a += (7 - x + 1 + 7) * x / 2; if(a>=b) cout<<"Yes"<<endl; else cout<<"No"<<endl; } }

浙公网安备 33010602011771号