C11-5.10循环嵌套(1)
原题链接:37014.笔记汇总2025-05-15 10:43:28
发布于:江苏
一、回文数的判断
#include <iostream>
using namespace std;
int main()
{
int n = 1234;
cin >> n;
int t = n;
int s = 0;
while (n){
s = s*10 + n%10; //4321
n /= 10;
}
// cout << s << endl;
if (t == s) cout<<"YES";
else cout <<"NO";
return 0;
}
/*
n%10
n/=10
12
4*10+3 = 43
s = 43*10 + 2 = 432
*/
二、循环的嵌套
外层循环表示行, 内层循环表示列
#include <iostream>
using namespace std;
int main()
{
int n; cin >>n;
for (int j=1; j<=5; j++){//外层循环
for (int i=1; i<=n; i++){//内层循环
cout << i <<' ';
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for(int i=1; i<=n; i++){
for (int j=1; j<=n-i; j++){ //输出空格
cout <<' ';
}
for (int j=1; j<=i; j++){
cout << "*";
}
cout << endl;
}
return 0;
}
#if 0
图形 5
n = 4 //n代表的是行
*
**
***
****
i行 空格 j列(打印多少个星号)
1 3 1
2 2 2
3 1 3
i n-i i
图形 4
n = 4 //n代表的是行
*
***
*****
*******
i行 j列(打印多少个星号)
1 1
2 3
3 5
i 2*i-1
for (int i=1; i<=n; i++){
for (int j=1; j<=2*i-1; j++){
cout << "*";
}
cout << endl;
}
图形 3
n = 4 //n代表的是行
*
**
***
****
for (int i=1; i<=n; i++){
for (int j=1; j<=i; j++){
cout << "*";
}
cout<< endl;
}
图形 2
n = 4 //n代表的是行
********
********
********
********
int n, m;
// cin >> n >> m;//每行打印m个星号,一共n行
for (int i=1; i<=10; i++){ //外层循环 (行)
for (int j=1; j<=3; j++) { //内层循环 (列)
cout << "*";
}
cout << endl;
}
图形 1
n = 8
********
for (int i=1; i<=n; i++) {
cout << "$";
}
#endif
三、 作业
这里空空如也
有帮助,赞一个