杭电acm 题1061 为什么提交错了,一直wa?? 求助
ProblemDescriptionGivenapositiveintegerN,youshouldoutputthemostrightdigitofN^N.InputT...
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.
#include<iostream>
using namespace std;
int f(int a,int b,int c)
{
int t;
if(b==0) return 1%c;
if(b==1) return a%c;
t=f(a,b/2,c);
t=t*t%c;
if(b&1) t=t*a%c;
return t;
}
int main()
{
int n;
cin>>n;
while(n--)
{
int m,k;
cin>>m;
k=m;
cout<<f(m,k,10)<<endl;
}
return 0;
} 展开
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.
#include<iostream>
using namespace std;
int f(int a,int b,int c)
{
int t;
if(b==0) return 1%c;
if(b==1) return a%c;
t=f(a,b/2,c);
t=t*t%c;
if(b&1) t=t*a%c;
return t;
}
int main()
{
int n;
cin>>n;
while(n--)
{
int m,k;
cin>>m;
k=m;
cout<<f(m,k,10)<<endl;
}
return 0;
} 展开
2个回答
展开全部
//******************************************************************************
// 杭电1061 Rightmost Digit 代码已AC
//
// N(1<=N<=1,000,000,000). N 可能很大。无法直接计算后求余。。
//不过可以考虑一些规律。。。。希望对你有所帮助
//
//******************************************************************************
#include<stdio.h>
int main()
{
int i, n, t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
if(n % 10 == 0 || n % 10 == 1 || n % 10 == 5 || n % 10 == 6 || n % 10 == 9)
printf("%d\n", n % 10);
else if(n % 10 == 2)
switch(n % 4)
{
case 0:
printf("6\n");
break;
case 1:
printf("2\n");
break;
case 2:
printf("4\n");
break;
default :
printf("8\n");
break;
}
else if(n % 10 == 3)
switch(n % 4)
{
case 1:
printf("3\n");
break;
case 2:
printf("9\n");
break;
default :
printf("7\n");
break;
}
else if(n % 10 == 4)
switch(n % 2)
{
case 0:
printf("6\n");
break;
case 1:
printf("4\n");
break;
}
else if(n % 10 == 7)
switch(n % 8)
{
case 0:
case 4:
printf("1\n");
break;
case 1:
case 5:
printf("7\n");
break;
case 2:
case 6:
printf("9\n");
break;
case 3:
case 7:
printf("3\n");
break;
}
else if(n % 10 == 8)
switch(n % 8)
{
case 0:
case 4:
printf("6\n");
break;
case 1:
case 5:
printf("8\n");
break;
case 2:
case 6:
printf("4\n");
break;
case 3:
case 7:
printf("2\n");
break;
}
}
}
//******************************************************************************
// 祝学习进步,更上一层楼 *(^-^)*
//******************************************************************************
// 杭电1061 Rightmost Digit 代码已AC
//
// N(1<=N<=1,000,000,000). N 可能很大。无法直接计算后求余。。
//不过可以考虑一些规律。。。。希望对你有所帮助
//
//******************************************************************************
#include<stdio.h>
int main()
{
int i, n, t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
if(n % 10 == 0 || n % 10 == 1 || n % 10 == 5 || n % 10 == 6 || n % 10 == 9)
printf("%d\n", n % 10);
else if(n % 10 == 2)
switch(n % 4)
{
case 0:
printf("6\n");
break;
case 1:
printf("2\n");
break;
case 2:
printf("4\n");
break;
default :
printf("8\n");
break;
}
else if(n % 10 == 3)
switch(n % 4)
{
case 1:
printf("3\n");
break;
case 2:
printf("9\n");
break;
default :
printf("7\n");
break;
}
else if(n % 10 == 4)
switch(n % 2)
{
case 0:
printf("6\n");
break;
case 1:
printf("4\n");
break;
}
else if(n % 10 == 7)
switch(n % 8)
{
case 0:
case 4:
printf("1\n");
break;
case 1:
case 5:
printf("7\n");
break;
case 2:
case 6:
printf("9\n");
break;
case 3:
case 7:
printf("3\n");
break;
}
else if(n % 10 == 8)
switch(n % 8)
{
case 0:
case 4:
printf("6\n");
break;
case 1:
case 5:
printf("8\n");
break;
case 2:
case 6:
printf("4\n");
break;
case 3:
case 7:
printf("2\n");
break;
}
}
}
//******************************************************************************
// 祝学习进步,更上一层楼 *(^-^)*
//******************************************************************************
展开全部
找一个循环节就可以了,其实也可以打表。
#include <cstdio>
using namespace std;
int main() {
int t, n;
scanf("%d", &t); while (t--) {
scanf("%d", &n);
int rm = n % 10;
int ret = rm;
int k = -1;
for (int i = 1; i < n; ++i) {
ret = ret * rm % 10;
if (ret == rm) {
k = i; break;
}
}
n = (n - 1) % k;
for (int i = 0; i < n; ++i) {
ret = ret * rm % 10;
}
printf("%d\n", ret);
}
return 0;
}
追问
为什么我的运行没错,可是提交wa呢?
追答
HDU上的数据不止题目中看到的那些,你可以自己输几个数据测试一下看看跟想像中的答案是不是相符。特殊一点的数据,边界数据都需要尝试。比如末位是0,是1。其实总共也就10种情况。= =
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询