游客 Signup | Login
中文 | En

3330 - 例题5-6 矩阵输出

输出以下4*5的矩阵

  1  2  3  4  5

  2  4  6  8 10

  3  6  9 12 15

  4  8 12 16 20

要求使用循环实现,注意每行输出5个数字,每个数字占3个字符的宽度,右对齐。

 

Input

Output

每行输出5个数字,每个数字占3个字符的宽度,右对齐。

 

Examples

Input

Output

  1  2  3  4  5
  2  4  6  8 10
  3  6  9 12 15
  4  8 12 16 20

Solution C

#include<stdio.h>
int main()
{
	int i,j,count;
	for(i=1;i<5;i++)
		for(j=1,count=1;j<6;j++,count++)
		{
			printf("%3d",i*j);
			if(count%5==0) printf("\n");
		}
	return 0;
}

Solution C++

#include <cstdio>
int main (void){
	int i,j,n,h;
	for(i=1;i<=4;i++){
		h=0,j=1;
		for(1;j<6;j++){
			n=i,n=n*j,h++;
			if(h<5)
				printf("%3d",n);
			else printf("%3d\n",n);
		}
	}
	return 0;
}
Time Limit 1 second
Memory Limit 12 MB
Discuss Stats
上一题 下一题