c++输入一个不多于5位数的正整数(1)求它是几位数;(2)分别打印出每位数字(3)按逆序打印出各位数字
c++输入一个不多于5位数的正整数(1)求它是几位数;(2)分别打印出每位数字(3)按逆序打印出各位数字
·
-
- 方法一:
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
char a[50];
cin>>a;
//求它是几位数
int b=strlen(a);
cout<<b<<endl;
//分别打印引出每位数字
for(int i=0;i<b;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
//按逆序打印出各位数字
for (int i=b-1;i>=0;i--)
{
cout<<a[i]<<" ";
}
}
-
- 方法二:
#include<iostream>
using namespace std;
int main()
{
int num;
cin>>num;
int a,b,c,d,e;
a=num/10000;
b=num/1000%10;
c=num/100%10;
d=num/10%10;
e=num%10;
if(a!=0)
{
cout<<e<<" "<<d<<" "<<c<<" "<<b<<" "<<a<<endl;
cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl;
}
else if (b!=0)
{
cout<<e<<" "<<d<<" "<<c<<" "<<b<<endl;
cout<<b<<" "<<c<<" "<<d<<" "<<e<<endl;
}
else if (c!=0)
{
cout<<e<<" "<<d<<" "<<c<<endl;
cout<<c<<" "<<d<<" "<<e<<endl;
}
else if (b!=0)
{
cout<<e<<" "<<d<<endl;
cout<<d<<" "<<e<<endl;
}
else
{
cout<<e<<endl;
cout<<e<<endl;
}
}
更多推荐
所有评论(0)