求后三位数


//求低次方的尾数,后三位数

//方法一:未知位数的求各个位数----求其数的位数-----按位求后三位数,比较复杂

//方法二:直接将结果除以1000得到后三位数的余数,然后按位求数

#include<iostream>

#include<cmath>

using namespace std;

int main()

    {

     int n,m;

     int s,t,f=100;

     cout<<"please input a value:"<<endl;

     cin>>n;

     cout<<"please input the cimi you want:"<<endl;

     cin>>m;

     n=pow(n,m);

     cout<<"the value to count is:  "<<n<<endl;

     s=n%1000;// 改变1000及f的初值,可以求任意指定位数的后几位数

     while(s!=0)

         {
           t=s/f;

           s=s%f;

           f=f/10;

           cout<<"the number is:"<<t<<endl;
        
         }

    return 0;}



//the another method first search the whole numbers of this value:

#include<iostream>

#include<cmath>

using namespace std;

int main()

    {
      int  m,n,f=1;

      int s,t;

      cout<<"input the value:"<<endl;

      cin>>n;

      cout<<"input the cimi:"<<endl;

      cin>>m;

    n=pow(n,m);

    t=n;

    cout<<"the result is:"<<n<<endl;

      //求n的位数

    while(n/10!=0)

        {
         n=n/10;

         f=f*10;
        
        }

     cout<<"the f is:"<<f<<endl;

    //求n的各个位数

    while(t!=0)

        {  
         s=t/f;

         t=t%f;

         if(f<=100) cout<<s<<endl;
//改变if 条件,可以显示任意个尾数
         f=f/10;
        
        }
        
    return 0;}


//这两种方法只适用于数值不超过int范围的数,对于次方很高,超过了Int 范围的,

//则可采用乘法的规则:乘积的最后三位的值只与乘数和被乘数的后三位有关,与乘数和被乘数的高位无关。

#include<iostream>

using namespace std;

int main()

    {
     int m,n;

     int last=1,i;

     cout<<"input the value:"<<endl;

     cin>>n;

     cout<<"input the cimi:"<<endl;

     cin>>m;

     for(i=1;i<=m;i++)

         last=n*last%1000;//每次乘积后只保留后三位----------改变1000的位数,可求任意位尾数

     cout<<"the result is:"<<last<<endl;

    return 0;}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值