当前位置:网站首页>鹏哥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/4/23]368. 最大整除子集
- C语言计算日期间隔天数
- algorithm
- json_ object_ put: Assertion `jso->_ ref_ count > 0‘ failed. Aborted (core dumped)
- Issue 5: the second essential skill for College Students
- The problem of large fluctuation of hx711 data
- 20210807#1 C语言程序结构
- Dry goods likeshop takeout order system is open source, 100% open source, no encryption
- 第8期:云原生—— 大学生职场小白该如何学
- Introduction to data analysis | kaggle Titanic mission
猜你喜欢
【机器学习小记】【人脸识别】deeplearning.ai course4 4th week programming
Our Web3 entrepreneurship project is yellow
文案秘籍七步曲至----文献团队协作管理
[leetcode每日一题2021/4/29]403. 青蛙过河
第6期:大学生应该选择哪种主流编程语言
Redis docker instance and data structure
Uniapp uses the simple method signalr (only for web debugging, cannot package apps)
sigmod 函数与softmax 函数对比
The problem of large fluctuation of hx711 data
hx711 数据波动大的问题
随机推荐
Analysis of the transaction problem of chained method call
C language callback function
MD5加密
Some web APIs you don't know
oracle 启动不了 tnslistener服务启动不了
数据库函数
第6期:大学生应该选择哪种主流编程语言
数据分析入门 | kaggle泰坦尼克任务
algorithm
Write to esp8266 burning brush firmware
SuperMap IClient for Leaflet 加载高斯克吕格投影三度分带CGCS2000大地坐标系WMTS服务
JS对象赋值问题
剑指Offer(二十):包含min函数的栈
Redis Docker实例与数据结构
构造器、方法重载、对象数组和static
剑指Offer(五):用两个栈实现队列
L2-005 set similarity (intersection of vector and set)
Inheritance method of simplified constructor (I) - combined inheritance
Agenda express | list of sub forum agenda on July 27
QRcode二维码(C语言)遇到的问题