zoukankan      html  css  js  c++  java
  • 201412-2 Z字形扫描 Java


    思路:
    观察输出可以发现,可以不用定义 “方向” ,看斜线,如果是第偶数条(0也是偶数),从左下到右上输出。如果是第奇数条,从右上到左下输出。

    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int a[][] = new int[n][n];
    		for(int i=0;i<n;i++) {
    			for(int j=0;j<n;j++) {
    				a[i][j] = sc.nextInt();
    			}
    		}
    		for(int i=0;i<2*n-1;i++) {//2n-1条斜线
    			int s = i<n?0:(i-n+1);
    			int e = i<n?i:(n-1);
    			if(i%2 == 0) {//偶数从左下到右上,第一条斜线是0
    				for(int j=s;j<=e;j++) {
    					System.out.print(a[i-j][j] + " ");
    				}
    			}else {
    				for(int j=s;j<=e;j++) {
    					System.out.print(a[j][i-j] + " ");
    				}
    			}
    		}
    		sc.close();
    	}
    }
    
  • 相关阅读:
    c# 字符串中某个词出现的次数及索引
    c# 冒泡排序
    WCF 高级知识
    Web Api基础知识
    Web Services基础知识
    WCF Demo
    IIS部署WCF常见错误
    IIS部署WCF
    IIS部署WCF错误
    WCF基础知识
  • 原文地址:https://www.cnblogs.com/yu-jiawei/p/12343214.html
Copyright © 2011-2022 走看看