Rightmost Digit & Leftmost Digit

做题恰巧碰到,归纳一下

                              Rightmost Digit  Hdu1061

                                                                             

                                              Rightmost Digit

                                                              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                                                 Total Submission(s): 52550    Accepted Submission(s): 19917



Problem Description Given a positive integer N, you should output the most right 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 rightmost digit of N^N.

 

Sample Input

2 3 4  


Sample Output

7 6
Hint In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7. In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.

解题思路: 对于这类的大数,用暴力直接算肯定别想。。。

最右位的数字只与相乘的数的最右位相关,即12的最右位为2^12得最右位。这样算还不行,本来想打表,打着打着发现有规律,有循环。然后,详见代码

#include <iostream>
#include<cstdio>
#include <cmath>

using namespace std;

int main()
{
    long long n;
    int k;
    scanf("%d",&k);
    while(k--)
    {
      scanf("%lld",&n);
      int t=n%10;
        if(!t)cout<<0<<endl;
        else if(t==1) cout<<1<<endl;
        //else if(t==4) cout<<6<<endl;
        else if(t==5) cout<<5<<endl;
        else if(t==6) cout<<6<<endl;
        else if(t==2||t==3||t==7||t==8)
        {
            int tt=n%4;
            int a[4][4]={{6,2,4,8},{1,3,9,7},{1,7,9,3},{6,8,4,2}};
            if(t==2) cout<<a[0][tt]<<endl;
            if(t==3) cout<<a[1][tt]<<endl;
            if(t==7) cout<<a[2][tt]<<endl;
            if(t==8) cout<<a[3][tt]<<endl;
        }
          else
          {
              int tt=n%2;
              int a[2][2]={{6,4},{1,9}};
               if(t==4) cout<<a[0][tt]<<endl;
               if(t==9) cout<<a[1][tt]<<endl;
          }
    }
    return 0;
}


Leftmost Digit HDU 1061

Leftmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17594    Accepted Submission(s): 6805

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.

解题思路:首先设:
n^n=a*10^b (1<=a<10)
a的整数位,就是最左位,现在就是求a
两边同时对10取对数: lg(n^n)= n*lg n =lg a + b*lg 10 =lg a + b
lg a = n*lg n + b
a = 10^(n*lg n + b), b = (int) n*lg n
a = 10^(n*lg n + (int)n*lg n)


ans=(int)a 代码如下:

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
    int kase;
    long long n, ans;
    long double t;
    scanf("%d", &kase);
    while (kase--)
    {
        scanf("%lld", &n);
        t = n * log10(n+0.0);
        t -= (long long)t;
        ans = pow((long double)10, t);
        printf("%lld\n", ans);
    }
    return 0;
}


































点赞