当前位置:网站首页>分支和循环语句题目练习
分支和循环语句题目练习
2022-07-27 22:10:00 【徐憨憨!】
因为个人原因,长时间没有更新博客,从今天开始,我将继续为大家更新C以及后面的知识。
接着上一次的博客,上一次的博客我们讲解了分支和循环语句的知识点,趁热打铁,我们更新几个我在学习过程中的相关题目,供大家一起学习!!!
1. 计算 n的阶乘。
思想:
1.形成1-n的数字
2.可以简化n的阶乘
例如:2!= 1!*2 3!= 2!*3.....n! = (n-1)!*n 其实我们只需要把每次生成的数乘以前一个数的阶乘即可(后期学过函数之后也可以采用递归的方法来写,这里我们只采用迭代的方法来写)
int main()
{
int n = 0;
scanf("%d", &n);
int ret = 1;//注意这里不能是0
//生成1-n的数字
int i = 0;
for (i = 1; i <= n; i++)
{
ret = ret * i;
}
printf("%d\n", ret);
return 0;
}
我们输入5可知5!=120。
2. 计算 1!+2!+3!+……+10!思想:这一题只是在第一题的基础上扩充了一下,我们只需要再定义一个sum变量,把每一次算出的阶乘加在sum上即可
int main()
{
int ret = 1;
int sum = 0;
//生成1-10的数字
int i = 0;
for (i = 1; i <= 10; i++)
{
ret = ret * i;
sum = sum + ret;
}
printf("%d\n", sum);
return 0;
} 
读者可自行设置变量来验证代码的逻辑性是否正确!
3. 在一个有序数组中查找具体的某个数字n。(讲解二分查找)思想:这个题目的主要思想就是二分查找,也就是常说的折半查找;首先我们需要定义一个left变量指向这个有序数组的第一个元素,定义一个right变量指向有序数组的最后一个元素(其中right变量我们可以采用sizeof的运算符求得,即right=sizeof(arr)/sizeof(arr[0])-1,再定义一个mid变量指向这个数组的中间元素,为防止mid变量越界,我们可以令mid=left+(right-left)/2;若查找的元素比mid指向的元素大则left=mid+1,若查找的元素比mid指向的元素小则right=mid-1,若相等则查找成功,每次查找可以排除一半的元素,其时间复杂度为logn,当left>right时,查找结束,此时若还没有查找到,则查找失败!
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };//定义一个整型数组
int k = 7;//若查找的元素时7
int sz = sizeof(arr) / sizeof(arr[0]);//计算整型数组的元素个数
int left = 0;
int right = sz - 1;
//查找
while (left <= right)
{
int mid = left + (right - left) / 2;
if (arr[mid] > k)
{
right = mid - 1;
}
else if (arr[mid] < k)
{
left = mid + 1;
}
else
{
printf("找到了,下标为:%d\n", mid);
break;//查找成功则跳出循环
}
}
if (left > right)
{
printf("查找失败\n");
}
return 0;
} 
注意:
1.mid变量一定要定义再while循环内部,因为每一次查找mid都会发生改变
2.求整型数组的长度以后都会采用sizeof(arr)/sizeof(arr[0])
3.为防止mid变量越界,我们一般采用mid = left+(right-mid)/2 ,读者可自行画图理解
4. 编写代码,演示多个字符从两端移动,向中间汇聚。思想:定义两个数组,再定义两个变量,一个指向数组的第一个元素,另一个指向数组的最后一个元素(同上题思想一样),每次把一个数组中的一个元素赋值给另外一个数组,然后令left++,right--,当left>right时,说明操作完成,退出循环#include <string.h> #include <windows.h> int main() { char arr1[] = "hello world!!!"; char arr2[] = "##############"; int sz = strlen(arr1);//求数组的长度 int left = 0; int right = sz - 1; //交换 while (left <= right) { system("cls");//清屏 arr2[left] = arr1[left]; arr2[right] = arr1[right]; left++; right--; printf("%s\n", arr2); Sleep(500);//停留500ms } return 0; }
因为每一次printf在循环内部,且每一次循环都会清屏,所以是一个动态过程,大家在编译的时候可以自己看见!
注意:
1.求字符数组的长度有两种方法:第一种是上一题利用sizeof求解,第二种就是这里写的利用strlen,需要调用库函数string.h
2.这里的system("cls")是清屏函数,Sleep(500)是为了让打印结果在屏幕中停留,停留的时间自己定义,单位是毫秒,需要调用库函数windows.h
5. 编写代码实现,模拟用户登录情景,并且只能登录三次。(只允许输入三次密码,如果密码正确则 提示登录成,如果三次均输入错误,则退出程序。思想:利用循环进行模拟登录,循环三次,若三次内密码正确则break跳出循环提示登录成功,否则登陆失败#include <string.h> int main() { char password[20] = { 0 };//假设密码是abcdef printf("请登录\n"); int i = 0;//记录登录次数 while (i < 3) { printf("请输入密码:"); scanf("%s", password); if (strcmp(password, "abcdef") == 0) { printf("登录成功\n"); break; } else { printf("密码输入错误,请重新输入\n"); i++; } } if (i == 3) { printf("三次密码输入错误,退出程序\n"); } return 0; }
登录成功:

登录失败:

注意:
比较字符串是否相等时,需要strcmp函数,相等则结果为0,需要调用库函数string.h
5.猜数字游戏实现
思想:
利用srand函数生成1-100的数字,进行猜数字,猜数字的逻辑:若所猜数字大于随机生成的数字则提示猜大了,若小于随机生成的数字则提示猜小了,否则猜成功
#include <stdlib.h>
#include <time.h>
//打印游戏菜单界面
void menu()
{
printf("************************\n");
printf("***** 猜数字游戏 *****\n");
printf("***** 1.play *****\n");
printf("***** 0.exit *****\n");
printf("************************\n");
}
//打印游戏菜单界面
void game()
{
int ret = rand() % 100 + 1;
int k = 0;
printf("猜数字\n");
while (1)
{
printf("请输入:");
scanf("%d", &k);
if (k < ret)
{
printf("猜小了\n");
}
else if (k > ret)
{
printf("猜大了\n");
}
else
{
printf("恭喜你,猜对了\n");
break;
}
}
}
int main()
{
srand((unsigned int)time(NULL));//定义随机数的生成起点
int input = 0;
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;
}
注意:
srand函数用来定义生成随机数的起点,这里采用的是时间戳,每一次游戏时间起点只需定义一次,所以srand函数定义在主函数一开始即可
关于循环和分支语句的题目就讲到这里,掌握这些题目该章节你的水平将有显著提升!
加油,感谢三连!!!
边栏推荐
- Is it amazing to extract text from pictures? Try three steps to realize OCR!
- Selection of FFT sampling frequency and sampling points
- Syntaxerror resolved: positive argument follows keyword argument
- Jmeter 如何解决乱码问题?
- LeetCode - 寻找两个正序数组的中位数
- 迷惑的单片机矩阵按键
- Intel AI practice day issue 56 | explore new trends in industry development
- LED, nixie tube and key of single chip microcomputer
- 【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
- scrollview、tableView嵌套解决方案
猜你喜欢
![Jerry's prompt sound processing when switching devices [chapter]](/img/b3/38f55143b5ca8c3b2059c5f6f1da37.png)
Jerry's prompt sound processing when switching devices [chapter]

Rational and perceptual activities and required skills in programmers' work

In July, a software testing engineer came to the company. He looked like a hairy boy. He didn't expect to be the new generation of roll King

Basic operations of MySQL database (2) --- Based on data table

Build Release Blogs

Strong collaboration and common development! Intel and Taiyi IOT held a seminar on AI computing box aggregation services

Point divide and conquer analysis

头补零和尾补零对FFT输出结果的影响

Rongyun IM & RTC capabilities on new sites

What are the namespaces and function overloads of + and @ in front of MATLAB folder
随机推荐
Selection of FFT sampling frequency and sampling points
《KMP复习 + AC自动机》前传
ASML launched the first generation HMI multi beam detector: the speed is increased by 600%, which is suitable for 5nm and more advanced processes
Logic of automatic reasoning 07 - predicate calculus
Network device hard core technology insider firewall and security gateway (VIII) virtualization artifact (middle)
mysql分表之后怎么平滑上线?
Network equipment hard core technology insider firewall and security gateway chapter (VI) security double repair under the law
程序员工作中的理性与感性活动及所需的技能素养
Jerry's Bluetooth can only link back to the last device [article]
MySQL limit usage and large paging problem solving
What is the org relationship mitigation strategy of Microsoft edge browser tracking prevention
LED, nixie tube and key of single chip microcomputer
Rendering problems
Leetcode 452. minimum number of arrows to burst balloons (medium)
2020年一季度可穿戴市场出货量达7260万部,苹果独占近三成市场份额
Data analysis: disassembly method (details)
scrollview、tableView嵌套解决方案
LeetCode_ Bit operation_ Medium_ 137. Number II that appears only once
估值360亿美元!即将进行首次载人发射的SpaceX筹资3.46亿美元
FFT 采样频率和采样点数的选取