• HDU Leftmost Digit


    Leftmost Digit

    http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=1&problemid=11

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 1524 Accepted Submission(s): 689
     
    Problem Description
    Given a positive integer N, you should output the leftmost digit of N^N.
     
    Input
    The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
    Each test case contains a single positive integer N(1<=N<=1,000,000,000).
     
    Output
    For each test case, you should output the leftmost digit of N^N.
     
    Sample Input
    2
    3
    4
     
    Sample Output
    2
    2
    Hint
    In the first case, 3 * 3 * 3 = 27, so the leftmost digit is 2. In the second case, 4 * 4 * 4 * 4 = 256, so the leftmost digit is 2.

    (转)

    初一想,这里的每一位在相乘的过程中对可能对最高位的值有贡献,所以相对求位数求余的求商肯定是不行了。   这里应该对 一个数有这样的理解,一个数是由每一位的基数乘以相对应的权值,例如 123456 , 基数"1"的权值为 10^5,  基数 "2" 的权值为 10^4......所以该题要求的就是最高位的基数。
        对 x^x 取对数,得 x* ln( x )/ ln( 10 ), 现假设这个值为 X.abcdeefg   那么 10^X 就是 最高位对应的权值,10^ 0.abcdefg 就是最高位的基数。注意这里得到的并不是一个整数,为什么呢? 因为这里是强行将后面位的值也转化到最高位上来了,这有点像大数中,如果不满进制却强行进位,显然那样会进给高位一个小数而不是一个天经地义的整数。得到 10^ 0.abcdefg 后,再用 double floor ( double ) 函数取下整就得到最高位的数值大小了
     
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    
    using namespace std;
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int t,n;
        scanf("%d",&t);
        while(t--){
            scanf("%d",&n);
            double tmp=n*log10(double(n));  //这里得强制转换,否则Compilation Error
            double res=tmp-floor(tmp);
            printf("%d\n",(int)pow(10.0,res));
        }
        return 0;
    }
  • 相关阅读:
    通过Web启动本地应用程序
    类似百度文库文档预览方式实现
    cas4.0 session中返回更多的用户信息
    word转pdf图片问题
    JSON数据转换成table表格
    2017年各银行卡跨行取款收费标准
    解决win8/8.1系统安装.net framework 3.5出现0x800F0906代码错误
    更改Thunderbird的默认语言
    java获取客户端ip地址
    为jquery ajax请求增加正在运行提示
  • 原文地址:https://www.cnblogs.com/jackge/p/2842830.html
Copyright © 2020-2023  润新知