Chinaunix首页 | 论坛 | 博客
  • 博客访问: 483559
  • 博文数量: 59
  • 博客积分: 345
  • 博客等级: 二等列兵
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-18 22:44
个人简介

to be myself

文章分类

全部博文(59)

文章存档

2017年(5)

2013年(47)

2012年(3)

2011年(4)

分类: C/C++

2013-03-02 17:27:03

double精度强悍!Pow函数威武!EXP够神!


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <math.h>

  3. int main()
  4. {
  5.     double m, p;
  6.     while(scanf("%lf%lf", &m, &p) != EOF)
  7.     {
  8.         printf("%.0lfn", pow(p, 1/m));
  9.     }
  10.     return 0;
  11. }
  12. -------------------------------------------------------


  13. 下面是我的错误代码:
  14. 没加二分是TLE,加了二分就WA.

  15. #include <stdio.h>
  16. #include <math.h>

  17. #define MAX 120

  18. int p[MAX];

  19. int Len(double x)
  20. {
  21.     int cnt = 0;
  22.     while(x - 1 >= 0)
  23.     {
  24.         cnt++;
  25.         x /= 10;
  26.     }
  27.     return cnt;
  28. }

  29. int main()
  30. {
  31.     int n, i, lenp, k, j, lent, z, tp, r, midd, x, y;
  32.     char c;
  33.     double t, sub;
  34.     freopen("in.txt", "r", stdin);
  35.     while(scanf("%d", &n) != EOF)
  36.     {
  37.         i = 0;
  38.         k = 1000000000;
  39.         getchar();
  40.         while((c = getchar()) != 'n')
  41.         {
  42.             if (c == EOF)
  43.             {
  44.                 break;
  45.             }
  46.             p[i++] = c - '0';
  47.         }
  48.         lenp = i;
  49.         x = 1;
  50.         y = k;
  51.         while (x < y)
  52.         {
  53.             midd = x + (y - x) / 2;
  54.             t = ceil(t = pow((double)midd, (double)n));
  55.             if ((lent = Len(t)) == lenp)
  56.             {
  57.                 if (lent > 16)
  58.                 {
  59.                     j = lenp - 16;
  60.                     t = ceil(t /= pow(10, j));
  61.                     j = 15;
  62.                     r = 16;
  63.                 }
  64.                 else
  65.                 {
  66.                     j = lent - 1;
  67.                     r = lenp;
  68.                 }
  69.                 z = (int)(t / pow(10, j));
  70.                 //printf("%d", z);
  71.                 tp = 0;
  72.                 while (tp < lenp && p[tp] == z)
  73.                 {
  74.                     sub = pow(10, j)*z;
  75.                     tp++;
  76.                     j--;
  77.                     t = ceil(t -= sub);
  78.                     z = (int)(t / pow(10, j));
  79.                 }
  80.                 if (tp == r)
  81.                 {
  82.                     printf("%dn", midd);
  83.                     break;
  84.                 }
  85.                 if (z > p[tp])
  86.                 {
  87.                     y = midd;
  88.                 }
  89.                 else
  90.                 {
  91.                     x = midd + 1;
  92.                 }
  93.             }
  94.             else if (lent > lenp)
  95.             {
  96.                 y = midd;
  97.             }
  98.             else
  99.             {
  100.                 x = midd + 1;
  101.             }
  102.         }
  103.     }
  104.     return 0;
  105. }

2011-03-26 16:23 发表于百度空间,今搬至CU。

阅读(1024) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~