โจทย์
ได้รับจำนวนบวก N และ M ทางเราต้อง หาเลขที่ M-th ที่ผลรวมตัวเลขซ้ำจนเหลือ 1 หลัก (Digital Root) เท่ากับ N
ตัวอย่าง
Input: N = 4, M = 6
Output: 49
แนวทางแก้ปัญหา
สังเกตว่า Digital Root ของจำนวนเต็มบวกจะวนรอบทุก 9 ตัว (1-9, 10-18, ...)
สูตรโดยตรง: M-th number = (M-1)*9 + N
โปรแกรมตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 4, m = 6;
int mNumber = (m - 1) * 9 + n;
cout << m << "-th number whose repeated sum of digits of a number is " << n << " is " << mNumber;
return 0;
}
ผลลัพธ์
6-th number whose repeated sum of digits of a number is 4 is 49