2021年度训练联盟热身训练赛第二场
A-Binarize It
链接:https://ac.nowcoder.com/acm/contest/12794/A
来源:牛客网
题目描述
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.
输入描述:
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).
输出描述:
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.
示例1
输入
3
900
16
4000
3
900
16
4000
输出
Input value: 900
1024
Input value: 16
16
Input value: 4000
4096
Input value: 900
1024
Input value: 16
16
Input value: 4000
4096
思路:水题,遍历循环即可。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<cmath>
using namespace std;
int main(){
int t,n,i,sum;
cin>>t;
while(t--){
cin>>n;
sum=1;
for(i=1;;i++){
sum*=2;
if(sum>=n){
cout<<"Input value: "<<n<<endl;
cout<<sum<<endl;
cout<<endl;
break;
}
}
}
return 0;
}
B-g2g c u l8r
链接:https://ac.nowcoder.com/acm/contest/12794/B
来源:牛客网
题目描述
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.
输入描述:
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.
输出描述:
Each line of the inputparagraph must be on one line of output. Theinput line must be printed in theoutput exactly the same (spacing). Theonly exception is that each abbreviation must be replaced by its expanded version, i.e., when an abbreviation isfound in the input, its expanded version must beoutput.
Note that an abbreviationmust match a word completely and not just part of a word. For example,if u is an abbreviation for “you”, then u must appear as a word by itself in the paragraph
in order to be replaced,i.e., if the abbreviation is part of a word in the paragraph (e.g.,the paragraph containsthe word buy or ugly or you),the u in these words should not be replaced.
示例1
输入
8
g2g got to go
g good
c see
l8 late
l8r later
d i am done
u you
r are
6
hi
how r u
you tell me
you are l8
d
c u l8r
8
g2g got to go
g good
c see
l8 late
l8r later
d i am done
u you
r are
6
hi
how r u
you tell me
you are l8
d
c u l8r
输出
hi
how are you
you tell me
you are late
i am done
see you later
hi
how are you
you tell me
you are late
i am done
see you later
思路:刚开始读题,文本太长读不下去也读不明白,直接看输入输出推出题意,
题目要求先给n组,每组先是简写字符串,空格后面跟着其原字符串,要求对p行英文进行原字符串的恢复;
那么就要用到字符串的读入和输出,读取单个字符串直接用cin即可,
读取整行字符串用getline(cin,str),要注意getchar函数读取空格,
定义stringstream类以便将整行字符串以空格分成单个单词,
使用map的count函数判断是否存在键值。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<cmath>
#include<sstream>
using namespace std;
int main(){
int n,p;
string s1,s,s2,a;
stringstream ss;//从string对象中读取字符,遇空格结束
map<string,string> mp;
mp.clear();
cin>>n;
while(n--){
cin>>s1;
getchar();
getline(cin,s2);//读取整行,包括前导和嵌入的空格
mp[s1]=s2;
}
cin>>p;
getchar