簡體   English   中英

我想使用 c 程序打印數字模式

[英]I want to print pattern of numbers using c program

如果 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");
}

我不太擅長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);
}
#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;


    }





}

這是你應該做什么的粗略草圖,它有一些小缺陷......但是,功能就在那里。 下次,如果你不能理解代碼調用的算法,我建議你把這個數組寫在一張紙上,並按照每一行。 注意每一行的放置位置,你應該開始想出一種方法來做到這一點(有比這更多的方法......)起初看起來可能很難,但如果你想進入這個領域,你有有它的心態。 我同意其他人的看法,這不是一個家庭作業網站,而是一個幫助建立您所知道的知識的網站,所以開始實際嘗試編寫程序,然后如果您遇到問題,請在此處提交。

#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);
}

這個程序運行完美。 但如果你想做出一些改變……自己動手。 下次請在詢問之前自己嘗試一些編碼。 這將為偶數打印不同的圖案。

#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;
    }
//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(" ");
                }
                }   
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM