【问题标题】:I want to print pattern of numbers using c program我想使用 c 程序打印数字模式
【发布时间】:2016-01-08 03:01:46
【问题描述】:

如果n=3,则输出为

1*2*3

7*8*9

4*5*6

如果n=5,则输出为

1*2*3*4*5

11*12*13*14*15

21*22*23*24*25

16*17*18*19*20

6*7*8*9*10

代码:

int i, j, a[50][50], k = 1, m = 0;
for (i = 0; i < n; i += 2) {
  for (j = 0; j < n; j++) {
    a[i][j] = k;
    k++;
  }
  printf("\n");
}
m = k;
for (i = 1; i <= n; i += 2) {
  for (j = 0; j < n; j++) {
    a[i][j] = m;
    m++;
  }
  printf("\n");
}
for (i = 0; i < n; i++) {
  for (j = 0; j < n; j++) {
    printf("%d", a[i][j]);
  }
  printf("\n");
}

【问题讨论】:

  • 是什么阻止你这样做?
  • 这里不是这样,请添加代码sn-p和有问题的输出
  • 向我们展示你的努力?
  • @BLUEPIXY 非常优雅的解决方案。

标签: c


【解决方案1】:

这是你应该做什么的粗略草图,它有一些小缺陷......但是,功能就在那里。下一次,如果你不明白代码需要什么算法,我建议你把这个数组写在一张纸上,然后按照每一行。注意每一行的放置位置,你应该开始想出一种方法来做到这一点(还有比这个更多的方法......)一开始可能看起来很难,但如果你想进入这个领域,你有有它的心态。我同意其他人的观点,这不是一个家庭作业网站,而是一个帮助建立你所知道的知识的网站,所以开始实际尝试编写程序,如果你遇到问题,然后在这里提交。

#include <stdio.h>

void loadNprint(int size, int a[][size]){
    int i,j,count=1,down=size-1, up =0;
    for(i=0; i<size; i++){
        for(j=0;j<size; j++){
            if((i%2) == 0)a[up][j] = count;
            if((i%2) == 1)a[down][j]= count;
        count++;
        }
        if((i%2) == 0)up++;//keeping track of the rows in ascending order
        if((i%2) == 1)down--;//keeping track of rows in descending order
    }
    for(i=0; i<size; i++){
        for(j=0; j<size; j++){
            printf("%4d",a[i][j]);
        }
        printf("\n");
    }
}

void main(){
    int input;
    printf("Enter a Value : ");
    scanf(" %d",&input);
    int myarray[input][input];
    loadNprint(input,myarray);
}

【讨论】:

  • 感谢大家的解决方案。因为我是这个网站的新手,所以我不知道我们也必须提交我们的代码 sn-ps。好吧,我一直在研究这个程序:int i,j,a[50][50],k=1,m=0; for(i=0;i
【解决方案2】:

我的c语言不太好,但我认为它会对你有所帮助。 请看一下,如果出现语法错误但逻辑清晰,您可以进行一些更改。

#include <stdio.h>
#include <math.h>

void printOutput(int n){

int k = ceil(n/2);
int m =1;
int j =1;
int l =k;
int i;
int b;
for(i=1;i<=n;i++){

for(b=m;b<=m+n;b++){
 printf(b);
}
printf("\n");
  if(i<k){
       j= (2*j);
    m =n*j+1;
  } else{
    int z = n-i-1;
     m= n+1 +n*(2)*z;
    l =z-2;

  }
}
}

void main(){
    int input;
    printf("Enter a Value : ");
    scanf(" %d",&input);
    printOutput(input);
}

【讨论】:

    【解决方案3】:

    这个程序完美运行。但是,如果您想进行一些更改..自己做。 下次请在询问之前自己尝试一些编码。这将为偶数打印不同的模式。

    #include<stdio.h>
    #include<conio.h>
    int n,beginnew,cpy;
    int main()
    {
        printf("Please enter a value : ");
        scanf("%d",&n);
    
        //process
        beginnew=n-n/2+1;//beginning of new pattern
        cpy=n-1;
        for(int i=1;i<n+1;i++)
        {
            if(i<beginnew)
            {
                for(int h=n-1;h>=0;h--)
            printf("%d * ", (n*(2*i-1)-h) );
            }
            else
            {
                for(int h=n-1;h>=0;h--)
            printf("%d * ",(n*(cpy)-h) );
            cpy=cpy-2;
        }
          printf("\n");
        }
          getch();
          return 0;
        }
    

    【讨论】:

      【解决方案4】:
      //this code print Diagonal Pattern if matrix is 
      1 2 3
      4 5 6
      7 8 9
      output is :
      1
      4 2
      7 5 3
      8 6
      9
      
          import java.util.*;
          class DiagonalPattern
          {
              public static void main(String args[])
              {
                  Scanner sc=new Scanner(System.in);
                  int x[][];
                  int i,j,row,col,p,temp=1,last=0;
                  System.out.println("how many array wants to create and size of array");
                  row=sc.nextInt();
                  col=sc.nextInt();
                  x=new int[row][col];
                  System.out.println("Enter  " +row*col+ "  elements of array of array");
                      for(i=0;i<row;i++)
                      {
                      for(j=0;j<col;j++)
                      {
                          x[i][j]=sc.nextInt();
                          last=j;
                      }
                      }
                  for(i=0;i<row;i++)
                      {
                      System.out.println("");
                      int k=i;
                      for(j=0;j<=i;j++,k--)
                      {
                          if(j==col)
                          {
                              break;
                          }
                          else
                          {
                              System.out.print(x[k][j]);  
                          System.out.print(" ");
                          }
                      }
                      }
                  for(p=x.length;p>0;p--,temp++)
                      {
                      System.out.println("");
                      i=x.length-1;
                      int k=i;
                      for(j=temp;j<=last;j++,k--)
                      {
                          System.out.print(x[k][j]);  
                          System.out.print(" ");
                      }
                      }   
              }
          }
      

      【讨论】:

      • 这只是一个代码转储,你没有给出任何关于它是如何工作的描述。不仅如此,而且与OP所要求的完全不同。当问题标记为 C 时,它也在 Java 中。
      【解决方案5】:
      #include<stdio.h>
      #include<conio.h>
      int k=1;
      int main(){
      
      
          int n;
          scanf("%d",&n);
      
          for(int i=1;i<=n/2+1;i++){
              for(int j=1;j<=n;j++){
              if(j!=1&&j!=n+1){
                  printf("*");
              }
              printf("%d",k);
      
              k++;    
              }
              printf("\n \n");
              k=k+n;
      
      
          }
          k=k-3*n;
      
      
              for(int i=1;i<=n/2;i++){
              for(int j=1;j<=n;j++){
              if(j!=1&&j!=n+1){
                  printf("*");
              }
              printf("%d",k);
      
              k++;    
              }
              printf("\n \n");
              k=k-(n/2+1)*n;
      
      
          }
      
      
      
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2020-09-03
        • 1970-01-01
        • 1970-01-01
        • 2015-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-16
        • 1970-01-01
        相关资源
        最近更新 更多