Alfredo’s Pizza Restaurant
Traditionally after the Local Contest, judges and contestants go to their favourite restaurant, Alfredos Pizza Restaurant. The contestants are really hungry after trying hard for five hours. To get their pizza as quickly as possible, they just decided to order one big pizza for all instead of several small ones. They wonder whether it is possible to put the big rectangular pizza on the surface of the round table such that it does not overhang the border of the table. Write a program that helps them!
Input
The input file contains several test cases. Each test case starts with an integer number r, the radius of the surface of the round table the contestants are sitting at. Input is terminated by r = 0. Otherwise, 1 ≤ r ≤ 1000. Then follow 2 integer numbers w and l specifying the width and the length of the pizza, 1 ≤ w ≤ l ≤ 1000.
Output
Output for each test case whether the ordered pizza will fit on the table or not. Adhere to the format shown in the sample output. A pizza which just touches the border of the table without intersecting it is considered fitting on the table, see example 3 for clarification.
Sample Input
38 40 60
35 20 70
50 60 80
0
Sample Output
Pizza 1 fits on the table.
Pizza 2 does not fit on the table.
Pizza 3 fits on the table.
注意:不要用int
#include<iostream>
using namespace std;
int main()
{
int n=0;
while(1)
{
double r;
cin>>r;
if(r==0)
break;
double w,l;
n++;
cin>>w>>l;
double wi=w/2;
double li=l/2;
if(wi*wi+li*li>r*r)
cout<<"Pizza "<<n<<" does not fit on the table."<<endl;
else
cout<<"Pizza "<<n<<" fits on the table."<<endl;
}
}
Encryption
You must have heard of an ancient encryption called Caesar cipher or ‘shift cipher’. That is, given the plaintext and a number D, you should replace every character c in the plaintext with another character which is D places after c in the alphabet. For example, if D = 2, you should replace ‘a’ with ‘c’, replace ‘b’ with ‘d’, … replace ‘y’ with ‘a’, and replace ‘z’ with ‘b’.
Given the plaintext and D, you should output the cipher text.
Input
The first line is an integer T, the number of test cases. Then T cases follows.
Each case contains only one line, consists of the plaintext and the number D, separated by a space. You can assume there are only lower case letters in the plaintext, and the length is no more than 100. 0 ≤ D < 26.
Output
Output one line for each test case, indicating the cipher text.
Sample Input
2
tjucs 1
abcd 0
Sample Output
ukvdt
abcd
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;//测试用例的数量
while(n--)
{
string s;
int d;
cin>>s>>d;//字符串和间隔
for(int i=0;i<s.length();i++)
{
s[i]='a'+(s[i]-'a'+d)%26;
}
cout<<s<<endl;
}
}
Counting Letters
As a talented student, your boss gave you a task. Given a text string, you should find out which letters appear most frequently.
Really simple, isn’t it?
Input
The first line of the input is the number of test cases. Then some test cases followed.
Each test case contains only one line, indicating the string mentioned above.
You can assume there is only lower case letters in the string. The length of the string is more than 0 and no more than 100.
Output
You should output one line for each test case, indicating the most frequent letters. Please note there may be more than one such letter, so you should output all of them, and these letters should be in the same order as the alphabet, that is, ‘a’, ‘b’, ‘c’, ‘d’ … ‘z’.
Sample Input
2
abcda
tjucstju
Sample Output
a
jtu
Hint
In the first sample test case, ‘a’ appears twice, while ‘b’,‘c’,‘d’ only appear once. So ‘a’ is the most frequent letter.
In the second sample test case, ‘t’, ‘j’ and ‘u’ appear twice, so you should output them all, and ‘j’ should be the first.
Please note you should not output any unnecessary spaces or empty lines, or else you may get ‘Presentation Error’.
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;//测试用例的数量
while(n--)
{
int alp[26]={0};
string s;
cin>>s;
for(int i=0;i<s.length();i++)
{
alp[s[i]-'a']+=1;
}
int maxnum=0;
//找到最大的那个值
for(int i=0;i<26;i++)
{
if(alp[i]>maxnum)
maxnum=alp[i];
}
for(int i=0;i<26;i++)
{
if(alp[i]==maxnum)
cout<<char('a'+i);
}
cout<<endl;
}
}
Sixth Grade Math
In sixth grade, students are presented with different ways to calculate the Least Common Multiple (LCM) and the Greatest Common Factor (GCF) of two integers. The LCM of two integers a and b is the smallest positive integer that is a multiple of both a and b. The GCF of two non-zero integers a and b is the largest positive integer that divides both a and b without remainder.
For this problem you will write a program that determines both the LCM and GCF for positive integers.
Input
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of data sets that follow. Each data set consists of a single line of input containing two positive integers, a and b, (1 ≤ a,b ≤ 1000) separated by a space.
Output
For each data set, you should generate one line of output with the following values: The data set number as a decimal integer (start counting at one), a space, the LCM, a space, and the GCF.
Sample Input
3
5 10
7 23
42 56
Sample Output
1 10 5
2 161 1
3 168 14
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;//测试用例的数量
int p=0;
while(n--)
{
int a,b;
cin>>a>>b;
int n=0,m=0;
if(a>b)
{
n=b;
m=a;
}
else
{
n=a;
m=b;
}
//求最大公约数
while(a%n!=0||b%n!=0)
{
n--;
}
//最小公倍数
while(m%a!=0||m%b!=0)
{
m=m+n;
}
cout<<++p<<" "<<m<<" "<<n<<endl;
}
}
Differences
CXB attended an interview and the interviewer given him a problem. The problem asked him to be in a sequence to find the difference between the maximum and minimum values.
Input
The first line of the input contains ca (ca ≤ 1000).
The following ca test case. For each test case, there are two lines. There is a integer number of n in the first line (1 ≤ n ≤ 1000). There are n number of integer numbers in next line. The range of each number is [-264+1,264-1].
Output
For each case, print the answer.
Sample Input
1
5
1 2 3 4 5
Sample Output
4
#include<iostream>
#include<limits.h>
using namespace std;
int main()
{
int n;
cin>>n;//测试用例的数量
while(n--)
{
int num;
cin>>num;//数字的个数
long long maxn=LONG_LONG_MIN,minn=LONG_LONG_MAX;
while(num--)
{
long long s;
cin>>s;
if(s<minn)
minn=s;
if(s>maxn)
maxn=s;
}
long long d=maxn-minn;
cout<<d<<endl;
}
}
Counting Subsequences
“47 is the quintessential random number,” states the 47 society. And there might be a grain of truth in that. For example, the first ten digits of the Euler’s constant are:
2 7 1 8 2 8 1 8 2 8
And what’s their sum? Of course, it is 47. Try walking around with your eyes open. You may be sure that soon you will start discovering occurences of the number 47 everywhere.
You are given a sequence S of integers we saw somewhere in the nature. Your task will be to compute how strongly does this sequence support the above claims. We will call a continuous subsequence of S interesting if the sum of its terms is equal to 47. E.g., consider the sequence S = (24, 17, 23, 24, 5, 47). Here we have two interesting continuous subsequences: the sequence (23, 24) and the sequence (47). Given a sequence S, find the count of its interesting subsequences.
Input
The first line of the input file contains an integer T specifying the number of test cases. Each test case is preceded by a blank line. The first line of each test case contains the length of a sequence N (N ≤ 1000000) The second line contains N space-separated integers - the elements of the sequence.
Output
For each test case output a single line containing a single integer - the count of interesting subsequences of the given sentence.
Sample Input
2
13
2 7 1 8 2 8 1 8 2 8 4 5 9
7
2 47 10047 47 1047 47 47
Sample Output
3
4
注意:这题有点坑。不要管空行,有空行就会Presentation Error
#include<iostream>
#include<map>
using namespace std;
int main()
{
int n;
cin>>n;//测试用例的数量
while(n--)
{
//cout<<endl;
int num;
cin>>num;
map<int,int> a;
a[0]=1;
int sum=0;
int count=0;
while(num--)
{
int s;
cin>>s;
sum=sum+s;
a[sum]++;
count=count+a[sum-47];
}
cout<<count<<endl;
}
}
How Many Islands?
Description
You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map.
Figure B-1: A marine area map
You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot.
You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands.
Input
The input consists of a series of datasets, each being in the following format.
w h
c1,1 c1,2 … c1,w
c2,1 c2,2 … c2,w
…
ch,1 ch,2 … ch,w
w and h are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of w×h squares of the same size. w and h are separated by a single space.
ci, j is either 0 or 1 and delimited by a single space. If ci,j = 0, the square that is the i-th from the left and j-th from the top on the map represents a sea area. Otherwise, that is, if ci, j = 1, it represents a land area.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
For each dataset, output the number of the islands in a line. No extra characters should occur in the output.
Sample Input
1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
Sample Output
0
1
1
3
1
9
这题的意思是求有多少个岛,1为岛,横向斜向纵向的1可以连在一起,算作一个大岛。可以采用深度优先搜索的方法,递归来实现。
#include<iostream>
using namespace std;
bool area[52][52]={false}; //0就不搜索
void search(int i,int j)
{
int x,y;//沿八个方向进行搜索
area[i][j]=false;//确保这个位置不再搜索了
for(x=-1;x<=1;x++)
{
for(y=-1;y<=1;y++)
{
if(area[i+x][j+y])//没有搜索过
search(i+x,j+y);
}
}
}
int main()
{
while(1)
{
int w,h;
cin>>w>>h;
if(w==0&&h==0)
break;
for(int i=1;i<=h;i++)
{
for(int j=1;j<=w;j++)
{
cin>>area[i][j];//输入0,1,但0就默认就不搜索
}
}
int count=0;//有多少个岛
for(int i=1;i<=h;i++) //注意必须要从1开始,不然的话在边界处向左搜索就会越界
{
for(int j=1;j<=w;j++)
{
if(area[i][j])//1的位置就搜索
{
search(i,j);
count++;
}
}
}
cout<<count<<endl;
}
}