| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1790 人关注过本帖
标题:m的n次方的最后一位的数字
只看楼主 加入收藏
fl8962
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:14
帖 子:539
专家分:2471
注 册:2012-10-17
结帖率:96.23%
收藏
已结贴  问题点数:100 回复次数:6 
m的n次方的最后一位的数字
输入两个数一个是m,另一个是n。编写程序得出m^n 的最后一位数字,比如2^2=4,所以最后一位数字为4,比如2^4=16,所以最后一位数字为6.
下面是我写的代码:
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
  int n,m;
  cout<<"input m firstly and then n"<<endl;
  cin>>m;
  cin>>n;
  int temp=1;
  for(int i=0;i!=n;++i)
    {
      temp=temp*m;
      if(temp>9)
        temp=temp%10;
    }
  cout<<temp<<endl;


  return 0;
}
这个思路我大概2分钟写完,但是后面用了几个小时都没能找出更快的方法。我求更快的算法,运行时间比我这个长的就别贴出来了
搜索更多相关主题的帖子: 编写程序 include return 
2015-09-12 01:57
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:34 
优化了算法,准确值底了,有点烂
#include<iostream>
#define TEMP 0.1
using namespace std;
int main()
{
      int n,m;
      cout<<"input m firstly and then n"<<endl;
      cin>>m;
      cin>>n;
      int temp_a =1;
      for(int i = 0; i < n; i++){
        temp_a *=  m;
    }
    int temp_b,temp_c;
    temp_c = temp_a * TEMP;
    temp_b = temp_a % temp_c;
    cout<<temp_a<< ":"<<temp_b<<endl;
    return 0;
}
2015-09-12 03:55
fl8962
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:14
帖 子:539
专家分:2471
注 册:2012-10-17
收藏
得分:0 
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
  cout<<"input m"<<endl;
  int m,n;
  int array_2[4]={2,4,8,6};
  int array_3[4]={3,9,7,1};
  int array_4[2]={4,6};
  int array_7[4]={9,3,1,7};
  int array_8[4]={4,2,6,8};
  int array_9[2]={9,1};
  cin>>m;
   cout<<"input n"<<endl;
  cin>>n;
  m=m%10;
  switch(m)
    {
    case 1:
      cout<<"the output is :"<<"1"<<endl;
      break;
    case 2:
      {
    int x=n%4;
    if(x==0)
      cout<<"the output is :"<<array_2[3]<<endl;
    else
      cout<<"the output is :"<<array_2[x-1]<<endl;
    break;
      }
    case 3:
      {
    int x=n%4;
    if(x==0)
      cout<<"the output is :"<<array_3[3]<<endl;
    else
      cout<<"the output is :"<<array_3[x-1]<<endl;
    break;
      }
    case 4:
      {
    int x=n%2;
    if(x==0)
      cout<<"the output is :"<<array_4[1]<<endl;
    else
      cout<<"the output is :"<<array_3[x-1]<<endl;
    break;
      }
    case 5:
      cout<<"the output is :"<<"5"<<endl;
      break;
    case 6:
      cout<<"the output is :"<<"6"<<endl;
      break;
    case 7:
      {
    int x=n%2;
    if(x==0)
      cout<<"the output is :"<<array_7[3]<<endl;
    else
      cout<<"the output is :"<<array_3[x-1]<<endl;
    break;
      }
    case 8:
      {
    int x=n%2;
    if(x==0)
      cout<<"the output is :"<<array_8[3]<<endl;
    else
      cout<<"the output is :"<<array_8[x-1]<<endl;
    break;
      }
    case 9:
      {
    int x=n%2;
    if(x==0)
      cout<<"the output is :"<<array_9[1]<<endl;
    else
      cout<<"the output is :"<<array_9[x-1]<<endl;
    break;
      }
    default :
      break;
    }
  return 0;
}
这是第二个想出的方法,使用时间:
input m
232398
input n
10000000000000000000000000000
the output is :4
0.000u 0.000s 0:13.37 0.0% (0.13秒):第一个方法测试这组数据大概要15秒,快了很多。大家有更快的方法贴出来

想抽苏烟了。
2015-09-12 04:14
fl8962
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:14
帖 子:539
专家分:2471
注 册:2012-10-17
收藏
得分:0 
回复 2楼 hjx1120
实际你这个算法比我之前写的还要差,直接看到你的temp_a=temp*m 就应该错了。int 型变量只有4个byte, 能存储多大的数?2的32次方就能让你溢出。。。

想抽苏烟了。
2015-09-12 04:26
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:34 
0^M    尾数都是0
1^M    尾数都是1
5^M    尾数都是5
6^M    尾数都是6
2^M    尾数2486循环
3^M    尾数3971循环
4^M    尾数4646循环
7^M    尾数7931循环
8^M    尾数8426循环
9^M    尾数9191循环

DO IT YOURSELF !
2015-09-12 07:50
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
哦 楼主3楼代码 已经使用了

DO IT YOURSELF !
2015-09-12 07:54
快速回复:m的n次方的最后一位的数字
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015358 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved