第九次作业

1. 猜数字:

随机产生一个0-99的数,猜猜看

如果大了 就提示大了点

如果小了  就提示小了点

直到猜对为止

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
    int a,b,c;
    srand((unsigned)time(NULL));
    a = rand();
	b=a%100;
	while(b!=c)
	{
	printf("请输入一个0--99的数字:");
	scanf("%d",&c);
		if (b<c)
			printf("大了点!\n");
		if (b>c)
			printf("小了点!\n");
	}
	printf("你猜中了!\n");
}

  

 

2. (1)编写一个程序,求1-1/2+1/3+....-1/100之值。

#include <stdio.h>
main()
{
	double a,b=1;
	double c=0.0;
	for (a=1;a<=100;a++)
	{
		c+=b/a;
		b=-b;
		
	}
	printf("%.2lf\n",c);
}

  

 

 3.

编写一个程序,求s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)的值。

#include <stdio.h>
int main()
{
	int sum1=0,i,n,sum2=0;
	printf("请输入一个正整数:");
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
		sum1+=i;
		sum2+=sum1;
	}
	printf("他们的和为%d",sum2);
	return 0;
}

  

 

4.编写程序,用户输入一个数,将这个数倒过来后输出.

#include <stdio.h>
 main()
{
	int a,b;
	printf("请输入一个正整数:");
	scanf("%d",&a);
	while (a>0)
	{
		b=a%10;
		printf("%d ",b);
		a=a/10;
	}
}

 

  

 

 5.100匹马驮100担货,大马一匹驮3担,中马一匹驮2担,小马两匹驮1担。试编写程序计算大、中、小马的数目。

#include <stdio.h>
int main()
{
	int a,b,c=0,i;//a为大马,b为中马,c为小马
	for (a=0;a<33;a++)
	{
		for (b=0;b<50;b++)
		{
			c=100-a-b;
			if(c%2!=0)
			{
			continue;
			}
			i=3*a+2*b+c/2;
			if (i==100)
			printf("大马有%d只,中马有%d只,小马有%d只\n",a,b,c);
		}
	}
	return 0;
}

  

 

posted @ 2021-11-13 00:21  陌辰1  阅读(35)  评论(0)    收藏  举报