#include <iostream>
#include <ctime>
using namespace std;
int main()
{
//添加随机种子
srand((unsigned int)time(NULL));
int num = rand() % 100 + 1;
int n = 0;
while (true)
{
cout << "开始猜数" << endl;
cin >> n;
if (n > num) {
cout << "过大了" << endl;
}
else if (n < num)
{
cout << "过小了" << endl;
}
else
{
cout << "恭喜你,猜对了" << endl;
break;
}
}
}
水仙花数
内容 什么是水仙花数,举个例子153,111+555+333=153
实现 输入一个数,判断是不是水仙花数;打印出所有三位数的水仙花数
#include <iostream>
using namespace std;
int main()
{
int n;
int a, b, c, m;
cout << "请输入数字猜测是否是水仙花数" << endl;
cin >> n;
if (n >= 100 && n <= 999) {
a = n % 100 %10;
b = n / 10 % 10;
c = n / 10 / 10;
m = a * a * a + b * b * b + c * c * c;
/* cout << a << endl;
cout << b<< endl;
cout << c << endl;
cout << m << endl;*/
if (m == n) {
cout << "输入的数是水仙花数" << endl;
}
else
{
cout << "输入的数不是水仙花数" << endl;
}
}
cout << "---------------------" << endl;
cout << "打印出三位数中所有的水仙花数" << endl;
int i = 100;
do
{
a = i % 100 % 10;
b = i / 10 % 10;
c = i / 10 / 10;
m = a * a * a + b * b * b + c * c * c;
if (m == i) {
cout << i << endl;
}
i++;
} while (i <1000);
}
猜数内容输入一个数,和系统产生的数进行比较,看你猜的数字是否正确!实现可以进行猜数并且对猜数的大小进行提示,直到你猜对数!#include <iostream>#include <ctime>using namespace std;int main(){ //添加随机种子 srand((unsigned int)time(NULL)); int num = rand() % 100 + 1; int n = 0; while (true) {