【005】优化猜数游戏

【题目】改进猜数游戏程序,功能是:

a.允许用户反复输入数,直至猜中程序选定的数。

b.输入的数如果大于选定的数,则提示“太大了”;如果小于选定的数,则提示“太小了”; 如果等于选定的数,则输出“你赢了”并结束程序。

【解答】

#include<iostream>

using namespace std;

int main()

{

int a;

cin>>a;

while(a!=5)

{

if(a<5)

cout<<"太小了"<<endl;

else

cout<<"太大了"<<endl;

cin>>a;

}

cout<<"你赢了!"<<endl;

return 0;

}

时间: 2024-08-08 23:00:14

【005】优化猜数游戏的相关文章

简单猜数游戏2

/*简单猜数游戏,magic number#2,版本*/#include<stdio.h>#include<stdlib.h> int main(void){ int magic; /*magic number*/ int guess; /*user's guess*/ printf("\nWelcome to the magic number game\n"); magic=rand(); /*产生随机数*/ printf("\nGuess the

简单猜数游戏1

/*简单猜数游戏,magic number#1,版本*/#include<stdio.h>#include<stdlib.h> int main(void){ int magic; /*magic number*/ int guess; /*user's guess*/ printf("\nWelcome to the magic number game\n"); magic=rand(); /*产生随机数*/ printf("\nGuess the

模拟算法_掷骰子游戏&amp;&amp;猜数游戏

模拟算法是用随机函数来模拟自然界中发生的不可预测的情况,C语言中是用srand()和rand()函数来生成随机数. 先来介绍一下随机数的生成: 1.产生不定范围的随机数 函数原型:int rand() 产生一个介于0~RAD_MAX间的整数,其具体值与系统有关系.Linux下为2147483647.我们可以在include文件夹中的stdlib.h中可以看到(Linux在usr目录下,Windows在安装目录下) 1 #include<stdio.h> 2 #include<stdlib

OJ_1003.猜数游戏

1003. 猜数游戏 (Standard IO) 时间限制: 1000 ms  空间限制: 262144 KB 题目描述 有一个"就是它"的猜数游戏,步骤如下:请你对任意输入的一个三位数x,在这三位数后重复一遍,得到一个六位数,467-->467467.把这个数连续除以7.11.13,输出最后的商. 输入 输入一个三位数x. 输出 输出最后的商. 样例输入 100 1 #include<stdio.h> 2 int main() 3 { 4 int a,b,c; 5

一个笨拙的猜数游戏代码参考

直接上代码!!! #include <stdio.h> #include <stdlib.h> #define TOP 1000 #define BOTTOM 0 /* 由Mr.Blue and Mr.Green制作于2016.7.31 21:17 本程序采用块状分段,使程序更加简单,但可读性降低,望见谅 */ int main(int argc, char * argv[]) { int toobig, toosmall, temp; char input; printf(&qu

猜数游戏-flag的运用

package my;import java.util.Scanner;public class MyJava {        public static void main(String[] args) {        // TODO Auto-generated method stub        @SuppressWarnings("resource")        Scanner input = new Scanner(System.in);              

BZOJ 2222: [Cqoi2006]猜数游戏【神奇的做法,傻逼题,猜结论】

2222: [Cqoi2006]猜数游戏 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 604  Solved: 260[Submit][Status][Discuss] Description 佳佳和明明玩一个猜数游戏.佳佳想一个1~n之间的整数,明明每次可以随便猜一个数.从第二次猜测起,佳佳告诉明明本次猜测的数和上次猜测的数相比哪个更接近.B表示本次猜测的数更接近,W表示上次猜测的数更接近.如果两次猜测的接近程度一样,则既可回答B也可回答W.

数组常见操作_猜数游戏

数组常见操作: 猜数游戏:从键盘中任意输入一个数据,判断数列中是否包含此数,并记录该数在数组中存在多少次 public class fortyNine { public static void main(String[] args) { Scanner input = new Scanner(System.in); int[] sum = {10,20,21,34,65,76,6,87,98,20}; System.out.println("猜数游戏开始!"); System.out.

猜数游戏

/*猜数游戏,magic number#4,版本*/#include<stdio.h>#include<stdlib.h>#include<time.h>int main(void){ int magic;/*magic number*/ int guess;/*user's number*/ int guessnum,response=1; time_t t; printf("\nWelcome to the magic number game\n"