当前位置:网站首页>C language, a number guessing game
C language, a number guessing game
2022-04-23 07:19:00 【Amyniez】
1.C The language realizes the guessing number game :
#define _CRT_SECURE_NO_WARNINGS
// Guess the number game :
// 1. A random number is automatically generated by the computer
// 2. Guess the number
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game()
{
//1. Generating random numbers
int ret = 0;
int guess = 0;
ret = rand() % 100 + 1;// Generate 1--100 Random number between
//2. Guess the number
while (1)
{
printf(" Please guess a number :");
scanf("%d",&guess);
if (guess > ret)
{
printf(" Guess the \n");
}
else if (guess < ret)
{
printf(" Guess a little \n");
}
else
{
printf(" That's right !\n");
break;
}
}
}
void menu()
{
printf("-----------------------------------\n");
printf("---------1.play 0.exit----------\n");
printf("-----------------------------------\n");
}
int main()
{
int input = 0;
// Use time stamp to set the starting point of random number generation
// Time stamp : Current computer time - Computer start time ( namely 1970 year 1 month 1 Japan 0 when 0 branch 0 second )
srand((unsigned int)time(NULL));//seed rand
do
{
menu();
printf(" Please select :");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice , Please re-enter \n");
break;
}
} while (input);// Not 0 It's true , Start the cycle ;0 For false , End of cycle
return 0;
}
2.python Realize the number guessing game :
import random
guess = random.randint(1,10)
# Number of guesses
i = 1
while True:
print(" The first %d Second guess , Please enter a number "%(i))
try:
temp=int(input())
i+=1
except ValueError:
print(" The input is invalid ")
continue
if temp == guess:
print(" Congratulations , Guessed it ")
break
elif temp < guess:
print(" Guess a little ")
elif temp >guess:
print(" Guess the ")
thus it can be seen ,Python Easier
#include <stdio.h>
//1. Calculation 1/1-1/2+1/3-1/4.......+1/99-1/100 Value , And print it out
int main()
{
int i = 0;
//int sum = 0;
double sum = 0.0;
int flag = 1;
for (i = 1; i <= 100; i++)
{
sum += flag * 1.0 / i;
flag = -flag;
}
printf("%lf\n", sum);
return 0;
}
//2. Find the maximum of ten numbers MAX
int main()
{
int arr[] = {
21, 42, 8, 19, 30, 97, 12, 25, 68, 85 };
int max = arr[0];// Be careful : It can't be written here 0, Because it's possible that all ten numbers are negative , be 0 Is the maximum number Max
int i = 0;
int size = sizeof(arr) / sizeof(arr[0]);// Calculation array How many numbers are there in the
for (i = 1; i < size; i++)
{
if (arr[i]>max)
{
max = arr[i];
}
}
printf("mmax = %d\n", max);
return 0;
}
//3. Output 99 multiplication table formula on the screen
int main()
{
int i = 0;
for (i = 1; i <= 9; i++)
{
int j = 1;
for (j = 1; j <= i; j++)
{
printf("%d*%d=%-2d ", i, j, i*j);//"-" Indicates left alignment , A positive number means right aligned ,2d Indicates that a number occupies two lattices
}
printf("\n");
}
return 0;
}

版权声明
本文为[Amyniez]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230610096610.html
边栏推荐
- launcher隐藏不需要显示的app icon
- c语言编写一个猜数字游戏编写
- Pytorch trains the basic process of a network in five steps
- ArcGIS License Server Administrator 无法启动解决方法
- 第2章 Pytorch基础1
- WebRTC ICE candidate里面的raddr和rport表示什么?
- Viewpager2 realizes Gallery effect. After notifydatasetchanged, pagetransformer displays abnormal interface deformation
- Cancel remote dependency and use local dependency
- MySQL笔记2_数据表
- 组件化学习(3)ARouter中的Path和Group注解
猜你喜欢

BottomSheetDialogFragment 与 ListView RecyclerView ScrollView 滑动冲突问题

C# EF mysql更新datetime字段报错Modifying a column with the ‘Identity‘ pattern is not supported

微信小程序 使用wxml2canvas插件生成图片部分问题记录

取消远程依赖,用本地依赖
![[recommendation of new books in 2021] practical IOT hacking](/img/9a/13ea1e7df14a53088d4777d21ab1f6.png)
[recommendation of new books in 2021] practical IOT hacking

机器学习 三: 基于逻辑回归的分类预测

Record WebView shows another empty pit

常用UI控件简写名

1.2 preliminary pytorch neural network

杂七杂八的学习
随机推荐
Easyui combobox 判断输入项是否存在于下拉列表中
树莓派:双色LED灯实验
“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
第8章 生成式深度学习
Reading notes - activity
Handlerthread principle and practical application
AVD Pixel_ 2_ API_ 24 is already running. If that is not the case, delete the files at C:\Users\admi
ViewPager2实现画廊效果执行notifyDataSetChanged后PageTransformer显示异常 界面变形问题
face_recognition人脸检测
组件化学习(1)思想及实现方式
Recyclerview 批量更新View:notifyItemRangeInserted、notifyItemRangeRemoved、notifyItemRangeChanged
launcher隐藏不需要显示的app icon
机器学习笔记 一:学习思路
MySQL笔记3_约束_主键约束
杂七杂八的学习
【動態規劃】不同路徑2
第5 章 机器学习基础
第3章 Pytorch神经网络工具箱
Component based learning (1) idea and Implementation
【动态规划】三角形最小路径和