poj 1086

这道题需要注意的是打印格式,每个Scenario之后有换行,并且点与点之间没有空格


刚看起来复杂,其实思路比较简单,总共有gas*2+1层,对于每一层,先算出到这一层需要的流量以及还剩余的油量

对于到一个点 (m, n) 到这个点需要的油量其实就是这一行距离中心点(gas, gas)的偏移量 |gas-m| + |gas-n|

import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		while(s.hasNext()){
			int count = s.nextInt();
			for(int i = 1; i <= count; i++){
				System.out.println("Scenario #"+i+":");
				int gas = s.nextInt();
				for(int j = 1; j <= gas * 2 + 1; j++){
					System.out.println("slice #"+j+":");
					if(gas == 0){
						System.out.println(0);
					}else{
						//到该层还剩下的油量
						int used = Math.abs(gas - (j - 1));
						int left = gas - Math.abs(gas - (j - 1));
						for(int m = 0; m < gas * 2 + 1; m++){
							for(int n = 0; n < gas * 2 + 1; n++){
								//到m行 n 列的耗油量
								int toMN = Math.abs(gas - m) + Math.abs(gas - n);
								if(toMN > left){
									System.out.print(".");
								}else {
									System.out.print((toMN + used));
								}
							}
							System.out.println();
						}
					}
				}
				System.out.println();
			}
		}
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值