3月14日练习记录

比赛题目

Binarize It

Title

Professor Boolando can only think in binary, or more specifically, in powers of 2. He converts any number you give him to the smallest power of 2 that is equal to or greater than your number. For example, if you give him 5, he converts it to 8; if you give him 100, he converts it to 128; if you give him 512, he converts it to 512.
The Problem:
Given an integer, your program should binarize it.

Input

The first input line contains a positive integer,n, indicating the numberof values to binarize. The values are on the followingninput lines, one per line. Each input will contain an integer between2 and 100,000 (inclusive).

Output

At thebeginning of each testcase, output “Inputvalue:v”wherevis the input value. Then,on the next output line, print the binarized version. Leave a blank line after the output for each test case.

Answer

#include<iostream>
#include<cmath>
using namespace std;
int main(){
    int t,n;
    cin >> t;
    while(t--){
        cin >> n;
        int i;
        for(i=1;;i++){
            if(pow(2,i) >= n) break;
        }
        cout << "Input value: " << n << endl;
        cout << pow(2,i) << endl;
        cout << endl;
    }
}

Hint

签到题,有更小时间复杂度解法。

g2g c u l8r

Title

According to the national statistics, a teenager sends/receives 100+ text messages a day. Dr. Orooji’s teenage children are no exception but the problem is Dr. O (an old-fashioned, face-to- face communicator) has difficulty reading text messages full of abbreviations (short-hands) sent to him by his children. Dr. O needs your help reading these text messages.
The Problem:
Given the list of abbreviations and a paragraph, you are to expand the text (paragraph) so that Dr. O can read it easily.

Input

The first input linecontains an integer,n (1 ≤ n ≤ 20),indicating the number of abbreviations. Theseabbreviations are on the followingninputlines, one per line. Each input linestarts in column 1 and containsan abbreviation (1-5 characters, consisting of only lowercaseletters and/or digits). The abbreviation is followed by exactly one space, and this isfollowed by the expanded version ofthe abbreviation (1-50 characters, consisting of only lowercase letters and spaces; assume the expandedversion does not start or end with aspace and contains no multiple consecutive spaces between words).Assume that all abbreviations are distinct, i.e., no duplicates.
The list of abbreviations isfollowed by a positive integer,p,indicating the number of input lines containingthe paragraph to be expanded. Theparagraph is on the followingpinputlines. Assume these input linesdo not exceed column 50, donot start or end with a space, and each line contains at least one word. The paragraph will contain only lowercaseletters, digits, and spaces. Assume that there will not be multipleconsecutive spaces in theinput paragraph.
A word is defined as aconsecutive sequence of letters/digits. Assume that a word will be entirely on oneinput line, i.e., a word isnot broken over two or more lines.

Output

At thebeginning of each testcase, output “Inputvalue:v”wherevis the input value. Then,on the next output line, print the binarized version. Leave a blank line after the output for each test case.

Answer

#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<map>
#include<vector>
using namespace std;
int n,m;
vector<string> rec[30];
map<string,int> mp;
int main(){
    cin >> n;
    for(int i=1;i<=n+1;i++){
        string line,t;
        getline(cin,line);
        if(i!=1){
            stringstream linestream;
            linestream.str(line);
            while(linestream>>t){
                rec[i].push_back(t);
            }
            mp[rec[i][0]]=i;
        }
    }
    cin >> m;
    for(int i=1;i<=m+1;i++){
        string line,t;
        getline(cin,line);
        if(i!=1){
            stringstream linestream;
            linestream.str(line);
            while(linestream>>t){
                if(mp[t]){
                    for(int j=1;j<rec[mp[t]].size();j++){
                        cout << rec[mp[t]][j] << " ";
                    }
                }
                else{
                    cout << t << " ";
                }
            }
            cout << endl;
        }
    }
}

Hint

先把缩写的字母定位一下,然后把他们的全程与向量存储起来,之后转换即可,用map进行定位。

Tip to be Palindrome

Title

One of the cool UCF CS alumni is Dr. Greg, The Palindrome Tipper. A palindrome is a string
that reads the same forward and backward, e.g., madam, abba, 3, 44, 525.
One cool thing about Dr. Greg is that he leaves at least 20% tip when he eats out, e.g., if the meal is 30, Dr. Greg leaves 30, Dr. Greg leaves 6 (30*.20) for tip. If the tip (20%) is not a whole dollar amount, he rounds up the tip to make it a whole number. For example, if the meal is 12, a 20% tip would be12,a202.40 (12*0.20) but Dr. Greg would leave $3 for tip.
Another cool thing about Dr. Greg is that he is a palindrome guru. If his total bill (meal plus tip) is not a palindrome, he will increase the total (by adding to the tip) to make the total a palindrome. He will, of course, add the minimum needed to make the total a palindrome.
The Problem:
Given Dr. Greg’s meal cost, your program should determine the tip amount for him (according to his rules) and the total bill.

Input

The first input line contains a positive integer, n, indicating the number of times Dr. Greg ate out. The meal costs are on the following n input lines, one per line. Each input will contain an integer between 5 and 10000 (inclusive).

Output

At the beginning of each test case, output “Input cost: c” where c is the input cost. Then, on the next output line, print the tip amount and the total bill, separated by one space. Leave a blank line after the output for each test case.

Answer

#include<iostream>
#include<cmath>
using namespace std;
bool isPar(int i){
    int a[6];
    int j=0;
    while(i){
        a[j++] = i%10;
        i/=10;
    }
    bool flag = true;
    if(j==1) return flag;
    for(int k = 0,l = j-1;k<=j/2;k++,l--){
            if(a[k]!=a[l]){
                flag = false;
                break;
            }
        }
    return flag;
}
int main(){
    int t,n;
    cin >> t;
    while(t--){
        cin >> n;
        int from = ceil(n*1.2);
        int i;
        for(i=from;;i++){
            if(isPar(i)){
                break;
            }
        }
        cout << "Input cost: " << n << endl;
        cout << i-n << " " << i << endl;
        cout << endl;
    }
}

Hint

先把缩写的字母定位一下,然后把他们的全程与向量存储起来,之后转换即可,用map进行定位。

Soccer Standings

Title

Soccer fever has gripped the world once again, and millions of people from dozens of countries
will be glued to their TV sets for the World Cup. Being an enterprising sort, you’ve started up your own internet World Cup Soccer Channel for streaming matches online. Recently you came up with the idea of filling up the time between matches by having a couple of ‘experts’ offer critical analysis of games. For this purpose, you have devised a unique ranking system for soccer teams, which you must now implement.
The Problem:
Given a list of teams and a list of match scores, you must compute several quantities for each team. These are: the total number of goals scored over all their games, the total number of goals scored against them (goals allowed, for short), the number of wins, draws and losses, and the number of points scored so far. Points are to be computed as follows: winning a match nets a team 3 points, losing gets them nothing. In the event of a tie, both teams get 1 point.
In addition to this, you must order the teams correctly according to your new system. Teams are ordered according to points, from highest to lowest. In the event of a tie in points, the team that has a higher goal difference comes first. The goal difference is defined as the total number of goals scored by the team minus the total number of goals scored against them.
If there is still a tie (i.e., two or more teams have the same points and the same goal differences), the team with higher total goals scored comes first. If even this is tied, the team whose name comes first in alphabetical order goes first.

Input

The first input line contains a positive integer, n, indicating the number of data sets to be processed. The first line of each data set consists of two positive integers T (T ≤ 30) and G (G ≤
400) – the number of teams in this group and the total number of games played by them. The next line contains T unique names separated by single spaces. Each name is a single uppercase word with no more than 15 characters.
Each of the next G input lines will contain the results of a match. Each line is of the form
<country_1> <score_1> <country_2> <score_2>. For example, “Greece 2 Nigeria 1” indicates that Greece and Nigeria played a game with score 2-1. All four terms will be separated by single spaces.

Output

At the beginning of output for each data set, output “Group g:” where g is the data set number, starting from 1. Next you should print a single line for each team, ordering teams as mentioned above. For each team, the line you print should be of the form “ ”. These items should be separated by single spaces. Leave a blank line after the output for each data set.

Answer

#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<map>
#include<vector>
using namespace std;
int t;
int n,m;
const int N = 410;
map<string,int> mp;
struct Node{
    string name;
    int total;
    int allow;
    int win;
    int lose;
    int eql;
    int dec;
    int score;
   
}team[N];
bool cmp(Node A,Node B){
    if(A.score > B.score){
        return true;
    }
    else if(A.score < B.score){
        return false;
    }
    else{
        if(A.dec > B.dec){
            return true;
        }
        else if(A.dec < B.dec){
            return false;
        }
        else{
           if(A.total > B.total){
               return true;
           }
           else if(A.total < B.total){
               return false;
           }
           else{
               if(A.name < B.name){
                   return true;
               }
               else{
                   return false;
               }
           }
           
        }
    }
}
void init(){
    for(int i=1;i<=N;i++){
        team[i].total = team[i].win = team[i].lose = team[i].eql = team[i].dec = team[i].score = team[i].allow = 0;
    }
}
int main(){
    cin >> t;
    int now = 1;
    while(t--){
        init();
        cin >> n >> m;
        for(int i = 1;i <= n;i++){
            string s;
            cin >> s;
            team[i].name = s;
            mp[s] = i;
        }
        for(int i = 1;i <= m;i++){
            string t1,t2;
            int s1,s2;
            cin >> t1 >> s1 >> t2 >> s2;
            
            if(s1 > s2){
                team[mp[t1]].win++;
                team[mp[t1]].total+=s1;
                team[mp[t1]].allow+=s2;
                team[mp[t2]].lose++;
                team[mp[t2]].total+=s2;
                team[mp[t1]].score+=3;
                team[mp[t2]].allow+=s1;
            }
            
            else if(s1 < s2){
                team[mp[t1]].lose++;
                team[mp[t1]].total+=s1;
                team[mp[t2]].win++;
                team[mp[t2]].total+=s2;
                team[mp[t2]].score+=3;
                team[mp[t2]].allow+=s1;
                team[mp[t1]].allow+=s2;
            }
            
            else if(s1 == s2){
                team[mp[t1]].eql++;
                team[mp[t1]].total+=s1;
                team[mp[t2]].eql++;
                team[mp[t2]].total+=s2;
                team[mp[t1]].score+=1;
                team[mp[t2]].score+=1;
                team[mp[t2]].allow+=s1;
                team[mp[t1]].allow+=s2;
            }
        }
        for(int i=1;i<=n;i++){
            team[i].dec = team[i].total - team[i].allow;
        }
        sort(team+1,team+1+n,cmp);
        cout << "Group " << now++ << ":" << endl;
        for(int i=1;i<=n;i++){
            cout << team[i].name << " " << team[i].score << " " << team[i].win << " " << team[i].lose << " " << team[i].eql << " " << team[i].total << " " << team[i].allow << endl;
        }
        cout << endl;
    }
}

Hint

写好比较方式即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值