分支与循环(2)

目录

找出对应数字在数组中的下标

折半/二分查找算法

逐个字母打印

模拟用户登录


找出对应数字在数组中的下标

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int main()
{
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int k = 17;
	//写一个代码,在arr数组中找到7
	int i = 0;
	int sz= sizeof(arr) / sizeof(arr[0]);
	for (i = 0; i < sz; i++)
	{
		if (k == arr[i])
		{
			printf("找到了,下是标d\n", i);
			break;
		}
	}
	if (i == sz)
		printf("the number could not find.\n");
	return 0;

折半/二分查找算法

查找效率为:log_{2}n (其中n为查找数组个数,假设有n=2_{}^{32}个有序的数字需要查找,那么至多查找

//half finding
int main()
{
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	int k = 20;
	int sz = sizeof(arr) / sizeof(arr[0]);//calculate the number of element in array
	int left = 0;//the left index start from zero
	int right = sz - 1;
	
	while (left<=right)
	{
		int mid = (left + right) / 2;
		if (arr[mid] > k)
		{
			right = mid - 1;
		}
		else if (arr[mid] < k)
		{
			left = mid + 1;
		}
		else
		{
			printf("the number have been found,the index is:%d\n", mid);
			break;
		}
	}
	if (left>right)
	{
		printf("The number can not be found.\n");
	}
	return 0;
}

32次即可找到)

逐个字母打印

编写代码,演示多个字符从两端移动,从中间汇聚。

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
#include<Windows.h>
#include<stdlib.h>
int main()
{
	char arr1[] = "welcome to my world.";
	char arr2[] = "********************";
	int left = 0;
	//int right = sizeof(arr1) / sizeof(arr[0]) - 2;//注意最后会跟一个\0,因此需要减2
	int right = strlen(arr1)-1;//索引值比元素个数少1,从0开始
	

	while (left<=right)
	{
		arr2[left] = arr1[left];
		arr2[right] = arr1[right];
		printf("%s\n", arr2);
		//pause 1 second
		Sleep(500);
		system("cls");//执行系统命令的一个函数-cls --清空屏幕
		left++;
		right--;

	}
	printf("%s\n", arr1);
	return 0;
}

模拟用户登录

对比两个字符串是否相等用strcmp(,);返回值为0则两个字符串相等

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
#include<Windows.h>
#include<stdlib.h>

int main()
{
	int i = 0;
	char password[20] = {0};
	for (i = 0; i <3; i++)
	{
		printf("please enter your password:");
		scanf("%s", password);

		if (strcmp(password,"123456")==0)//==等号不能用来比较两个字符串是否相等
			//正确用法应该使用库函数strcmp,返回值为0
		{
			printf("log in sucessfully.\n");
			break;
		}
		else
		{
			printf("Wrong, please try again\n");
		}
	}
	if (i == 3)
	{
		printf("3 times try have been used.Exiting program...\n");
	}

	return 0;

}

goto语句

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>//为了使用strcmp字符串对比函数
#include<Windows.h>
#include<stdlib.h>
#include<math.h>//为了使用sqrt
#include<stdlib.h>//为了使用随机生成
#include<time.h>//使用时间戳

int main()
{
	char input[20] = { 0 };
	//shutdown-s -t 60 意思为60秒后关机
	//system() - 执行系统命令的
	system("shutdown -s -t 60");
again:
	printf("请注意,你的电脑再一分钟内关机,若输入:我是猪,取消关机。请输入\n");
	scanf("%s", input);
	if (strcmp(input, "我是猪") == 0)//用来比较两个字符串
	{
		system("shutdown -a");//取消关机
	}
	else
	{
		goto again;
	}
	return 0;
}

不用goto

int main()
{
	char input[20] = { 0 };
	//shutdown-s -t 60 意思为60秒后关机
	//system() - 执行系统命令的
	system("shutdown -s -t 60");
	while (1)
	{
		printf("请注意,你的电脑再一分钟内关机,若输入:我是猪,取消关机。请输入\n");
		scanf("%s", input);
		if (strcmp(input, "我是猪") == 0)//用来比较两个字符串
		{
			system("shutdown -a");//取消关机
			break;
		}

	}

	return 0;
}

使用循环的替代写法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

华为账号hw_Zixin 小鱼儿梦想+

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值