HDOJ---1061 Rightmost Digit[简单数学题]

本文介绍了一道编程题目RightmostDigit的解决方案,该题目要求输出正整数N的N次方最右侧的数字。通过使用预计算表格的方法,有效地解决了大规模输入情况下的计算效率问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Rightmost Digit

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


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.
 

 

Author
Ignatius.L
 
 
 
 
 
 
 
 
code:
 1 #include<iostream>
 2 using namespace std;
 3 
 4 int table[10][10]={
 5     0,0,0,0,0,0,0,0,0,0,
 6     1,1,1,1,1,1,1,1,1,1,
 7     2,4,8,6,2,4,8,6,2,4,
 8     3,9,7,1,3,9,7,1,3,9,
 9     4,6,4,6,4,6,4,6,4,6,
10     5,5,5,5,5,5,5,5,5,5,
11     6,6,6,6,6,6,6,6,6,6,
12     7,9,3,1,7,9,3,1,7,9,
13     8,4,2,6,8,4,2,6,8,4,
14     9,1,9,1,9,1,9,1,9,1
15 };
16 
17 int cnt[10]={10,10,4,4,2,10,10,4,4,2};
18 
19 int main()
20 {
21     int t;
22     int n;
23     scanf("%d",&t);
24     while(t--)
25     {
26         scanf("%d",&n);
27         int temp=n;
28         temp%=10;
29         if(!temp)
30         {
31             printf("0\n");
32             continue;
33         }
34         printf("%d\n",table[temp][(n-1)%cnt[temp]]);
35     }
36     return 0;
37 }

 

转载于:https://www.cnblogs.com/XBWer/archive/2012/08/18/2645000.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值