2018年華科計算機機試題目
pro01, 單詞及字母數統計
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100+5
#define LL_MAX ((1LL<<63)-1)
#define ll long long
#define mem(a, n) memset(a, n, sizeof(a))
char str[MAXN];
char strToInt[26];
int len, cnt[26];
void solve() {
len=strlen(str);
mem(cnt, 0);
int i=0, wordNum=0, alphaNum=0;
len=strlen(str);
while(i < len) {
if(str[i]!=' ') {
while(i<len && str[i]!=' ') {
strToInt[i] = str[i]-'A';
alphaNum++;
if(str[i]>='a'&&str[i]<='z')
strToInt[i] -= 32;
int tmp=strToInt[i];
cnt[tmp] ++;
i++;
}
wordNum++;
} else {
i++;
}
}
printf("字母個數:%d\n單詞個數:%d\n", alphaNum, wordNum);
int maxNum=0;
for(i=0; i<26; i++) {
if(cnt[i] > maxNum) {
maxNum = cnt[i];
}
}
int flag=0;
printf("最多的字母:");
for(i=0; i<26; i++) {
if(cnt[i] == maxNum) {
if(flag)
printf(", ");
printf("%c", i+'a');
flag=1;
}
}
printf("\n出現的次數:%d\n", maxNum);
}
int main() {
freopen("dataIn.txt", "r", stdin);
freopen("dataOut.txt", "w", stdout);
while(fgets(str, MAXN, stdin)!=NULL) {
solve();
}
return 0;
}
pro02,進制轉換
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100+5
#define LL_MAX ((1LL<<63)-1)
#define ll long long
#define mem(a, n) memset(a, n, sizeof(a))
char str[MAXN];
int num[MAXN], cnt;
void twe2ten() {
int i=0;
cnt=0;
while(str[i]!='\0') {
int tmp=str[i++]-'0';
if(tmp>10) {
tmp=tmp+48-97+10;
}
num[cnt++] = tmp;
}
}
int convert() {
int res=0, i;
for(i=0; i<cnt; i++) {
res = res*12+num[i];
}
return res;
}
void calcu(int x) {
int res[32], i=32;
while(x) {
res[--i] = x%2;
x /= 2;
}
while(i>0) {
res[--i]=0;
}
for(i=0; i<32; i++) {
if(i&&i%8==0) printf(" ");
printf("%d", res[i]);
}
printf("\n");
}
int main() {
while(~scanf("%s", str)) {
twe2ten();
int i;
for(i=0; i<cnt; i++) {
if(i!=cnt-1) printf("%d ", num[i]);
else printf("%d\n", num[i]);
}
int res=convert();
printf("%d\n", res);
calcu(res);
}
return 0;
}
pro03,分數轉小數
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100+5
#define LL_MAX ((1LL<<63)-1)
#define ll long long
#define mem(a, n) memset(a, n, sizeof(a))
int N[3], D[3];
int res[MAXN], mod[MAXN];
void solve(int n, int d) {
int cnt=1, pos=0;
res[0] = n/d, mod[0] = n%d;
int i;
while(cnt<MAXN && mod[cnt-1]!=0) {
res[cnt] = (mod[cnt-1]*10)/d;
mod[cnt] = (mod[cnt-1]*10)%d;
if(mod[cnt]==0) break;
for(i=1; i<cnt; i++) {
if(mod[i] == mod[cnt]) {
pos=i;
break;
}
}
if(pos!=0) break;
cnt++;
}
printf("%d/%d=", n, d);
if(res[0]!=0) printf("%d", res[0]);
printf(".");
if(pos==0) {
for(i=1; i<=cnt; i++) {
printf("%d", res[i]);
}
} else {
for(i=1; i<pos; i++) {
printf("%d", res[i]);
}
printf("(");
for(i=pos; i<cnt; i++) {
printf("%d", res[i]);
}
printf(")");
}
puts("");
}
int main() {
int i;
for(i=0; i<3; i++) {
scanf("%d/%d", &N[i], &D[i]);
}
for(i=0; i<3; i++) {
solve(N[i], D[i]);
}
return 0;
}