HDOJ 1061 Rightmost Digit(循环问题)
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
题意:很简单,就是输出n^n的最后一个数字时什么。
思路:前面有过一个0-9的n次方的题目,HDOJ1097题,那一题中我用代码推出了循环节,这个题目,我用的循环节全为4了.
HDOJ1097题博客链接:http://blog.csdn.net/qq_26525215/article/details/50949847
import java.util.Scanner;
public class Main{
static long db[][] = new long[10][4];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
dabiao();
// System.out.println(db[4][0]);
// System.out.println(db[4][3]);
//
long t = sc.nextLong();
while(t-->0){
int n = sc.nextInt();
int f=n%10;
int m=n%4;
//System.out.println(m);
m--;
if(m<0){
m=3;
}
System.out.println(db[f][m]);
}
}
private static void dabiao() {
for(int i=1;i<=9;i++){
for(int j=0;j<4;j++){
db[i][j]=dabiao(i,j);
//System.out.print(db[i][j]+" ");
}
//System.out.println();
}
}
private static long dabiao(long i, long j) {
long m=i;//4,3
for(int k=1;k<=j;k++){
m=(m*i)%10;
}
m=m%10;
return m;
}
}
HDOJ 1061 Rightmost Digit(循环问题)的更多相关文章
- HDOJ 1061 Rightmost Digit
找出数学规律 原题: Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- hdoj 1061 Rightmost Digit【快速幂求模】
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1061 Rightmost Digit --- 快速幂取模
HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...
- 杭电 1061 Rightmost Digit计算N^N次方的最后一位
Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...
- 题解报告:hdu 1061 Rightmost Digit(快速幂取模)
Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...
- 快速幂 HDU 1061 Rightmost Digit *
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 【HDOJ】1061 Rightmost Digit
这道题目可以手工打表,也可以机器打表,千万不能暴力解,会TLE. #include <stdio.h> #define MAXNUM 1000000001 ][]; int main() ...
- HDU 1061 Rightmost Digit解决问题的方法
求大量N^N的值最右边的数字,即最低位. 它将能够解决一个简单二分法. 只是要注意溢出,只要把N % 10之后.我不会溢出,代替使用的long long. #include <stdio.h&g ...
- hdu 1061 Rightmost Digit
解决本题使用数学中的快速幂取余: 该方法总结挺好的:具体参考http://www.cnblogs.com/PegasusWang/archive/2013/03/13/2958150.html #in ...
随机推荐
- [Angular 2] Passing Template Input Values to Reducers
Angular 2 allows you to pass values from inputs simply by referencing them in the template and passi ...
- jQuery UI Widget(1.8.1)工作原理--转载
先看下代码的相关注释: /*! * jQuery UI Widget 1.8.1 * * Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/abo ...
- [AngularJS 2 实践 一]My First Angular App
最近一直在看关于AngularJS 2的资料,查看了网上和官网很多资料,接下来就根据官网教程步骤一步步搭建我的第一个Angular App AngularJS 2说明请参考:http://cnodej ...
- Linux GRUB 2及修改默认启动项
The GRUB 2 boot loader makes sure that you can boot Linux. GRUB 2 is installed in the boot sector of ...
- 一个简单的Verilog计数器模型
一个简单的Verilog计数器模型 功能说明: 向上计数 向下计数 预装载值 一.代码 1.counter代码(counter.v) module counter( input clk, input ...
- Oracle 空间管理
表空间:组数据文件的一种途径 分类: 目录表空间(sysaux) 常表空间(system) 系统临时表空间(temp) 用户临时表空间(user) undo表空间 创建表空间: //表空间名为name ...
- Try,Catch,Finally三块中如果有Return是怎么个运行顺序
今天看一个Java SSH的面试题,题目大概意思是:try.catch中存在return语句,还会执行finally块吗?如果执行,是return先执行还是finally先执行?如果有多个return ...
- Eclipse利用代理快速安装插件
在eclipse启动时增加以下参数: eclipse.exe -vmargs -DproxySet=true -DproxyHost=aProxyAddress -DproxyPort=aProxyP ...
- Asp.Net WebAPI 中Cookie 获取操作方式
1. /// <summary> /// 获取上下文中的cookie /// </summary> /// <returns></returns> [H ...
- c#0银行存款计算器
简介: 为银行存款客户提供一个超级计算器,简单直观操作界面,提供一个银行本意到期金额结算查询程序,方便用户选择存款方式. 功能截图: 实验步骤:利用工具栏建造窗体设计如图: 1.建立2个GroupBo ...