Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

菜鸟贴代码和思路(二分求得层数)

Posted by zerocpp at 2011-03-30 01:10:18 on Problem 1969
#include <stdio.h>
int h(int n)
{
    return n+n*(n-1)/2;
}
int g(int num)
{
    int l = 1, r = 5000, m;
    while (l <= r)
    {
        m = ((l+r)>>1);
        if (h(m) < num)
        {
            l = m + 1;
        }
        else if (h(m) > num)
        {
            r = m - 1;
        }
        else
        {
            return m;
        }
    }
    if (h(r) > num) return r;
    else return r+1;
}
void f(int num, int &a, int &b)
{
    int t = g(num);
    int sum = h(t) - h(t-1) + 1;//两个数的和
    if (t%2!=0)
    {
        a = h(t) - num + 1;
        b = sum - a;
    }
    else
    {
        b = h(t) - num + 1;
        a = sum - b;
    }
}
int main()
{
    int num, a, b;
    while (~scanf("%d", &num))
    {
        f(num,a,b);
        printf("TERM %d IS %d/%d\n",num,a,b);
    }
    return 0;
}
/*思路就是:一个等差数列,已知An(输入的数),A1=1,D=1,求N,使用二分查找(刚刚看了一下别人的解题报告,好像暴力求N也可以),然后根据层数(N)的奇偶决定两个数谁在前谁在后(就是蛇形排列的解决办法)。说得有点儿语无伦次,太困了。第一次发代码和思路,多指教~~
*/

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator