1.输入三个数判断大小
#include<iostream>
using namespace std;
int main() {
int a = 0;
int b = 0;
int c = 0;
cout << "输入第1只猪的重量:" << endl;
cin >> a;
cout << "输入第2只猪的重量:" << endl;
cin >> b;
cout << "输入第3只猪的重量:" << endl;
cin >> c;
if (a > b)
{
if (a > c)
{
cout << "第1只猪最重" << endl;
}
else
{
cout << "第3只猪最重" << endl;
}
}
else
{
if (b > c)
{
cout << "第2只猪最重" << endl;
}
else
{
cout << "第3只猪最重" << endl;
}
}
}
2.猜数字
#include<iostream>
#include<ctime>
using namespace std;
int main() {
srand((unsigned int)time(NULL)); //每次生成的随机数不同
int n = rand() % 100 + 1; //生成1-100之间的一个随机数
int i = 0;
while (i != n) {
cin >> i;
if (i > n) {
cout << "数字大了" << endl;
}
else if(i < n){
cout << "数字小了" << endl;
}
else {
cout << "猜对了" << endl;
break;
}
}
}
3.判断水仙花数
#include<iostream>
using namespace std;
int main() {
int i = 100;
int sum = 0;
while (i < 1000) {
int a = i % 10; //个位
int b = i / 10 % 10; //十位
int c = i / 100; // 百位
if (i == a * a * a + b * b * b + c * c * c) {
cout << i << endl;
}
i++;
}
}
4.输出100以内和7有关的数字
#include<iostream>
using namespace std;
int main() {
int i = 1;
for (; i < 101; i++) {
if (i % 7 == 0) { //7的倍数
cout << "敲桌子" << endl;
}
else if (i % 10 == 7) { //个位数字是7的数
cout << "敲桌子" << endl;
}
else if (i / 10 == 7) { //十位数字是7的数
cout << "敲桌子" << endl;
}
else {
cout << i << endl;
}
}
}