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. android动画效果编程基础--Android Animation

    动画效果编程基础--Android Animation 动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 tran ...

  2. Cobar是提供关系型数据库(MySQL)分布式服务的中间件

    简介 Cobar是提供关系型数据库(MySQL)分布式服务的中间件,它可以让传统的数据库得到良好的线性扩展,并看上去还是一个数据库,对应用保持透明. 产品在阿里巴巴稳定运行3年以上. 接管了3000+ ...

  3. 阿里云ECS安全改造:改用AWS EC2私钥方式登录

    阿里云ECS有多烂,用过AWS的人都知道,ECS有什么问题呢,简单总结一下: 1)ubuntu居然root直接访问 2)端口全开 3)WEB控制台无法定义安全组及关闭端口 4)云盾黑洞 的确用起来很糟 ...

  4. OD: Heap Overflow (XP SP2 - 2003) & DWORD SHOOT via Chunk Resize

    微软在堆中也增加了一些安全校验操作,使得原本是不容易的堆溢出变得困难重重: * PEB Random:在 Windows XP SP2 之后,微软不再使用固定的 PEB 基址 0x7FFDF000,而 ...

  5. QT 5.1.1 for Android 开发环境搭建与配置【Windows 7】

    前言:本人操作系统为Windows7 64位,用的是32位的安装包,32位系统没有验证. 一.首先下载以下安装包,如果提供的链接失效请自行下载: (1) Android SDK (Windows 32 ...

  6. [BZOJ]3737 [Pa2013]Euler

    从这个FB开始写博客啦. 也不知道会坚持多久…… = =似乎要加一句转载请注明出处 http://www.cnblogs.com/DancingOnTheTree/p/4026076.html htt ...

  7. Integer和int的详细比较(转)

    Integer与int的区别我们耳熟详的有两点:1.Integer是int的包装类.2.Integer的默认初始值是null,而int的默认初试值是0. 下面通过代码进行详细比较. public cl ...

  8. [转载] HDFS and Erasure Codes (HDFS-RAID)

    The Hadoop Distributed File System has been great in providing a cloud-type file system. It is robus ...

  9. shell脚本实现仅保留某目录下最新的两个文件

    #!/bin/sh export DS_DIR=/home/cxy/test if [ ! -d $DS_DIR ]; then mkdir $DS_DIR else echo "$DS_D ...

  10. OpenStack REST API使用

    以前一直想写博客,但因各种因素一直没写.最近在做OpenStack,而OpenStack对外提供REST API,今天就简要介绍一下OpenStack REST API 使用,关于什么是REST AP ...