在 C++ 中查詢第 M 個數字,其數字的重複和為 N
在這個問題中,我們給定兩個正數 N 和 M。我們的任務是 *查詢第 M 個數字,其數字的重複和為 N*。
問題描述:在這裡,我們需要找到第 M 個數字,其數字的和直到和變為一位數等於 N。
讓我們舉個例子來理解這個問題,
輸入:N = 4 M = 6
輸出:49
解決方案方法
問題的簡單解決方案是找到所有數字並計算數字和為 N 的數字,並返回第 m 個數字。
解決問題的另一種方法是使用公式來查詢數字和等於 N 的第 M 個數字,
第 M 個數字 = (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
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP