HDOJ 1062 Rightmost Digit

來源:互聯網
上載者:User

標籤:acm   hdoj   杭電   

【題意】求N^N,輸出最右邊的那一位。

【代碼1:獲得周期】

#include <iostream>#include <iomanip>#include <cstring>#include <cstdlib>#include <cstdio>using namespace std;int main(){    int N = 0;    cin >> N;    while (N--)    {        int mul = 1, n = 0, r = 0, i = 0;        cin >> n;        r = n%10;        for (i = 0; i < n; i++)        {            mul *= r;            mul = mul%10;        }        cout << mul << endl;    }    return 0;}

【代碼2:根據周期輸出】

<pre name="code" class="cpp">#include <iostream>#include <iomanip>#include <cstring>#include <cstdlib>#include <cstdio>using namespace std;int main(){    int N = 0;    int ans[10][4] = {{0,0,0,0},{1,1,1,1},                    {2,4,8,6},{3,9,7,1},                    {4,6,4,6},{5,5,5,5},                    {6,6,6,6},{7,9,3,1},                    {8,4,2,6},{9,1,9,1}};    cin >> N;    while (N--)    {        int n = 0, r = 0, l = 0;        cin >> n;        r = n%10;        l = (n-1)%4;        cout << ans[r][l] << endl;    }    return 0;}



方法二:

來自:http://blog.csdn.net/lovelyloulou/article/details/5241471

這種方法用標誌數組去標誌周期,但是周期不是1的情況下,如果周期序列中連續出現了兩個值相同這種方法就會有bug。但是某一個數連續相乘可能也不會出現這種問題。

#include <iostream>#include <cstring>#include <stdio.h>using namespace std;bool l[10];int r[10];int main(){    int t;    while(cin>>t)    {        while(t--)        {            memset(l,0,sizeof(l));            memset(r,0,sizeof(r));            int n = 0;            cin>>n;            int a=n%10;            int b=a;            int i=1;            l[b]=true;            r[0]=b;            b=(b*a)%10;            while(!l[b])            {                l[b]=true;                r[i++]=b;                b=(b*a)%10;            }            cout<<r[(n-1)%i]<<endl;        }    }    return 0;}



HDOJ 1062 Rightmost Digit

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.