首页 > ACM题库 > HDU-杭电 > HDU 1391 Number Steps[解题报告] C++
2013
12-09

HDU 1391 Number Steps[解题报告] C++

Number Steps

问题描述 :

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.

输入:

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.

输出:

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

样例输入:

3
4 2
6 6
3 4

样例输出:

6
12
No Number

2011-12-21 10:39:00

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1391

题意:给出坐标,输出坐标上的数字或No Number。

代码:

# include <stdio.h>


int gao(int x, int y)
{
    int ans ;
    if (x == y) return 2*x - (x&1) ;
    return gao(x, y+2) - 2  ;
}


int main ()
{
    int T, x, y ;
    scanf ("%d", &T) ;
    while (T--)
    {
        scanf ("%d%d", &x, &y) ;
        if (x != y && x != y+2) printf ("No Number\n") ;
        else printf ("%d\n", gao(x,y)) ;
    }
    return 0 ;
}

解题报告转自:http://www.cnblogs.com/lzsz1212/archive/2012/01/06/2315316.html


  1. 博主您好,这是一个内容十分优秀的博客,而且界面也非常漂亮。但是为什么博客的响应速度这么慢,虽然博客的主机在国外,但是我开启VPN还是经常响应很久,再者打开某些页面经常会出现数据库连接出错的提示

  2. 站长,你好!
    你创办的的网站非常好,为我们学习算法练习编程提供了一个很好的平台,我想给你提个小建议,就是要能把每道题目的难度标出来就好了,这样我们学习起来会有一个循序渐进的过程!

  3. 5.1处,反了;“上一个操作符的优先级比操作符ch的优先级大,或栈是空的就入栈。”如代码所述,应为“上一个操作符的优先级比操作符ch的优先级小,或栈是空的就入栈。”

  4. #include <cstdio>

    int main() {
    //answer must be odd
    int n, u, d;
    while(scanf("%d%d%d",&n,&u,&d)==3 && n>0) {
    if(n<=u) { puts("1"); continue; }
    n-=u; u-=d; n+=u-1; n/=u;
    n<<=1, ++n;
    printf("%dn",n);
    }
    return 0;
    }

  5. 代码是给出了,但是解析的也太不清晰了吧!如 13 abejkcfghid jkebfghicda
    第一步拆分为 三部分 (bejk, cfghi, d) * C(13,3),为什么要这样拆分,原则是什么?