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. 程序中使用7-zip(7z)压缩文件

    Email:longsu2010 at yeah dot net 工作中难免遇到需要压缩文件的情况,比如有一千万个小文件,每个文件约100k,如果使用7-zip压缩后可能十几k,可以节省很多磁盘空间. ...

  2. 苹果iOS操作系统整体架构层次讲解

     iOS的系统架构分为四个层次:核心操作系统层(Core OS layer).核心服务层(Core Services layer).媒体层(Media layer)和可触摸层(Cocoa Touch ...

  3. iOS UITextField 设置内边距

    [self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"_paddingTop"]; [self.your ...

  4. 第三章:真正弄清楚一个Mod的组织结构

    <基于1.8 Forge的Minecraft mod制作经验分享> 首先看看一个mod的文件结构,懂Java的应该都看得懂: src/main/ --java/com.xxxxxxxx.x ...

  5. C:应用于字符串处理函数

    出于对C的不够熟悉,在读代码的过程中,平凡出现的字符串处理函数,成为了一个理解代码的大问题. 为了更方便的读取和理解代码,特意将接触到的字符串处理函数列出,方便查询: 1.strstr(str1,st ...

  6. Activity 状态的保存和恢复

    Activity状态保存的两种情况 一.Activity状态保持概念 保存Activity的状态是非常重要的,例如我们在玩一个游戏的时候,突然来了一个电话,这个时候在接听完电话之后我们返回到游戏中,这 ...

  7. 95秀-PullToRefreshListView 示例

        正在加载.暂无数据页面 public class RefreshGuideTool {     private RelativeLayout rl_loading_guide;//整个View ...

  8. webform 复杂点的服务器控件

    1  , dropdownlist:  下拉框 属性items  列表集合,  里面的每一个元素是一个 listitem . 联动的时候注意要 设置属性 .Autopostback 为ture: 注注 ...

  9. adb服务无法启动

    今天学习android编程发现调试出错 The connection to adb is down, and a severe error has occured. You must restart ...

  10. 原生js 学习之array 数组

    Array的原生方法:  concat(): 连接两个或更多的数组哦 join(): 把数组的所有元素放在一个字符串中 pop():删除并返回数组的最后一个元素 push():向数组的末尾添加一个元素 ...