日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言题目练习100例——题目+题目分析+源代码(91—100)

發(fā)布時(shí)間:2023/12/31 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言题目练习100例——题目+题目分析+源代码(91—100) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【題目91】

題目:在屏幕中輸入一個(gè)數(shù)字,編程求出其平方根。
1.題目分析:
2.題目源代碼如下:

#include "stdio.h" #include "math.h"int main() {double a,x0,x1;printf("Please input a number: \n");scanf("%lf",&a);if(a<0){printf("Error! \n");}else{x0=a/2;x1=(x0+a/x0)/2;do{x0=x1;x1=(x0+a/x0)/2;}while(fabs(x0-x1)>=1e-6);}printf("Result: \n");printf("sqrt(%g)=%g \n",a,x1);getchar();return 0; }

【題目92】

題目:用計(jì)算機(jī)隨機(jī)生成1~100的一個(gè)數(shù)字,然后由用戶來猜這個(gè)數(shù),根據(jù)用戶猜測的次數(shù)分別給出不同的提示。
1.題目分析:
2.題目源代碼如下:

#include "stdio.h" #include "time.h" #include "stdlib.h"int main() {int n,m,i=0;srand(time(NULL));n=rand()%100+1;do{printf("輸入你猜的數(shù)字: ");scanf("%d",&m);i++;if(m>n)printf("錯(cuò)誤!數(shù)太大了! \n");else if(m<n)printf("錯(cuò)誤!數(shù)太小了! \n");}while(m!=n);printf("回答正確! \n");printf("共猜測了%d次。 \n");if(i<=5){printf("你太聰明了,這么快就猜出了! "); } else if(i>5){printf("還需改進(jìn)方法,以便更快猜出來!");}getchar();return 0; }

【題目93】

題目:由用戶輸入骰子數(shù)量和參賽人數(shù),然后由計(jì)算機(jī)隨機(jī)生成每一粒骰子的點(diǎn)數(shù),再累加得到每一個(gè)選手的總點(diǎn)數(shù)。
1.題目分析:
2.題目源代碼如下:

#include "stdio.h" #include "time.h" #include "stdlib.h"void play(int n) {int i,m=0,t=0;for(i=0;i<n;i++){t=rand()%6+1;m+=t;printf("\t第%d粒:%d; \n",i+1,t);}printf("\t總點(diǎn)數(shù)為:%d \n",m); } int main() {int c; //參賽人數(shù)int n; //骰子數(shù)量int i,m;do{srand(time(NULL));printf("設(shè)置骰子數(shù)量(輸入0則退出):");scanf("%d",&n);if(n==0)break; //至少一個(gè)骰子printf("\n輸入本輪參賽人數(shù)(輸入0則退出): ");scanf("%d",&c);if(c==0)break; for(i=0;i<c;i++){printf("\n第%d位選手?jǐn)S出的骰子為: \n",i+1);play(n);}printf("\n");}while(1);return 0; }

【題目94】

題目:創(chuàng)建一個(gè)鏈表。
1.題目分析:
2.題目源代碼如下:

#include "stdio.h" #include "stdlib.h" #include "malloc.h"typedef struct LNode{int data;struct LNode *next; }LNode,*LinkList;LinkList CreateList(int n); void print(LinkList h);int main() {LinkList Head=NULL;int n;scanf("%d",&n);Head=CreateList(n);printf("剛剛建立的各個(gè)鏈表元素的值為:\n");print(Head);printf("\n\n");system("pause");return 0; } LinkList CreateList(int n) {LinkList L,p,q;int i;L=(LNode*)malloc(sizeof(LNode));if(!L)return 0;L->next=NULL;q=L;for(i=1;i<=n;i++){p=(LinkList)malloc(sizeof(LNode));printf("請(qǐng)輸入第%d個(gè)元素的值:",i);scanf("%d",&(p->data));p->next=NULL;q->next=p;q=p;}return L; } void print(LinkList h) {LinkList p=h->next;while(p!=NULL){printf("%d ",p->data);p=p->next;} }

【題目95】

題目:動(dòng)態(tài)分配13個(gè)整型儲(chǔ)存區(qū)域,然后賦值并輸出
1.題目分析:
2.題目源代碼如下:

#include "stdio.h" #include "stdlib.h"int main() {int count,*array;if((array=(int *)malloc(13*sizeof(int))) == NULL){printf("Cannot succeed the assignment storage space. ");exit(1);}for(count=0;count<13;count++) //給數(shù)組賦值{array[count]=count;}for(count=0;count<13;count++) //打印數(shù)組元素{printf("%4d",array[count]);}return 0; }

【題目96】

題目:通過malloc函數(shù)分配一個(gè)大的內(nèi)存,然后再分配一個(gè)小的內(nèi)存并查看是否分配成功,如果不成功則使用free來釋放。
1.題目分析:
2.題目源代碼如下:

#include "stdio.h" #include "stdlib.h"int main() {long *buf1,*buf2;long size=13000*sizeof(long);buf1=(long *)malloc(size);if(buf1!=NULL){printf("\n Allocation of %ld bytes successful. \n",size);}else{printf("\n Attempt to allocate %ld bytes failed. \n",size);exit(1); }buf2=(long *)malloc(size);if(buf2 != NULL) //若分配成功則輸出成功的信息 {printf("\n Second allocation of %ld bytes successful. \n",size);exit(0);}else //若失敗則輸出失敗的信息 {printf("\n Second attempt to allocate %ld bytes failed. \n",size);}free(buf1); //釋放第1個(gè)內(nèi)存 printf("\n Freeing first block. \n");if((buf2=(long *)malloc(size))!=NULL){printf("\n After free(),allocation of %ld bytes successful. \n",size); } return 0; }

【題目97】

題目:計(jì)算學(xué)生的平均成績和不及格的人數(shù)
1.題目分析:
2.題目源代碼如下:

#include "stdio.h" struct stu{int num;char *name;char sex;float score; }boy[3]={{101,"Li ping",'F',45},{102,"zhang san",'M',122},{101,"Wang er",'M',80}, }; int main() {int i,c=0;float ave,s=0;for(i=0;i<3;i++){s+=boy[i].score;if(boy[i].score<60)c+=1;}printf("s=%f \n",s);ave=s/3;printf("average=%f \ncount=%d \n",ave,c);return 0; }

【題目98】

題目:用指針變量輸出結(jié)構(gòu)數(shù)組.
1.題目分析:
2.題目源代碼如下:

#include "stdio.h" struct stu{int num;char *name;char sex;float score; }boy[2]={{101,"Li ping",'F',45},{103,"Wang er",'M',80},}; int main() {struct stu*ps;printf("No\tName\t\tSex\tScore\t\n");for(ps=boy;ps<boy+2;ps++){printf("%d\t%s\t\t%c\t%f\t\n",ps->num,ps->name,ps->sex,ps->score);}return 0; }

【題目99】

題目:用結(jié)構(gòu)體存放學(xué)生的信息(包括學(xué)號(hào)、姓名、性別、家庭住址,輸出該學(xué)生的信息。
1.題目分析:
2.題目源代碼如下:

#include "stdio.h"typedef struct student{int ID;char Name[10];char Sex;char Add[20]; }student; int main() {student stu1={1,"夏侯惇",'M',"王者峽谷666號(hào)"};printf("\n 學(xué)號(hào):%d 姓名:%s 性別:%c 家庭住址:%s \n",stu1.ID,stu1.Name,stu1.Sex,stu1.Add); return 0; }

【題目100】

題目:手機(jī)信息系統(tǒng)。
1.題目分析:
2.題目源代碼如下:

#include "stdio.h" #define MAX_TITLE_SIZE 30 #define MAX_AUTHOR_SIZE 40 #define MAX_SIZE 2 //構(gòu)造一個(gè)結(jié)構(gòu)體BOOK,用于存放title,author,price struct book {char title[MAX_TITLE_SIZE];char author[MAX_AUTHOR_SIZE];float price; }; int main() {int count=0; //設(shè)置一個(gè)計(jì)數(shù)器,用來計(jì)數(shù)輸入的次數(shù) int index=0; //設(shè)置另外一個(gè)計(jì)數(shù)器,用來遍歷顯示輸入的book struct book lib[MAX_SIZE];printf("手機(jī)信息錄入系統(tǒng) \n");while(count<MAX_SIZE && printf("手機(jī)型號(hào)是:") && gets(lib[count].title) != NULL && lib[count].title[0]!='\n'){printf("制造廠商: \t");gets(lib[count].author);printf("價(jià)格: \t");scanf("%f",&lib[count].price);count++;while(getchar()!='\n'){continue;}if(count<MAX_SIZE){printf("輸入下一款手機(jī)信息\n");}}if(count>0){printf("下面是手機(jī)列表 \n"); //遍歷結(jié)構(gòu)體數(shù)組 for(index=0;index<count;index++){printf("手機(jī)型號(hào)是%s制造廠商是%s價(jià)格是%f \n",lib[index].title,lib[index].author,lib[index].price);}}return 0; }

總結(jié)

以上是生活随笔為你收集整理的C语言题目练习100例——题目+题目分析+源代码(91—100)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。