[Loops]D. Liang 4.18 Printing four patterns using loop

Description
Write a program that reads two intergers p(1<=p<=4) and n(3<=n<=10), then display one of the following patterns.

For example, when n = 6, the outputs are as follow:

Pattern 1

  1
  1  2
  1  2  3
  1  2  3  4
  1  2  3  4  5
  1  2  3  4  5  6
  Pattern 2

  1  2  3  4  5  6
  1  2  3  4  5
  1  2  3  4
  1  2  3
  1  2
  1
Pattern 3

                 1
              1  2
           1  2  3
        1  2  3  4
     1  2  3  4  5
  1  2  3  4  5  6
Pattern 4

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

Input
two integers, p (1<=p<=4) and n (3<=n<=10).

Output
Pattern p.

You should specify the width of each number’s print field to 3, justify the output to right.

There is no extra space at the end of a line.

Sample Input
1 6
Sample Output

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

作业记录:

#include<stdio.h>
int i=1,r=1,a=1; 
void p13(int p,int n);
void p24(int p,int n);
int main()
{    
    int n,p;
    scanf("%d %d",&p,&n);
    if(p==1||p==3) p13( p, n);
    else p24( p, n);
        return 0;
 }
 void p13(int p,int n){ 
    for(a=n;r<=n;){
        if(i==1&&r<n&&p==3){--a;printf("%*c",3*a,32);}
        printf("%3d",i);
        if(r==i){printf("\n");i=i-r;r++;}
        i++;
    }
} 
void p24(int p,int n){
    for(r=n;r>0;){
        if(i==1&&r<n&&p==4){printf("%*c",3*a,32);a++;}
        printf("%3d",i);
        if(r==i){printf("\n");i=i-r;r--;}
        i++;
    }
}

使用函数以降低复杂度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值