zoukankan      html  css  js  c++  java
  • 关于左位右位的问题

    n的n次方最右位

    用快速幂做

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    int fast_pow(ll a,ll b,ll c)
    {
        int ans=1;
        a%=c;
        while(b)
        {
            if(b&1)
            {
                ans=a*ans%c;
            }
            a=a*a%c;
            b>>=1;
        }
        return ans%c;
    }
    int main()
    {
        ll n,a,ans=0;
        scanf("%lld",&n);
        while(n--)
        {
            scanf("%lld",&a);
            ans=fast_pow(a,a,10);
            printf("%lld
    ",ans);
        }
        return 0;
    }

    n的n次方最左位

    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. 
             

    解题思路:因为N^N可以写作a*10^k的形式,输出强制转化为整形的a就表示的最高位,然后比如说3*3*3=27,就可以写作2.7*10^1的形式。所以k就代表的是结果的位数-1,所以k=(int)log10(n^n)+1-1。因为n^n=a*10^k所以log10(n^n)=log10(a)+log10(10^k).可以知道n*log10(n)=log10(a)+k,所以a=10^(n*log10(n)-k)。 

    #include <stdio.h>
    #include <math.h>
    int main()
    {
        int i,k;
        double n,a;
        while(scanf("%lf",&n)!=EOF)
        {
            k=n*log10(n);
            a=pow(10,(n*log10(n))-k);
            printf("%d
    ",(int) a);
        }
        return 0;
    }
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    int main()
    {
        int n,m;
        double x;
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d",&n);
            x=n*log10((double)n);
            x-=(ll)x;
            x=(int)pow(10,x);
            printf("%.0lf
    ",x);
        }
    }
    

    n的阶乘右面第一个不为0的数

    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    int main()
    {
        ll n,i,ans=1;
        scanf("%lld",&n);
        for(i=1;i<=n;i++)
        {
            ans*=i;
            while(1)
            {
                if(ans%10!=0)
                    break;
                ans/=10;
            }
            ans%=10000;
        }
        printf("%lld
    ",ans%10);
        return 0;
    }
    

    n的阶乘末尾0的个数

    输入:

    6
    3
    60
    100
    1024
    23456
    8735373

    输出:

    0
    14
    24
    253
    5861
    2183837

    一个数 n 的阶乘末尾有多少个 0 取决于从 1 到 n 的各个数的因子中 2 和 5 的个数, 而 2 的个数是远远多余 5 的个数的, 因此求出 5 的个数即可. 题解中给出的求解因子 5 的个数的方法是用 n 不断除以 5, 直到结果为 0, 然后把中间得到的结果累加. 例如, 100/5 = 20, 20/5 = 4, 4/5 = 0, 则 1 到 100 中因子 5 的个数为 (20 + 4 + 0) = 24 个, 即 100 的阶乘末尾有 24 个 0.

    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    int main()
    {
        ll n,i,sum=0,m;
        scanf("%lld",&n);
        while(n--)
        {
            sum=0;
            scanf("%lld",&m);//635有4个5
            while(m>=5)
            {
                sum+=m/5;
                m/=5;
            }
            printf("%lld
    ",sum);
        }
    
        return 0;
    }
    
  • 相关阅读:
    7-9 红色警报 (25 分) 数据结构连通分量应用
    & | ^运算
    Codeblocks自动代码格式化快捷键(自带)
    网络攻击与防御实验四
    网络攻击与防御实验三
    网络攻击与防御实验二
    网络攻击与防御实验一
    C语言实验7
    C语言实验6
    C语言实验5
  • 原文地址:https://www.cnblogs.com/zcy19990813/p/9702710.html
Copyright © 2011-2022 走看看