Posted on 2011-09-17 16:41
Uriel 阅读(171)
评论(0) 编辑 收藏 引用 所属分类:
考研&保研复试上机题
各种水题
1. 斐波那契数列
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


int main()
{
int cse, a, b, c, n, i;
scanf("%d", &cse);

while(cse--)
{
scanf("%d", &n);
if(n == 1 || n == 2) puts("1");

else
{
a = 1; b = 1;

for(i = 3; i <= n; ++i)
{
c = a + b;
b = a;
a = c;
}
printf("%d\n", c);
}
}
return 0;
}2. 统计字符数
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int cnt[30];
char s[2000];


int main()
{
int cse, i, fg, mx;
scanf("%d", &cse);
getchar();

while(cse--)
{
gets(s);
memset(cnt, 0, sizeof(cnt));

for(i = 0; s[i]; ++i)
{
cnt[s[i] - 'a']++;
}
mx =0;

for(i = 0; i < 26; ++i)
{

if(cnt[i] > mx)
{
mx = cnt[i];
fg = i;
}
}
printf("%c %d\n", fg + 'a', mx);
}
return 0;
}3. 猴子选大王
NC地理解错题意... 纠结了20min还...
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int n, m, fg[400];


int main()
{
int i, cnt, stp;

while(scanf("%d %d", &n, &m), n | m)
{
memset(fg, 0, sizeof(fg));
cnt = i = 0;

while(cnt < n - 1)
{
stp = 0;

while(stp < m)
{
i++;
if(i >= n) i %= n;
if(fg[i]) continue;
stp++;

if(stp == m)
{
fg[i] = 1;
cnt++;
break;
}
}
}

for(i = 0; i < n; ++i)
{
if(!fg[i]) break;
}
if(i == 0) i += n;
printf("%d\n", i);
}
return 0;
}