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++;
}
}
使用函数以降低复杂度。