6、
//斐波那契数列
#include<stdio.h>
int fibo(int n){
if(n==1) return 1;
if(n==2) return 1;
return fibo(n-1)+fibo(n-2);
}
int main(){
int n;
while(scanf("%d",&n)&&n!=-1){
for(int i=1;i<=n;i++){
printf("%d ",fibo(i));
}
printf("\n");
}
return 0;
}
7、
//输入一行以空格分割的英文,判断其一共有多少个单词,不能包括冠词A a。
//如:A pen drop from tree一共有四个单词
#include<stdio.h>
int isWord(char *pStr,int length){//该函数用来排除冠词A或者a
if(length==1 && (*pStr=='a' || *pStr=='A'))//单词是A 或者 a,返回0
return 0;
return 1;
}
int wordCount(char *pStr){
int count=0;
while(*pStr!='\0'){
char wordArray[20]={0};//用来存放一个单词
int wordLength=0;//单词长度
while(*pStr!=' '&&*pStr!='\0'){//读取一个单词
wordArray[wordLength]=*pStr;
wordLength++;
pStr++;
}
int flag=isWord(wordArray,wordLength);
if(flag==1)//如果该单词不是冠词,数量加1
++count;
while(*pStr==' ' && *pStr!='\0')//过滤单词之间的空格
++pStr;
}
return count;
}
int main(){
char str[100];
gets(str);//不可以用scanf,scanf不能读回车
printf("%s\n",str);
int count=wordCount(str);
printf("%d\n",count);
return 0;
}
8、?
//两个乒乓球队,甲队有a,b,c三名球员,乙队有d,e,f三名球员。
//甲队a不愿意和d比赛,c不愿意跟d,f比赛。求合适的赛手名单
#include<stdio.h>
void getSchedule(char firstTeam[],char secondTeam[]){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(firstTeam[i]=='a'&&secondTeam[j]=='d')
continue;
if(firstTeam[i]=='c'&&secondTeam[j]=='d')
continue;
if(firstTeam[i]=='c'&&secondTeam[j]=='f')
continue;
printf("%c %c\n",firstTeam[i],secondTeam[j]);
}
}
}
int main(){
char firstTeam[3]={'a','b','c'};
char secondTeam[3]={'d','e','f'};
getSchedule(firstTeam,secondTeam);
return 0;
}
9、
//将3个字符串由小到大排序
#include<stdio.h>
#include<string.h>
void swapStr(char *pStr1,char *pStr2){
char temp[100];
strcpy(temp,pStr1);
strcpy(pStr1,pStr2);
strcpy(pStr2,temp);
}
void strSort(char *pStr1,char *pStr2,char *pStr3){
if(strcmp(pStr1,pStr2)>0)
swapStr(pStr1,pStr2);
if(strcmp(pStr1,pStr3)>0)
swapStr(pStr1,pStr3);
if(strcmp(pStr2,pStr3)>0)
swapStr(pStr2,pStr3);
}
int main(){
char str1[100];
char str2[100];
char str3[100];
gets(str1);
gets(str2);
gets(str3);
strSort(str1,str2,str3);
printf("%s %s %s\n",str1,str2,str3);
return 0;
}
10、
//一篇文本中共有3行文字,每行不多于5个字符。
//要求分别统计出每行中的大写字母,小写字母,数字,空格,以及其他字符的个数。
#include<stdio.h>
void countEachLine(char *str,int length){
int uppLetter=0,lowLetter=0,num=0,blank=0,other=0;
for(int i=0;i<length;i++){
if(str[i]>='A'&&str[i]<='Z')
uppLetter++;
else if(str[i]>='a'&&str[i]<='z')
lowLetter++;
else if(str[i]>='0'&&str[i]<='9')
num++;
else if(str[i]==' ')
blank++;
else
other++;
}
printf("%d %d %d %d %d\n",uppLetter,lowLetter,num,blank,other);
}
void countOfText(char *str){
while(*str!='\0'){
char lineArray[10]={0};
int length=0;
while(*str!='\n' && *str!='\0'){
lineArray[length]=*str;
length++;
str++;
}
countEachLine(lineArray,length);
while(*str=='\n'&&*str!='\0')
str++;
}
}
int main(){
char text[100]="abc123 \n1098jk .[\n \n";
printf("%s\n",text);
countOfText(text);
return 0;
}
|