在C中打印数字钻石

时间:2016-02-16 08:39:55

标签: c

int print_pattern(){
    int x;
    int y;
    int i;
    //for loop for the bottom and the top, 0 is the top and 1 is the bottom while it stops at anything above 2.
    for (i = 0; i<2;i++){
        //loop to the current number
        for (x=1;x<=input;x+=2){
            // top or bottom, this is the top because i had made the top of the diamond the 0
            // therefore this makes my diamond print the top of the function.
            if ( i == 0){
                //starts for the top of the diamond. and counts the spaces.
                for (y=1; y<=input-x; y++){
                    printf(" ");
                }
                //starts the printing of the diamond.
                for (y=1; y<2*x;y++){
                    printf("%d ", y);
                }
            }
            //bottom of the diamond, which is from the 1. For this spot it take in the one from the for loop to
            // this if statement.
            if (i==1){
                //counting spaces again
                for(y = 1; y<=x; y++){
                    printf(" ");
                }
                //printing again but this is the bottom of the pyramid. #really need to comment more
                for(y = 1; y<(input-x)*2;y++){
                    printf("%d ", y);
                }
            }
            //next line starts when printing out the numbers in the output.
            printf("\n");
        }
    }
}

输出应该看起来像数字的菱形,每行以奇数号结尾。但它输入的数字是+2,然后也不打印最后一行。哪个应该有一个。

                       1                     1
                     1 2 3                 1 2 3 4 5
                   1 2 3 4 5             1 2 3 4 5 6 7 8 9
                     1 2 3                1 2 3 4 5 6 7
                       1                    1 2 3

左边是预期的,右边是我输入5时的目标。

4 个答案:

答案 0 :(得分:5)

由于您已在上半部分将 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "ID,FirtsName,LastName,PhoneNumber")] Customers customers) { if (ModelState.IsValid) { customers.UserID = User.Identity.GetUserId(); db.Customers.Add(customers); db.SaveChanges(); return RedirectToAction("Index"); } return View(customers); } 增加2,因此您无需让打印循环运行到x。它应该只能运行到y<2*x

下半部分的打印循环受x可能应为y<(input-x)*2的影响(您希望每次打印少于2张)。

一般来说,我会尝试以更具说服力的方式命名变量,例如y<input-x*2printStartPosition等。这样可以通过惊人的余量来理解程序。

作为增强功能,取决于maxNumToPrint循环内i值的两个代码块在结构上非常相似。可以尝试利用它并将它们折叠成一个函数,该函数获得一个布尔参数,如“升序”,当为真时递增x,在假时递减它。是否可以改善或阻碍可读性。

另外,如果可能,请将变量保持为本地。

答案 1 :(得分:3)

彼得施耐德已在答案中提出了一些有效的观点。

考虑一下打印高度为5的钻石时你需要做什么:

  • 打印1居中;
  • 打印1 2 3居中;
  • 打印1 2 3 4 5居中;
  • 打印1 2 3居中;
  • 打印1居中。

你可以编写一个函数来打印一行中的数字从1到 n 并用 n = 1,3,5,3,1来调用它这可以通过两个独立的循环来实现,一个递增 n 乘以2,另一个递减它。

另一种方法是递归:在深入时打印线条,将 n 递增2,直到达到目标宽度,此时您不会递归,但返回并打印你上去时再次使用相同的参数。除了中间的一行之外,这将打印两行。

这是一个递归解决方案:

#include <stdlib.h>
#include <stdio.h>

void print_line(int i, int n)
{
    int j;

    for (j = i; j < n; j++) putchar(' ');
    for (j = 0; j < i; j++) printf("%d ", (j + 1) % 10);
    putchar('\n');
}

void print_pattern_r(int i, int n)
{
    print_line(i, n);                   // print top as you go deeper
    if (i < n) {
        print_pattern_r(i + 2, n);      // go deeper
        print_line(i, n);               // print bottom as you return
    }
}

void print_pattern(int n)
{
    if (n % 2 == 0) n++;                // enforce odd number
    print_pattern_r(1, n);              // call recursive corefunction
}

int main(int argc, char **argv)
{
    int n = 0;

    if (argc > 1) n = atoi(argv[1]);    // read height from args, if any
    if (n <= 0) n = 5;                  // default: 5

    print_pattern(n);

    return 0;
}

答案 2 :(得分:1)

A JAVA STAR PATTERN PROGRAM FOR DIAMOND SHAPE转换为C程序。代码注释将解释变化和流程。

#include <stdio.h>
#include <string.h>

void myprintf(const char* a) {
    static int iCount = 0;
    if (strcmp(a, "\n") == 0) {
        iCount = 0; //if it is new line than reset the iCount
        printf("\n"); //And print new line
    } else            
        printf(" %d", ++iCount); //Print the value
}

void main() {
    int i, j, m;
    int num = 5; //Enter odd number
    for (i = 1; i <= num; i += 2) { //+=2 to skip even row generation
        for (j = num; j >= i; j--)
            printf(" ");
        for (m = 1; m <= i; m++) 
            myprintf(" *"); //display of star converted to number
        myprintf("\n");
    }
    num -= 2; //Skip to generate the middle row twice
    for (i = 1; i <= num; i += 2) { //+=2 to skip even row generation
        printf("  ");
        for (j = 1; j <= i; j++)
            printf(" ");
        for (m = num; m >= i; m--)
            myprintf(" *"); //display of star converted to number
        myprintf("\n");
    }
}

输出:

               1      
             1 2 3    
           1 2 3 4 5    
             1 2 3      
               1     

答案 3 :(得分:0)

这是这种钻石的简称。

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int w = 9;
    int l;

    for(l=0; l < w; ++l)
    {
        printf("%*.*s\n", abs(w/2 - l)+abs((2*l+1)-(2*l+1>w)*2*w), abs((2*l+1)-(2*l+1>w)*2*w), "123456789");
    }

    return 0;
}