我想用c程序打印数字模式

时间:2015-10-10 05:08:21

标签: c

如果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

CODE:

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

5 个答案:

答案 0 :(得分:0)

我的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);
}

答案 1 :(得分:0)

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


    }





}

答案 2 :(得分:-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);
}

答案 3 :(得分:-1)

这个程序很完美。但如果你想做一些改变......就自己吧。 下次请先问自己一些编码。这将为偶数打印不同的模式。

#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 :(得分:-1)

Message               : Exception while executing: 
{"requestId":"64b3#1638e55058c","result":[{"batchId":1037,"importId":"1037","status":"Queued"}],"success":true}
                                         ^
You cannot compare a value of type ::array.
Payload               : {"requestId":"64b3#1638e55058c","result":[{"batchId":1037,"importId":"1037","status":"Queued"}],"success":true}
Payload Type          : java.lang.String
Element               : /marketing-dbmkt-etl-marketoFlow/processors/8 @ marketing-dbmkt-etl-marketo:marketing-dbmkt-etl-marketo.xml:69 (Transform Message)
Element XML           : <dw:transform-message doc:name="Transform Message" metadata:id="90448cfd-5884-441a-a989-e32e4877ac24">
                        <dw:input-payload mimeType="application/json" doc:sample="sample_data\batchreturnObject.dwl"></dw:input-payload>
                        <dw:set-payload>%dw 1.0%output application/json---batchexecution:{batchid:payload.result[0].batchid,status: payload.result[0].status,success:payload.success}  when ((payload.result != null) and (sizeOf payload.result  > 0))otherwise{batchid: 0,status:"Not queued",success:false}</dw:set-payload>
                        </dw:transform-message>
--------------------------------------------------------------------------------
Root Exception stack trace:
com.mulesoft.weave.mule.exception.WeaveExecutionException: Exception while executing: 
{"requestId":"64b3#1638e55058c","result":[{"batchId":1037,"importId":"1037","status":"Queued"}],"success":true}
                                         ^
You cannot compare a value of type ::array.
    at com.mulesoft.weave.mule.exception.WeaveExecutionException$.apply(WeaveExecutionException.scala:10)
    at com.mulesoft.weave.mule.WeaveMessageProcessor.execute(WeaveMessageProcessor.scala:121)
    at com.mulesoft.weave.mule.WeaveMessageProcessor.process(WeaveMessageProcessor.scala:67)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:108)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
    at org.mule.processor.BlockingProcessorExecutor.executeNext(BlockingProcessorExecutor.java:88)
    at org.mule.processor.BlockingProcessorExecutor.execute(BlockingProcessorExecutor.java:59)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
    at org.mule.processor.BlockingProcessorExecutor.executeNext(BlockingProcessorExecutor.java:98)
    at org.mule.processor.BlockingProcessorExecutor.execute(BlockingProcessorExecutor.java:59)
    at org.mule.interceptor.AbstractEnvelopeInterceptor.processBlocking(AbstractEnvelopeInterceptor.java:58)
    at org.mule.processor.AbstractRequestResponseMessageProcessor.process(AbstractRequestResponseMessageProcessor.java:47)
    at org.mule.processor.AsyncInterceptingMessageProcessor.processNextTimed(AsyncInterceptingMessageProcessor.java:129)
    at org.mule.processor.AsyncInterceptingMessageProcessor$AsyncMessageProcessorWorker$1.process(AsyncInterceptingMessageProcessor.java:213)
    at org.mule.processor.AsyncInterceptingMessageProcessor$AsyncMessageProcessorWorker$1.process(AsyncInterceptingMessageProcessor.java:206)
    at org.mule.execution.ExecuteCallbackInterceptor.execute(ExecuteCallbackInterceptor.java:16)
    at org.mule.execution.CommitTransactionInterceptor.execute(CommitTransactionInterceptor.java:35)
    at org.mule.execution.CommitTransactionInterceptor.execute(CommitTransactionInterceptor.java:22)
    at org.mule.execution.HandleExceptionInterceptor.execute(HandleExceptionInterceptor.java:30)
    at org.mule.execution.HandleExceptionInterceptor.execute(HandleExceptionInterceptor.java:14)
    at org.mule.execution.BeginAndResolveTransactionInterceptor.execute(BeginAndResolveTransactionInterceptor.java:67)
    at org.mule.execution.ResolvePreviousTransactionInterceptor.execute(ResolvePreviousTransactionInterceptor.java:44)
    at org.mule.execution.SuspendXaTransactionInterceptor.execute(SuspendXaTransactionInterceptor.java:50)
    at org.mule.execution.ValidateTransactionalStateInterceptor.execute(ValidateTransactionalStateInterceptor.java:40)
    at org.mule.execution.IsolateCurrentTransactionInterceptor.execute(IsolateCurrentTransactionInterceptor.java:41)
    at org.mule.execution.ExternalTransactionInterceptor.execute(ExternalTransactionInterceptor.java:48)
    at org.mule.execution.RethrowExceptionInterceptor.execute(RethrowExceptionInterceptor.java:28)
    at org.mule.execution.RethrowExceptionInterceptor.execute(RethrowExceptionInterceptor.java:13)
    at org.mule.execution.TransactionalErrorHandlingExecutionTemplate.execute(TransactionalErrorHandlingExecutionTemplate.java:110)
    at org.mule.execution.TransactionalErrorHandlingExecutionTemplate.execute(TransactionalErrorHandlingExecutionTemplate.java:30)
    at org.mule.processor.AsyncInterceptingMessageProcessor$AsyncMessageProcessorWorker.doRun(AsyncInterceptingMessageProcessor.java:205)
    at org.mule.work.AbstractMuleEventWork.run(AbstractMuleEventWork.java:53)
    at org.mule.work.WorkerContext.run(WorkerContext.java:301)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)

********************************************************************************