HDU 1597 find the nth digit

数学题。


弄懂了之久事实上就是解一个一元二次方程 x*(x-1)/2=y 。

假设y==0了。表明刚好是这个数。

不是的话,就x取大一位然后 y-n 再%9 。

只是记得 %9==0的时候是9。

不会出现0。




<pre name="code" class="cpp">#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
#define LL long long
using namespace std;
int main()
{

//    int a=0x3fffffff+1;
//    cout<<a;
//    1
//    freopen("in.txt","r",stdin);
//    freopen("2.txt","w",stdout);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        double n,x;
        int y;
        scanf("%lf",&n);
        x = int((sqrt(1.0 + 8.0*n) -1)/2);
        y = int(n-x*(x+1)/2);

        if(y == 0)//刚好是 n*(n+1)/2
        {
            if(int(x)%9 == 0)
                printf("9\n");
            else
                printf("%d\n", int(x)%9);
        }
        else//在之间
        {
            if(y%9 == 0)
                printf("9\n");
            else
                printf("%d\n", y%9);
        }
    }
}




posted @ 2017-07-10 09:36  gccbuaa  阅读(192)  评论(0)    收藏  举报