题目:
国际乒联主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及。其中11分制改革引起了很大的争议,有一部分球员因为无法适应新规则只能选择退役。明明就是其中一位,他退役之后走上了乒乓球研究工作,意图弄明白11分制和21分制对选手的不同影响。在开展他的研究之前,明明首先需要对他多年比赛的统计数据进行一些分析,所以需要你的帮忙。 (注:11(21)分制,在一局比赛中,选手A先得到11(21)分且此时领先选手B 2分或2分以上时,则选手A赢得此局;若当双方打成10(20)平后,则先多得2分的一方为胜方,赢得此局。)
明明通过以下方式进行分析,首先将比赛每个球的胜负列成一张表,然后分别计算在11分制和21分制下,双方的比赛结果(截至记录末尾)。一局比赛的开始比分为0比0。 比如现在有这么一份记录,(其中W表示明明获得一分,L表示明明的对手获得一分):
WWWWWWWWWWWWWWWWWWWWWWLW
在11分制下,此时比赛的结果是明明第一局11比0获胜,第二局11比0获胜,正在进行第三局,当前比分1比1。
在21分制下,此时比赛结果是明明第一局21比0获胜,正在进行第二局,当前比分2比1。
再如有这么一份记录,(其中W表示明明获得一分,L表示明明的对手获得一分):
WLWLWLWLWLWLWLWLWLWLWLWLWL
在11分制下,此时比赛的结果是明明和对手打成13比13,这局比赛仍没有分出胜负,因为任何一方都没有领先其对手2分。
在21分制下,此时比赛的结果是明明和对手打成13比13,这局比赛仍在进行中。
由于明明参加过多年的比赛,比赛的数据量相当庞大,如果仅仅使用手工统计,在短时间内统计出结果对于明明来说是相当困难的。因此明明求助于你,希望你能写一个程序,帮助他快速地统计出结果来。
明明的问题可以归结为:给你一系列的比赛数据(WL形式),分别按照11分制和21分制的比赛规则进行统计,然后输出统计结果。
个人总结:
- 注意获胜条件:11(21)分制,在一局比赛中,选手A先得到11(21)分且此时领先选手B 2分或2分以上时,则选手A赢得此局;若当双方打成10(20)平后,则先多得2分的一方为胜方,赢得此局。
- 该题输入方式要注意,全部输入后进行判断,按E为分界
cpp
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
void cntpoint(string res, int n) {
int w = 0, l = 0;
for (char c : res) {
if (c == 'E') break;
if (c == 'W') w++;
else if (c == 'L') l++;
if ((w >= n || l >= n) && abs(w - l) >= 2) {
cout << w << ":" << l << endl;
w = 0;
l = 0;
}
}
if (w == 0 && l == 0)return;
cout << w << ":" << l << endl;
}
int main() {
string line = "";
string res = "";
while (getline(cin, line)) {
if (line.empty()) break;
res += line;
}
int t = 0;
string k = "";
for (char a : res) {
if (a == 'E') {
if (t != 0)cout << endl;
t = 1;
cntpoint(k, 11);
cout << endl;
cntpoint(k, 21);
k = "";
}
else {
k += a;
}
}
return 0;
}
题目:
明明最近在做一个有关字符串的统计工作。两个由小写字母组成的字符串s1和s2,明明需要统计出以下四种关系:
(1)在s1或s2中存在的字母(包括在s1和s2中都存在的字母);
(2)在s1中且在s2中的字母;
(3)在s1中但不在s2中的字母,在s2中但不在s1中的字母;
(4)不在s1中且也不在s2中的字母;
例如两个字符串s1为"lkjsvoahs",s2为"qglhskjdfg":
(1)在s1或者在s2或者s1、s2中都存在的字母:adfghjkloqsv;
(2)在s1中且在s2中的字母:hjkls;
(3)在s1中但不在s2中的字母,在s2中但不在s1中的字母:adfgoqv;
(4)不在s1中且也不在s2中的字母:bceimnprtuwxyz;
明明统计了很久,但是由于统计过程十分繁琐,且很容易出错,导致明明的进度非常慢,很有可能因为统计不完而错过了晚上的约会。因此明明想请你帮个忙,帮他写一个程序,用程序来统计出以上几项内容。
明明的问题可以归结为:
输入两串由小写字母组成的字符串s1和s2,比较其中的字母,输出以下四项,输出的字母以字典顺序排列:
(1)在s1或s2中存在的字母(包括在s1和s2中都存在的字母);
(2)在s1中且在s2中的字母;
(3)在s1中但不在s2中的字母,在s2中但不在s1中的字母;
(4)不在s1中且也不在s2中的字母;
例如字符串s1为sadf,s2为asdf,则需输出以下四行(注意输出的格式):
in s1 or s2:adfs
in s1 and s2:adfs
in s1 but not in s2 ,or in s2 but not in s1:
not in s1 and s2:bceghijklmnopqrtuvwxyz
cpp
#include<iostream>
#include<string>
#include<cmath>
//(1)在s1或s2中存在的字母(包括在s1和s2中都存在的字母); a
//(2)在s1中且在s2中的字母;b a
//(3)在s1中但不在s2中的字母,在s2中但不在s1中的字母;c
//(4)不在s1中且也不在s2中的字母;d
using namespace std;
int main() {
string s1, s2;
int t = 0;
while (cin >> s1 >> s2) {
string a, b, c, d;
for (char i = 'a'; i <= 'z'; i++) {
if (s1.find(i) == string::npos && s2.find(i) == string::npos) {
d.push_back(i);
}
else if (s1.find(i) != string::npos && s2.find(i) != string::npos) {
b.push_back(i);
a.push_back(i);
}
else if ((s1.find(i) == string::npos && s2.find(i) != string::npos)
|| (s1.find(i) != string::npos && s2.find(i) == string::npos)) {
c.push_back(i);
a.push_back(i);
}
}
if (t)cout << endl;
t = 1;
cout << "in s1 or s2:" << a << endl;
cout << "in s1 and s2:" << b << endl;
cout << "in s1 but not in s2 ,or in s2 but not in s1:" << c << endl;
cout << "not in s1 and s2:" << d << endl;
}
return 0;
}
题目:
有时候程序员有很奇怪的方法来隐藏他们的口令。
Billy"Hacker"Geits会选择一个字符串S(由L个小写字母组成,5<=L<=100,000),然后他把S顺时针绕成一个圈。
如字符串cbadfa,绕成一个圈后,我们认为字符串首尾相连。
每次取其中一个字母作为起始字母,并顺时针依次取字母而组成一个字符串。这样将得到一些字符串。
比如字符串cbadfa,按照以上规则取出的字符串有:
cbadfa badfac adfacb dfacba facbad acbadf
我们找到最小的那个字符串,可知为acbadf,也可知道它的第一个字符'a'在原字符串cbadfa中为第6个字符(位置从1开始),
将得到的结果6减1得到5,这就是我们需要的口令。
再比如字符串alabala,绕成一个圈后,每次取其中一个字母作为起始字母,并顺时针依次取字母而组成一个字符串。这样将得到一些字符串:
alabala labalaa abalaal balaala alaalab laalaba aalabal
我们找到最小的那个字符串,可知为aalabal,它的第一个字母'a'在原字符串中位置为7,7-1=6,则6为口令。
注:如果按照规则有两个字符串都是最小的,则取前面那一个。
个人总结:
- 输入len后,需要清除回车键(用cin.ignore())
cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
int len;
string line, a;
cin >> len;
cin.ignore();
while (getline(cin, line)) {
if (line.empty()) break;
a += line;
}
string min = a;
int index = 0;
for (int i = 0; i < len; i++) {
string c = "";
for (int j = i; j < len; j++) {
c += a[j];
}
for (int j = 0; j < i; j++) {
c += a[j];
}
if (c < min) {
min = c;
index = i;
}
}
cout << index << endl;
return 0;
}
Translation:
To return, then, to the switching capabilities of a moderncomputer:computers in the 1970s were generally able to handle eight switches at a time. That is, they could deal with eight binary digits, or bits, of data, at every cycle. A group of eightbits is called a byte, each byte containing 256 possible patternsof ONs and OFFs (or 1s and 0s). Each pattern is the equivalent ofan instruction, a part of an instruction, or a particular type ofdatum, such as a number or a character or a graphics symbol.The pattern 11010010, for example, might be binary data---in this case, the decimal number 210---or it might be an instructiontelling the computer to compare data stored in its switches todata stored in a certain memory-chip location.
那么,回到现代计算机的开关功能:20世纪70年代的计算机通常一次能处理8个开关。也就是说,它们每个周期可以处理8个二进制数字(即比特)的数据。8个比特组成一组,称为一个字节,每个字节包含256种可能的开/关(或1/0)模式。每种模式相当于一条指令、指令的一部分,或者某种特定类型的数据,比如一个数字、一个字符或一个图形符号。例如,11010010这个模式可能是二进制数据------在这种情况下,它对应的十进制数字是210------也可能是一条指令,告诉计算机将其开关中存储的数据与某个存储芯片位置中存储的数据进行比较。
The development of processors that can handle 16, 32, and 64bits of data at a time has increased the speed of computers. The complete collection of recognizable patterns---the total list of operations---of which a computer is capable is called its instruction set. Both factors---the number of bits that can behandled at one time, and the size of instruction sets---continueto increase with the ongoing development of modern digitalcomputers.
能够一次处理16位、32位和64位数据的处理器的发展提高了计算机的速度。计算机能够识别的模式的完整集合------即操作的总列表------被称为其指令集。随着现代数字计算机的不断发展,这两个因素------一次可处理的位数和指令集的大小------都在持续增加。
Modern digital computers are all conceptually similar,regardless of size. Nevertheless, they can be divided into severalcategories on the basis of cost and performance: the personalcomputer or microcomputer, a relatively low-cost machine,usually of desktop size (though "laptops" are small enough to fit in a briefcase, and "palmtops" can fit into a pocket); theworkstation, a microcomputer with enhanced graphics andcommunications capabilities that make it especially useful foroffice work; the minicomputer, generally too expensive for personal use, with capabilities suited to a business, school, orlaboratory; and the mainframe computer, a large, expensivemachine with the capability of serving the needs of majorbusiness enterprises, government departments, scientificresearch establishments, or the like (the largest and fastest of these are called supercomputers).
现代数字计算机虽然在规模上有所不同,但在概念上都是相似的。不过,根据成本和性能,它们可以分为几类:个人计算机或微型计算机,这是一种成本相对较低的机器,通常是台式机大小(尽管"笔记本电脑"小到可以放进公文包,"掌上电脑"则能放进衣袋);工作站,一种具有增强的图形和通信能力的微型计算机,特别适用于办公室工作;小型计算机,通常价格昂贵,不适合个人使用,其性能适合企业、学校或实验室;以及大型计算机,一种大型、昂贵的机器,能够满足大型企业、政府部门、科研机构等的需求(其中最大、最快的被称为超级计算机)。
- palmtop 掌上电脑
