HDOJ 2051-2060

2050Bitset

Problem Description
Give you a number on base ten,you should output it on base two.(0 < n < 1000)
 
Input
For each case there is a postive number n on base ten, end of file.
 
Output
For each case output a number on base two.
 
Sample Input
1 2 3
 
Sample Output
1 10 11
人话:十进制转二进制
#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

Problem Description
Give you the width and height of the rectangle,darw it.

 

Input
Input contains a number of test cases.For each case ,there are two numbers n and m (0 < n,m < 75)indicate the width and height of the rectangle.Iuput ends of EOF.
 
Output
For each case,you should draw a rectangle with the width and height giving in the input.
after each case, you should a blank line.
 
Sample Input
3 2

 

Sample Output
+---+
|   |
|   |
+---+
#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

Problem Description
There are many lamps in a line. All of them are off at first. A series of operations are carried out on these lamps. On the i-th operation, the lamps whose numbers are the multiple of i change the condition ( on to off and off to on ).
 
Input
Each test case contains only a number n ( 0< n<= 10^5) in a line.
 
Output
Output the condition of the n-th lamp after infinity operations ( 0 - off, 1 - on ).
 
Sample Input
1
5
 
Sample Output
1
0
 
Consider the second test case:
 
 
The initial condition : 0 0 0 0 0 …
After the first operation : 1 1 1 1 1 …
After the second operation : 1 0 1 0 1 …
After the third operation : 1 0 0 0 1 …
After the fourth operation : 1 0 0 1 1 …
After the fifth operation : 1 0 0 1 0 …
 
The later operations cannot change the condition of the fifth lamp any more. So the answer is 0.
 

翻译:有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?

Problem Description
Give you two numbers A and B, if A is equal to B, you should print "YES", or print "NO".
 
Input
each test case contains two numbers A and B.

 

Output
for each case, if A is equal to B, you should print "YES", or print "NO".
 
Sample Input
1 2
2 2
3 3
4 3
 
Sample Output
NO
YES
YES
NO

 

#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

Problem Description
we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, ... f(Z) = 26, f(z) = -26;
Give you a letter x and a number y , you should output the result of y+f(x).
 
Input
On the first line, contains a number T.then T lines follow, each line is a case.each case contains a letter and a number.
 
Output
for each case, you should the result of y+f(x) on a line.
 
Sample Input
6
R 1
P 2
G 3
r 1
p 2
g 3
 

 

Sample Output
19
18
10
-17
-14
-4

 

#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

Problem Description
Given two rectangles and the coordinates of two points on the diagonals of each rectangle,you have to calculate the area of the intersected part of two rectangles. its sides are parallel to OX and OY .
 
Input
Input The first line of input is 8 positive numbers which indicate the coordinates of four points that must be on each diagonal.The 8 numbers are x1,y1,x2,y2,x3,y3,x4,y4.That means the two points on the first rectangle are(x1,y1),(x2,y2);the other two points on the second rectangle are (x3,y3),(x4,y4).

 

Output
Output For each case output the area of their intersected part in a single line.accurate up to 2 decimal places.
 
Sample Input
1.00 1.00 3.00 3.00 2.00 2.00 4.00 4.00
5.00 5.00 13.00 13.00 4.00 4.00 12.50 12.50

 

Sample Output
1.00
56.25
翻译:给定两个矩形和每个矩形对角线上两个点的坐标,你必须计算两个矩形相交部分的面积。它的两边平行于OX和OY。
求出这四个点所能表示的矩形的长宽
①相离:长或宽大于原矩形长宽之和
②相交:长宽均小于原矩形长宽之和
③重合是特殊的相交

#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

Problem Description
There must be many A + B problems in our HDOJ , now a new one is coming.
Give you two hexadecimal integers , your task is to calculate the sum of them,and print it in hexadecimal too.
Easy ? AC it !
 
Input
The input contains several test cases, please process to the end of the file.
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.
 
Output
For each test case,print the sum of A and B in hexadecimal in one line.
 
Sample Input
+A -A
+1A 12
1A -9
-1A -12
1A -AA
 
Sample Output
0
2C
11
-2C
-90

 

#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

Problem Description
Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M.
 
Input
Input contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 1000000000).input ends with N = M = 0.
 
Output
For each test case, print all the possible sub-sequence that its sum is M.The format is show in the sample below.print a blank line after each test case.
 
Sample Input
20 10
50 30
0 0
 
Sample Output
[1,4]
[10,10]
 
[4,8]
[6,9]
[9,11]
[30,30]
 
#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龟兔赛跑

Problem Description
据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下沙某农业园卧薪尝胆潜心修炼,终于练成了绝技,能够毫不休息得以恒定的速度(VR m/s)一直跑。兔子一直想找机会好好得教训一下乌龟,以雪前耻。
最近正值HDU举办50周年校庆,社会各大名流齐聚下沙,兔子也趁此机会向乌龟发起挑战。虽然乌龟深知获胜希望不大,不过迫于舆论压力,只能接受挑战。
比赛是设在一条笔直的道路上,长度为L米,规则很简单,谁先到达终点谁就算获胜。
无奈乌龟自从上次获胜以后,成了名龟,被一些八卦杂志称为“动物界的刘翔”,广告不断,手头也有了不少积蓄。为了能够再赢兔子,乌龟不惜花下血本买了最先进的武器——“"小飞鸽"牌电动车。这辆车在有电的情况下能够以VT1 m/s的速度“飞驰”,可惜电池容量有限,每次充满电最多只能行驶C米的距离,以后就只能用脚来蹬了,乌龟用脚蹬时的速度为VT2 m/s。更过分的是,乌龟竟然在跑道上修建了很多很多(N个)的供电站,供自己给电动车充电。其中,每次充电需要花费T秒钟的时间。当然,乌龟经过一个充电站的时候可以选择去或不去充电。
比赛马上开始了,兔子和带着充满电的电动车的乌龟并列站在起跑线上。你的任务就是写个程序,判断乌龟用最佳的方案进军时,能不能赢了一直以恒定速度奔跑的兔子。

 

Input
本题目包含多组测试,请处理到文件结束。每个测试包括四行:
第一行是一个整数L代表跑道的总长度
第二行包含三个整数N,C,T,分别表示充电站的个数,电动车冲满电以后能行驶的距离以及每次充电所需要的时间
第三行也是三个整数VR,VT1,VT2,分别表示兔子跑步的速度,乌龟开电动车的速度,乌龟脚蹬电动车的速度
第四行包含了N(N<=100)个整数p1,p2...pn,分别表示各个充电站离跑道起点的距离,其中0<p1<p2<...<pn<L
其中每个数都在32位整型范围之内。
 
Output
当乌龟有可能赢的时候输出一行 “What a pity rabbit!"。否则输出一行"Good job,rabbit!";
题目数据保证不会出现乌龟和兔子同时到达的情况。
 
Sample Input
100
3 20 5
5 8 2
10 40 60
100
3 60 5
5 8 2
10 40 60
 
Sample Output
Good job,rabbit!
What a pity rabbit!

 

动态规划算法(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

Problem Description
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
翻译:

 斯诺克规则是总共有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;
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2023-03-03 19:01  EleclouD  阅读(270)  评论(0)    收藏  举报