?? 取n位的數字.cpp
字號:
#include<iostream>
using namespace std;
#include<cmath>
int f(unsigned long x,int n,int &Lxn);
void main()
{
unsigned long x;
int n,Lxn;
cout<<"請輸入在0-4294967295之間的整數,否則后果自負"<<endl;
cin>>x;
cout<<"請輸入要獲得數字在第幾位(從左往右):"<<endl;
cin>>n;
cout<<x<<"的第"<<n<<"位上的數字為"<<Lxn<<endl
<<"倒數第"<<n<<"位上的數字為"<<f(x,n,Lxn)<<endl;
}
int f(unsigned long x,int n,int &Lxn)
{
int a;
int i,j;
int w;
if(x<10)
{
while(n>1)
{cout<<"超出了位數,請重新輸入:";
cin>>n;}
Lxn=x;
return a=x;
}
else if (x<pow(10,2))w=2;
else if (x<pow(10,3)) w=3;
else if (x<pow(10,4)) w=4;
else if (x<pow(10,5)) w=5;
else if (x<pow(10,6)) w=6;
else if (x<pow(10,7)) w=7;
else if (x<pow(10,8)) w=8;
else if(x<pow(10,9)) w=9;
else w=10;
while(n>w)
{cout<<"超出了位數,請重新輸入:";
cin>>n;}
unsigned long x1=x;
for (i=1;i<=w-n;i++) //把n以后的位數都除去
x=x/10;
Lxn=x%10; //和10取余得到第n位數字
for (j=1;j<=n-1;j++)
x1=x1/10;
a=x1%10;
return a;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -