2-28 用穷举法找出1~100的质数并显示出来。分别使用while、do-while、for循环语句实现。
// 使用while循环
#include <iostream>
using namespace std;
int main() {
int number = 2;
cout << "1~100之间的质数有:";
while (number <= 100) {
int divisor = 2;
bool isPrime = true;
while (divisor <= number / 2) {
if (number % divisor == 0) {
isPrime = false;
break;
}
divisor++;
}
if (isPrime) {
cout << number << " ";
}
number++;
}
cout << endl;
return 0;
}
//使用do...while语句
#include <iostream>
using namespace std;
int main() {
int number = 2;
cout << "1~100之间的质数有:";
do {
int divisor = 2;
bool isPrime = true;
do {
if (number % divisor == 0) {
isPrime = false;
break;
}
divisor++;
} while (divisor <= number / 2);
if (isPrime) {
cout << number << " ";
}
number++;
} while (number <= 100);
cout << endl;
return 0;
}
//使用for循环语句
#include <iostream>
using namespace std;
int main() {
cout << "1~100之间的质数有:";
for (int number = 2; number <= 100; ++number) {
bool isPrime = true;
for (int divisor = 2; divisor <= number / 2; ++divisor) {
if (number % divisor == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
cout << number << " ";
}
}
cout << endl;
return 0;
}
2-30 声明一个表示时间的结构体,可以精确表示年、月、日、小时、分、秒;提示用户输入年、月、小时、分、秒的值,然后完整地显示出来。
#include <iostream>
using namespace std;
// 定义表示时间的结构体
struct Time {
int year;
int month;
int day;
int hour;
int minute;
int second;
};
int main() {
// 创建时间结构体变量
Time time;
// 提示用户输入时间信息
cout << "请输入年份: ";
cin >> time.year;
cout << "请输入月份: ";
cin >> time.month;
cout << "请输入日期: ";
cin >> time.day;
cout << "请输入小时: ";
cin >> time.hour;
cout << "请输入分钟: ";
cin >> time.minute;
cout << "请输入秒数: ";
cin >> time.second;
// 显示完整的时间信息
cout << "输入的时间为:" << time.year << "年" << time.month << "月" << time.day << "日 "
<< time.hour << "时" << time.minute << "分" << time.second << "秒" << endl;
return 0;
}
2-31 在程序中定义一个整型变量,赋以1~100的值,要求用户猜这个数,比较两个数的大小,把结果提示给用户,直到猜对为止。分别使用while、do...while语句实现循环。
//使用while循环
#include <iostream>
using namespace std;
int main() {
int targetNumber = 42; // 要猜的数字
int userGuess;
cout << "猜一猜1~100之间的数字: ";
// 使用while循环
while (true) {
cin >> userGuess;
if (userGuess == targetNumber) {
cout << "恭喜你,猜对了!" << endl;
break; // 结束循环
} else if (userGuess < targetNumber) {
cout << "猜的数字太小了,请再试一次: ";
} else {
cout << "猜的数字太大了,请再试一次: ";
}
}
return 0;
}
// 使用do...while语句
#include <iostream>
using namespace std;
int main() {
int targetNumber = 42; // 要猜的数字
int userGuess;
cout << "猜一猜1~100之间的数字: ";
// 使用do...while循环
do {
cin >> userGuess;
if (userGuess == targetNumber) {
cout << "恭喜你,猜对了!" << endl;
} else if (userGuess < targetNumber) {
cout << "猜的数字太小了,请再试一次: ";
} else {
cout << "猜的数字太大了,请再试一次: ";
}
} while (userGuess != targetNumber);
return 0;
}
2-32 口袋中有红、黄、蓝、白、黑5种颜色的球若干个。每次从口袋种取出3个不同颜色的球,问有多少种取法?
#include <iostream>
#include <string>
using namespace std;
int main() {
const int totalColors = 5; // 总颜色数
const int ballsPerDraw = 3; // 每次取出的球数
string colors[] = {"红", "黄", "蓝", "白", "黑"};
int combinationCount = 0;
// 循环遍历所有可能的排列
for (int color1 = 0; color1 < totalColors; ++color1) {
for (int color2 = 0; color2 < totalColors; ++color2) {
for (int color3 = 0; color3 < totalColors; ++color3) {
if (color1 != color2 && color1 != color3 && color2 != color3) {
// 输出当前排列和编号
cout << combinationCount + 1 << " ";
cout << colors[color1] << " " << colors[color2] << " " << colors[color3] << endl;
combinationCount++;
}
}
}
}
// 输出总的排列数
cout << "总共有 " << combinationCount << " 种取法。" << endl;
return 0;
}
2-33 输出九九乘法表
#include<bits/stdc++.h>
using namespace std;
//打印九九乘法表
int main(){
int i=0;
int j=0;
for(i=1;i<=9;i++){
for(j=1;j<=i;j++){
cout<<j<<"*"<<i<<"="<<i*j<<" ";
}
cout<<endl;
}
return 0;
}