gpt4 book ai didi

c - 将数字放入空心形状中

转载 作者:行者123 更新时间:2023-11-30 18:13:20 24 4
gpt4 key购买 nike

我想在形状中放入可以是一、二、三、四等数字的数字。尽管如此,额外的空间还是被转移了。此外,中心发生了转移。我尝试了不同的方法,但是,我不能。我该如何解决这个问题。感谢大家的赞赏回答。

输出示例是: http://i.stack.imgur.com/Kozr1.png

#include <stdio.h>

/* Function Prototypes */
int countNumber(int number);

int main() {

int n; /* number to put in the center of the shape */
int column, row; /* take values of column and row via user */
int columnCount, rowCount; /* count number of column and row */
int i; /* count how many spaces remained */

printf("Enter column:\n>");
scanf("%d",&column);
printf("\nEnter row:\n>");
scanf("%d",&row);
printf("\nEnter number for center:\n>");
scanf("%d",&n);

if((10<column && column<40) && (10<row && row<40)){
for (columnCount = 0; columnCount < column; columnCount++) {

for (rowCount = 0; rowCount < row; rowCount++) {

/* Middle Row */
if (columnCount == column/2) {
printf("#");

for (i = 0; i < ((row*2-3)-countNumber(n))/2;i++)
printf(" ");

printf("%d", n);

for (i = 0; i < ((row*2-3)-countNumber(n))/2;i++)
printf(" ");

printf("#");
break;
}

if (columnCount==0 || columnCount==column-1 || rowCount==0 || rowCount==row-1)
printf("# ");
else
printf(" ");

}
printf("\n");
}
}
else
printf("Please enter value btw 10-40");
return 0;
}
/* Function */
int countNumber(int number) {

int count;

for(count=0;0<number;count++) /* the number how many decimal places have */
number/=10;

return count;
}

最佳答案

这个适用于任何号码

#include <stdio.h>
int digits(int n)
{
int i;
for(i=0;n;i++,n=n/10);
return i;
}
int main()
{
int i,j;
int column,row;
int n,cnt;

printf("Enter column: ");
scanf("%d",&column);
printf("Enter row: ");
scanf("%d",&row);
scanf("%d",&n);
cnt=digits(n);

for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
if(i==0 || i==(row-1) || j==0 || j==(column-1))
printf("*");
else if((i==(row/2)) && (j==((column-cnt)/2)))
{
printf("%d",n);
j=j+(cnt-1);
}
else
printf(" ");
}
printf("\n");
}
return 0;
}

关于c - 将数字放入空心形状中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26201895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com