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

【题目91】

题目:在屏幕中输入一个数字,编程求出其平方根。
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】

题目:用计算机随机生成1~100的一个数字,然后由用户来猜这个数,根据用户猜测的次数分别给出不同的提示。
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("输入你猜的数字: ");
		scanf("%d",&m);
		i++;
		if(m>n)
		printf("错误!数太大了! \n");
		else if(m<n)
		printf("错误!数太小了! \n");
	}while(m!=n);
	printf("回答正确! \n");
	printf("共猜测了%d次。 \n");
	if(i<=5)
	{
		printf("你太聪明了,这么快就猜出了! ");	
	}	
	else if(i>5)
	{
		printf("还需改进方法,以便更快猜出来!");
	}
	getchar();
	return 0;
} 

【题目93】

题目:由用户输入骰子数量和参赛人数,然后由计算机随机生成每一粒骰子的点数,再累加得到每一个选手的总点数。
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总点数为:%d \n",m);
}
int main()
{
	int c;		//参赛人数
	int n;		//骰子数量
	int i,m;
	do{
		srand(time(NULL));
		printf("设置骰子数量(输入0则退出):");
		scanf("%d",&n);
		if(n==0)break;		//至少一个骰子
		printf("\n输入本轮参赛人数(输入0则退出): ");
		scanf("%d",&c);
		if(c==0)break; 
		for(i=0;i<c;i++)
		{
			printf("\n第%d位选手掷出的骰子为: \n",i+1);
			play(n);
		}
		printf("\n");
	}while(1);
	return 0;
}

【题目94】

题目:创建一个链表。
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("刚刚建立的各个链表元素的值为:\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("请输入第%d个元素的值:",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】

题目:动态分配13个整型储存区域,然后赋值并输出
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++)		//给数组赋值
	{
		array[count]=count;
	}
	for(count=0;count<13;count++)		//打印数组元素
	{
		printf("%4d",array[count]);
	}
	return 0;
}

【题目96】

题目:通过malloc函数分配一个大的内存,然后再分配一个小的内存并查看是否分配成功,如果不成功则使用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个内存 
	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】

题目:计算学生的平均成绩和不及格的人数
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】

题目:用指针变量输出结构数组.
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】

题目:用结构体存放学生的信息(包括学号、姓名、性别、家庭住址,输出该学生的信息。
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号"};
	printf("\n 学号:%d 姓名:%s 性别:%c 家庭住址:%s \n",stu1.ID,stu1.Name,stu1.Sex,stu1.Add); 
	return 0; 
}

【题目100】

题目:手机信息系统。
1.题目分析:
2.题目源代码如下:

#include "stdio.h"
#define MAX_TITLE_SIZE 30
#define MAX_AUTHOR_SIZE 40
#define MAX_SIZE 2
//构造一个结构体BOOK,用于存放title,author,price 
struct book
{
	char title[MAX_TITLE_SIZE];
	char author[MAX_AUTHOR_SIZE];
	float price;
};
int main()
{
	int count=0;		//设置一个计数器,用来计数输入的次数 
	int index=0;		//设置另外一个计数器,用来遍历显示输入的book 
	struct book lib[MAX_SIZE];
	printf("手机信息录入系统 \n");
	while(count<MAX_SIZE && printf("手机型号是:") && gets(lib[count].title) != NULL && lib[count].title[0]
	!='\n')
	{
		printf("制造厂商: \t");
		gets(lib[count].author);
		printf("价格: \t");
		scanf("%f",&lib[count].price);
		count++;
		while(getchar()!='\n')
		{
			continue;
		}
		if(count<MAX_SIZE)
		{
			printf("输入下一款手机信息\n");
		}
	}
	if(count>0)
	{
		printf("下面是手机列表 \n");		//遍历结构体数组 
		for(index=0;index<count;index++)
		{
			printf("手机型号是%s制造厂商是%s价格是%f \n",lib[index].title,lib[index].author,lib[index].price);
		}
	}
	return 0;
}
  • 2
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值