从顶部的最高数字开始在 C 中打印数字金字塔
printing numeric pyramid in C starting with the highest number at top
我已经在网站上和互联网上搜索过关于这种金字塔的信息,但没有找到与之相近的东西,我是编码新手,无法通过 myslef 弄清楚我 guess.I 已经尝试过修改其他代码以获得我想要的结果但没有成功。
我必须编写打印金字塔的代码,从顶部输入的数字开始,然后逐渐扩展为以下数字。
示例输入:5
输出:
55
4554
345543
23455432
1234554321
我能把金字塔问题的一半说到底
#include <stdio.h>
#include <math.h>
int main(){
int i,j,num;
printf("number:");
scanf("%d",&num);
for(i=1;i<=num;i++){
j=num;
for(j;j>=i;j--){
printf("%d",j);
}printf("\n");
}
return 0;
}
输出:
number:5
54321
5432
543
54
5
对于另一半,我试图通过循环中的小改动来镜像它,但是
我不知道如何去掉一些数字
这是代码:
#include <stdio.h>
#include <math.h>
int main(){
int i,j,num,k;
printf("number:");
scanf("%d",&num);
for(i=1;i<=num;i++){
for(k=1;k<=num;k++){
printf("%d",k);
}
j=num;
for(j;j>=i;j--){
printf("%d",j);
}printf("\n");
}
return 0;
}
输出:
number:5
1234554321
123455432
12345543
1234554
123455
#include <stdio.h>
int baseNum; //These global variables exist just to help the beginners to
int col; //read the program and understand the logic involved
void print() { //In the real word col and baseNum is passed to print
if (col < baseNum)
printf(" ");
else
printf("%d", col);
}
int main() {
int num;
printf("number:"); scanf("%d",&num); //num < 11
baseNum = num;
for (int line = 0; line < num; ++line) {
col = 1;
do { //Left line side
print();
} while (++col <= num);
do { //Right line side
--col;
print();
} while (col);
--baseNum;
printf("\n");
}
return 0;
}
我的五分钱。:)
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const int UPPER_LIMIT = 10;
printf( "Enter a non-negative integer number less than %d (0 - exit): ",
UPPER_LIMIT );
int n;
if ( scanf( "%d", &n ) != 1 || n <= 0 ) break;
if ( !( n < UPPER_LIMIT ) ) n = UPPER_LIMIT - 1;
putchar( '\n' );
for ( int i = 0; i < n; i++ )
{
printf( "%*d", n - i, n - i );
int j = n - i;
while ( j++ != n ) putchar( '0' + j );
while ( j-- != n - i ) putchar( '0' + j );
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
程序输出可能如下所示
Enter a non-negative integer number less than 10 (0 - exit): 9
99
8998
789987
67899876
5678998765
456789987654
34567899876543
2345678998765432
123456789987654321
Enter a non-negative integer number less than 10 (0 - exit): 8
88
7887
678876
56788765
4567887654
345678876543
23456788765432
1234567887654321
Enter a non-negative integer number less than 10 (0 - exit): 7
77
6776
567765
45677654
3456776543
234567765432
12345677654321
Enter a non-negative integer number less than 10 (0 - exit): 6
66
5665
456654
34566543
2345665432
123456654321
Enter a non-negative integer number less than 10 (0 - exit): 5
55
4554
345543
23455432
1234554321
Enter a non-negative integer number less than 10 (0 - exit): 4
44
3443
234432
12344321
Enter a non-negative integer number less than 10 (0 - exit): 3
33
2332
123321
Enter a non-negative integer number less than 10 (0 - exit): 2
22
1221
Enter a non-negative integer number less than 10 (0 - exit): 1
11
Enter a non-negative integer number less than 10 (0 - exit): 0
我已经在网站上和互联网上搜索过关于这种金字塔的信息,但没有找到与之相近的东西,我是编码新手,无法通过 myslef 弄清楚我 guess.I 已经尝试过修改其他代码以获得我想要的结果但没有成功。
我必须编写打印金字塔的代码,从顶部输入的数字开始,然后逐渐扩展为以下数字。
示例输入:5
输出:
55
4554
345543
23455432
1234554321
我能把金字塔问题的一半说到底
#include <stdio.h>
#include <math.h>
int main(){
int i,j,num;
printf("number:");
scanf("%d",&num);
for(i=1;i<=num;i++){
j=num;
for(j;j>=i;j--){
printf("%d",j);
}printf("\n");
}
return 0;
}
输出:
number:5
54321
5432
543
54
5
对于另一半,我试图通过循环中的小改动来镜像它,但是 我不知道如何去掉一些数字 这是代码:
#include <stdio.h>
#include <math.h>
int main(){
int i,j,num,k;
printf("number:");
scanf("%d",&num);
for(i=1;i<=num;i++){
for(k=1;k<=num;k++){
printf("%d",k);
}
j=num;
for(j;j>=i;j--){
printf("%d",j);
}printf("\n");
}
return 0;
}
输出:
number:5
1234554321
123455432
12345543
1234554
123455
#include <stdio.h>
int baseNum; //These global variables exist just to help the beginners to
int col; //read the program and understand the logic involved
void print() { //In the real word col and baseNum is passed to print
if (col < baseNum)
printf(" ");
else
printf("%d", col);
}
int main() {
int num;
printf("number:"); scanf("%d",&num); //num < 11
baseNum = num;
for (int line = 0; line < num; ++line) {
col = 1;
do { //Left line side
print();
} while (++col <= num);
do { //Right line side
--col;
print();
} while (col);
--baseNum;
printf("\n");
}
return 0;
}
我的五分钱。:)
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const int UPPER_LIMIT = 10;
printf( "Enter a non-negative integer number less than %d (0 - exit): ",
UPPER_LIMIT );
int n;
if ( scanf( "%d", &n ) != 1 || n <= 0 ) break;
if ( !( n < UPPER_LIMIT ) ) n = UPPER_LIMIT - 1;
putchar( '\n' );
for ( int i = 0; i < n; i++ )
{
printf( "%*d", n - i, n - i );
int j = n - i;
while ( j++ != n ) putchar( '0' + j );
while ( j-- != n - i ) putchar( '0' + j );
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
程序输出可能如下所示
Enter a non-negative integer number less than 10 (0 - exit): 9
99
8998
789987
67899876
5678998765
456789987654
34567899876543
2345678998765432
123456789987654321
Enter a non-negative integer number less than 10 (0 - exit): 8
88
7887
678876
56788765
4567887654
345678876543
23456788765432
1234567887654321
Enter a non-negative integer number less than 10 (0 - exit): 7
77
6776
567765
45677654
3456776543
234567765432
12345677654321
Enter a non-negative integer number less than 10 (0 - exit): 6
66
5665
456654
34566543
2345665432
123456654321
Enter a non-negative integer number less than 10 (0 - exit): 5
55
4554
345543
23455432
1234554321
Enter a non-negative integer number less than 10 (0 - exit): 4
44
3443
234432
12344321
Enter a non-negative integer number less than 10 (0 - exit): 3
33
2332
123321
Enter a non-negative integer number less than 10 (0 - exit): 2
22
1221
Enter a non-negative integer number less than 10 (0 - exit): 1
11
Enter a non-negative integer number less than 10 (0 - exit): 0