Posted on 2011-09-25 19:33
Uriel 閱讀(265)
評論(0) 編輯 收藏 引用 所屬分類:
考研&保研復試上機題
貌似。。哈工大的機試題都極其的水。。。
1. 數組逆置
本來想寫個遞歸的。。但是覺得實在是畫蛇添足。。
//2011年哈爾濱工業大學計算機研究生機試題 數組逆置
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char s[1000];


int main()
{
int i;

while(gets(s) != NULL)
{
for(i = strlen(s) - 1; i >= 0; --i) putchar(s[i]);
puts("");
}
return 0;
}2. 最大公約數
大水不解釋
//2011年哈爾濱工業大學計算機研究生機試題 最大公約數
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;


int gcd(int a, int b)
{
if(!b) return a;
return gcd(b, a % b);
}


int main()
{
int a, b;

while(~scanf("%d %d", &a, &b))
{
if(a < b) swap(a, b);
printf("%d\n", gcd(a, b));
}
return 0;
}3. 眾數
memset不小心開到while外面去了。。WA*1
//2011年哈爾濱工業大學計算機研究生機試題 眾數
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int fg[20];


int main()
{
int mx, mk, a, i;

while(~scanf("%d", &a))
{
memset(fg, 0, sizeof(fg));
fg[a]++;

for(i = 1; i < 20; ++i)
{
scanf("%d", &a);
fg[a]++;
}
mx = 0;

for(i = 1; i <= 10; ++i)
{

if(fg[i] > mx)
{
mx = fg[i];
mk = i;
}
}
printf("%d\n", mk);
}
return 0;
}