题目描述:
名名的妈妈从外地出差回来,带了一盒好吃又精美的巧克力给名名(盒内共有 N 块巧克力,20 > N >0)。
妈妈告诉名名每天可以吃一块或者两块巧克力。
假设名名每天都吃巧克力,问名名共有多少种不同的吃完巧克力的方案。
例如:
如果N=1,则名名第1天就吃掉它,共有1种方案;
如果N=2,则名名可以第1天吃1块,第2天吃1块,也可以第1天吃2块,共有2种方案;
如果N=3,则名名第1天可以吃1块,剩2块,也可以第1天吃2块剩1块,所以名名共有2+1=3种方案;
如果N=4,则名名可以第1天吃1块,剩3块,也可以第1天吃2块,剩2块,共有3+2=5种方案。
现在给定N,请你写程序求出名名吃巧克力的方案数目。
输入:
输入只有1行,即整数N。
输出:
可能有多组测试数据,对于每组数据,
输出只有1行,即名名吃巧克力的方案数。
样例输入:
4
样例输出:
5
源代码:
#include <stdio.h>
int main()
{
int i;
int N,count[20]; //count数组记录吃糖方案数
count[0]=1,count[1]=2;
for(i=2;i<20;i++)
count[i]=count[i-1]+count[i-2];
while(scanf("%d",&N)!=EOF)
printf("%d\n",count[N-1]);
return 0;
}
程序截图:
2. (2002年华中科技大学计算机研究生机试真题)守形数
题目描述:
守形数是这样一种整数,它的平方的低位部分等于它本身。
比如25的平方是625,低位部分是25,因此25是一个守形数。
编一个程序,判断N是否为守形数。
输入:
输入包括1个整数N,2<=N<100。
输出:
可能有多组测试数据,对于每组数据,
输出"Yes!”表示N是守形数。
输出"No!”表示N不是守形数。
样例输入:
25
4
样例输出:
Yes!
No!
源代码:
#include <stdio.h>
int main()
{
int i,N;
int t,sum,wei; //sum-累加和 wei-数位对应权值
int flag; //判断是否是守形数
while(scanf("%d",&N)!=EOF)
{
sum=0;
wei=1;
t=N*N;
flag=0;
while(t)
{
sum+=((t%10)*wei); //从低位到高位累加求和 (如625:5->25->625)
wei*=10;
t/=10;
if(sum==N)
{
printf("Yes!\n");
flag=1;
break;
}
}
if(flag==0)
printf("No!\n");
}
return 0;
}
程序截图:
3. (2008年北京大学软件所计算机研究生机试真题)字符串排序
题目描述:
先输入你要输入的字符串的个数。然后换行输入该组字符串。每个字符串以回车结束,每个字符串少于一百个字符。
如果在输入过程中输入的一个字符串为“stop”,也结束输入。
然后将这输入的该组字符串按每个字符串的长度,由小到大排序,按排序结果输出字符串。
输入:
字符串的个数,以及该组字符串。每个字符串以‘\n’结束。如果输入字符串为“stop”,也结束输入.
输出:
可能有多组测试数据,对于每组数据,
将输入的所有字符串按长度由小到大排序输出(如果有“stop”,不输出“stop”)。
样例输入:
5
sky is grey
cold
very cold
stop
3
it is good enough to be proud of
good
it is quite good
样例输出:
cold
very cold
sky is grey
good
it is quite good
it is good enough to be proud of
【分析】注意对输入字符串的保存;输入中的回车可能会对结果产生干扰,故每次输入n结束后输入字符串前使用getchar(); “清空缓冲区”
源代码:
法一:二维字符数组
#include <stdio.h>
#include <string.h>
#define maxn 1000
#define maxlen 101
void Sort(char str[][maxlen],int n) //按串长对字符串排序
{
char t[maxlen];
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(strlen(str[j]) > strlen(str[j+1]))
{
strcpy(t,str[j]);
strcpy(str[j],str[j+1]);
strcpy(str[j+1],t);
}
}
}
}
int main()
{
int i,n,num;
char str[maxn][maxlen]; //二维字符数组保存输入的字符串
char temp[maxlen]; //记录输入的字符串
while(scanf("%d",&n)!=EOF)
{
getchar(); //getchar()接收回车符,避免对后续输入输出产生干扰
for(i=0;i<n;i++)
{
gets(temp);
if(strcmp(temp,"stop")==0)
break;
strcpy(str[i],temp);
}
num=i;
Sort(str,num);
for(i=0;i<num;i++)
printf("%s\n",str[i]);
}
return 0;
}
法二:法一基础上结合STL完成排序功能,降低算法复杂度
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define maxn 1000
#define maxlen 101
int cmp(const void *a,const void *b)
{
return strlen((char *)a)-strlen((char *)b);
}
int main()
{
int i,n,num;
char str[maxn][maxlen]; //二维字符数组 保存输入的字符串
char temp[maxlen]; //记录输入的字符串
while(scanf("%d",&n)!=EOF)
{
getchar(); //getchar()接收回车符,避免对后续输入输出产生干扰
for(i=0;i<n;i++)
{
gets(temp);
if(strcmp(temp,"stop")==0)
break;
strcpy(str[i],temp);
}
num=i;
qsort(str,num,sizeof(str[0]),cmp);
for(i=0;i<num;i++)
printf("%s\n",str[i]);
}
return 0;
}
法三:C语言环境下,自定义结构体,构造“字符串类型”
源代码:
#include <stdio.h>
#include <string.h>
#define maxn 1000
#define maxlen 101
typedef struct //自定义字符串类型
{
char str[maxlen];
int len;
} String;
void Sort(String string[],int n)
{
String t;
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(string[j].len>string[j+1].len)
{
t=string[j];
string[j]=string[j+1];
string[j+1]=t;
}
}
}
}
int main()
{
int i,n,num;
String string[maxn];
while(scanf("%d",&n)!=EOF)
{
i=0;
getchar();
while(i<n)
{
gets(string[i].str);
string[i].len=strlen(string[i].str);
if(strcmp(string[i].str,"stop")==0)
break;
i++;
}
num=i;
Sort(string,num);
for(i=0;i<num;i++)
printf("%s\n",string[i].str);
}
return 0;
}
程序截图:
※4. (2005年浙江大学计算机及软件工程研究生机试真题)最大连续子序列
题目描述:
给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个,例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和为20。现在增加一个要求,即还需要输出该子序列的第一个和最后一个元素。
输入:
测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( K< 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。
输出:
对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。
样例输入:
6
-2 11 -4 13 -5 -2
10
-10 1 2 3 4 -5 -23 3 7 -21
6
5 -8 3 2 5 0
1
10
3
-1 -5 -2
3
-1 0 -2
0
样例输出:
20 11 13
10 1 4
10 3 5
10 10 10
0 -1 -2
0 0 0
【分析】注意找连续的一组正数,且满足和最大
源代码:
#include <stdio.h>
#define maxn 10000
int main()
{
int a[maxn],K;
int i,j,tmp,maxsum; //j-终点"伪指针" tmp-数组前i项和 maxsum-最大连续子序列和
int start,end; //最大连续子序列起点与终点下标
while(scanf("%d",&K)!=EOF)
{
if(K==0)
break;
j=0,tmp=0,maxsum=-1;
start=0,end=K-1;
for(i=0;i<K;i++)
scanf("%d",&a[i]);
for(i=0;i<K;i++)
{
tmp+=a[i];
if(tmp>maxsum) //当前数组前i+1项和>最大连续子序列和
{
start=j;
end=i;
maxsum=tmp;
}
else if(tmp<0) //tmp为负时j指向当前元素后继并将tmp归零
{
j=i+1;
tmp=0;
}
}
if(maxsum<0) //输入的元素均为负,最大连续子序列和置为0
maxsum=0;
printf("%d %d %d\n",maxsum,a[start],a[end]);
}
return 0;
}
程序截图: