Rightmost Digit

Rightmost Digit_hdu_ hdu题解的答案十分简单的一个题,思路清晰,特别简单 立即下载

题目描述
Given a positive integer N, you should output the most right digit of N^N.

输入
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).

输出
For each test case, you should output the rightmost digit of N^N.

样例输入
2
3
4
样例输出
7
6

题目意思就是,求输入数N的N次方个位上的数。

#include<stdio.h>
int main()
{
    int n,a,i,b,c,d;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&a);
        b=a%10;
        c=a%4;
        if(c==0)
        c=4;
        d=1;
        for(i=0;i<c;i++)
        d*=b;
        d%=10;
        printf("%d\n",d); 
    }
 } 

对于过大的数据,不能一味地用暴力循环的方法解决问题,要么找到更加高效的方法,或者就是寻找是否有周期,有规律可循。
以上的代码是可以通过OJ的,在这之前有一个代码通过不了OJ,输出结果却是和以上的代码一样,待以后学会debug或是请教大神解决。以下是代码

#include<stdio.h>
int main()
{
    int i,m,j,n,p,a[5];
    scanf("%d",&p);
    for(i=1;i<=p;i++)
    {
        scanf("%d",&n);
        a[1]=n%10;
        for(m=2;m<=4;m++)
        {
            //a[m]=(a[m-1]*n%10)%10;
            a[m]=(a[m-1]*(n%10))%10;
        }
        if(n%4==0)
        {
            printf("%d\n",a[4]);
        }
        else
        {
            printf("%d\n",a[n%4]);
        }
    }
 } 

以上代码已做修改,可以通过oj,错误处用//标出,如果没有(),计算机会优先计算*,这样就会使数字超出运算范围,有效范围为1000000000,如果带入的数为999999998,在运算时就会运算到999999998*8,超出有效范围,使运算错误,以后写代码还是尽量保持各变量之间的独立性,以防变量在循环中发生改变。

华为云HCS解决方案笔记HUAWEI CLOUD Stack【面试篇】 华为云解决方案介绍、华为云服务介绍,华为云网络平面规划等 阅读详情

相关推荐

四自由度机械臂Matlab仿真全流程:从DH参数建模到轨迹规划实战

本文详细介绍了四自由度机械臂在Matlab中的完整仿真流程,从DH参数建模到轨迹规划实战。通过Robotics Toolbox工具,读者可以学习如何构建机械臂模型、验证运动学特性,并实现平滑的轨迹规划。文章还提供了性能优化技巧和常见问题解决方案,帮助开发者高效完成机械臂仿真项目。

weixin_29061041的博客 241

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. Eac

weixin_54664477的博客 293

Arcgis填补栅格空缺值Nodata

Arcgis填补栅格空缺值Nodata

pass17的博客 1万+

Rightmost Digit(HDU1061) 两种方法 快速幂、打表

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

Shanhj的博客 397

WIN10环境下STK11.5(含Analyzer, EOIR, Scheduler插件)的安装与破解全攻略

本文提供了一份详细的WIN10环境下STK11.5软件及其核心插件(Analyzer, EOIR, Scheduler)的安装与破解全攻略。内容涵盖从安装包准备、系统环境检查,到基础版安装、插件部署、破解文件替换及许可证配置的完整步骤,并附有常见问题排错指南,旨在帮助用户免费搭建功能齐全的卫星仿真分析环境。

solidity8miner的博客 758

简易背单词系统C/C++

#include&lt;stdio.h&gt; #include&lt;string.h&gt; int F=20,N=100,k=0; int score[100]; int count=0; void hello(); void add(); void preview(); void mdelete(); void etc(); void cte(); void mscore(); void...

青木年华 9154

Rightmost Digit(快速幂)

Rightmost Digit(快速幂) 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 foll

Eagle222的博客 358

服务器取证——服务器基础知识

服务器基础知识

记录并分享学习安全的知识点.. 2483
上一篇: 石头剪刀布
下一篇: Elevator
Ethan_YS
博客等级 码龄10年 3粉丝 7原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值