1. for循环
2. for循环补充知识
3. 彻底搞定for循环
99乘法练习
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::setfill('0');
for (int i{}; i < 10; i++) {
for (int j{}; j < 10; j++) {
//if (i * j == 56) goto tip;
std::cout << std::setw(2) << i * j << " ";
}
std::cout << std::endl;
}
//tip:
return 0;
}
1000以内所有的质数
#include <iostream>
#include <iomanip>
int main()
{
for (int x = 1; x <= 1000; x++) {
bool is_prime = true;
for (int i = 2; i * i <= x; i++)
{
if (x % i == 0) {
is_prime = false;
break;
}
}
if(is_prime)
std::cout << x << std::endl;
}
return 0;
}
采购方案
#include <iostream>
#include <iomanip>
int main()
{
//第一种
for (int a = 0; a <= 100; a++) {
for (int b = 0; b <= 100; b++) {
for (int c = 0; c <= 100; c++)
{
if ((a + b + c) == 100 && (a * 500 + b * 300 + c * 100 / 3) == 10000) {
std::cout << "高级桌子: " << a << " 普通桌子: " << b << " 椅子: " << c << std::endl;
}
}
}
}
std::cout << std::endl;
//第二种
int acount = 10000 / 500;
int bcount = 10000 / 300;
for (int a = 0; a < acount; a++)
{
for (int b = 0; b < bcount; b++) {
int c = 100 - a - b;
if (c % 3 != 0) continue;
if(a * 500 + b * 300 + c * 100 / 3 == 10000)
std::cout << "高级桌子: " << a << " 普通桌子: " << b << " 椅子: " << c << std::endl;
}
}
std::cout << std::endl;
for (int num{}; num <= 3; num++) {
std::cout << "高级桌子: " << num * 4 << " 普通桌子: " << 25 - num * 7 << " 椅子:" << 75 + 3 * num << std::endl;
}
return 0;
}
打印等级经验
#include <iostream>
#include <iomanip>
#include <conio.h>
int main()
{
char c = _getch();
if ((c == 'l') || (c == 'L')) {
for (int lv{ 1 }; lv <= 100; lv++)
{
std::cout << std::setfill(' ');
std::cout << "\n等级:" << std::setw(3) << lv << " 所需经验:" << std::setw(10) << lv * lv * 100 << std::endl;
}
}
return 0;
}