七、分支与循环(3)

1.作业

(1)for循环求a

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
#include <math.h>
//for循环求a
int main()
{	
	int a = 0;
	int b = 0;
	for(a=1,b=1;a<=100;a++)
	{
		if(b>=20)
		{
			break;
		}
		if(b%3==1)
		{
			b=b+3;
			continue;
		}
		b=b-5;
	}
	printf("a=%d\n",a);
	printf("b=%d\n",b);
	return 0;
}

(2)switch-case无break结束

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
// switch-case无break结束
int fun (int a)
{
	int b = 0;
	switch(a)
	{
	case 1:b=30;
	case 2:b=20;
	case 3:b=16;
	default :b=0;
	}
	return b;
}
	
int main()
{	
	int x = 1;
	printf("%d\n",fun(x));
	return 0;
}

(3)switch-case嵌套

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
//switch-case 嵌套
int main()
{	
	int x = 3;
	int y = 3;
	switch(x%2)
	{
	case 1:
		switch(y)
		{
		case 0:
			printf("first\n");
		case 1:
			printf("second\n");
		default :
			printf("hello\n");
		}
	case 2:
		printf("third\n");
	}
	return 0;
}

(4)for循环里面的if赋值语句

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
//循环里面的if赋值语句
int main()
{
	int i = 0;
	for(i=0;i<10;i++)
	{
		if(i=5)
		{
			printf("%d\n",i);
		}
	}
	return 0;
}

(5)从大到小依次输出三个数

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
//从大到小依次输出三个数
int main()
{	
	int a = 0;
	int b = 0;
	int c = 0;
	int tmp = 0;
	printf("请输入a:");
	scanf("%d",&a);
	printf("请输入b:");
	scanf("%d",&b);
	printf("请输入c:");
	scanf("%d",&c);
	if(a<b)
	{
	   	tmp=a;
		a=b;
		b=tmp;
	}
	if(a<c)
	{
		tmp=a;
		a=c;
		c=tmp;
	}
	if(b<c)
	{
		tmp=b;
		b=c;
		c=tmp;
	}
	printf("这三个数从大到小是:%d %d %d\n",a,b,c);
	return 0;
}

(6)打印1~100之间3的倍数(1)

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
//打印1到100之间3的倍数(1)
int main()
{	
	int i = 1;
	int n = 0;
	printf("3的倍数有:\n");
	while(i<=100)
	{
		if(i%3==0)
		{
			printf("%d ",i);
			n++;
		}
		i++;
	}
	printf("\n");
	printf("1到100一共有%d个3的倍数\n",n);
	return 0;
}

(7)打印1~100之间3的倍数(2)

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
//打印1到100之间3的倍数(2)
int main()
{	
	int i = 3;
	int n = 0;
	printf("3的倍数有:\n");
	while(i<=100)
	{
		printf("%d ",i);
		i+=3;
		n++;
	}
	printf("\n");
	printf("1到100一共有%d个3的倍数\n",n);
	return 0;
}

(8)求两个数的最大公约数

辗转相除法

例:a = 24   b = 18

a%b = 6         b%6 = 0(能整除时,除数是最大公约数。)

则6是24与18的最大公约数

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
//求两个数的最大公约数,辗转相除法。
int main()
{	
	int a = 0;
	int b = 0;
	int c = 0;
	printf("请输入第一个数:\n");
	scanf("%d",&a);
	printf("请输入第二个数:\n");
	scanf("%d",&b);
	while(c=a%b)
	{
		a=b;
		b=c;
	}
	printf("这两个数的最大公约数是:%d\n",b);
	return 0;
}

(9)打印1000年到2000年之间的闰年

能被4整除并且不能被100整除,或者能被400整除

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
//打印1000年到2000年之间的闰年
int main()
{	
	int year = 1000;
	int count = 0;
	printf("闰年有:\n");
	while(year<=2000)
	{
		//if((year%4==0 && year%100!=0)||(year%400==0))
		if((year%4)==0 && (year%100)!=0)
			{
				printf("%d年 ",year);
				count++;
			}
		else if(year%400==0)
			{
				printf("%d年 ",year);
				count++;
			}
		year++;
	}
	printf("\n一共有%d个闰年\n",count);
	return 0;
}

(10)1~100所有整数中出现9的个数

i%10 = 9 //个位

                               考虑99的特殊情况

i/10 = 9//十位

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
#include <math.h>
//1~100所有整数中出现9的个数
int main()
{	
	int i = 1;
	int count = 0;
	while(i<=100)
	{
		if(i%10==9)
		{
			count++;
			printf("%d " ,i);
		}
		if(i/10==9)
		{
			count++;
			printf("%d ",i);
		}
		i++;
	}
	printf("\n一共有%d个\n",count);
	return 0;
}

(11)计算1/1-1/2+1/3-1/4+1/5.....+1/99-1/100的值

整数/整数,取商,淹没小数部分的数值,解决:被除数/除数,至少有一个是浮点数

例:a = 5       b = 3

c = a×1.0/b 或  c = a/b×1.0   或 c = a×1.0/b× 1.0

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
#include <math.h>
//计算1/1-1/2+1/3-1/4+1/5.....+1/99-1/100的值
int main()
{	
	int i = 0;
	double sum = 0;
	int flag = 1;
	for(i=1;i<=100;i++)
	{
		sum+=flag*1.0/i;
		flag=-flag;
	}
	printf("最后结果为:%lf\n",sum);
	return 0;
}

(12)求十个整数的最大值

十个整数创建一个10个整型数组元素的数组,给一个变量赋值数组中某个元素的值,然后依次比较。

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
#include <math.h>
//求十个数的最大值
int main()
{	
	int arr [] = {1,98,3,4,5,33,10,8,99,9};
	int max = arr[0];
	int sz = sizeof(arr)/sizeof(arr[0]);
	int i = 0;
	for(i=0;i<sz;i++)
	{
		if(arr[i]>max)
		{
			max=arr[i];
		}
	}
	printf("最大值是%d\n",max);
	for(i=0;i<sz;i++)
	{
		if(arr[i]==max)
		{
			break;
		}
	}
	printf("下标是%d\n",i);
	return 0;
}

(13)屏幕输出9×9乘法口诀表

%2d    占两位,右对齐,不够空格补齐

%-2d   左对齐

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
#include <math.h>
//屏幕输出9×9乘法口诀表
int main()
{	
	int i = 0;
	int j = 0;
	int n = 0;
	printf("9*9乘法口诀表:\n");
	for(i=1;i<=9;i++)
	{
		for(j=1;j<=i;j++)
		{
			n=i*j;
			printf("%d*%d=%-3d ", i, j, n);
		}
		printf("\n");
	}
	return 0;
}

(14)试除法求素数

素数:除1和它本身外无整除的正整数

1.打印100~200之间的素数(质数)

被除数从100到200循环,除数从2到被除数,依次进行除法对比,当%为0时,跳出,当被除数 = 除数时,则为素数打印。

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
//打印100~200之间的素数(质数)
int main()
{	
	int a = 100;
	int b = 2;
	int count = 0;
	printf("素数有:");
	for(a=100;a<=200;a++)
	{
		for(b=2;b<a;b++)
		{
			if(a%b==0)
			{
				break;
			}
		}
		if(a==b)
		{
			printf("%d ",a);
			count++;
		}
	}
	printf("\n一共有%d个素数 \n",count);
	return 0;
}

2.优化素数

若一个数能写成i = a×b的形式,则a/b至少有一个数<=\sqrt{i}

sqrt   -> 开平方的库函数   <math.h>

sqrt中要使用浮点型数据,例:int a = 1;  sqrt (a*1.0)

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
#include <math.h>
//优化素数
int main()
{	
	int a = 100;
	int b = 2;
	int count = 0;
	printf("素数有:");
	for(a=100;a<=200;a++)
	{
		for(b=2;b<=sqrt (a*1.0);b++)
		{
			if(a%b==0)
			{
				break;
			}
		}
		if(b>sqrt (a*1.0))
		{
			printf("%d ",a);
			count++;
		}
	}
	printf("\n一共有%d个素数 \n",count);
	return 0;
}

3.优化素数pro版

跳过被除数的所有偶数判断

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
#include <math.h>
//素数优化pro版
int main()
{	
	int a = 101;
	int b = 2;
	int count = 0;
	for(a=101;a<=199;a+=2)
	{
		for(b=2;b<=sqrt(a*1.0);b++)
		{
			if(a%b==0)
			{
				break;
			}
		}
		if(b>sqrt(a*1.0))
		{
			printf("%d ",a);
			count++;
		}
	}
	printf("\n一共有%d个素数 \n",count);
	return 0;
}

优化的是时间复杂度,不是代码长度。

while循环中,无其它特殊情况,条件表达式(判断语句)的执行次数总是比循环体的执行次数多一次。

2.猜数字游戏

rand    <stdlib.h>    int rand (void);//返回int类型的随机数0~32767

srand   void srand(unsigned int seed);//生成随机数的起始点(种子),而且seed要变化,void是空类型。

时间戳:当前时间 - 计算机起始时间( 1970.1.1  0:0:0) = ()秒

time  <time.h>    time_t(long)     time  (time_t*timer)(指针变量);

一般用NULL,NULL指针是一个定义在标准库中值为0的常量(空指针)

一般写法:srand((unsigned int)time(NULL));//设置随机数的种子

例:srand((unsigned int)time(NULL));

int a = rand()%100+1//%100,产生0~99之间的数

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
//猜数字游戏
void menu ()
{
	printf("*****************************\n");
	printf("***      猜数字游戏       ***\n");
	printf("***   1.play   0.exit     ***\n");
	printf("*****************************\n");
}
void game ()
{
	 int ret = 0;
	 int guess = 0;
	 int count = 0;
	 ret = rand()%100+1;
	 //printf("随机数为:%d\n",ret);
	 printf("开始游戏\n");
	 while(1)
	{
		printf("请猜数字>:");
	   	scanf("%d",&guess);
		if(guess>ret)
		{
			printf("猜大了\n");
		}
		else if(guess<ret)
		{
			printf("猜小了\n");
		}
		else
		{
			printf("恭喜你,猜对了\n");
			count++;
			break;
		}
		count++;
	}
	printf("一共猜了%d次\n",count);
}
int main()
{
	int input = 0;
	srand((unsigned int)time (NULL));
	do
	{
		menu();
		printf("请输入:");
		scanf("%d",&input);
		system("cls");
		switch(input)
		{
			case 1:
					game();
					break;
			case 0:
					printf("退出游戏\n");
					break;
			default:
					printf("输入无效,请重新输入。\n");
					break;
		}
	}
	while(input);
	return 0;
}

3.goto语句(传送门)

例:again:

                printf

                goto again;

不建议使用,某些场合,可跳出多层循环嵌套。

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
//goto语句
int main()
{
again:
	printf("Hello,World\n");
	goto again;
	return 0;
}

4.goto语句实现关机程序

shutdown -s -t 60(秒数)(电脑cmd关机命令)

shutdown -a  取消关机命令

C语言中:system("shutdown -s -t 60");

                 system("shutdown -a");

.exe程序移植到电脑服务的.exe程序

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
//goto语句实现关机程序
int main()
{
	char input [20] = {0};
	int count1 = 0;
	int count2 = 0;
	int count = 0;
	system("shutdown -s -t 60");
	count1 = time(NULL);
	printf("您的电脑将在60s后关机,请输入:666取消关机。\n");
again:
	printf("请输入>:");
	scanf("%s",input);
	if(strcmp(input,"666")==0)
	{
		system("shutdown -a");
	}
	else
	{
		count2=time(NULL);
		count=count2-count1;
		printf("输入错误,小老弟,还有%d秒后关机!!!\n",60-count);
		goto again;
	}
	return 0;
}

5.拆炸弹式关机

用随机数设置密码,并且只有三次机会。

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
//拆炸弹式关机程序
int main()
{  
	int count1 = 0;
	int count2 = 0;
	int count = 0;

	int m = 0;
	int n = 0;
	int i = 1;

	srand((unsigned int)time(NULL));
	m = rand()%10+1;
	//printf("随机数为:%d\n",m);

	count1 = time(NULL);
	printf("您的电脑将在一分钟后关机,请猜数(1~10)\n");
	printf("你一共有三次机会\n");
	while(1)
	{
		printf("请输入:");
		scanf("%d",&n);
		if(n==m)
		{
			printf("binggo\n");
			break;
			//system("shutdown -a");
		}
		else if(i==3)
		{
			printf("测试\n");
			break;
			//system("shutdown -s -t 0");
		}
		else
		{
			count2=time(NULL);
			count=count2-count1;
			printf("输入错误,%d秒后自动关机!!!\n",60-count);
			printf("还有%d次机会\n",3-i);
			i++;
		}
	}
	return 0;
}
	

6.思维导图

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值