Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int reverseNumber(int num);
- int main()
- {
- int num;
- cin >> num;
- int reversedNum;
- reversedNum=reverseNumber(num);
- cout << reversedNum << endl;
- return 0;
- }
- int reverseNumber(int num)
- {
- // 當數字為 0 時直接輸出 0
- if (num == 0)
- {
- cout << num << endl;
- return 0;
- }
- // 反轉數字同時忽略尾部的零
- int reversedNum = 0;
- while (num > 0)
- {
- int digit = num % 10; // 獲取最後一位數
- num /= 10; // 移除最後一位數
- if (digit == 0 && reversedNum == 0)
- {
- // 忽略尾部的零
- continue;
- }
- reversedNum = reversedNum * 10 + digit; // 將數字反轉
- }
- return reversedNum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement