1. (2008年华中科技大学计算机研究生机试真题)八进制
题目描述:
输入一个整数,将其转换成八进制数输出。
输入:
输入包括一个整数N(0<=N<=100000)。
输出:
可能有多组测试数据,对于每组数据,
输出N的八进制表示数。
样例输入:
7
8
9
样例输出:
7
10
题目描述:
输入一个整数,将其转换成八进制数输出。
输入:
输入包括一个整数N(0<=N<=100000)。
输出:
可能有多组测试数据,对于每组数据,
输出N的八进制表示数。
样例输入:
7
8
9
样例输出:
7
10
11
法一:递归法:
#include <stdio.h>
void fun(int N)
{
if(N)
{
fun(N/8);
printf("%d",N%8);
}
}
int main()
{
int N;
while(scanf("%d",&N)!=EOF)
{
if(N==0) //注意N为0时输出其自身,同非递归法
printf("0");
else
fun(N);
printf("\n");
}
return 0;
}
法二:非递归法:
#include <stdio.h>
#define maxn 101
void fun(int N)
{
int i,j=0;
int a[maxn];
if(N==0)
printf("0");
else
{
while(N)
{
a[j++]=N%8;
N/=8;
}
for(i=j-1;i>=0;i--)
printf("%d",a[i]);
}
printf("\n");
}
int main()
{
int N;
while(scanf("%d",&N)!=EOF)
fun(N);
return 0;
}
程序截图:
2. (2008年北京大学图形实验室计算机研究生机试真题)首字母大写
题目描述:
对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。
在字符串中,单词之间通过空白符分隔,空白符包括:空格(' ')、制表符('\t')、回车符('\r')、换行符('\n')。
输入:
输入一行:待处理的字符串(长度小于100)。
输出:
可能有多组测试数据,对于每组数据,
输出一行:转换后的字符串。
样例输入:
if so, you already have a google account. you can sign in on the right.
样例输出:
If So, You Already Have A Google Account. You Can Sign In On The Right.
源代码:
#include <stdio.h>
#include <string.h>
#define maxlen 105
int main()
{
char str[maxlen];
int i;
while(gets(str)!=NULL)
{
if(str[0]>='a' && str[0]<='z')
str[0]-=32;
for(i=0;i<strlen(str);i++) //考虑第一个单词前有非字母的情况
{
if(str[i]==' ' || str[i]=='\t' || str[i]=='\r' || str[i]=='\n')
{
if(str[i+1]>='a' && str[i+1]<='z')
str[i+1]-=32;
}
}
puts(str);
}
return 0;
}
程序截图:
3. (2008年华中科技大学计算机研究生机试真题)最长&最短文本
题目描述:
输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出。
输入:
输入包括多行字符串,字符串的长度len,(1<=len<=1000)。
输出:
按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出。
样例输入:
hello
she
sorry
he
样例输出:
he
hello
sorry
源代码:
#include <stdio.h>
#include <string.h>
#define maxlen 1001
int main()
{
char str[maxlen][maxlen]; //二维字符串 同时保存字符串序号及串内容
int len[maxlen];
int i,j,max,min;
i=max=min=0;
while(scanf("%s",str[i])!=EOF) //注意退出输入时按两次Ctrl+Z
{
len[i]=strlen(str[i]);
if(len[i]>len[max])
max=i;
if(len[i]<len[min])
min=i;
i++;
}
for(j=0;j<i;j++)
{
if(len[j]==len[min])
printf("%s\n",str[j]);
}
for(j=0;j<i;j++)
{
if(len[j]==len[max])
printf("%s\n",str[j]);
}
return 0;
}
程序截图: