程序设计的逻辑思维
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void Guess(const char src[])
{
int i
int count = 1//猜次数的次数
int A_num//猜中的A的个数
int B_num//猜中的B的个数
char one, two, three, four//分别表示我们所输入的四个数字
while(1)
{
A_num = B_num = 0
fflush(stdin)
printf("请输入你第%d次猜测的数字(4位不重复):", count++)
scanf(" %c %c %c %c", &one, &two, &three, &four)
for(i = 0i <4++i)
{
if(src[i] == one)
{
if(i == 0)
++A_num
else
++B_num
}
else if(src[i] == two)
{
if(i == 1)
else
}
else if(src[i] == three)
{
if(i == 2)
else
}
else if(src[i] == four)
{
if(i == 3)
else
}
}
printf("------------------------------------------ ")
if(A_num == 4)
{
printf("恭喜你,全部猜对了! ")
break
}
else
{
printf("你猜测的结果是:%dA%dB ", A_num, B_num)
}
}
}
int main()
{
char chNum[4]//用于存放系统产生的随机4位数字
int i, index
srand((unsigned)time(0))//初始化随机种子
index = 0
//system("color F0")
//使用do...while循环让系统产生一个随机的4位数字
do
{
chNum[index] = rand() % 10 + '0'
//判断一下这个数字是否和前面产生的字符相同
for(i = 0i <index++i)
{
if(chNum[i] == chNum[index])
--index
}
++index
}while(index <4)
Guess(chNum)
return 0
}
#include<time.h>#include<Stdlib.h>
#include<stdio.h>
void guessfigure(int x)
{ int magic,guess,i
srand(time(NULL))
magic=rand()%100
for(i=1i<=xi++)
{ printf(" \n \n Please guess a magic number:")
scanf("%d",&guess)
if(guess==magic)
{ printf("\n\n Right! %dtimes",i)
return
}
else if(guess>magic)
printf("\n\n Wrong!Too high!")
else
printf("\n\n Wrong!Too low!")
}
}
int main()
{ int x
printf(" guess figure \n\n")
char k
int i=0
do
{
if(i!=0)
printf("\nwelcome to antother \n")
x=10
guessfigure(x)
printf("\n exit: q go-on:anyone else \n")
scanf("%c",&k)
}while(k!='q')
return 0
}
#include<iostream.h>#include<stdlib.h>
#include<time.h>
void
main()
{
int
m,s,t,flag
char
ch
for()
{
flag=0
s=0
srand(time(0))
do
m=rand()
while(m<0||m>100)
cout<<"我已经想到了一个0-100的整数,请你猜猜看。"<<endl
for()
{
if(flag)break
cin>>t
s++
if(t>m)
cout<<"你猜的数太大了。"<<endl
else
if(t<m)
cout<<"你猜的数太小了。"<<endl
else
{
cout<<"恭喜!你猜对了!你猜了"<<s<<"次"<<endl
cout<<"还想玩吗?(Y/N)"
cin>>ch
if(!(ch=='Y'||ch=='y'))
return
else
flag=1
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)