数位分离
就是将一个数字的各个位上的数取出来,例如:5437,分离:千位5,百位4,十为3,个位7。
写法一:
#include <iostream>
using namespace std;
int main()
{
int n; //我们以四位正整数为例
cout<<"请输入一个四位数:";
cin>>n; //保证输入的是4位数
cout<<"个位:"<<n%10<<endl;
cout<<"十位:"<<n%100/10<<endl;
cout<<"百位:"<<n%1000/10<<endl;
cout<<"千位:"<<n/1000;
}
写法二:
#include <iostream>
using namespace std;
int main()
{
int n; //我们以四位正整数为例
cout<<"请输入一个四位数:";
cin>>n; //保证输入的是4位数
cout<<"个位:"<<n%10<<endl;
cout<<"十位:"<<n/10%10<<endl;
cout<<"百位:"<<n/100%10<<endl;
cout<<"千位:"<<n/1000;
}
—<完>—