//求低次方的尾数,后三位数
//方法一:未知位数的求各个位数----求其数的位数-----按位求后三位数,比较复杂
//方法二:直接将结果除以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;}