引用 | 编辑
choan76
2007-06-26 20:16 |
楼主
▼ |
||
x0
1.是利用乱数产生10~50只间五个不虫负的整数放入阵列中,并将结果白续后显示在表单上。2.试写猜数字游戏 开始执行时min=0 max=100 输入职内轻成空白,并 .. 访客只能看到部份内容,免费 加入会员 x0
|
引用 | 编辑
Inndy
2010-02-05 20:42 |
1楼
▲ ▼ |
大概就是这样吧...
不过我不会用MessageBox.... 我是初学者~XD PS:我用的编译器是Dev-C++ 4.9.9.2 复制程式 #include <iostream.h> int main() { //初始化变数 int min=0,max=100,guess,guess_time=0,stop=0,rand_num=rand()%100+1; std::string again; cout << "====================" << endl; cout << " C++ 猜数字" << endl; cout << "====================" << endl << endl; while(stop==0){ cout << " Max=" << max << " ; Min=" << min << " ;" << endl; cout << " I Guess :"; cin >> guess; cout << endl; if(guess==rand_num&&guess_time==0) { cout << "真厉害!! 一次就猜中!" << endl; cout << "另起新局? (Y/N)" << endl; cout << " ------>"; cin >> again; if(again=="Y"||again=="y") { min=0; max=100; guess_time=0; stop=0; rand_num=rand()%100+1; } else { break; } } else if(guess==rand_num) { cout << "获胜!!! 您一共猜了" << guess_time << "次!" << endl; cout << "另起新局? (Y/N)" << endl; cout << " ------>"; cin >> again; if(again=="Y"||again=="y") { min=0; max=100; guess_time=0; stop=0; rand_num=rand()%100+1; } else { break; } } else if(guess>rand_num) { if(guess<max){max=guess;} guess_time++; } else if(guess<rand_num) { if(guess>min){min=guess;} guess_time++; } } } x0 |
引用 | 编辑
翁赐维
2010-02-07 15:32 |
2楼
▲ ▼ |
这是第一个程式码,第二个我再写看看
你先参考看看吧!! #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() { srand(time(0)); //产生乱数种子 int random[5]; for(int i=0;i<5;i++) { random=rand()%50; //先把乱数存入阵列里 if(random < 10) //再判断是否再有大于10 random=random+10; //若是小于10,则加上10; } for(int i=0;i<5;i++) cout << random << " "; system("pause"); return 0; } x0 |
引用 | 编辑
翁赐维
2010-02-07 17:21 |
3楼
▲ ▼ |
这个是第二个程式码
因为我觉得还满简单的,所以我就没写注解了 另外,你的题目有点看不懂,所以我写的可能跟你要的不一样 #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() { srand(time(0)); //产生乱数种子 int y; y=rand()%100; int min=0,max=100,guess; int time=0; char try_again; do { cout << "*************************" << endl; cout << " c++ 猜数字游戏 " << endl; cout << "*************************" << endl; cout << "\n"; cout << "输入数字 : "; cin >> guess; if(guess == y) { cout << "一次就答对了 !!" << endl; cout << "Try again ?(y/n) "; cin >> try_again; } else { cout << "再输入一次" << endl; int i=0; for(i=0;i>=0;i++) { cout << "输入数字 : "; cin >> guess; if(guess == y) break; else { if(guess > y) { cout << "小一点" << endl; if(guess < max) max=guess; } else if(guess < y) { cout << "大一点" << endl; if(guess > min) min=guess; } cout << "介于" << min << "和" << max << "之间" << endl; } } cout << "你总共输入" << i+2 << "次" << endl; cout << "Try again ?(y/n)"; cin >> try_again; } }while(try_again == 'Y' || try_again == 'y'); system("pause"); return 0; } x0 |
引用 | 编辑
backtolive
2010-02-12 10:23 |
4楼
▲ |
2楼的Code有一点点不妥
使用 rand()%50 之后再去判断并不是合理作法 因为虽然说随机,但是按照你的作法10~19的机会比20~50的机会大 10~50 应该使用 10 + rand()%41 随机范围刚好就是10~50 另外也少写了判断不重复的部份 抱歉抄了一下你的Code 复制程式 #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() { srand(time(0)); int random[5]; for(int i=0;i<5;i++) { GetNumber: random[i]=10+rand()%41; for(int j=0;j<i;j++) { if (random[i] == random[j]) goto GetNumber; } } for(int i=0;i<5;i++) cout << random[i] << " "; system("pause"); return 0; } 理论上应该使用function call然后加上递回会比较好 不过懒得写了 x0 |