HDOJ 1391 Number Steps(打表DP)

简介: 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.

image.png


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]就可以了。

import java.util.Scanner;
public class Main{
    static int[][] db = new int[5005][2];
    public static void main(String[] args) {
        dabiao();
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0){
            int x = sc.nextInt();
            int y = sc.nextInt();
            if(x==0&&y==0){
                System.out.println(0);
                continue;
            }
            if(x==1&&y!=1){
                System.out.println("No Number");
                continue;
            }
            if(x==1&&y==1){
                System.out.println(1);
                continue;
            }
            if(x==y||x==(y+2)){
                if(x==(y+2)){
                    System.out.println(db[x][0]);
                }else{
                    System.out.println(db[x][1]);
                }
            }else{
                System.out.println("No Number");
            }
        }
    }
    private static void dabiao() {
        int num=2;
        db[0][0]=0;
        boolean is = true;
        for(int i=2;i<=5003;i++){
            if(is){
                db[i][0]=num;
                num++;
                db[i+1][0]=num;
                num++;
                is=!is;
            }else if(!is){
                db[i-1][1]=num;
                num++;
                db[i][1]=num;
                num++;
                is=!is;
            }
        }
//      System.out.println(db[2][0]);
//      System.out.println(db[2][1]);
//      System.out.println(db[3][0]);
//      System.out.println(db[3][1]);
//      System.out.println(db[4][0]);
//      System.out.println(db[4][1]);
//      System.out.println(db[5][0]);
//      System.out.println(db[5][1]);
    }
}
目录
相关文章
|
人工智能 算法
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
121 0
HDOJ(HDU) 1562 Guess the number(水题,枚举就行)
HDOJ(HDU) 1562 Guess the number(水题,枚举就行)
121 0
HDOJ 1266 Reverse Number(数字反向输出题)
HDOJ 1266 Reverse Number(数字反向输出题)
120 0
|
Java
HDOJ 1018 Big Number(大数位数公式)
HDOJ 1018 Big Number(大数位数公式)
116 0
HDOJ 1005 Number Sequence
HDOJ 1005 Number Sequence
104 0
|
前端开发
HDOJ 1212 Big Number
HDOJ 1212 Big Number
113 0
|
Java
HDOJ 1018 Big Number(大数位数公式)
Problem Description In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc.
927 0
HDOJ 1005 Number Sequence
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
867 0