A - 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 189312, and these digits also sum up to 21. But in
hexadecimal 2991 is BAF16, 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 BB016), 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
…
思路
这道题,我认为做主要的便是求一个数i的x进制的每位数的和,
首先,得到这个数的末位数字,即与基数x取余,再用基数x整除这个数。
方法一:运用递归
int fun(int num)
{
if(num<10)
return num;
return num%10+fun(num/10);
}
方法二:
while(x>0){
ans += x%base;
x /= base;
}
方法三:
for(;x;x/=base)
ans += x%base;
B - Pig-Latin
输入和输出
请您编写一个程序,输入任意数量行的文本,并以Pig Latin输出。 每行文本将包含一个或多个单词。一个“单词”被定义为一个连续的字母序列(大写字母和/或小写字母)。单词根据以下的规则 转换为PigLatin,非单词的字符在输出时则和输入中出现的完全 一样:
[1]以元音字母(a、e、i、o或u,以及这些字母的大写形式) 开 头的单词,要在其后面附加字符串“ay”(不包括双引号)。例 如,“apple”变成“appleay”
[2]以辅音字母(不是A,a,E,e,l,i,O,o, U或u的任何字母)开头的单词,要去掉第一个辅音字母,并将之附加在单词的末尾,然 后再在单词的末尾加上“ay”。例如,“hello” 变成“ellohay”。
[3]不要改变任何字母的大小写。
Sample Input
This is the input.
Sample Output
hisTay isay hetay inputay.
思路:
①输入任意行的文本:每输入一行执行一次下述循环,直到不再输入;
while(a[n++]=getchar()!=EOF)
②每到新的一行,记录字符串始末位置的变量left,right都初始化为0;
详细见代码
#include
using namespace std;
#define N 100005
char a[N];
bool isab(char c){
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
return true;
else return false;
}
bool vowel(char c){
if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U')
return true;
else return false;
}
int main(){
int n,left,right;
while((a[n++] = getchar())!=EOF){
if(a[n-1]=='\n'){
left = 0;
right = 0;
while(a[left]){
//存在
if(!isab(a[left])){
// 先找到left到right的一个单词
cout << a[left++];
right = left;
}
else if(isab(a[right]))
right++;
else{
if(!vowel(a[left])){
//如果首字母不是元音字母
for(int i=left+1;i<right;i++)
cout << a[i];
cout << a[left];
}
else //首字母是元音字母
for(int i=left;i<right;i++)
cout << a[i];
cout << "ay";
left = right;
}
}
}
}
return 0;
}
C - Tic Tac Toe
输入的第一行给出N,表示测试用例的数目。然后给出 4N-1行,说明N个用空行分隔的网格图。
输出
对于每个测试用例,在一行中输出"yes"或"no",表示该 网格图是否是有效的三连棋游戏的一个步骤。
思路
综上所述,可以从两个方面判断网格图是无效的:
①‘O’与‘X’的个数:O > X || X - O > 1
②当X赢时,X - O!= 1
③当O赢时,X!= O
#include
using namespace std;
#define N 5
char a[N][N];
int n,x,o;
bool flag = true;
//函数判断是否是x或o获胜
bool judge(char c){
int j;//变量j用来统计变量的个数——直接在for循环中执行
//按行判断
for(int i=0;i<3;i++){
for(j=0;j<3&&a[i][j]==c;j++);
if(j==3)return true;
}
//按列判断
for(int i=0;i<3;i++){
for(j=0;j<3&&a[j][i]==c;j++);
if(j==3)return true;
}
//对角线判断
for(j=0;j<3&&a[j][j]==c;j++);
if(j==3)return true;
for(j=0;j<3&&a[j][2-j]==c;j++);
if(j==3)return true;
//不符合条件
return false;
}
int main(){
cin >> n;
while(n--){
cin >> a[0] >> a[1] >> a[2];
x = o = 0;
//统计x,o的个数
for(int i=0;i<3;i++)
for(int j=0;j<3;j++){
if(a[i][j]=='X')
x++;
else if(a[i][j]=='O')
o++;
}
//首先判断是否满足条件①
if(x-o>1||x<o){
flag = false;
}
else if(judge('O')&&x!=o){
flag = false;
}
else if(judge('X')&&x-o!=1){
flag = false;
}
cout << (flag?"yes":"no") << endl;
}
}
D - Factorial! You Must be Kidding!!!
Input
The input file contains several lines of input. Each line contains a single integern. No integer has morethan 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 integern! if thevalue ofn! ts within the unsigned long integer of Arif’s computer. Otherwise the line will contain one of the following two words
Overflow!(Whenn!>6227020800)
Underflow!(Whenn!<10000)
Sample Input
2
10
100
Sample Output
Underflow!
3628800
Overflow!
思路
由于n可以为负数。所以需要强行拓展关于阶乘的定义。
factorial(n) = n ∗ factorial(n − 1),利用题目中给出的这个公式,factorial(0) = 0 ∗ factorial(-1)。再稍作变形factorial(-1) = factorial(0) / 0,所以-1的阶乘等于∞。继续按照这个思路扩展,factorial(-1) = -1 ∗ factorial(-2),所以-2的阶乘等于-∞。
以此类推,
在n为负数的前提下,n如果是奇数答案就是Overflow!,如果是偶数答案就是Underflow!
循环一直执行,直到不再输入
while(scanf("%d",&n)!=EOF)
题解
#include
using namespace std;
#define N 20
long long int f[N];
int n;
int main(){
f[0] = 1;
for(int i=1;i<=13;i++){
f[i] = i * f[i-1];
}
while(scanf("%d",&n)!=EOF){
if(n>13||(n<0&&(-n)%2==1)){
cout << "Overflow!" << endl;
}
else if(n<=7||(n<0&&(-n)%2==0)){
cout << "Underflow!" << endl;
}
else
cout << f[n] << endl;
}
return 0;
}