HDOJ 1391 Number Steps(打表DP)

Problem Description
Starting from point (0,0) on a plane, we have written all non-negative integers 0, 1, 2,… as shown in the figure. For example, 1, 2, and 3 has been written at points (1,1), (2,0), and (3, 1) respectively and this pattern has continued.

You are to write a program that reads the coordinates of a point (x, y), and writes the number (if any) that has been written at that point. (x, y) coordinates in the input are in the range 0…5000.

Input
The first line of the input is N, the number of test cases for this problem. In each of the N following lines, there is x, and y representing the coordinates (x, y) of a point.

Output
For each point in the input, write the number written at that point or write No Number if there is none.

Sample Input
3
4 2
6 6
3 4

Sample Output
6
12
No Number

不能直接开[5005][5005]的数组,这样内存不够。
因为大部分数据没用,没列只有2个有效数据,所以开[5005][2]就可以了。

1import java.util.Scanner; 2 3public class Main{ 4 static int[][] db = new int[5005][2]; 5 public static void main(String[] args) { 6 7 8 9 dabiao(); 10 Scanner sc = new Scanner(System.in); 11 int t = sc.nextInt(); 12 while(t-->0){ 13 14 15 16 int x = sc.nextInt(); 17 int y = sc.nextInt(); 18 if(x==0&&y==0){ 19 20 21 22 System.out.println(0); 23 continue; 24 } 25 26 if(x==1&&y!=1){ 27 28 29 30 System.out.println("No Number"); 31 continue; 32 } 33 34 if(x==1&&y==1){ 35 36 37 38 System.out.println(1); 39 continue; 40 } 41 42 if(x==y||x==(y+2)){ 43 44 45 46 if(x==(y+2)){ 47 48 49 50 System.out.println(db[x][0]); 51 }else{ 52 53 54 55 System.out.println(db[x][1]); 56 } 57 }else{ 58 59 60 61 System.out.println("No Number"); 62 } 63 } 64 } 65 66 private static void dabiao() { 67 68 69 70 int num=2; 71 db[0][0]=0; 72 boolean is = true; 73 for(int i=2;i<=5003;i++){ 74 75 76 77 if(is){ 78 79 80 81 db[i][0]=num; 82 num++; 83 db[i+1][0]=num; 84 num++; 85 is=!is; 86 }else if(!is){ 87 88 89 90 db[i-1][1]=num; 91 num++; 92 db[i][1]=num; 93 num++; 94 is=!is; 95 } 96 } 97// System.out.println(db[2][0]); 98// System.out.println(db[2][1]); 99// System.out.println(db[3][0]); 100// System.out.println(db[3][1]); 101// System.out.println(db[4][0]); 102// System.out.println(db[4][1]); 103// System.out.println(db[5][0]); 104// System.out.println(db[5][1]); 105 } 106}

本文同步分享在 博客“谙忆”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang

typeScript数据类型

//布尔类型letisDone:booleanfalse;//数字类型所有数字都是浮点数numberletdecLiteral:number6;lethexLiteral:number0xf00d;letbinaryLiteral:number0b101