POJ1008 1013 1207 2105 2499(全部水题)

时间:2022-02-18 23:25:08

做了一天水题,挑几个还算凑合的发上来。

POJ1008 Maya Calendar

分析:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath> using namespace std; char Haab[][] = {"pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin", "mol",
"chen", "yax", "zac", "ceh", "mac", "kankin", "muan", "pax", "koyab", "cumhu", "uayet"}; char Tzo[][] = {"imix", "ik", "akbal", "kan", "chicchan", "cimi", "manik", "lamat",
"muluk", "ok", "chuen", "eb", "ben", "ix", "mem", "cib", "caban", "eznab", "canac", "ahau"}; int main() {
int year, day, cnt, month;
char szmonth[];
int T; scanf("%d", &T); printf("%d\n", T);
for(int k=; k<T; k++) {
scanf("%d.%s%d", &day, szmonth, &year); for(int i=; i<; i++) if(strcmp(Haab[i], szmonth) == ) month = i; cnt = year*+month*+day+; int ty=, td=, tm=; ty=(cnt-+)/-;
tm=(cnt-+)%;
td=(cnt-+)%+; printf("%d %s %d\n", td, Tzo[tm], ty);
} return ;
}

POJ1013 Counterfeit Dollar

分析:

超级暴力:分别对A~L这12个字母枚举轻重两种情况。

#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; char _left[][], _right[][], cond[][]; int cmp(char s1[], char s2[], int k, int j) {
/*
* 用来比较天平的平衡
* 右边up 返回 1
* 平衡 返回 0
* 右边down 返回 -1
*/
int len = strlen(s1);
int lef, rig;
lef = rig = ; for(int i=; i<len; i++) {
if(s1[i] == 'A'+k) lef += j;
if(s2[i] == 'A'+k) rig += j;
} if(lef < rig) return ;
else if(lef == rig) return ;
else return -;
} int main(){
int T, i, c, j;
scanf("%d", &T); while(T--) {
for(i=; i<; i++) {
scanf("%s %s %s", _left[i], _right[i], cond[i]);
} for(i=; i<; i++) { //分别枚举12个字母
for(j=-; j<=; j++) { //轻重两种情况
if(j==) continue; for(c=; c<; c++) { //三组
int res = cmp(_left[c], _right[c], i, j);
if( ! ((res == && cond[c][] == 'u') ||
(res == && cond[c][] == 'e') ||
(res == - && cond[c][] == 'd')))
break;
}
if(c >= ) break;
}
if(c >= ) break;
} printf("%c is the counterfeit coin and it is ", i+'A');
if(j == -) printf("heavy.\n");
else printf("light.\n");
} return ;
}

POJ1207 The 3n + 1 problem

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath> using namespace std; const int maxn = +; int hash[maxn]; int get(int n) {
if(!hash[n]) {
int t = n;
int cnt = ;
while(true) {
cnt++;
if(n == ) return (hash[t] = cnt);
if(n % == ) n = *n+;
else n = n/;
}
}
else return hash[n];
} int main() {
int a, b, n, m; memset(hash, , sizeof(hash)); while(scanf("%d %d", &n, &m) == ) {
a = n, b = m;
if(a > b) swap(a, b);
int max_v = -;
for(int i=a; i<=b; i++) {
if(max_v < get(i)) {
max_v = get(i);
}
} printf("%d %d %d\n", n, m, max_v);
} return ;
}

POJ2105 IP Address

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath> using namespace std; const int maxn = ; char s[maxn]; int main() {
int n;
scanf("%d", &n); while(n--) {
scanf("%s", s); for(int i=; i<; i++) {
int ans = ;
for(int j=; j<; j++) {
ans += (s[i*+j]-'')*(<<(-j));
}
if(i == ) printf("%d", ans);
else printf(".%d", ans);
}
putchar('\n');
} return ;
}

POJ2499 Binary Tree

分析:

用减的话会超时的。用除省时间。

#include <iostream>
#include <cstdio> using namespace std; int main(){
int n, a, b;
int cnt1, cnt2;
scanf("%d", &n);
for(int kase=; kase<=n; kase++) {
cnt1 = cnt2 = ;
scanf("%d%d", &a, &b); while(a != b) {
if(a > b) {
cnt1 += a/b;
a %= b;
if(a == ) {
cnt1--; break;
}
}
else {
cnt2 += b/a;
b %= a;
if(b == ) {
cnt2--; break;
}
}
} printf("Scenario #%d:\n", kase);
printf("%d %d\n\n", cnt1, cnt2);
}
return ;
}