最大可由X整除的k个数C++程序

给出了整数X和K。任务是找到可被X整除的最高K位数。

null

例如:

Input : X = 30, K = 3
Output : 990
990 is the largest three digit 
number divisible by 30.

Input : X = 7, K = 2
Output : 98

有效解决方案 就是用下面的公式。

ans = MAX - (MAX % X)
where MAX is the largest K digit 
number which is  999...K-times

这个公式适用于简单的学校教学法。我们去掉余数得到最大的可除数。

// CPP code to find highest K-digit number divisible by X
#include <bits/stdc++.h>
using namespace std;
// Function to compute the result
int answer( int X, int K)
{
// Computing MAX
int MAX = pow (10, K) - 1;
// returning ans
return (MAX - (MAX % X));
}
// Driver
int main()
{
// Number whose divisible is to be found
int X = 30;
// Max K-digit divisible is to be found
int K = 3;
cout << answer(X, K);
}


输出:

990

请参阅完整的文章 可被X整除的最大K位数 更多细节!

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享