HDU acm step:Chapter one section two



Box of Bricks

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9216 Accepted Submission(s): 2157
 
Problem Description
Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. “Look, I\'ve built a wall!”, he tells his older sister Alice. “Nah, you should make all stacks the same height. Then you would have a real wall.”, she retorts. After a little consideration, Bob sees that she is right. So he sets out to rearrange the bricks, one by one, such that all stacks are the same height afterwards. But since Bob is lazy he wants to do this with the minimum number of bricks moved. Can you help?
 
Input
The input consists of several data sets. Each set begins with a line containing the number n of stacks Bob has built. The next line contains n numbers, the heights hi of the n stacks. You may assume 1≤n≤50 and 1≤hi≤100.

The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.

The input is terminated by a set starting with n = 0. This set should not be processed.
 
Output

            For each set, print the minimum number of bricks that have to be moved in order to make all the stacks the same height.
Output a blank line between each set.
 
Sample Input
6
5 2 4 1 7 5
0
 
Sample Output
Set #1
The minimum number of moves is 5.



求平均数,计算大于或者小于平均数的所有值与平均数的差值的和。


#include <bits/stdc++.h>
using namespace std;
int a[100];

int main()
{
    int n, aver, k = 0;
    while(cin >> n && n != 0)
    {
        k++;
        int sum = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
            sum += a[i];
        }
        aver = sum / n;
        sum = 0;
        for(int i = 0; i < n; i++)
        {
            if(a[i] > aver)
            {
                sum += (a[i] - aver);
            }
        }
        printf("Set #%d\n", k);
        printf("The minimum number of moves is %d.\n\n", sum);
    }

    return 0;
}


An Easy Task

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3720 Accepted Submission(s): 2146
 
Problem Description
Ignatius was born in a leap year, so he want to know when he could hold his birthday party. Can you tell him?

Given a positive integers Y which indicate the start year, and a positive integer N, your task is to tell the Nth leap year from year Y.

Note: if year Y is a leap year, then the 1st leap year is year Y.
 
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 two positive integers Y and N(1<=N<=10000).
 
Output
For each test case, you should output the Nth leap year from year Y.
 
Sample Input
3
2005 25
1855 12
2004 10000
 
Sample Output
2108
1904
43236


      
      
Hint
We call year Y a leap year only if (Y%4==0 && Y%100!=0) or Y%400==0.


计算出从这年开始(包括这一年)的第一个闰年,然后开始向后以加四的形式查找,剔除非闰年的存在,达到一定个数即输出年份。


#include <bits/stdc++.h>
using namespace std;
int a[100];

bool isleap(int y)
{
    if(y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main()
{
    int n;
    cin >> n;
    while(n--)
    {
        int y, b;
        while(cin >> y >> b)
        {
            while(y)
            {
                if(isleap(y))
                {
                    b--;
                    break;
                }
                else
                {
                    y++;
                }
            }
            int k = 0;
            while(b)
            {
                y += 4;
                if(isleap(y))
                {
                    b--;
                }
            }
            cout << y << endl;
        }
    }
    return 0;
}




Find your present (2)

Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7496 Accepted Submission(s): 1990
 
Problem Description
In the new year party, everybody will get a "special present".Now it's your turn to get your special present, a lot of presents now putting on the desk, and only one of them will be yours.Each present has a card number on it, and your present's card number will be the one that different from all the others, and you can assume that only one number appear odd times.For example, there are 5 present, and their card numbers are 1, 2, 3, 2, 1.so your present will be the one with the card number of 3, because 3 is the number that different from all the others.
 
Input
The input file will consist of several cases.
Each case will be presented by an integer n (1<=n<1000000, and n is odd) at first. Following that, n positive integers will be given in a line, all integers will smaller than 2^31. These numbers indicate the card numbers of the presents.n = 0 ends the input.
 
Output

            For each case, output an integer in a line, which is the card number of your present.
 
Sample Input
5
1 1 3 2 2
3
1 2 1
0
 
Sample Output
3
2


      
      
Hint
Hint
use scanf to avoid Time Limit Exceeded


题目的意思我不知道是指要找出来的是那个数是奇数个还是只有一个,但是解法是一样的。使用异或的性质。不断异或,最后得到的就是出现奇数次的那个数。

也可以使用set,出现一次插入,再次出现删去,最后仅剩的一个就是所求的。

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    int a, b;
    while(cin >> n && n != 0)
    {
        scanf("%d", &a);
        for(int i = 1; i < n; i++)
        {
            scanf("%d", &b);
            a = a ^ b;
        }
        cout << a << endl;
    }
    return 0;
}



#include <set>
#include <stdio.h>
using namespace std;
int main()
{
    int n,x;
    set <int> S;
    while(scanf("%d",&n),n)
    {
        while(n--)
        {
            scanf("%d",&x);
            if(S.find(x) == S.end())    
                S.insert(x);
            else                        
                S.erase(x);
        }
        printf("%d\n",*S.begin());
        S.clear();
    }
    return 0;
}





Rightmost Digit

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7882 Accepted Submission(s): 2025
 
Problem Description
Given a positive integer N, you should output the most right digit of N^N.
 
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 positive integer N(1<=N<=1,000,000,000).
 
Output
For each test case, you should output the rightmost digit of N^N.
 
Sample Input
2
3
4
 
Sample Output
7
6


      
      
Hint
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7. In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.

开始的做法是先找出最后一位。每次乘完一次或者两次或者三次,就去除最低一位。但是都超时了。

最后是用找规律的方法:找出末位,1.5.6.0都是怎么乘都等于本身,而其他的都是四个一循环。于是便可以得出结果了。(是不是很像高中的程序框图)


#include <bits/stdc++.h>
using namespace std;
int getnum(int n)
{
    while(n >= 10)
    {
        n = n % 10;
    }
    return n;
}

int pow1(int a, int b)
{
    if(b == 0)
    {
        b = 4;
    }
    int res = 1;
    for(int i = 0; i < b; i++)
    {
        res = res * a;
    }
    res = getnum(res);
    return res;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        int m = getnum(n);
        int res1;
        if(m == 0 || m == 1 || m == 5 || m == 6)
        {
            cout << m << endl;
        }
        else
        {
            m = pow1(m, n%4);
            cout << m << endl;
        }
    }
    return 0;
}






Buildings

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2791 Accepted Submission(s): 1979
 
Problem Description
We divide the HZNU Campus into N*M grids. As you can see from the picture below, the green grids represent the buidings. Given the size of the HZNU Campus, and the color of each grid, you should count how many green grids in the N*M grids.

 
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
The first line of each test case contains two integers n and m(1<=n,m<=100), the size of the campus. Then follow n lines, each line containing m integers. The j-th integer in the i-th line is the color of that grid, 0 stands for white color, while 1 stands for green.
 
Output

            Results should be directed to standard output. For each case, output an integers T, the total green grids in the N*M size campus.
 
Sample Input
2
2 2
1 1
0 0
3 3
1 0 1
0 0 1
1 1 0
 
Sample Output
2
5
 


枚举,找到一的个数即可。


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int m, n;
        scanf("%d%d", &m, &n);
        int sum = 0, x;
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                scanf("%d", &x);
                if(x == 1)
                {
                    sum++;
                }
            }
        }
        printf("%d\n", sum);
    }

    return 0;
}



Balloon Comes!

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6191 Accepted Submission(s): 1946
 
Problem Description
The contest starts now! How excited it is to see balloons floating around. You, one of the best programmers in HDU, can get a very beautiful balloon if only you have solved the very very very... easy problem.
Give you an operator (+,-,*, / --denoting addition, subtraction, multiplication, division respectively) and two positive integers, your task is to output the result.
Is it very easy?
Come on, guy! PLMM will send you a beautiful Balloon right now!
Good Luck!
 
Input
Input contains multiple test cases. The first line of the input is a single integer T (0<T<1000) which is the number of test cases. T test cases follow. Each test case contains a char C (+,-,*, /) and two integers A and B(0<A,B<10000).Of course, we all know that A and B are operands and C is an operator.
 
Output
For each case, print the operation result. The result should be rounded to 2 decimal places If and only if it is not an integer.
 
Sample Input
4
+ 1 2
- 1 2
* 1 2
/ 1 2
 
Sample Output
3
-1
2
0.50
 

开始一直出错,是直接将除法当作是没有保留小数点的计算,忽略了可能出现取整的情况。同时printf的保留是四舍五入的。


#include <bits/stdc++.h>
using namespace std;

int main()
{
    char oper;
    int a, b;
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%s", &oper);
        if(oper != '/')
        {
            scanf("%d%d", &a, &b);
            if(oper == '+')
            {
                printf("%d\n", a+b);
            }
            if(oper == '-')
            {
                printf("%d\n", a-b);
            }
            if(oper == '*')
            {
                printf("%d\n", a*b);
            }
        }
        else
        {
            int a, b;
            scanf("%d%d", &a, &b);
            if(a/b*b == a)
            {
                printf("%d\n", a/b);
            }
            else
            {
                printf("%.2f\n", double(a)/double(b));
            }
        }
    }
    return 0;
}







GPA

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4294 Accepted Submission(s): 1754
 
Problem Description
Each course grade is one of the following five letters: A, B, C, D, and F. (Note that there is no grade E.) The grade A indicates superior achievement , whereas F stands for failure. In order to calculate the GPA, the letter grades A, B, C, D, and F are assigned the following grade points, respectively: 4, 3, 2, 1, and 0.
 
Input
The input file will contain data for one or more test cases, one test case per line. On each line there will be one or more upper case letters, separated by blank spaces.
 
Output
Each line of input will result in exactly one line of output. If all upper case letters on a particular line of input came from the set {A, B, C, D, F} then the output will consist of the GPA, displayed with a precision of two decimal places. Otherwise, the message "Unknown letter grade in input" will be printed.
 
Sample Input
A B C D F
B F F C C A
D C E F
 
Sample Output
2.00
1.83
Unknown letter grade in input


卡的点在于%.2f写成了%.2F。没有难度,直接扫。


#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str;
    while(getline(cin, str))
    {
        double sum = 0;
        int flag = 0;
        int n = str.size();
        double k = (n+1)/2;
        for(int i = 0; i < n; i = i + 2)
        {
            if(str[i] == 'A')
            {
                sum += 4;
            }
            else if(str[i] == 'B')
            {
                sum += 3;
            }
            else if(str[i] == 'C')
            {
                sum += 2;
            }
            else if(str[i] == 'D')
            {
                sum += 1;
            }
            else if(str[i] == 'F')
            {
                sum += 0;
            }
            else
            {
                flag = 1;
            }
        }
        if(flag == 0)
        {
            printf("%.2f\n", sum/k);
        }
        else
        {
            printf("Unknown letter grade in input\n");
        }
    }
    return 0;
}






IBM Minus One

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4655 Accepted Submission(s): 1800
 
Problem Description
You may have heard of the book '2001 - A Space Odyssey' by Arthur C. Clarke, or the film of the same name by Stanley Kubrick. In it a spaceship is sent from Earth to Saturn. The crew is put into stasis for the long flight, only two men are awake, and the ship is controlled by the intelligent computer HAL. But during the flight HAL is acting more and more strangely, and even starts to kill the crew on board. We don't tell you how the story ends, in case you want to read the book for yourself :-)

After the movie was released and became very popular, there was some discussion as to what the name 'HAL' actually meant. Some thought that it might be an abbreviation for 'Heuristic ALgorithm'. But the most popular explanation is the following: if you replace every letter in the word HAL by its successor in the alphabet, you get ... IBM.

Perhaps there are even more acronyms related in this strange way! You are to write a program that may help to find this out.
 
Input
The input starts with the integer n on a line by itself - this is the number of strings to follow. The following n lines each contain one string of at most 50 upper-case letters.
 
Output
For each string in the input, first output the number of the string, as shown in the sample output. The print the string start is derived from the input string by replacing every time by the following letter in the alphabet, and replacing 'Z' by 'A'.

Print a blank line after each test case.
 
Sample Input
2
HAL
SWERC
 
Sample Output
String #1
IBM

String #2
TXFSD


直接利用转化关系,但是要注意的是转化时候 Z是转化成A的,而且转化那里只能写成相互形式,而不能是+‘1’



<pre name="code" class="cpp">#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    char s[100];
    for(int i = 1; i <= t; i++)
    {
        cin >> s;
        for(int j = 0; j < strlen(s); j++)
        {
            if(s[j] == 'Z')
            {
                s[j] = 'A';
            }
            else
            {
                s[j] = s[j] + ('I' - 'H');
            }
        }
        printf("String #%d\n", i);
        printf("%s\n\n", s);
    }
    return 0;
}



 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值