Posted on 2011-09-24 22:16
Uriel 閱讀(230)
評論(0) 編輯 收藏 引用 所屬分類:
考研&保研復試上機題
唉。。本來是想找一道菜題秒殺的。。結果糾結了半天。。
1. 統計單詞
忘記考慮兩個單詞間有多個空格的情況,WA*1。。
//2002年華中科技大學計算機研究生機試題 統計單詞
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int l[1000];
char s[10000];
int main() {
int i, cnt, ll;
while(gets(s) != NULL) {
cnt = ll = 0;
for(i = 0; s[i]; ++i) {
if(s[i] == ' ' || s[i] =='.') {
if(!ll) continue;
l[cnt] = ll;
cnt ++;
ll = 0;
}
else {
ll++;
}
}
for(i = 0; i < cnt - 1; ++i) printf("%d ", l[i]);
printf("%d\n", l[cnt - 1]);
}
return 0;
}
2. 守形數
大水不解釋
//2002年華中科技大學計算機研究生機試題 守形數
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int n, t;
int main() {
while(~scanf("%d", &n)) {
t = n * n;
while(n > 0) {
if((n % 10) != (t % 10)) break;
n /= 10;
t /= 10;
}
if(!n) puts("Yes!");
else
puts("No!");
}
return 0;
}
3. 二叉樹遍歷
這題糾結死我了。。RE不下10次。。
理解錯題意了輸入中的'#'貌似都是空格,貌似還有不合法數據。。坑爹啊。。
//2002年華中科技大學計算機研究生機試題 二叉樹遍歷
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
int l, r;
char c;
}p[40000];
int stp;
char s[40000];
void build(int pt, int lr) {
if(stp >= strlen(s)) return;
else if(s[stp] == ' ' || s[stp] == '#') {
stp++;
return;
}
else {
int rt = stp;
p[stp].c = s[stp];
p[stp].l = p[stp].r = -1;
if(~pt) {
if(!lr) p[pt].l = stp;
else
p[pt].r = stp;
}
++stp;
build(rt, 0);
build(rt, 1);
}
}
void inorder(int idx) {
if(idx == -1) return;
else {
inorder(p[idx].l);
printf("%c ", p[idx].c);
inorder(p[idx].r);
}
}
int main() {
int i;
while(gets(s) != NULL) {
if(!strlen(s) || s[0] == ' ' || s[0] == '#') {
puts("");
continue;
}
stp = 0;
build(-1, -1);
inorder(0);
puts("");
}
return 0;
}