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

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]);
}
}

HDOJ 1391 Number Steps(打表DP)的更多相关文章

  1. HDU 1391 number steps(找规律,数学)

    Starting from point (0,0) on a plane, we have written all non-negative integers 0, 1, 2,... as shown ...

  2. hdu 1391 Number Steps(规律)

    题意:找规律 思路:找规律 #include<iostream> #include<stdio.h> using namespace std; int main(){ int ...

  3. HDU-1391 Number Steps

    http://acm.hdu.edu.cn/showproblem.php?pid=1391 Number Steps Time Limit: 2000/1000 MS (Java/Others)   ...

  4. ACM/ICPC 之 数据结构-邻接表+DP+队列+拓扑排序(TSH OJ-旅行商TSP)

    做这道题感觉异常激动,因为在下第一次接触拓扑排序啊= =,而且看了看解释,猛然发现此题可以用DP优化,然后一次A掉所有样例,整个人激动坏了,哇咔咔咔咔咔咔咔~ 咔咔~哎呀,笑岔了- -|| 旅行商(T ...

  5. Number Steps

    Number Steps Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  6. HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包)

    HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包) 题意分析 裸的完全背包问题 代码总览 #include <iostream> #include <cstdio> ...

  7. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  8. ZOJ 1414:Number Steps

    Number Steps Time Limit: 2 Seconds      Memory Limit: 65536 KB Starting from point (0,0) on a plane, ...

  9. Rabin_Karp(hash) HDOJ 1711 Number Sequence

    题目传送门 /* Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个 ...

随机推荐

  1. linux if 判断字符串是否相等

    在命令行中修改时间: 如果linux系统时间等于2017-09-09,则ok:否则将当前系统时间修改为2017-09-09 var=`date '+%Y-%m-%d'`;if [ "$var ...

  2. 【高级算法】模拟退火算法解决3SAT问题(C++实现)

    转载请注明出处:http://blog.csdn.net/zhoubin1992/article/details/46453761 ---------------------------------- ...

  3. [转] 关于SIGPIPE导致的程序退出

    PS: 如果服务器程序不忽略SIGPIPE,在某些时候TCP writer收到这个信号,会导致进程退出 The rule that applies is: When a process writes ...

  4. C# 面向对象 , 继承

    继承 class A { Console.WriteLine("hello world"); } class B:A { } 以上书写,表示B 是A 的子类,  B 同时继承A 中 ...

  5. Avast注册以及更新

    昨天无聊在网上看Avast,然后下了玩. Avast有三种版本,免费版,网络版,高级版. 下了个高级版,在贴吧和论坛找激活码,发现大部分都没用,最后找了一个许可文件 关于Avast的注册,有在线和离线 ...

  6. Jquery插件-Html5图片上传并裁剪

    /** * 图片裁剪 * @author yanglizhe * 2015/11/16 */ (function($){ /** * Drag */ var Drag={obj:null,init:f ...

  7. linux 第二天

    文件权限和用户组 groupadd 组名 groupadd policeman 查看组 vi /etc/group cat /etc/group 创建用户,并同时指定将该用户分配到哪个组 userad ...

  8. Centos 5.5下安装samba

    1.安装: Centos 5.5下安装samba,具体步骤如下: [root@bogon ~]# rpm -q samba Package samba is not installed [root@b ...

  9. Using Notepad++ to Execute Oracle SQL

    原文链接:http://www.toadworld.com/products/toad-for-oracle/b/weblog/archive/2013/08/21/using-notepad-to- ...

  10. ContentProvider URI的组成

    ContentProvider URI由哪几部分组成 ContentProvider URI与HTTP URI类似,由以下4部分组成: 1.content://   相当于HTTP URI中的http ...