当前位置:网站首页>鹏哥C语言20210811程序结构作业
鹏哥C语言20210811程序结构作业
2022-07-26 10:36:00 【竹某】
程序结构的课程已经告一段落,在这里写几道作业加以巩固。
Assignment1:
找到指定范围[inf,supper]内含有9的整数。比如91就是十位含有9的整数,901就是百位还有9的整数。
我自己的思路有两种:一种是遍历法,另一种是生成法。遍历法就是从inf到supper一个一个试过去,找到符合条件(含有9)的数;而生成法是找到符合条件的数的生成规律(往往是通项公式),在指定范围内加以生成。最后采用的是遍历法。
#include <stdio.h>
#include <math.h>
int core_find9(int, int);
int computeBit(int);
int find9(int, int);
int main() {
find9(100,10);
return 0;
}
//upper为上界,inf为下界.作用是找到[inf,supper]范围内含有9的数字,并计数.返回总数.
int find9(int upper, int inf) {
/*算法思想是:遍历该范围内的全部的整数,逐个判断它们是否含有9.
核心是判断给定数是否含有9.为此编写core_find9函数,功能如下.
由于这个函数需要指定位数,所以编写computeBit函数,用于指定位数.位数为upper(上界)的位数.
*/
int count = 0;
int bit = computeBit(upper);
for (int i = inf; i <= upper; i++) {
if (core_find9(i, bit) == 1)
++count;
}
return count;
}
//判断给定的数num从第1位到第bit位是否含有9.含有,则返回1;否则,返回0.
//比如令num=901,bit=2.函数返回0,因为1-2位没有含有9.若bit改为3,则返回1.
int core_find9(int num, int bit) {
/*算法思想是:从低位到高位判断有无9存在,一遇到9就返回1.
第k位存在9的条件是:(num%10^k)/(9*10^(k-1))!=0
bit的作用是用于循环次数的控制*/
for (int i = 1; i <= bit; ++i) {
if ((num % ((int)pow(10, i))) / (9 * ((int)pow(10, i - 1))) != 0) {
printf("%d ", num);
return 1;
}
}
return 0;
}
//用于计算指定的数字upper的位数,并返回.
int computeBit(int upper) {
int bit = 1;
while ((upper=upper / 10) > 0) {
++bit;
}
return bit;
}Assignment 2:
猜数游戏。范围1-100(inclusive)。大则报大,小则报小,直至猜中。可重复体验。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef unsigned int u_int;
void game();
void menu();
int main() {
int input = 0;
menu();
do {
game();
printf("你还想玩吗?>:");
scanf("%d",&input);
} while (input);
return 0;
}
//一次游戏
void game() {
int guess = 0;
//这里生成一个随机数,一次游戏只生成一次
srand((u_int)time(NULL));
int random = rand()%100+1;
while (1) {
printf("请输入一个数字");
scanf("%d", &guess);
if (guess>random) {
printf("偏大。");
}
else if (guess < random) {
printf("偏小。");
}
else {
printf("猜对了!");
break;
}
}
}
//每次启动游戏的时候启动一次
void menu() {
printf("******************************\n");
printf("******1.play 0.exit******\n");
printf("******************************\n");
}这里面最重要的知识点是如何生成一个随机数。
void rand(void)是stdlib.h中的函数,它会生成一个一个伪随机数,界于[0, RAND_MAX]之间。之所以叫做伪随机数是应为这个随机数实际上是使用一个种子生成的,种子固定的话多次调用得到的序列也是一样的。为rand()函数设置种子的办法是调用void srand(unsigned int seed)函数,这个seed可以由time.h中的long time(long *)函数提供,这个函数返回时间戳,即现在时间到1970-01-01 00:00:00的时间间隔。
一般使用就为:
srand(time(NULL));
int random = rand();
而:
srand(5);
int random = rand();
无论seed是何种固定值,反复调用rand()函数都会生成一个固定的序列。
使用时应该注意,一个程序中只能有一个srand((unsigned int)time(NULL)).这不是语法硬性规定的,而是从功能上讲的。
首先,一个srand()函数能为多个rand()函数提供seed;
其次,见下列代码:
srand((unsigned int)time(NULL));
x=rand();
srand((unsigned int)time(NULL));
y=rand();
x和y的值往往是相同的,这是因为time(NULL)返回值极有可能是相同的。Assignment 3:
goto的用法。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char string1[20] = {0};
again:
printf("请输入\"我是猪\",否则你的电脑将在1 min内关机!\n");
system("shutdown -s -t 60");
scanf("%s", string1);
if (!strcmp(string1, "我是猪")) {
system("shutdown -a");
}
else {
goto again;
}
return 0;
}
//其实不使用goto也可以实现这个代码。goto的使用要谨慎,否则会扰乱正常的代码执行流程,从而产生bug。
//goto的唯一适合的场所就是跳出深层嵌套。因为break只能跳出一层循环,想要跳出多层循环的话会大大增加代码的冗余度。此时可以使用goto。
/*
for(){
for(){
for(){
if(something wrong)
goto error;
}
}
}
error:
block;
*/
/*
这里使用的system("......");用于执行系统命令。目前碰到过的三个为:
system("cls");清屏(指的是清命令行窗口cmd)
system("shutdown -s -t 60");使电脑在60s后关机
system("shutdown -a");取消关机计划
system函数在stdlib.h中
*/边栏推荐
猜你喜欢
![[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间](/img/e7/a48bb5b8a86cbc4cd5b37bb16661a8.png)
[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间
![[leetcode每日一题2021/2/14]765. 情侣牵手](/img/be/8639a05c733638bf0b3fdeb11abccf.png)
[leetcode每日一题2021/2/14]765. 情侣牵手
![[leetcode每日一题2021/8/31]1109. 航班预订统计【中等】差分数组](/img/9d/5ce5d4144a9edc3891147290e360d8.png)
[leetcode每日一题2021/8/31]1109. 航班预订统计【中等】差分数组

第8期:云原生—— 大学生职场小白该如何学

第5期:大学生入职必备技能之二
![[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】](/img/13/c6cb176d7065035f60d55ad20ed1bf.png)
[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】

Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images

Navicat15 MySQL (centos7) connected to local virtual machine

英语基础句型结构------起源

uniapp使用简单方法signalR(仅用于web调试,无法打包app)
随机推荐
Okaleido生态核心权益OKA,尽在聚变Mining模式
Asynctask < T> decoration and await are not used in synchronous methods to obtain asynchronous return values (asynchronous methods are called in synchronous methods)
MD5加密
Issue 7: how do you choose between curling up and lying flat
常见的类(了解)
Analyze the hybrid construction objects in JS in detail (construction plus attributes, prototype plus methods)
反射机制简述
一文详解Nodejs中fs文件模块与path路径模块
10 令 operator= 返回一个 reference to *this
13 以对象管理资源
[leetcode每日一题2021/4/23]368. 最大整除子集
QRcode二维码(C语言)遇到的问题
[Halcon vision] threshold segmentation
[Halcon vision] Fourier transform of image
C语言计算日期间隔天数
英语基础句型结构------起源
[leetcode每日一题2021/8/31]1109. 航班预订统计【中等】差分数组
GIS方法类期刊和论文的综述(Introduction)怎么写?
剑指Offer(八):跳台阶
MLX90640 红外热成像仪测温传感器模块开发笔记(六)红外图像伪彩色编码