复兴无基础第十二课 循环嵌套
2025-09-07 20:16:22
发布于:上海
T1m次1~n
#include <iostream>
using namespace std;
int main() {
int m, n;
cin >> m >> n;
for (int i = 1; i <= m; i++) { //外层的for循环控制输出次数
for (int j = 1; j <= n; j++) { //内层的for循环输出1到n
cout << j << " ";
}
cout << endl;
}
return 0;
}
T2探险之旅——第三扇门
#include <iostream>
#include <string>
using namespace std;
int main() {
for (int i = 1;i <= 4; i++){
for (int j = 1; j <= i; j++){
cout << "*";
}
cout << endl;
}
return 0;
}
T3探险之旅——第四扇门
#include <iostream>
#include <string>
using namespace std;
int main() {
for (int i = 1;i <= 4; i++){
for (int j = 1; j <= 4 - i; j++){
cout << " ";
}
for (int j = 1; j <= i; j++){
cout << "*";
}
cout << endl;
}
return 0;
}
T4探险之旅——第五扇门
#include <iostream>
#include <string>
using namespace std;
int main() {
for (int i = 1;i <= 5; i++){
for (int j = 1; j <= 5 - i; j++){
cout << " ";
}
for (int j = 1; j <=2*i-1; j++){
cout << "*";
}
cout << endl;
}
return 0;
}
T5打印三角形1
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 2 * i - 1; j++) {
cout << '*';
}
cout << endl;
}
return 0;
}
T6数1的个数
#include <iostream>
using namespace std;
int main() {
int n, cnt = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
int x = i; //用x保存i的值
while (x) { //如果x的个位为1,则计数
if (x % 10 == 1) {
cnt++;
}
x /= 10;
}
}
cout << cnt;
return 0;
}
T7输出正方形1
#include <iostream>
using namespace std;
int main() {
int n; // 列数
char c; // 画图的字符
cin >> n >> c;
int m = n / 2; // 行数
// 第 1 行,输出 n 个 c
for (int j = 1; j <= n; j++) {
cout << c;
}
cout << endl;
// 第 2 行 ~ 第 m-1 行,每一行:先输出一个 c,然后输出 n-2 个空格,再输出 一个 c
for (int i = 2; i <= m - 1; i++) {
cout << c;
for (int j = 1; j <= n - 2; j++) {
cout << " ";
}
cout << c << endl;
}
// 第 m 行,输出 n 个 c
for (int j = 1; j <= n; j++) {
cout << c;
}
return 0;
}
T8整数各位之和
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
int sum = 0;
for (int i = a; i <= b; i++) { // 从 a 数到 b
// 把 i 这个数字进行分解,加到 sum 中
int t = i;
while (t > 0) {
sum += t % 10; // 得到当前 t 的个位,并加到 sum 中
t /= 10; // 把刚刚得到的个位去掉
}
}
cout << sum;
return 0;
}
T9菱形的一半
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
int m = (n + 1) / 2; //菱形上三角的高度
for(int i = 1; i <= m; i++){ //1~m行,每一行有i个*
for(int j = 1; j <= i; j++){
cout << "*";
}
cout << endl;
}
for(int i = 1; i <= m - 1; i++){ //菱形的下三角1~m-1行,每一行有m-i个*
for(int j = 1; j <= m - i; j++){
cout << "*";
}
cout << endl;
}
return 0;
}
T10区间变化
#include <iostream>
using namespace std;
int main() {
int a[1010];
int n, k, l, r, t;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= k; i++) {
cin >> l >> r >> t;
for (int j = l; j <= r; j++) {
a[j] += t;
}
}
for (int i = 1; i <= n; i++) {
cout << a[i] << " ";
}
return 0;
}
T11水仙花数
#include <iostream>
using namespace std;
int main() {
for (int a = 1; a <= 9; a++) {
for (int b = 0; b <= 9; b++) {
for (int c = 0; c <= 9; c++) {
if (a * a * a + b * b * b + c * c * c == a * 100 + b * 10 + c) {
cout << a << b << c << endl;
}
}
}
}
return 0;
}
T12四叶玫瑰花数
#include <iostream>
#include <cmath>
using namespace std;
int main() {
for (int a = 9; a >= 1; a--) {
for (int b = 9; b >= 0; b--) {
for (int c = 9; c >= 0; c--) {
for (int d = 9; d >= 0; d--) {
if (pow(a, 4) + pow(b, 4) + pow(c, 4) + pow(d, 4) == a * 1000 + b *100 + c * 10 + d) {
cout << a << b << c << d << endl;
}
}
}
}
}
return 0;
}
这里空空如也
有帮助,赞一个