给出一个不多于3位的正整数 n 要求:求出它是几位数;分别打印出每一位数字数字之间加一个空格;按逆序打印出各位数字数字之间加一个空格用devc++
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int count = 0; // 记录位数
int temp = n;
while (temp > 0) {
count++;
temp /= 10;
}
cout << "位数:" << count << endl;
cout << "每位数字:";
for (int i = count; i > 0; i--) {
int digit = n / pow(10, i-1);
cout << digit << " ";
n %= (int)pow(10, i-1);
}
cout << endl;
cout << "逆序数字:";
while (n > 0) {
int digit = n % 10;
cout << digit << " ";
n /= 10;
}
cout << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/NjI 著作权归作者所有。请勿转载和采集!