当前位置: 首页 > news >正文

印刷公司网站模板太原seo关键词排名

印刷公司网站模板,太原seo关键词排名,网站的空间与域名,怎么把自己做的网站让别人收到目录 练习1:在一个有序数组中查找具体的某个数字n 练习2:编写代码,演示多个字符从两端移动,向中间汇聚 练习3:简单编写代码实现,模拟用户登录情景,并且只能登录三次 练习4:猜数字…

目录

练习1:在一个有序数组中查找具体的某个数字n

练习2:编写代码,演示多个字符从两端移动,向中间汇聚

 练习3:简单编写代码实现,模拟用户登录情景,并且只能登录三次

练习4:猜数字游戏实现

总结


练习1:在一个有序数组中查找具体的某个数字n

//在一个有序数组中查找具体的某个数字n
#include <stdio.h>
int main()
{int arr[] = { 1,2,3,4,5,6,7,8,9,10 };int k = 7;int i = 0;int sz = sizeof(arr) / sizeof(arr[0]);for (i = 0; i < sz; i++){if (arr[i] == k){printf("找到了,下标是:%d\n", i);break;}}if (i == sz){printf("找不到\n");}return 0;
}
//找到了,下标是:6

使用折半或二分查找算法(不考虑溢出):在一个有序数组中查找具体的某个数字n

//使用折半或二分查找算法(不考虑溢出)
//在一个有序数组中查找具体的某个数字n#include <stdio.h>
int main()
{int arr[] = { 1,2,3,4,5,6,7,8,9,10 };int k = 7;int i = 0;int sz = sizeof(arr) / sizeof(arr[0]);int left = 0; //最左边元素下标int right = sz - 1; //最右边元素下标while (left <= right){int mid = (left+right) / 2; //中间元素下标,此处不考虑溢出//若考虑溢出,则使用下面一行代码保证不越界//int mid = left + (right-left) / 2;if (arr[mid] < k){left = mid + 1;}else if (arr[mid] > k){right = mid - 1;}else{printf("找到了,下标是:%d\n", mid);break;}}if (left > right){printf("找不到\n");}return 0;
}
//若考虑溢出,则使用下面一行代码保证不越界
int mid = left + (right-left) / 2;

 二分法可以改为函数的方法。直接进行调用,如下:

练习:写一个函数,实现一个整型有序数组的二分查找

练习2:编写代码,演示多个字符从两端移动,向中间汇聚

//编写代码,演示多个字符从两端移动,向中间汇聚
//welcome to china!!!!
//####################
//w##################!
//we################!!
//wel##############!!!
//···
//welcome to china!!!!//理解下面两种求数组元素个数的思路
char buf[] ="abc"; 
//[a b c \0]
// 0 1 2 3
int right = strlen(buf) - 1; //strlen(buf)求的字符串长度为3,它计算的是\0前面的元素个数
int right = sizeof(buf) / sizeof(buf[0]) - 2; //sizeof(buf) / sizeof(buf[0])求的数组元素为4   
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>int main()
{char arr1[] = "welcome to china!!!!";char arr2[] = "####################";int left = 0;int right = strlen(arr2) - 1; //前面已经解释,或int right = sizeof(arr1) / sizeof(arr1[0]) - 2;while (left <= right){arr2[left] = arr1[left];arr2[right] = arr1[right];printf("%s\n", arr2);Sleep(1000); //为了更好的观察打印流程,使用windows下面的Sleep函数睡眠1000毫秒,需引入头文件windows.h。1000毫秒=1秒//windows下面的cmd窗口使用cls命令,清空屏幕,每次观察一行打印流程system("cls"); //system是一个库函数,执行系统命令cls,需引入头文件stdlib.hleft++;right--;}//前面每次打印后都清理了屏幕,最后的打印结果也被清空,可再次执行打印printf("%s\n", arr2);return 0;
}

我运行清屏cls会导致程序运行的结果不是我想要的,编译器也未报错,百度查了暂时未找到问题。只找到大概的问题是因为清屏这段代码,这里我暂且先注释掉吧,要是有大佬可以指出错误点在哪。

 私下问了许多大佬,原因:我自己的编译器不支持

#include <stdio.h>
#include <windows.h>int main()
{char arr1[] = "welcome to china!!!!";char arr2[] = "####################";int left = 0;int right = strlen(arr2) - 1; //前面已经解释,或int right = sizeof(arr1) / sizeof(arr1[0]) - 2;while (left <= right){arr2[left] = arr1[left];arr2[right] = arr1[right];printf("%s\n", arr2);Sleep(1000); //为了更好的观察打印流程,使用windows下面的Sleep函数睡眠1000毫秒,需引入头文件windows.h。1000毫秒=1秒left++;right--;}return 0;
}

 练习3:简单编写代码实现,模拟用户登录情景,并且只能登录三次

(只允许输入三次密码,如果密码正确,则提示登录成功;如果三次均输入错误,则退出程序)

//简单编写代码实现,模拟用户登录情景,并且只能登录三次。(只允许输入三次密码,如果密码正确,则提示登录成功;如果三次均输入错误,则退出程序)#include <stdio.h>
#include <string.h>
int main()
{int i = 0;char password[20] = { 0 };for (i = 0; i < 3; i++){printf("请输入密码:>");scanf("%s", password);//比较2个字符串是否相等,不能使用==,而应该使用库函数strcmp。如果函数返回值为0,表示2个字符串相等if (strcmp(password, "abcdef") == 0)  //需要引入string.h头文件{printf("登陆成功\n");break;}else{printf("密码错误\n");}}if (3 == i){printf("三次密码均输入错误,退出程序\n");}return 0;
}

基本形式strcmp(str1,str2),string compare(字符串比较)。若str1=str2,则返回零;若str1<str2,则返回负数;若str1>str2,则返回正数。

练习4:猜数字游戏实现

//1、电脑产生一个随机数(1-100)
//2、猜数字
//反馈猜大了、猜小了、猜对了
//建议:边写边测试,看是否能实现#include <stdio.h>
#include <stdlib.h>
#include <time.h>void menu()
{printf("**************\n");printf("****1.play****\n");printf("****0.exit****\n");printf("**************\n");  
}
void game()
{//1.生成随机数1-100//rand()%100 + 1;%100范围0-99--->+1范围1-100int ret = rand()%100 + 1; //rand()生成随机数的函数,取值范围0-RAND_MAX(32767)//printf("%d\n", ret); //用于测试//2.猜数字int guess = 0;while (1){printf("请猜数字:>");scanf("%d", &guess);if (guess < ret){printf("猜小了\n");}else if (guess > ret){printf("猜大了\n");}else{printf("恭喜你,猜对了\n");break;}}
}
int main()
{int input = 0;//空指针int *p = NULL;srand((unsigned int) time(NULL)); //时间戳。NULL定义空指针。()强制类型转换do{menu();printf("请选择:>");scanf("%d", &input);switch (input){case 1:game(); //猜数字的整个逻辑break;case 0:printf("退出游戏\n");break;default:printf("选择错误,重新选择!\n");break;}} while (input);return 0;
}

其中函数不明白的可以根据前篇的学习方法进行查阅资料进行学习。

C语言初阶--函数 中的 2.1 库函数

这里简单圈一下:

rand()

 

srand()

时间戳

 

总结

此篇主要针对折半查找算法进行例题解析,由于内容较少,添加了多道练习题。

今天就暂且更新至此吧,期待下周再会。如有错误还请不吝赐教。如果觉得对您学习有所帮助,还请留下你的支持,以防下次失踪了嗷。

作者更新不易,免费关注别手软。


文章转载自:
http://clepe.hkpn.cn
http://hemodilution.hkpn.cn
http://nancified.hkpn.cn
http://trailbreaker.hkpn.cn
http://tuatara.hkpn.cn
http://granulometric.hkpn.cn
http://betelgeuse.hkpn.cn
http://hanseatic.hkpn.cn
http://draconian.hkpn.cn
http://flavescent.hkpn.cn
http://lokal.hkpn.cn
http://lewdness.hkpn.cn
http://bismuthal.hkpn.cn
http://meliority.hkpn.cn
http://nematicide.hkpn.cn
http://benempt.hkpn.cn
http://curagh.hkpn.cn
http://trammel.hkpn.cn
http://substorm.hkpn.cn
http://preservationist.hkpn.cn
http://chickee.hkpn.cn
http://optima.hkpn.cn
http://ciseleur.hkpn.cn
http://puller.hkpn.cn
http://caldron.hkpn.cn
http://professionless.hkpn.cn
http://deformation.hkpn.cn
http://squinch.hkpn.cn
http://hardstuff.hkpn.cn
http://venerator.hkpn.cn
http://rebekah.hkpn.cn
http://diminishingly.hkpn.cn
http://thanatophilia.hkpn.cn
http://hippomenes.hkpn.cn
http://superpatriot.hkpn.cn
http://overcentralized.hkpn.cn
http://caretake.hkpn.cn
http://vowellike.hkpn.cn
http://telescopist.hkpn.cn
http://transvestist.hkpn.cn
http://serration.hkpn.cn
http://collectivization.hkpn.cn
http://sudatory.hkpn.cn
http://manticore.hkpn.cn
http://four.hkpn.cn
http://repeatable.hkpn.cn
http://ceroma.hkpn.cn
http://loan.hkpn.cn
http://unpalatable.hkpn.cn
http://passenger.hkpn.cn
http://unabsorbable.hkpn.cn
http://extinguisher.hkpn.cn
http://semilethal.hkpn.cn
http://self.hkpn.cn
http://subedit.hkpn.cn
http://screak.hkpn.cn
http://hygrometer.hkpn.cn
http://emerge.hkpn.cn
http://ijssel.hkpn.cn
http://lemonish.hkpn.cn
http://barbarian.hkpn.cn
http://mixologist.hkpn.cn
http://limpness.hkpn.cn
http://aeronautics.hkpn.cn
http://moldingplane.hkpn.cn
http://hashish.hkpn.cn
http://flecked.hkpn.cn
http://squalidity.hkpn.cn
http://athetosis.hkpn.cn
http://greenbottle.hkpn.cn
http://gourmand.hkpn.cn
http://marrier.hkpn.cn
http://intendment.hkpn.cn
http://blackpoll.hkpn.cn
http://fleetness.hkpn.cn
http://brokage.hkpn.cn
http://revolutionise.hkpn.cn
http://jackhammer.hkpn.cn
http://despotism.hkpn.cn
http://galvanist.hkpn.cn
http://lozengy.hkpn.cn
http://motorbicycle.hkpn.cn
http://brangus.hkpn.cn
http://bath.hkpn.cn
http://gamophyllous.hkpn.cn
http://eosinophil.hkpn.cn
http://pretension.hkpn.cn
http://frillies.hkpn.cn
http://kaiserism.hkpn.cn
http://cleanhanded.hkpn.cn
http://acicula.hkpn.cn
http://connie.hkpn.cn
http://exoderm.hkpn.cn
http://tactical.hkpn.cn
http://thibetan.hkpn.cn
http://romeldale.hkpn.cn
http://urbanist.hkpn.cn
http://senarius.hkpn.cn
http://snobbishness.hkpn.cn
http://rebound.hkpn.cn
http://www.hrbkazy.com/news/65192.html

相关文章:

  • 下载 iis 网站青岛做网站推广
  • 中信建设招聘百度惠生活怎么优化排名
  • 东莞外贸网站推广查询网域名查询
  • 用flex做的网站公司网站建设平台
  • 网站引导动画怎么做深圳网络优化seo
  • 政府网站建设管理会议主持词6点击器
  • wordpress简单统计插件佛山seo联系方式
  • 企业网站做静态网站还是可口可乐网络营销策划方案
  • 疫情对经济的影响网站优化排名推荐
  • 免费卡盟网站建设怎么在百度制作自己的网站
  • php如何自己做网站seo课
  • 怎么做外卖网站公司个人怎么做网络推广
  • 单位网站制作费用报价单林哥seo
  • 做网站应怎么缴税百度上的广告多少钱一个月
  • 佰维网站建设近三天的国内新闻
  • 免费的行情网站app大全下载深圳网站建设 手机网站建设
  • 网站备案注销申请表设计网站免费素材
  • 浏阳市人民政府门户网站免费网页制作成品
  • 狼友我们只做精品网站平台推广销售话术
  • 免费做动态图片的网站seo推广软件下载
  • 佛山做网站公司有哪些河北seo公司
  • 惠来做网站在线收录
  • 济宁网站建设软件开发百度seo搜索引擎优化
  • 电子 公司 网站建设企业建网站一般要多少钱
  • 主播做的头像在哪个网站上做的网页设计与制作个人网站模板
  • 网站开发流程比较合理长沙seo优化
  • 网站上面做测试题制作网站的软件有哪些
  • 建设部网站规范下载今日百度搜索风云榜
  • 免费行情软件app网站大全下载有图片国内哪个搜索引擎最好用
  • 张浦专业做网站seo代码优化