哈喽大家好,我是@菜就多练,输不起,就别玩,今天我来和大家分享一下数字拆分
数字拆分就是把数字拆开来
比如12345把他变成1 2 3 4 5,这就是数字拆分
那么,我们来看代码吧
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
cin>>n;
while(n!=0){
cout<<n%10<<" ";
n/=10;
}
return 0;
}
这个代码是把n进行数位分离,但是,数字拆分之后这个分离的数是倒过来的,大家别忘记
如果我们知道了数位分离,那我们就可以做很多事情
比如数字和
#include<bits/stdc++.h>
using namespace std;
int n,s=0;
int main(){
cin>>n;
while(n!=0){
s+=n%10;
n/=10;
}
cout<<s;
return 0;
}
回文数判断
#include<bits/stdc++.h>
using namespace std;
bool hws(int x){
int y=x;
int s=0;
while(x!=0){
s=s*10+x%10;
x/=10;
}
if(s==y) return true;
return false;
}
int n;
int main(){
cin>>n;
if(hws(n)==true){
cout<<"yes";
}
else cout<<"no";
return 0;
}
等等等等
好,今天的分享就到这里了
有兴趣的小伙伴欢迎留言给我
那么我们下期不见不散,拜拜