北大ACM(POJ)1109我的解决代码:

/*思路:该题主要是通过阶段讨论来分阶段解决问题,每个阶段中再根据具体的情况讨论。
算法的时间和空间复杂度都可望达到O(1)*/
#include<stdio.h>
#include<math.h>
#define N1 45
#define N2 9000
#define N3 1386450
#define N4  20891*9000
#define N 189414495
int main()
{
 unsigned int n;  //测试用例个数
 unsigned int num;//每个测试用例输入的值
    unsigned int x;
 unsigned int flag;
 unsigned int temp;
 unsigned int t;
 scanf("%d",&n);
 while(n--)
 {
  scanf("%d",&num);
  if(num<=45&&num>0)                 // 测试用例为1~1,2~1,3~2,10~4,11~1,45~9
  {
   x = (int)(sqrt(0.25+2*num)-0.5);
   if((x*x+x) == 2*num)
   {
    printf("%d/n",x);
    continue;
   }
   else
   {
    num = num - (x*x+x)/2;
    printf("%d/n",num);
    continue;
   }
  }
  else if(num>45&&num<=9045)        //测试用例为 46~1,47~2,56~0,57~1,80~0,9044~9,9045~9
  {
   num = num - 45;
   x  = (int)(sqrt(0.01*num+25*0.01)*10-5);
   if((x+10)*x == num)
   {
    printf("%d/n",(9+x)%10);
    continue;
   }
   else
   {
                num = num - (10+x)*x;
    if(num>=1&&num<=9)
    {
     printf("%d/n",num);
     continue;
    }
    else
    {
     flag = num%2;
     if(flag==1)
     {
      printf("%d/n",(9+(num-9)/2)%10);
     }
     else
     {
      printf("%d/n",(9+(num+1-9)/2)/10);
     }

    }
   }
  }
  else if(num>9045&&num<=N1+N2+N3)       //test case: 9046~1,9235~1,9236~0,9236~0
  {                                      //1395493~9,1395494~9,1395495~9
   num = num-9045;
   x = (int)(sqrt(381.0/36.0*381+24.0/36.0*num)-381.0/6.0);
   if((3*x+381)*x == 2*num)
   {
    printf("%d/n",(99+x)%10);
    continue;
   }
   else
   {
    num = num - ((3*x+381)*x)/2;
    if(num>=1&&num<=9)
    {
     printf("%d/n",num);
     continue;
    }
    else if(num>9&&num<=189)
    {
     flag = num%2;
     if(flag==1)
     {
      printf("%d/n",((num-9)/2+9)%10);
         continue;
     }
     else
     {
      printf("%d/n",((num+1-9)/2+9)/10);
      continue;
     }
    }
    else
    {
     flag = num%3;
     if(flag==0)
     {
      printf("%d/n",((num-189)/3+99)%10);
      continue;
     }
     else if(flag==2)
     {
      temp = ((num+1-189)/3+99)/10;
      temp = temp%10;
      printf("%d/n",temp);
      continue;
     }
     else
     {
      temp = ((num+2-189)/3+99)/10;
      temp = temp/10;
      printf("%d/n",temp);
      continue;
     }

    }
   }
  }
  else if(num>N1+N2+N3&&num<=N1+N2+N3+N4) //test cases:(1395495+1)~1,(1395495+2893-3)~1,(1395495+2893-2)~0
  {                                       //(1395495+2893-1)~0,(1395495+2893)~0,(1398389)~1,(189414495)~9,(189414495-1)~9
   num = num-N1-N2-N3;                 //(189414495-2)~9,(189414495-3)~9
   x =(int)(sqrt(2891.0/16.0*2891+8.0/16.0*num)-2891.0/4.0);
   if((2*x+2891)*x == num)
   {
    temp = (999+x)%10;
    printf("%d/n",temp);
    continue;
   }
   else
   {
    num =  num-(2891+2*x)*x;
    if(num>=1&&num<=9)
    {
     printf("%d/n",num);
     continue;
    }
    else if(num>9&&num<=189)
    {
     flag = num%2;
     if(flag==1)
     {
      temp = ((num-9)/2+9)%10;
      printf("%d/n",temp);
      continue;
     }
     else
     {
      temp = ((num+1-9)/2+9)/10;
      printf("%d/n",temp);
      continue;
     }
    }
    else if(num>189&&num<=2889)
    {
     flag = (num-189)%3;
     if(flag==0)
     {
                        temp = ((num-189)/3+99)%10;
      printf("%d/n",temp);
      continue;
     }
     else if(flag == 1)
     {
      temp = ((num+2-189)/3+99)/10;
      temp = temp/10;
      printf("%d/n",temp);
      continue;

     }
     else
     {
      temp = ((num+1-189)/3+99)/10;
      temp = temp%10;
      printf("%d/n",temp);
      continue;
     }
    }
    else if(num>2889&&num<=38889)
    {
     flag = (num-2889)%4;
     if(flag==0)
     {
      temp = ((num-2889)/4+999)%10;
      printf("%d/n",temp);
      continue;
     }
     else if(flag==3)
     {
                         temp = ((num+1-2889)/4+999)/10;
       temp%=10;
       printf("%d/n",temp);
       continue;
     }
     else if(flag==2)
     {
      temp = ((num+2-2889)/4+999)/10;
      temp/=10;
      temp%=10;
      printf("%d/n",temp);
      continue;
     }
     else if(flag==1)
     {
      temp = ((num+3-2889)/4+999)/10;
      temp /= 10;
      temp /= 10;
      printf("%d/n",temp);
      continue;
     }

    }

    
   }

  }
  else  if(num>N1+N2+N3+N4&&num<=2147483647)              //test cases:189453389~0,189453388~0,189453387~0,189453386~0,189453385~1,189453390~1
  {
               num = num-N1-N2-N3-N4;                          //2147483647~2
      //printf("%d/n",num);
      x = (int)(sqrt(40.0/100*num+77783.0/100*77783.0)-77783.0/10.0);
     // printf("x = %d/n",x);
               if((int)(2.5*x*x+38891.5*x)==num)
      {
       printf("%d/n",(9999+x)%10);
       continue;
      }
      else
      {
       //t = (int)(2.5*x*x+38891.5*x);
       //printf("t = %d/n",t);
       num = num-(int)(2.5*x*x+38891.5*x);
       //printf("num = %d/n",num);
       if(num>=1&&num<=9)
       { 
                      printf("%d/n",num);
       continue;
       }
       else if(num>9&&num<=189)
       {
        flag = (num-9)%2;
       // temp = (num-9)/2+9;
        if(flag==0)
        {
                         temp = ((num-9)/2+9)%10;
       printf("%d/n",temp);
       continue;
        }
        else if(flag==1)
        {
                           temp = ((num+1-9)/2+9)/10;
         printf("%d/n",temp);
         continue;
        }
       }
       else if(num>189&&num<=2889)
       {
                       flag = (num-189)%3;
        if(flag==0)
        {
         temp = ((num-189)/3+99)%10;
         printf("%d/n",temp);
         continue;
        }
        else if(flag==2)
        {
         temp = ((num+1-189)/3+99)/10;
         temp = temp%10;
         printf("%d/n",temp);
         continue;
        }
        else
        {
         temp  = ((num+2-189)/3+99)/10;
         temp = temp/10;
         printf("%d/n",temp);
         continue;
        }

       }
       else if(num>2889&&num<=38889)
       {
        flag = (num-2889)%4;
        if(flag==0)
        {
         temp = ((num-2889)/4+999)%10;
         printf("%d/n",temp);
         continue;
        }
        else if(flag==3)
        {
         temp = ((num+1-2889)/4+999)/10;
         temp = temp%10;
         printf("%d/n",temp);
         continue;
        }
        else if(flag==2)
        {
         temp = ((num+2-2889)/4+999)/10;
         temp = temp/10;
         temp = temp%10;
         printf("%d/n",temp);
         continue;
        }
        else if(flag==1)
        {
         temp = ((num+3-2889)/4+999)/10;
         temp = temp/10;
         temp = temp/10;
         printf("%d/n",temp);
         continue;
        }

       }
       else if(num>38889)
       {
        flag = (num-38889)%5;
        if(flag==0)
        {
                           temp = ((num-38889)/5+9999)%10;
         printf("%d/n",temp);
         continue;
        }
        else if(flag==4)
        {
         temp = ((num+1-38889)/5+9999)/10;
         temp = temp % 10;
         printf("%d/n",temp);
         continue;
        }
        else if(flag==3)
        {
         temp = ((num+2-38889)/5+9999)/10;
         temp = temp/10;
         temp = temp%10;
         printf("%d/n",temp);
         continue;
        }
        else if(flag==2)
        {
         temp = ((num+3-38889)/5+9999)/10;
         temp = temp/10;
         temp = temp/10;
         temp = temp%10;
         printf("%d/n",temp);
         continue;
        }
        else
        {
         temp = ((num+4-38889)/5+9999)/10;
         temp = temp/10;
         temp = temp/10;
         temp = temp/10;
       
         printf("%d/n",temp);
         continue;
        }
       }
            }
    }
 }
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值