zoukankan      html  css  js  c++  java
  • HDU 5170 GTY's math problem 水题

    题目链接:

    hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5170

    bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=567&pid=1001

    题解:

    先用java大数幂写的,t了

     1 import java.util.*;
     2 import java.math.*;
     3 public class Main {
     4     public static void main(String args[]){
     5         Scanner cin = new Scanner(System.in);
     6         BigInteger a,c;
     7         int b,d;
     8         
     9         while(cin.hasNext()){
    10             a=cin.nextBigInteger();
    11             b=cin.nextInt();
    12             c=cin.nextBigInteger();
    13             d=cin.nextInt();
    14             
    15             a=a.pow(b);
    16             c=c.pow(d);
    17             //四则运算
    18             
    19             if(a.compareTo(c)>0) System.out.println(">");
    20             else if(a.compareTo(c)<0) System.out.println("<");
    21             else System.out.println("=");
    22         }
    23     }
    24 }
    View Code

    然后用java写了一个大数的快速幂,同t

     1 import java.util.*;
     2 import java.math.*;
     3 public class Main {
     4     public static BigInteger solve(BigInteger a,int n){
     5         BigInteger ret=BigInteger.ONE;
     6 //        System.out.println("a:"+a.toString());
     7         while(n>0){
     8             if((n&1)>0){
     9 //                System.out.println("fuck!");
    10                 ret=ret.multiply(a);
    11             }
    12             a=a.multiply(a);
    13             n/=2;
    14         }
    15 //        System.out.println("ret!"+ret.toString());
    16         return ret;
    17     }
    18     public static void main(String args[]){
    19         Scanner cin = new Scanner(System.in);
    20         BigInteger a,c;
    21         int b,d;
    22         
    23         while(cin.hasNext()){
    24             a=cin.nextBigInteger();
    25             b=cin.nextInt();
    26             c=cin.nextBigInteger();
    27             d=cin.nextInt();
    28             
    29             a=solve(a,b);
    30             c=solve(c,d);
    31             
    32 //            System.out.println(a.toString());
    33 //            System.out.println(c.toString());
    34             //四则运算
    35             
    36             if(a.compareTo(c)>0) System.out.println(">");
    37             else if(a.compareTo(c)<0) System.out.println("<");
    38             else System.out.println("=");
    39         }
    40     }
    41 }
    View Code

    终于意识到,只要转化为等幂,比较底数大小就可以了。。

    a^b,(c^(d/b))d,只要比较a和c^(d/b)的大小。

    第一发,没考虑进度,wa了

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cmath>
     4 using namespace std;
     5 
     6 int main() {
     7     int a, b, c, d;
     8     while (scanf("%d%d%d%d", &a, &b, &c, &d) == 4) {
     9         double tmp = d*1.0 / b;
    10         tmp = pow(c*1.0, tmp);
    11         if (a*1.0 > tmp) puts(">");
    12         else if (a*1.0 < tmp) puts("<");
    13         else puts("=");
    14     }
    15     return 0;
    16 }
    View Code

    贴正解:。。

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cmath>
     4 using namespace std;
     5 
     6 const double eps = 1e-8;
     7 
     8 int main() {
     9     int a, b, c, d;
    10     while (scanf("%d%d%d%d", &a, &b, &c, &d) == 4) {
    11         double tmp = d*1.0 / b;
    12         tmp = pow(c*1.0, tmp);
    13         if (a*1.0-tmp>eps) puts(">");
    14         else if (a*1.0 - tmp<-eps) puts("<");
    15         else puts("=");
    16     }
    17     return 0;
    18 }
    View Code

    总结:

    1、不要太冲动,一有想法就上键盘,多考虑下是否有更简单的解决方案。

    2、写浮点数,一定要考虑精度!!!!,罚时伤不起。。

  • 相关阅读:
    IntelliJ IDEA隐藏不想看到的文件或文件夹
    IntelliJ IDEA更改字体和大小
    Dubbo实践(一)入门示例
    部署Jar包到远程Maven仓库
    Linux网卡别名
    ORACLE NLS_LENGTH_SEMANTICS 参数的用途
    Oracle编辑数据时提示:这些查询结果不可更新,请使用ROWI或者SELECT……FOR UPDATE获得可更新结果
    Notepad++格式化JSON字符串
    Xshell配色为Ubuntu风格
    【剑指offer】判断一个序列是否是二叉搜索树的后序遍历,C++实现
  • 原文地址:https://www.cnblogs.com/fenice/p/5437771.html
Copyright © 2011-2022 走看看