杭电ACM基础题(1562、1563、1570、1587、1673、1678、1718)

1562、Guess the number

Happy new year to everybody!
Now, I want you to guess a minimum number x betwwn 1000 and 9999 to let
(1) x % a = 0;
(2) (x+1) % b = 0;
(3) (x+2) % c = 0;
and a, b, c are integers between 1 and 100.
Given a,b,c, tell me what is the number of x ?
Input
The number of test cases c is in the first line of input, then c test cases followed.every test contains three integers a, b, c.
Output
For each test case your program should output one line with the minimal number x, you should remember that x is between 1000 and 9999. If there is no answer for x, output “Impossible”.
Sample Input

2
44 38 49
25 56 3

Sample Output

Impossible
2575

Code:

//猜数 
/*x % a = 0;
(x+1) % b = 0;
(x+2) % c = 0;*/
#include<iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int a,b,c;//[1,100]
        cin>>a>>b>>c;
        //x[1000,9999]
        bool flag=false;
        for(int i=1000;i<=9999;i++){
            if(i%a==0&&(i+1)%b==0&&(i+2)%c==0){
                cout<<i<<endl;
                flag=true;
                break;
            }
        }
        if(flag==false)
            cout<<"Impossible"<<endl;
    }
}

1563、Find your present![寻找一行数中值为唯一的数]

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.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<=200, and n is odd) at first. Following that, n positive integers will be given in a line. 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

Code

//寻找一行数中值为唯一的数,如1 1 3 2 2,值为3
 #include<iostream>
 using namespace std;
 int main(){
     int n;//奇数,[1,200]
     int num[200]={0};
     while(cin>>n){
         if(n==0) return 0;
         
        for(int i=0;i<n;i++){
            cin>>num[i];
        }
        
        //找出num[]中个数为1 的值 
        for(int i=0;i<n;i++){
            int count=0;
            for(int j=0;j<n;j++){
                if(num[i]==num[j]){
                    count++;
                }
            }
            if(count==1){
                cout<<num[i]<<endl;
                break;
            }
        }
     }
    
 }

1570、AC[排列组合]

Are you excited when you see the title “AC” ? If the answer is YES , AC it ;

You must learn these two combination formulas in the school . If you have forgotten it , see the picture.
在这里插入图片描述
Now I will give you n and m , and your task is to calculate the answer .
Input
In the first line , there is a integer T indicates the number of test cases.
Then T cases follows in the T lines.
Each case contains a character ‘A’ or ‘C’, two integers represent n and m. (1<=n,m<=10)
Output
For each case , if the character is ‘A’ , calculate A(m,n),and if the character is ‘C’ , calculate C(m,n).
And print the answer in a single line.
Sample Input

2
A 10 10
C 4 2

Sample Output

3628800
6

Code
阶乘的计算

//计算AC,排列组合
#include<iostream>
using namespace std;

//阶乘的计算 :n! 
int f(int n){
    int result=1,m=n;
    if(n==0) return 1;
    for(int i=n;i>0;i--){
        result=result*i;
    } 
    return result;
} 

int main(){
    int t;//t个测试用例
    cin>>t;
    while(t--){
        char ch;
        int n,m,a,b,c;
        cin>>ch>>n>>m;
        
        switch(ch){ 
            case 'A':
                //计算A(n,m)
                a=f(n);
                b=f(n-m);
                cout<<a/b<<endl;
                break;
            case 'C':
                //计算C(n,m) 
                a=f(n);
                b=f(m);
                c=f(n-m);
                cout<<a/(b*c)<<endl;
                break;
        }
    } 
} 

1587、Flowers[买到最多的花]

As you know, Gardon trid hard for his love-letter, and now he’s spending too much time on choosing flowers for Angel.
When Gardon entered the flower shop, he was frightened and dazed by thousands kinds of flowers.
“How can I choose!” Gardon shouted out.
Finally, Gardon-- a no-EQ man decided to buy flowers as many as possible.
Can you compute how many flowers Gardon can buy at most?
Input
Input have serveral test cases. Each case has two lines.
The first line contains two integers: N and M. M means how much money Gardon have.
N integers following, means the prices of differnt flowers.
Output
For each case, print how many flowers Gardon can buy at most.
You may firmly assume the number of each kind of flower is enough.
Sample Input

2 5
2 3

Sample Output

2

Code

//一定的钱最多能买到多少花
#include<iostream>
#include<algorithm>
using namespace std;
int p[1000];
int main(){
    int n,m;
    while(cin>>n>>m){
        for(int i=0;i<n;i++){
            cin>>p[i];
        }
        sort(p,p+n);
        cout<<m/p[0]<<endl;//全部买价格最便宜的花
    }
} 

1673、Optimal Parking[需要步行的最短距离]

When shopping on Long Street, Michael usually parks his car at some random location, and then walks to the stores he needs.
Can you help Michael choose a place to park which minimises the distance he needs to walk on his shopping round?
Long Street is a straight line, where all positions are integer.
You pay for parking in a specific slot, which is an integer position on Long Street. Michael does not want to pay for more than one parking though. He is very strong, and does not mind carrying all the bags around.
Input
The first line of input gives the number of test cases, 1 <= t <= 100. There are two lines for each test case. The first gives the number of stores Michael wants to visit, 1 <= n <= 20, and the second gives their n integer positions on Long Street, 0 <= xi <= 99.
Output
Output for each test case a line with the minimal distance Michael must walk given optimal parking.
Sample Input

2
4
24 13 89 37
6
7 30 41 14 39 42

Sample Output

152
70

Code
读懂题意,很简单

//一条路上有n家商店,某人停车在某点后,要逛完所有商店回到车出。停车地点自选,求最少需要步行多远。
//实际上就是求最小的位置到最大位置的两倍 
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int t,position[20];//t个测试用例[1,100]
    cin>>t;
    while(t--){
        int n;//要去逛的商店数目 [1,20]
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>position[i];
        }
        sort(position,position+n);
        cout<<(position[n-1]-position[0])*2<<endl;
    } 
} 

1678、Shopaholic[买三送一,计算最优惠折扣]

Lindsay is a shopaholic. Whenever there is a discount of the kind where you can buy three items and only pay for two, she goes completely mad and feels a need to buy all items in the store. You have given up on curing her for this disease, but try to limit its effect on her wallet.
You have realized that the stores coming with these offers are quite elective when it comes to which items you get for free; it is always the cheapest ones. As an example, when your friend comes to the counter with seven items, costing 400, 350, 300, 250, 200, 150, and 100 dollars, she will have to pay 1500 dollars. In this case she got a discount of 250 dollars. You realize that if she goes to the counter three times, she might get a bigger discount. E.g. if she goes with the items that costs 400, 300 and 250, she will get a discount of 250 the first round. The next round she brings the item that costs 150 giving no extra discount, but the third round she takes the last items that costs 350, 200 and 100 giving a discount of an additional 100 dollars, adding up to a total discount of 350.
Your job is to find the maximum discount Lindsay can get.
Input
The first line of input gives the number of test scenarios, 1 <= t <= 20. Each scenario consists of two lines of input. The first gives the number of items Lindsay is buying, 1 <= n <= 20000. The next line gives the prices of these items, 1 <= pi <= 20000.
Output
For each scenario, output one line giving the maximum discount Lindsay can get by selectively choosing which items she brings to the counter at the same time.
Sample Input

1
6
400 100 200 350 300 250

Sample Output

400

Code
给定n种商品的价格,已知该商店的商品买三送一,问买n件商品可以得到最大的折扣是多少?将n件商品的价格从大到小排序,则第三件、第六件…是免费的

//某商店买三送一,求最大折扣
#include<iostream>
#include<algorithm>
using namespace std;
int price[20000];//商品的价格

bool cmp(int x,int y){
    return x>y;
} 
 
int main(){
    int t;//t个测试用例[1,20]
    cin>>t;
    while(t--){
        int n;
        cin>>n;//商品数量[1,20000]
        for(int i=0;i<n;i++){
            cin>>price[i];
        }
        
        sort(price,price+n,cmp);//按照从大到小的顺序输出 
    
        int discount=0;
        for(int j=1;j<=n;j++){
            if(j%3==0){
                discount=discount+price[j-1];
            }
        }
        cout<<discount<<endl;
    } 
} 

1718、Rank[找出编号为id的同学的分数的排名]

Jackson wants to know his rank in the class. The professor has posted a list of student numbers and marks. Compute Jackson’s rank in class; that is, if he has the top mark(or is tied for the top mark) his rank is 1; if he has the second best mark(or is tied) his rank is 2, and so on.
Input
The input consist of several test cases. Each case begins with the student number of Jackson, an integer between 10000000 and 99999999. Following the student number are several lines, each containing a student number between 10000000 and 99999999 and a mark between 0 and 100. A line with a student number and mark of 0 terminates each test case. There are no more than 1000 students in the class, and each has a unique student number.
Output
For each test case, output a line giving Jackson’s rank in the class.
Sample Input

20070101
20070102 100
20070101 33
20070103 22
20070106 33
0 0

Sample Output

2

Code
利用一个结构体数组存储学生的学号和分数

//找出编号为id的同学的分数的排名 
//排名,输出分数比你高的同学的个数
#include<iostream>
#include<string>
using namespace std;

struct Student{
    string id;
    double score;//[0,100] 
} stu[1000];

int main(){
    string s_id;
    while(cin>>s_id){    
        int i=0;
        while(cin>>stu[i].id>>stu[i].score){
            if(stu[i].id=="0"&&stu[i].score==0) break;
            i++;
        }
        double s_score;
        //找到s_id学生的分数 
        for(int j=0;j<i;j++){
            if(stu[j].id==s_id){
                s_score=stu[j].score; 
            }
        }
        //计算比s_score分数高的学生的个数 
        int count=0;
        for(int j=0;j<i;j++){
            if(stu[j].score>s_score){
                count++;
            } 
        }
        //人数+1即为名次 
        cout<<count+1<<endl; 
    } 
    
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值