เราได้รับตัวเลข N เป้าหมายคือการนับจำนวนการหมุนเวียนของ N ที่ทำให้ได้เลขคี่ และการหมุนเวียนที่ทำให้ได้เลขคู่
เช่น N = 123 การหมุนเวียนคือ 123, 321, 132 เลขคี่: 123, 321 (2 ตัว) เลขคู่: 132 (1 ตัว)
ตัวอย่าง
อินพุต: N = 54762
เอาต์พุต:
Count of rotations of N which are Odd are: 2 Count of rotations of N which are Even are: 3
คำอธิบาย: การหมุนเวียน: 54762, 25476, 62547, 76254, 47625 เลขคู่: 54762, 25476, 76254 (3 ตัว) เลขคี่: 62547, 47625 (2 ตัว)
อินพุต: N = 3571
เอาต์พุต:
Count of rotations of N which are Odd are: 4 Count of rotations of N which are Even are: 0
แนวทางการแก้ปัญหา
เลขคี่หรือคู่ขึ้นอยู่กับหลักหน่วย ขณะหมุนเวียน ทุกหลักจะได้เป็นหลักหน่วย สามารถหาร 10 แล้วดูหลักหน่วยว่าเป็นคี่/คู่แล้วนับตามนั้น
การนำไปใช้ (Implementation)
#include <bits/stdc++.h>
using namespace std;
void Even_Odd_rotation(int N) {
int Even_rotation = 0;
int Odd_rotation = 0;
do {
int value = N % 10;
if (value % 2 == 1) {
Odd_rotation++;
} else {
Even_rotation++;
}
N = N / 10;
} while (N != 0);
cout << "Count of rotations of N which are Odd are: " << Odd_rotation;
cout << "\nCount of rotations of N which are Even are: " << Even_rotation;
}
int main() {
int N = 341;
Even_Odd_rotation(N);
return 0;
}
เอาต์พุต
Count of rotations of N which are Odd are: 2 Count of rotations of N which are Even are: 1