您的位置:首页 > 其它

HDOJ 1061 Rightmost Digit

2014-05-17 18:56 323 查看

Rightmost Digit

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


[align=left]Problem Description[/align]
Given a positive integer N, you should output the most right digit of N^N.

[align=left]Input[/align]
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).

[align=left]Output[/align]
For each test case, you should output the rightmost digit of N^N.

[align=left]Sample Input[/align]

2
3
4

[align=left]Sample Output[/align]

7 6
本题的大意是输入一个1到1000000000的整数N,要求输出N^N次方后的最后一个数字.....
解题思路:因为数据很大,只需要输出最后一位,所以对每个数据先求余得到最后一位数字,然后找出其最后一位的周期即可....

/**************************************************
HDOJ 1061 已经AC
***************************************************/
#include <iostream>
using namespace std;
int main()
{
int N,T;
int round=0;
int r[100]={0};
int i=0;
cin>>T;
while(T--)
{
cin>>N;
r[1]=N%10;//一定要对其求余
r[2]=(N%10)*(N%10)%10;//求余后再相乘,然后再求余,因为如果直接相乘时碰到1000000000时会发生错误
for(i=3;;i++)
{
r[i]=r[i-1]*(N%10)%10;
if(r[i]==r[1])
{round=i-1;break;}
}
if(N%round==0)
cout<<r[round]<<endl;
else cout<<r[N%round]<<endl;
}
//while(1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: