我想在形状中加上可以是一个,两个,三个,四个等数字的数字。尽管如此,额外的空间也会发生变化。此外,中心转移。然而,我尝试不同的方式,我不能。我该如何解决这个问题。谢谢你们所有的赞赏。
输出示例是: 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;
}
答案 0 :(得分:0)
这个可以使用任何数字
#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;
}
答案 1 :(得分:-1)
这应该有效!
它仍有很小的误差,但我试图解决它
/*Import*/
#include <stdio.h>
/*Function Prototyp*/
int countNumber(int number);
int main() {
/*Variable*/
int number, column, row;
int columnCount, rowCount;
int i;
printf("Enter column:\n>");
scanf("%d",&column);
printf("\nEnter row:\n>");
scanf("%d",&row);
printf("\nEnter number for center:\n>");
scanf("%d",&number);
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(number))/2;i++)
printf(" ");
printf("%d", number);
for (i = 0; i < ((row*2-3)-countNumber(number))/2;i++)
printf(" ");
if (countNumber(number % 2 == 0))
printf(" *");
else
printf("*");
break;
}
if (columnCount==0 || columnCount==column-1 || rowCount==0 || rowCount==row-1)
printf("* ");
else
printf(" ");
}
printf("\n");
}
return 0;
}
int countNumber(int number) {
int count;
for (count = 0; number > 0; count++)
number = number / 10;
return count;
}