`

打印一个矩阵

    博客分类:
  • Java
J# 
阅读更多

打印如下形式的矩阵(其中n为参数,此时n=4):

 

0000
0111
0122
0123

 

public class Test
{
	public static void main(String[] args)
	{
		new Test().Display(4);
	}
	
	public void Display(int n)
	{
		int k = 0;
		int t = 0;
		
		for(int i=0; i<n; i++)
		{
			k = 0;
			t = i;
			
			for(int j=0; j<n; j++)
			{
				if(j < t)
				{
					System.out.print(k);
					k++;
				}
				else
				{
					System.out.print(k);
				}
			}
			
			System.out.println();
		}
	}
}

 

0
0
分享到:
评论
2 楼 yoyo08 2009-11-20  
ariesmonster 写道
这是前两天网梯的笔试题吧?

嗯 是啊 老兄你也去了 你的方法更简单更好
1 楼 ariesmonster 2009-11-20  
简化版:
public class Test  
{  
    public static void main(String[] args)  
    {  
        new Test().Display(4);  
    }  
      
    public void Display(int n)  
    {  
        for(int i=0; i<n; i++)  
        {  
            for(int j=0; j<n; j++)  
            {  
                if(j < i)  
                {  
                    System.out.print(j);  
                }  
                else 
                {  
                    System.out.print(i);  
                }  
            }  
              
            System.out.println();  
        }  
    }  

这是前两天网梯的笔试题吧?

相关推荐

Global site tag (gtag.js) - Google Analytics