1.c++简单猜数字游戏#day1#
进行猜 1~100 的数字,若猜数字不正确则提示猜数字是大了还是小了,直到猜中数字则结束本局。
#include#include #include #include using namespace std; int main() { //添加随机数种子,作用利用当前系统时间生成随机数,防止每次随机都是固定数字 srand((unsigned int)time(NULL));//跟随系统时间发生变化达到随机效果 //1.系统生成随机数 //2.玩家进行猜测 //3.判断玩家猜测数 int c = rand() % 100 +1; // rand()%100 生成0-99之间的随机数,在后面加就把范围右移到1-100 cout << "生成的随机数为:" << c << endl; int val = 0; while (1) { cin >> val; if (val > c) { cout << "dale" << endl; //大了 } else if (val < c) { cout << "xiaole" << endl;///小了 } else { cout << "yes" << endl; break;//此处break达成条件自动跳出while循环 游戏结束 } } return 0; }
2.c++经典100-1000内水仙花数
“水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身。例如:1^3 + 5^3+ 3^3 = 153。”
#includeusing namespace std; int main() { int number = 100,a,b,c;//定义起始值最小三位数100 do { a = number % 10; //个位 b = number / 10 % 10; //十位 c = number / 100; //百位 if (a*a*a+b*b*b+c*c*c == number) cout << number << endl; number++; } while (number < 1000); return 0; }
3.c++ 练习案例敲桌子
#includeusing namespace std; int main() { int i,a,b;//设立循环1-100 for (i = 1; i <= 100; i++) { if (i % 10 == 7) cout << "qiaozhuozi" << endl; else if (i / 10 == 7) cout << "qiaozhuozi" << endl; else if (i % 7 == 0) cout << "qiaozhuozi" << endl; else cout << i << endl; } return 0; }



