求输入正整数的位数,逆序输出
方法一:
知识点:if语句,while循环
//要求:给出一个不多于5位的正整数
//1)求出是几位数 2)分别打印出每一位数字 3)按逆序打印出各位数字,如原数123,输出321
#include<iostream>
using namespace std;
int main()
{
long int n;
cout << "请输入0—99999之间的数字:";
cin >> n;
while (n < 0 || n>99999)
{
cout << "输入错误,请再次输入0—99999之间的数字:";
cin >> n;
}
int ten_thousand, thousand, hundred, ten, ones;
ten_thousand = n / 10000;
thousand = n / 1000;
hundred = n / 100;
ten = n / 10;
//ones = n;
int w, q, b, s, g,x;
w = n / 10000;
q = n / 1000 % 10;
b = n / 100 % 10;
s = n / 10 % 10;
g = n % 10;
if (ten_thousand)
{
cout << n << "是" << 5 << "位数" << endl;
/*w = n / 10000;
q = n / 1000 % 10;
b = n / 100 % 10;
s = n / 10 % 10;
g = n % 10;*/
cout << "万位数为:" << ten_thousand << endl;
cout << "千位数为:" << q << endl;
cout << "百位数为:" << b << endl;
cout << "十位数为:" << s << endl;
cout << "个位数为:" << g << endl;
x = g * 10000 + s * 1000 + b * 100 + q * 10 + ten_thousand;
cout << "逆序数为:" << x << endl;
}
else if (thousand)
{
/*q = n / 1000 ;
b = n / 100 % 10;
s = n / 10 % 10;
g = n % 10;*/
cout << n << "是" << 4 << "位数" << endl;
cout << "千位数为:" << thousand << endl;
cout << "百位数为:" << b << endl;
cout << "十位数为:" << s << endl;
cout << "个位数为:" << g << endl;
x = g * 1000 + s * 100 + b * 10 + thousand;
cout << "逆序数为:" << x << endl;
}
else if (hundred)
{
cout << n << "是" << 3 << "位数" << endl;;
cout << "百位数为:" << hundred << endl;
cout << "十位数为:" << s << endl;
cout << "个位数为:" << g << endl;
x = g * 100 + s * 10 + hundred;
cout << "逆序数为:" << x << endl;
}
else if (ten)
{
cout << n << "是" << 2 << "位数" << endl;
cout << "十位数为:" << ten << endl;
cout << "个位数为:" << g << endl;
x = g * 10 + ten;
cout << "逆序数为:" << x << endl;
}
else
{
cout << n << "是" << 1 << "位数" << endl;
cout << "个位数为:" << g << endl;
x = g ;
cout << "逆序数为:" << x << endl;
}
return 0;
}
方法二:
知识点:if语句,switch语句
#include<iostream>
using namespace std;
int main()
{
long int num;
int indiv, ten, hundred, thousand, ten_thousand, place;
cout << "enter number 0-99999:";
cin >> num;
if (num > 9999)
place = 5;
else if (num > 999)
place = 4;
else if (num > 99)
place = 3;
else if (num > 9)
place = 2;
else
place = 1;
cout << "place = " << place << endl;
//计算各位数字
ten_thousand = num / 10000;
thousand = (num - ten_thousand * 10000) / 1000;
hundred = (num - ten_thousand * 10000 - thousand * 1000) / 100;
ten = (num - ten_thousand * 10000 - thousand * 1000 - hundred * 100) / 10;
indiv = num - ten_thousand * 10000 - thousand * 1000 - hundred * 100 - ten * 10;
cout << "original order:";
switch (place)
{
case(5):cout << ten_thousand << "," << thousand << "," << hundred << "," << ten << "," << indiv << endl;
cout << "reverse order:";
cout << indiv << ten << hundred << thousand << ten_thousand << endl;
break;
case(4):cout << thousand << "," << hundred << "," << ten << "," << indiv << endl;
cout << "reverse order:";
cout << indiv << ten << hundred << thousand << endl;
break;
case(3):cout << hundred << "," << ten << "," << indiv << endl;
cout << "reverse order:";
cout << indiv << ten << hundred << endl;
break;
case(2):cout << ten << "," << indiv << endl;
cout << "reverse order:";
cout << indiv << ten << endl;
break;
case(1):cout << indiv << endl;
cout << "reverse order:" << indiv << endl;
break;
}
return 0;