其他分享
首页 > 其他分享> > HDU 1720、1062、2104、1064、2734、1170、1197、2629

HDU 1720、1062、2104、1064、2734、1170、1197、2629

作者:互联网

1720:

 (计算两个16进制的数的10进制形式的和)

Input may contain multiple test cases. Each case contains A and B in one line.
A, B are hexadecimal number.
Input terminates by EOF.

  Output Output A+B in decimal number in one line.   Sample Input 1 9 A B a b   Sample Output 10 21 21
#include<stdio.h>
int main()
{
    int x, y;
    while (scanf("%x%x", &x, &y) != EOF)//以十六进制输入,o为八进制,没有二进制输入法。
    {
        printf("%d\n", x + y);//十进制输出
    }
    return 0;
}
View Code

心得: 可通过“%x”控制输入是十六进制的数,输出就照常%d“”,十进制输出就行

 

1062:

(把输入的每一个词的字母反过来输出)

Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

  Input The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.   Output For each test case, you should output the text which is processed.   Sample Input 3 olleh !dlrow m'I morf .udh I ekil .mca   Sample Output hello world! I'm from hdu. I like acm. 
#include<stdio.h>
#include<string.h>
int main()
{
    int n, i, j, flag;
    char str[1005];
    scanf("%d", &n);
    getchar();
    while (n--)
    {
        gets(str);
        flag = 0;
        int l = strlen(str);
        for (i = 0; i < l; i++)
        {
            if (str[i] == ' ')
            {
                for (j = i - 1; j >= flag; j--)         //不用真的翻转,倒序输出就行,当然string使用reverse函数也行
                {
                    printf("%c", str[j]);
                }
                printf(" ");
                flag = i + 1;
            }
        }
        for (j = l - 1; j >= flag; j--)
        {
            printf("%c", str[j]);
        }
        printf("\n");
    }
    return 0;
}
View Code

心得:数组字符串的使用 

 

2104:

(总个数为n,每次跳过m-1个位置,问从1开始跳能不能在有限次数后找到n)

Problem Description

The Children’s Day has passed for some days .Has you remembered something happened at your childhood? I remembered I often played a game called hide handkerchief with my friends.
Now I introduce the game to you. Suppose there are N people played the game ,who sit on the ground forming a circle ,everyone owns a box behind them .Also there is a beautiful handkerchief hid in a box which is one of the boxes .
Then Haha(a friend of mine) is called to find the handkerchief. But he has a strange habit. Each time he will search the next box which is separated by M-1 boxes from the current box. For example, there are three boxes named A,B,C, and now Haha is at place of A. now he decide the M if equal to 2, so he will search A first, then he will search the C box, for C is separated by 2-1 = 1 box B from the current box A . Then he will search the box B ,then he will search the box A.
So after three times he establishes that he can find the beautiful handkerchief. Now I will give you N and M, can you tell me that Haha is able to find the handkerchief or not. If he can, you should tell me "YES", else tell me "POOR Haha".   Input There will be several test cases; each case input contains two integers N and M, which satisfy the relationship: 1<=M<=100000000 and 3<=N<=100000000. When N=-1 and M=-1 means the end of input case, and you should not process the data.   Output For each input case, you should only the result that Haha can find the handkerchief or not.   Sample Input 3 2 -1 -1   Sample Output YES (1)凭借我查找的资料和多年玩丢手绢的经验,丢手绢是跑一圈找不到就算失败,我对这个题的理解是只能跑一圈也就是最多只能找n/m次数,所以有如下代码,:
//by lmc in 2020年6月6日15:14:49

#include<stdio.h>

int main()
{
    int n,m,i;
    while (scanf("%d%d", &n, &m) != EOF&&n!= -1 ,m != -1)
    {
        
        if (n<m+1) 
            printf("no\n");
        else
            for (i = 0; i < n / m; i++)
            {

                if (n = 1 + (m - 1) * i)
                    printf("yes\n");
                else
                    continue;

            }
    }
    


}
View Code (2)但是大家的答案是理解为可以无限跑圈找到为止,使用了辗转相除法,具体步骤如图:  
#include<stdio.h>
int main()
{
    int n, m, r;
    while (scanf("%d%d", &n, &m) != EOF && n != -1, m != -1)
    {
        r = 1;
        while (r != 0)
        {
            r = m % n;
            m = n;
            n = r;
        }
        if (m == 1)
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}
View Code

 

1064:

(算数求平均值)

Problem Description Larry graduated this year and finally has a job. He’s making a lot of money, but somehow never seems to have enough. Larry has decided that he needs to grab hold of his financial portfolio and solve his financing problems. The first step is to figure out what’s been going on with his money. Larry has his bank account statements and wants to see how much money he has. Help Larry by writing a program to take his closing balance from each of the past twelve months and calculate his average account balance.   Input The input will be twelve lines. Each line will contain the closing balance of his bank account for a particular month. Each number will be positive and displayed to the penny. No dollar sign will be included.   Output The output will be a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by the end-of-line. There will be no other spaces or characters in the output.   Sample Input 100.00 489.12 12454.12 1234.10 823.05 109.20 5.27 1542.25 839.18 83.99 1295.01 1.75   Sample Output $1581.42
//by lmc in 2020年6月6日16:30:22
#include<stdio.h>
int main()
{
    int i;
    float  sum=0, n, m;
    for (i = 1; i <=12; i++)
    {
        scanf("%f", &n);
        sum += n;
        getchar();
    }
    printf("$%.2f",sum/12);
}
View Code

 

 

 

标签:2734,box,HDU,int,2629,will,Input,line,he
来源: https://www.cnblogs.com/lumc5/p/13053090.html