A-Specialized Four-Digit Numbers
题意:
找到并列出所有具有这样特性的十进制的四位数:其4位数字的和等于这个数字以十六进制表示时的4位数字的和,也等于这个数字以十二进制表示时的4位数字的和。
思路:按题意模拟,求不同进制下位数和即可。
/********************
*author:
*topic:
*source:
灵动ICPC冬令营基础-1
POJ 2196
A - Specialized Four-Digit Numbers
*******************/
#include <iostream>
#define ll long long
using namespace std;
const int N = 1e5+7;
int n;
ll calc(int base,int n){
ll res = 0;
while(n){
res += n % base;
n /= base;
}
return res;
}
int main(){
for(int i = 2992; i <= 9999; ++i){
if(calc(10,i) == calc(12,i) && calc(12,i) == calc(16,i)){
cout<<i<<"\n";
}
}
return 0;
}
B - Pig-Latin
题意:
字符串转化,如果是辅音开头把单词的开头放到最后,然后后加上ay后缀即可。
思路:
按照题意模拟。
/********************
*author:
*topic:
*source:
灵动ICPC冬令营基础-1
B - Pig-Latin
UVA 492
*******************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <string>
using namespace std;
const int N = 1e5+7;
string str;
int s = 0,t = 0;
int solve(char c){
c = tolower(c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' )
return 1;
return 0;
}
int main(){
getline(cin,str);
while (str[s])
if (!isalpha(str[s]) ) {
printf("%c",str[s++]);
t = s;
}else if (isalpha(str[t]))
t++;
else {
if (!solve(str[s])){
for ( int i = s+1 ; i < t ; ++ i )
printf("%c",str[i]);
printf("%c",str[s]);
}else
for (int i = s ; i < t ; ++ i )
printf("%c",str[i]);
printf("ay");
s = t;
}
printf("\n");
return 0;
}
C - Tic Tac Toe
题意:
判断当前的井字棋局面是否合法。
思路:
考虑不同的的非法情况,进行判断
/********************
*author:
*topic:
*source:
灵动ICPC冬令营基础-1
C - Tic Tac Toe
POJ 2361
*******************/
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 7;
int n,cas;
char chess[N][N];
int iswin(char x){
int flag = 0;
for(int i = 1; i <= 3; ++i){
if(chess[i][1] == x && chess[i][2] == x && chess[i][3] == x)
flag = 1;
if(chess[1][i] == x && chess[2][i] == x && chess[3][i] == x)
flag = 1;
}
if(chess[1][1] == x && chess[2][2] == x && chess[3][3] == x)
flag = 1;
if(chess[1][3] == x && chess[2][2] == x && chess[3][1] == x)
flag = 1;
return flag;
}
int main(){
scanf("%d",&cas);
while(cas--){
int cnt_O =0,cnt_X = 0;
getchar();
for(int i = 1; i <= 3; ++i){
scanf("%s",chess[i]+1);
for(int j = 1; j <= 3; ++j){
if(chess[i][j] == 'O') cnt_O++;
if(chess[i][j] == 'X') cnt_X++;
}
}
if(cnt_X < cnt_O || cnt_X > cnt_O + 1){
puts("no");
continue;
}
if(cnt_X == cnt_O && iswin('X')){
puts("no");
continue;
}
if(cnt_X != cnt_O && iswin('O')){
puts("no");
continue;
}
if(iswin('X') && iswin('O')){
puts("no");
continue;
}
puts("yes");
}
return 0;
}
D - Factorial! You Must be Kidding!!!
题意:
给定数字n,计算n!并输出。有两个特例,如果n!超过6227020800则输出Overflow!
,如果n!小于10000则输出Underflow!
。
思路:
13的阶乘是 6227020800,刚大于10000和的是8阶乘,这之间可以考虑之间输出。
根据factorial(0) = 0 ∗ factorial(-1)
,所以-1的阶乘等于
∞
\infty
∞。可以知道factorial(-1) = -1 ∗ factorial(-2)
,所以-2的阶乘等于
−
∞
-\infty
−∞。
以此类推,在n为负数的时候,
n
n
n 如果是奇数答案就是Overflow!
,如果是偶数答案就是Underflow!
/********************
*author:
*topic:
*source:灵动ICPC冬令营基础-1
D - Factorial! You Must be Kidding!!!
UVA 10323
*******************/
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 1e5+7;
ll fact[27];
int main(){
// freopen("data.in", "r", stdin);
// freopen("data.out", "w", stdout);
fact[0] = 1;
for(int i=1;i<=13;i++)
fact[i] = i * fact[i-1];
int n;
while(scanf("%d",&n) == 1){
if(n > 13 || (n < 0 && (-1*n%2)== 1))
puts("Overflow!");
else if(n <= 7 || (n < 0 && (-1*n%2) == 0))
puts("Underflow!");
else
printf("%lld\n",fact[n]);
}
return 0;
}
E - Function Run Fun
题意:
求如下的函数值
思路:
朴素递归会超时,使用记忆化搜索,加速递归。
/********************
*author:
*topic:
*source:灵动ICPC冬令营基础-1
E - Function Run Fun
POJ 1579
*******************/
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 27;
int n;
ll f[N][N][N];
ll fun(int a,int b,int c){
if(a <= 0 || b <= 0 || c <= 0){
return 1;
}
if(a > 20 || b > 20 || c > 20){
return fun(20, 20, 20);
}
if(f[a][b][c] != 0){
return f[a][b][c];
}
if(a < b && b < c){
f[a][b][c] = fun(a, b, c-1) + fun(a, b-1, c-1) - fun(a, b-1, c);
return f[a][b][c];
}
f[a][b][c] = fun(a-1, b, c) + fun(a-1, b-1, c) + fun(a-1, b, c-1) - fun(a-1, b-1, c-1);
return f[a][b][c];
}
int main(){
int a,b,c;
while(scanf("%d %d %d",&a,&b,&c)){
if(a == -1 && b == -1 && c == -1) break;
printf("w(%d, %d, %d) = %lld\n",a,b,c,fun(a,b,c));
}
return 0;
}
F - Simple Addition
题意:
给出
[
L
,
R
]
[L,R]
[L,R],求
∑
i
=
L
R
F
(
i
)
\displaystyle \sum_{i = L}^{R}F(i)
i=L∑RF(i),其中
思路:
找规律
/********************
*author:
*topic:
*source:灵动ICPC冬令营基础-1
F - Simple Addition
UVA 10944
*******************/
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 1e5+7;
int n;
ll solve(ll n) {
if (n <= 0) return 0;
ll res = 0;
ll r = 0;
while(n){
res += n / 10 * 45;
r = n % 10;
res += 1ll*r*(1+r) / 2;
n /= 10;
}
return res;
}
int main() {
int p,q;
while (scanf("%d%d", &p, &q) && p != -1 && q != -1) {
if(q == -1 && p == -1) break;
printf("%lld\n", solve(q) - solve(p - 1));
}
return 0;
}
// UVA还未返回结果
G - A Contesting Decision
题意:
ACM赛制,一共四题,
n
n
n 个队伍。给出各个队伍每题的提交次数和正确解答的时间,问哪支队伍是冠军。
思路:
结构体排序直接解决
/********************
*author:
*topic:
*source:灵动ICPC冬令营基础-1
G - A Contesting Decision
*******************/
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 1007;
int n;
struct Team{
string name;
int p[5];
int s[5];
int sub_cnt;
ll all_time;
}t[N];
bool cmp(const Team &a,const Team &b){
if(a.sub_cnt == b.sub_cnt){
return a.all_time < b.all_time;
}
return a.sub_cnt > b.sub_cnt;
}
int main(){
cin>>n;
for(int i = 1; i <= n; ++i){
cin>>t[i].name;
int cnt = 0;
ll sum = 0;
for(int j = 1; j <= 4; ++j){
cin>>t[i].s[j]>>t[i].p[j];
if(t[i].p[j] > 0){
cnt++;
sum += 20 * (t[i].s[j]-1) + t[i].p[j];
}
}
t[i].all_time = sum;
t[i].sub_cnt = cnt;
}
sort(t+1,t+n+1,cmp);
cout<<t[1].name<<" "<<t[1].sub_cnt<<" "<<t[1].all_time<<"\n";
return 0;
}