เราได้รับจำนวนธรรมดา num งานคือการนับจำนวนธรรมดาที่การเรียงสับเปลี่ยน (permutation) ทุกอย่างของมันมีค่ามากกว่าหรือเท่ากับตัวเลขนั้น
เงื่อนไข − ข้อมูลเป็นจำนวนธรรมดาเท่านั้น การเรียงสับเปลี่ยนทุกอย่างของจำนวนนั้นต้องเท่ากับหรือมากกว่าจำนวนที่ให้มา เช่น เลข 20
พิจารณาตัวเลข 1 ถึง 20
ตรวจสอบเลขที่การเรียงสับเปลี่ยนเท่ากับหรือมากกว่า 20 ได้ 1,2,3,4,5,6,7,8,9,11=11, 12<21, 13<31,... รวม 18 ตัว
อินพุต − num = 10
เอาต์พุต − count is 9
คำอธิบาย − เลข 1-9 การเรียงสับเปลี่ยนเท่ากับตัวมันเอง
อินพุต − num = 13
เอาต์พุต − count is 12
แนวทางที่ใช้
รับค่า num
ตั้ง max_size = 9 (มีอย่างน้อย 9 ตัวที่ตรงเงื่อนไข)
วนลูปสร้างรายการตัวเลขที่ตรงเงื่อนไขโดยสร้างตัวเลขจากหลักสิบและหลักหน่วย
ตัวอย่าง
#include<bits/stdc++.h>
using namespace std;
void count(int num){
int count = 0;
int max_size = 9;
for (int i = 1; i <= max_size; i++){
list<int> lists;
if (i <= num){ lists.push_back(i); count++; }
for(auto iter = lists.end(); iter != lists.begin(); ++iter){
int first_ele = lists.front(); lists.pop_front();
for (int next = first_ele%10; next <= 9; next++){
int temp = first_ele*10 + next;
if (temp <= num){ lists.push_front(temp); count++; }
}
}
}
cout<<"count of num "<<num<<" is "<<count<<endl;
}
int main(){
count(1); count(9); count(7); count(0); count(12);
return 0;
}
เอาต์พุต
count of num 1 is 1 count of num 9 is 9 count of num 7 is 7 count of num 0 is 0 count of num 12 is 11