zoukankan      html  css  js  c++  java
  • HDOJ 1061 Rightmost Digit

    找出数学规律

    原题:

    Rightmost Digit

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


    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.

    讲解:(摘自网上)

      对于数字0~9, 它们的N次方,我们只看最后一个数字:
      0^1=0; 0^2=0; 循环周期T=1;
      1^1=1; 1^2=1; 循环周期T=1;
      2^1=2; 2^2=4; 2^3=8; 2^4=6 2^5=2; 循环周期T=4;
      3^1=3; 3^2=9; 3^3=7; 3^4=1;3^5=3; 循环周期T=4;
      4^1=4; 4^2=6; 4^3=4; 循环周期T=2;
      
      后面的大家可以自己算,会得出一个规律:
      他们的循环周期只有1,2,4这三种,所以对于源代码里的a,我们可以只算a%4次就可以了,大大减少了循环的次数

    源代码:

     1 #include <iostream>
     2 using namespace std;
     3 
     4 int zhou[10];
     5 
     6 int main()    {
     7     int N;    cin >> N;
     8     while (N--)    {
     9         long long int num;
    10         cin >> num;
    11         int a = num % 10;
    12         int b = (a * a) % 10;
    13         int c = (b * a) % 10;
    14         int d = (c * a) % 10;
    15         int m = num % 4;
    16         switch(m)    {
    17             case 0: cout << d << endl;    break;
    18             case 1:    cout << a << endl;    break;
    19             case 2: cout << b << endl;    break;
    20             case 3: cout << c << endl;    break;
    21         }
    22     }
    23     return 0;
    24 } 
  • 相关阅读:
    windows7上使用docker容器
    centos7 docker镜像加速器配置
    用列表生成器打印九九乘法表
    -bash: wget: command not found的两种解决方法
    centos7 Dockerfile安装nginx
    centos6.5关闭防火墙命令
    centos7开机启动tomcat7
    centos7安装tomcat7
    CentOS7防火墙firewalld
    poj_3662 最小化第k大的值
  • 原文地址:https://www.cnblogs.com/QingHuan/p/4270594.html
Copyright © 2011-2022 走看看