博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C语言经典例题100(68~82)
阅读量:6981 次
发布时间:2019-06-27

本文共 7096 字,大约阅读时间需要 23 分钟。

六十八、有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。

#include
void move_array(int a[],int n,int m){ int i; int b[100]; if(m>n) { printf("input error(%d must less than %d).\n",m,n); } else { for(i=0;i<(n-m);i++) b[i]=a[i]; for(i=0;i

 

六十九、有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出 圈子,问最后留下的是原来第几号的那位。

程序分析:用一个数组存放初始n个人,每个数组元素值设为(i+1),从1~3报数,若报到3,则将对应数组元素值设为0,同时设置报到3的计数器,设0同时计数器加1.当计数器数值为(n-1)时候,报数结束,找出数组元素值不为0的那一项。

#include
#define mynumber 17int pick_num(int a[],int n){ int i=0; //数组下标 int j=1; //报数游标 int k=0; //清出计数器 while((n-k)!=1) { if(a[i]==0) //当元素值为0时不报数 { i++; } else //当元素值不为0时才可以报数 { if(j%3==0) { k++; //当报数后为3才清出 a[i]=0; //报数为0将数组元素值设为0 } i++; //指向下一个数组元素 j++; //报下一个数 } if(i==n) i=0; //当到末尾时候,重新指向数组首元素 } i=0; while(a[i]==0) i++; //找出非0元素 printf("最终数是第%d个数字。\n",i+1); return i+1;}int main(){ int a[mynumber]; int i; for(i=0;i

 

七十、写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。

#include
int str_len(char *p){ int i=0; while(*(p+i)!='\0') i++; return i;}int main(){ char ps[1000]; //分配一段安全的内存空间 printf("input a string:\n"); gets(ps); printf("your string:%s, length is %d",ps,str_len(ps)); getch(); return 0;}

 

七十二、创建一个链表。

#include
#include
struct node{ int n; struct node *pnext;};struct node * creat_list(int size){ struct node *phead,*ptemp,*p; int i; phead=(struct node *)malloc(sizeof(struct node)); phead->n=0; p=phead; for(i=1;i
n=i; p->pnext=ptemp; p=ptemp; } p->pnext=NULL; printf("List created succeed!\n"); return phead;}int main(){ struct node *px,*ptemp; int size; printf("请输入要创建的链表长度:\n"); scanf("%d",&size); px=creat_list(size); ptemp=px; while(ptemp!=NULL) { printf("%d--->",ptemp->n); ptemp=ptemp->pnext; } getch(); return 0;}

 

七十三、反向输出一个链表。

#include
#include
struct node{ int n; struct node *pnext;};struct node * creat_list(int size){ struct node *phead,*ptemp,*p; int i; phead=(struct node *)malloc(sizeof(struct node)); phead->n=0; p=phead; for(i=1;i
n=i; p->pnext=ptemp; p=ptemp; } p->pnext=NULL; printf("List created succeed!\n"); return phead;}//递归算法,反转链表输出void invert_print_list(const struct node *p){ struct node *ptemp; ptemp=p; if(ptemp->pnext==NULL) printf("%d--->",ptemp->n); else { invert_print_list(p->pnext); printf("%d--->",ptemp->n); }} int main(){ struct node *px,*ptemp; int size; void invert_print_list(const struct node *p); printf("请输入要创建的链表长度:\n"); scanf("%d",&size); px=creat_list(size); ptemp=px; while(ptemp!=NULL) { printf("%d--->",ptemp->n); ptemp=ptemp->pnext; } printf("\n"); invert_print_list(px); getch(); return 0;}

 

七十四、连接两个链表。

#include
#include
struct node{ int n; struct node *pnext;};struct node * creat_list(int size){ struct node *phead,*ptemp,*p; int i; phead=(struct node *)malloc(sizeof(struct node)); phead->n=0; p=phead; for(i=1;i
n=i; p->pnext=ptemp; p=ptemp; } p->pnext=NULL; printf("List created succeed!\n"); return phead;}//递归算法,反转链表输出void invert_print_list(const struct node *p){ struct node *ptemp; ptemp=p; if(ptemp->pnext==NULL) printf("%d-->",ptemp->n); else { invert_print_list(p->pnext); printf("%d-->",ptemp->n); }} //连接两个链表void link_list(struct node *ha,struct node *hb){ struct node *pt; pt=ha; while(pt->pnext!=NULL) pt=pt->pnext; pt->pnext=hb;}void print_list(const struct node *p){ struct node *pt; pt=p; while(pt!=NULL) { printf("%d-->",pt->n); pt=pt->pnext; } printf("\n");}int main(){ struct node *pa,*pb,*pt; int sizea,sizeb,i; void invert_print_list(const struct node *p); void print_list(const struct node *p); void link_list(struct node *ha,struct node *hb); printf("请输入要创建的链表A长度:\n"); scanf("%d",&sizea); printf("请输入要创建的链表B长度:\n"); scanf("%d",&sizeb); pa=creat_list(sizea); pb=creat_list(sizeb); printf("链表A为:\n"); print_list(pa); printf("链表B为:\n"); print_list(pb); printf("重置链表B的数值:\n"); pt=pb; for(i=0;i
n+=10; pt=pt->pnext; } printf("新链表B为:\n"); print_list(pb); link_list(pa,pb); printf("组合后的新链表为:\n"); print_list(pa); printf("新链表反转为:\n"); invert_print_list(pa); getch(); return 0;}

 

七十六、编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数)

#include
double sumx(int n);double sumx(int n){ int i,num; double sum; if(n%2==0) { num=n/2; sum=0.0; for(i=1;i<=num;i++) sum+=1.0/(2*i); printf("你输入的是一个偶数,结果为:%8f\n",sum); return sum; } else { num=(n+1)/2; sum=0.0; for(i=1;i<=num;i++) sum+=1.0/(2*i-1); printf("你输入的是一个奇数,结果为:%8f.\n",sum); return sum; }}int main(){ int n; printf("请输入一个整数:\n"); scanf("%d",&n); sumx(n); getch(); return 0;}

 

七十七、填空练习(指向指针的指针)

//这里s是一个字符型指针数组,s[i]均为字符型指针数组元素,元素值指向各字符串首地址;因此与q匹配的自然就是&s[i]; //另外,注意到s是一个数组,空间在程序运行时候分配5个大小为char*型并且连续的空间(即5×4Byte),s作为数组名,表示的为数组首元素 //地址,即&s[0],那么第i个元素的地址即为s+i;所以这里答案也可以为s+k main(){ char *s[]={
"man","woman","girl","boy","sister"};char **q;int k;for(k=0;k<5;k++){       ;/*这里填写什么语句*/  printf("%s\n",*q);}}

七十八、找到年龄最大的人,并输出。请找出程序中有什么问题。

?有啥问题,没看出来啥问题

#define N 4#include "stdio.h"static struct man{ char name[20];int age;} person[N]={
"li",18,"wang",19,"zhang",20,"sun",22};main(){
struct man *q,*p;int i,m=0;p=person;for (i=0;i
age)  q=p++;  m=q->age;}printf("%s,%d",(*q).name,(*q).age);}

八十、海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

程序分析:设有一堆桃子数量为x1=x,则(x1-1)能被5整除;第二次数量为x2=(x-1)*4/5,并且x2-1能被5整除,如此进行下去,至少可以进行5次。

因此设计一函数,参数为int型。该参数能按上述规则,进行至少5次以上,则可为最初的数量。

#include
int count(long n){ int i=0,m=n; //计数器 while((m-1)%5==0) { i++; m=(m-1)*4/5; } return i;}int main(){ long i=101; for(i=2;;i++) { if(count(i)>=5) break; } printf("桃子最少有%d个。\n",i); getch(); return 0;}

 八十二、八进制转换为十进制。

#include<stdio.h>

int main()

{
int x,y=0,k; //x为模拟八进制数,y为转换后的十进制数
int m; //中转数
int i=1;
printf("输入一个10位以下的八进制数(0,7):\n");
scanf("%d",&x);
k=x;
do
{
m=(k%10)*i;
y+=m;
i=i*8;
k=(k-k%10)/10;
}while(k!=0);
printf("八进制数%ld转换为十进制数为:%ld.\n",x,y);
getch();
return 0;
}

 

 

转载于:https://www.cnblogs.com/tsembrace/p/3252554.html

你可能感兴趣的文章
Mac Mysql Access denied for user 'root'@'localhost
查看>>
Python学习三级菜单
查看>>
访问控制
查看>>
JPA中EntityListeners注解的使用
查看>>
苹果手机如何恢复通讯录联系人呢?怎样恢复
查看>>
SQL基础教程
查看>>
如何编辑修改PDF,PDF怎么删除页眉页脚
查看>>
科学热点:国内首家无人书店亮相,315曝光机器人打骚扰电话
查看>>
600万死难者记忆由AI守护!以色列博物馆用AI索引数字资产
查看>>
iOS开发之网络编程--小文件下载
查看>>
CSS强制文本在一行内显示若有多余字符则使用省略号表示
查看>>
蛋花花谈Web前端工程师掌握这些技能更加顺利工作
查看>>
AJPFX的反射学习笔记
查看>>
Android学习--08-ListView
查看>>
polymer中的notify和reflectToAttribute
查看>>
Java排序二叉树
查看>>
从其他机构转投乾颐堂的同学pass感言
查看>>
卷积神经网络CNN原理以及TensorFlow实现
查看>>
教程:怎样处理资源管理器崩溃退出的问题
查看>>
JavaScript和jquery分别实现简单的跑马灯效果
查看>>