冬令营第一天记录
因为种种不可抗力因素,简单写写题解。
第一题:Specialized Four-Digit Numbers
这个题是个水题写几个函数就过了
题干
Find and list all four-digit numbers in decimal notation that have the property that the sum of its four digits equals the sum of its digits when represented in hexadecimal (base 16) notation and also equals the sum of its digits when represented in duodecimal (base 12) notation.
For example, the number 2991 has the sum of (decimal) digits 2+9+9+1 = 21. Since 2991 = 11728 + 8144 + 9*12 + 3, its duodecimal representation is 1893 12, and these digits also sum up to 21. But in hexadecimal 2991 is BAF 16, and 11+10+15 = 36, so 2991 should be rejected by your program.
The next number (2992), however, has digits that sum to 22 in all three representations (including BB0 16), so 2992 should be on the listed output. (We don’t want decimal numbers with fewer than four digits – excluding leading zeroes – so that 2992 is the first correct answer.)
Input
There is no input for this problem
Output
Your output is to be 2992 and all larger four-digit numbers that satisfy the requirements (in strictly increasing order), each on a separate line with no leading or trailing blanks, ending with a new-line character. There are to be no blank lines in the output. The first few lines of the output are shown below.
Sample Input
There is no input for this problem
Sample Output
2992
2993
2994
2995
2996
2997
2998
2999
…
题解
#include <iostream>
using namespace std;
int ten(int m)
{
int sum=0;
while(m>0)
{
sum+=m%10;
m/=10;
}
return sum;
}//求十进制所有位数和;
int twelve(int m)
{
int sum=0;
while(m>0)
{
sum+=m%12;
m/=12;
}
return sum;
}//求十二进制所有位数和;
int sixteen(int m)
{
int sum=0;
while(m>0)
{
sum+=m%16;
m/=16;
}
return sum;
}//求十六进制所有位数和;
int equal(int a,int b,int c)
{
if(a==b && b==c) return 1;
else return 0;
}//判断三个数是否相等;
int main()
{
for(int i=2992;i<=9999;i++)
{
if(equal(ten(i),twelve(i),sixteen(i))) cout<<i<<endl;
}
return 0;
}
第二题:Pig-Latin
题干
You have decided that PGP encryptation is not strong enough for your email. You have decided to
supplement it by first converting your clear text letter into Pig Latin before encrypting it with PGP.
Input and Output
You are to write a program that will take in an arbitrary number of lines of text and output it in Pig
Latin. Each line of text will contain one or more words. A “word” is defined as a consecutive sequence
of letters (upper and/or lower case). Words should be converted to Pig Latin according to the following
rules (non-words should be output exactly as they appear in the input):
- Words that begin with a vowel (a, e, i, o, or u, and the capital versions of these) should just
have the string “ay” (not including the quotes) appended to it. For example, “apple” becomes
“appleay”. - Words that begin with a consonant (any letter than is not A, a, E, e, I, i, O, o, U or u) should
have the first consonant removed and appended to the end of the word, and then appending “ay”
as well. For example, “hello” becomes “ellohay”. - Do not change the case of any letter.
Sample Input
This is the input.
Sample Output
hisTay isay hetay inputay.
题解
#include<iostream>
#include<algorithm>
using namespace std;
char a[10005];
int pdzm(char c)//判断是否字母
{
if(c>='a'&&c<='z')
return 1;
if(c>='A'&&c<='Z')
return 1;
return 0;
}
int pdyy(char c)//判断是否元音
{
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
return 1;
if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
return 1;
return 0;
}
int main()
{
while(gets(a))
{
int s=0,t=0;//s代表当前字母,t代表单词长度.
while(a[s])
if(!pdzm(a[s]))
{
cout<<a[s++];
t=s;
}
//不是字母就输出它.
else
if(pdzm(a[t]))
t++;
//是字母就记录长度
else //如果是辅音开头把单词的开头放到最后,然后后加上ay后缀即可。
{
if(!pdyy(a[s]))
{
for(int i=s+1;i<t;i++)cout<<a[i];
cout<<a[s];
}
else
for(int i=s;i<t;i++)
cout<<a[i];
cout<<"ay";
s=t;
}
cout<<endl;
}
return 0;
}
第三题 Tic Tac Toe
题干
Tic Tac Toe is a child’s game played on a 3 by 3 grid. One player, X, starts by placing an X at an unoccupied grid position. Then the other player, O, places an O at an unoccupied grid position. Play alternates between X and O until the grid is filled or one player’s symbols occupy an entire line (vertical, horizontal, or diagonal) in the grid.
We will denote the initial empty Tic Tac Toe grid with nine dots. Whenever X or O plays we fill in an X or an O in the appropriate position. The example below illustrates each grid configuration from the beginning to the end of a game in which X wins.
… X… X.O X.O X.O X.O X.O X.O
… … … … .O. .O. OO. OO.
… … … …X …X X.X X.X XXX
Your job is to read a grid and to determine whether or not it could possibly be part of a valid Tic Tac Toe game. That is, is there a series of plays that can yield this grid somewhere between the start and end of the game?
Input
The first line of input contains N, the number of test cases. 4N-1 lines follow, specifying N grid configurations separated by empty lines.
Output
For each case print “yes” or “no” on a line by itself, indicating whether or not the configuration could be part of a Tic Tac Toe game.
Sample Input
2
X.O
OO.
XXX
O.X
XX.
OOO
Sample Output
yes
no
题解
该题用的模拟代码如下
#include <iostream>
#include <stdio.h>
using namespace std;
const int N = 3;
char grid[N][N + 1];
bool judge(char c)
{
int j;
for(int i = 0; i < N; i++)
{
for(j = 0; j < N && grid[i][j] == c; j++);
if(j == N)
return true;
for(j = 0; j < N && grid[j][i] == c; j++);
if(j == N)
return true;
}
for(j = 0; j < N && grid[j][j] == c; j++);
if(j == N)
return true;
for(j = 0; j < N && grid[j][N - 1 - j] == c; j++);
if(j == N)
return true;
return false;
}
int main()
{
int n;
scanf("%d", &n);
while(n--)
{
int xcnt = 0, ocnt = 0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
cin>>grid[i][j];
if(grid[i][j]=='X') xcnt++;
else if(grid[i][j]=='O') ocnt++;
}
if(xcnt<ocnt||xcnt-ocnt>1||(judge('O')&&ocnt!=xcnt)||(judge('X')&&xcnt-ocnt!=1))
cout<<"no"<<endl;
else cout<<"yes"<<endl;
}
return 0;
}
第四题:Factorial! You Must be Kidding!!!
题干
Arif has bought a super computer from Bongobazar. Bongobazar is a place in Dhaka where second hand goods are found in plenty. So the super computer bought by him is also second hand and has some bugs. One of the bugs is that the range of unsigned long integer of this computer for C/C++ compiler has changed. Now its new lower limit is 10000 and upper limit is 6227020800. Arif writes a program in C/C++ which determines the factorial of an integer. Factorial of an integer is defined recursively as:
factorial(0) = 1
factorial(n) = n ∗ f actorial(n − 1).
Of course one can manipulate these expressions. For example, it can be written as
factorial(n) = n ∗ (n − 1) ∗ f actorial(n − 2)
This definition can also be converted to an iterative one.
But Arif knows that his program will not behave rightly in the super computer. You are to write program which will simulate that changed behavior in a Normal Computer.
Input
The input file contains several lines of input. Each line contains a single integer n. No integer has more than six digits. Input is terminated by end of file.
Output
For each line of input you should output a single line. This line will contain a single integer n! if the value of n! fits within the unsigned long integer of Arif’s computer. Otherwise the line will contain one of the following two words
Overflow! (When n! > 6227020800)
Underflow! (When n! < 10000)
Sample Input
2
10
100
Sample Output
Underflow!
3628800
Overflow!
该题狗的地方就是阶乘可以有负的
题解如下:
/* UVA10323 Factorial! You Must be Kidding!!! */
#include <iostream>
#include <algorithm>
using namespace std;
const long long FACT1 = 10000;
const long long FACT2 = 6227020800;
const int N = 13;
long long fact[N + 1];
void init()
{
fact[0] = 1;
for(int i=1; i<=N; i++) {
fact[i] = i * fact[i - 1];
#ifdef DEBUG
if(fact[i] > FACT2) {
printf("N=%d\n", i - 1);
break;
}
#endif
}
}
int main()
{
init();
int n;
while(~scanf("%d", &n))
if(n > N || (n < 0 && (n * -1 & 1)== 1))
printf("Overflow!\n");
else if(fact[n] < FACT1 || (n < 0 && (n * -1 & 1) == 0))
printf("Underflow!\n");
else
printf("%lld\n", fact[n]);
return 0;
}
第五题 Function Run Fun
题干
Description
We all love recursion! Don’t we?
Consider a three-parameter recursive function w(a, b, c):
if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:
1
if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:
w(20, 20, 20)
if a < b and b < c, then w(a, b, c) returns:
w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
otherwise it returns:
w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
This is an easy function to implement. The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15, b = 15, c = 15), the program takes hours to run because of the massive recursion.
Input
The input for your program will be a series of integer triples, one per line, until the end-of-file flag of -1 -1 -1. Using the above technique, you are to calculate w(a, b, c) efficiently and print the result.
Output
Print the value for w(a,b,c) for each triple.
Sample Input
1 1 1
2 2 2
10 4 6
50 50 50
-1 7 18
-1 -1 -1
Sample Output
w(1, 1, 1) = 2
w(2, 2, 2) = 4
w(10, 4, 6) = 523
w(50, 50, 50) = 1048576
w(-1, 7, 18) = 1
题解
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
while ( ~scanf("%d",&n) ) {
if ( (n>=0&&n<8) || (n<0&&((n*-1)%2)==0) ) printf("Underflow!\n");
if ( n == 8 ) printf("40320\n");
if ( n == 9 ) printf("362880\n");
if ( n == 10 ) printf("3628800\n");
if ( n == 11 ) printf("39916800\n");
if ( n == 12 ) printf("479001600\n");
if ( n == 13 ) printf("6227020800\n");
if ( n > 13 || (n<0&&((n*-1)%2)==1) ) printf("Overflow!\n");
}
return 0;
}
第六题:Simple Addition
题干
Lets define a simple recursive function F(n), where
F(n) = p(x) =
n%10, if (n%10) > 0
0, if n = 0
F(n/10), Otherwise
Lets define another function S(p, q),
S(p, q) = ∑q
i=p
F(i)
In this problem you have to Calculate S(p, q) on given value of p and q.
Input
The input file contains several lines of inputs. Each line contains two non negative integers p and q
(p ≤ q) separated by a single space. p and q will fit in 32 bit signed integer. In put is terminated by a
line which contains two negative integers. This line should not be processed.
Output
For each set of input print a single line of the value of S(p, q).
Sample Input
1 10
10 20
30 40
-1 -1
Sample Output
46
48
52
题解
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std ;
long long ans , p , q ;
long long f( long long n )
{
if( n == 0 )
return 0 ;
else if( n % 10 )
{
return n % 10 ;
}
else
{
return f( n / 10 ) ;
}
}
void DFS( long long x , long long y )
{
long long i , j ;
if( y - x < 10 )
{
for( int i = x ; i <= y ; ++i )
{
ans += f( i ) ;
}
return ;
}
for( i = x ; i % 10 != 0 ; ++i )
{
ans += f( i ) ;
}
for( j = y ; j % 10 != 0 ; --j )
{
ans += f( j ) ;
}
ans += 45 * ( ( j - i ) / 10 );
DFS( i / 10 , j / 10 ) ;
}
int main()
{
while( scanf( "%lld%lld" , &p , &q ) != EOF )
{
if( p == -1 && q == -1 )
break ;
ans = 0 ;
DFS( p , q ) ;
printf( "%lld\n" , ans ) ;
}
return 0 ;
}
第六题 A Contesting Decision
题干:
Problem Description
Judging a programming contest is hard work, with demanding contestants, tedious decisions,and monotonous work. Not to mention the nutritional problems of spending 12 hours with only donuts, pizza, and soda for food. Still, it can be a lot of fun.
Software that automates the judging process is a great help, but the notorious unreliability of some contest software makes people wish that something better were available. You are part of a group trying to develop better, open source, contest management software, based on the principle of modular design.
Your component is to be used for calculating the scores of programming contest teams and determining a winner. You will be given the results from several teams and must determine the winner.
Scoring
There are two components to a team’s score. The first is the number of problems solved. The second is penalty points, which reflects the amount of time and incorrect submissions made before the problem is solved. For each problem solved correctly, penalty points are charged equal to the time at which the problem was solved plus 20 minutes for each incorrect submission. No penalty points are added for problems that are never solved.
So if a team solved problem one on their second submission at twenty minutes, they are charged 40 penalty points. If they submit problem 2 three times, but do not solve it, they are charged no penalty points. If they submit problem 3 once and solve it at 120 minutes, they are charged 120 penalty points. Their total score is two problems solved with 160 penalty points.
The winner is the team that solves the most problems. If teams tie for solving the most problems,then the winner is the team with the fewest penalty points.
Input
For the programming contest your program is judging, there are four problems. You are guaranteed that the input will not result in a tie between teams after counting penalty points.
Line 1 < nTeams >
Line 2 - n+1 < Name > < p1Sub > < p1Time > < p2Sub > < p2Time > … < p4Time >
Output
The output consists of a single line listing the name of the team that won, the number of problems they solved, and their penalty points.
Sample Input
4
Stars 2 20 5 0 4 190 3 220
Rockets 5 180 1 0 2 0 3 100
Penguins 1 15 3 120 1 300 4 0
Marsupials 9 0 3 100 2 220 3 80
Sample Output
Penguins 3 475
题解:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef struct
{
char name[30];
int sub[4];
int endtime[4];
int total;
int score;
}team;
team teams[100000];
bool cmp(team a , team b)
{
if(a.total == b.total) return a.score < b.score;
return a.total > b.total;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i = 0; i < n; i ++)
{
scanf("%s",teams[i].name);
teams[i].score = teams[i].total = 0;
for(int j = 0; j < 4; j ++)
{
scanf("%d %d",&teams[i].sub[j],&teams[i].endtime[j]);
if(teams[i].endtime[j]){
teams[i].total ++;
teams[i].score +=(teams[i].sub[j]*20-20+teams[i].endtime[j]);
}
}
}
sort(teams , teams+n , cmp);
printf("%s %d %d\n",teams[0].name ,teams[0].total ,teams[0].score);
}
return 0;
}