Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ n位数可被给定数整除的数_C++_Modulo - Fatal编程技术网

C++ n位数可被给定数整除的数

C++ n位数可被给定数整除的数,c++,modulo,C++,Modulo,我需要找到任何大于0且有n个数字的数字(已更新 这是你需要的吗 第一个数字n是数字的个数。 第二个数字m是除法器 #include <iostream> #include <string> using namespace std; int m, n; bool searchDivisibleNumbers(int n, int m); int main() { cin >> n >> m; while (searchDivis

我需要找到任何大于0且有n个数字的数字(已更新 这是你需要的吗

第一个数字n是数字的个数。 第二个数字m是除法器

#include <iostream>
#include <string>

using namespace std;
int m, n;
bool searchDivisibleNumbers(int n, int m);

int main()
{
    cin >> n >> m;

    while (searchDivisibleNumbers(n,m))

    return 0;
}

bool searchDivisibleNumbers(int n, int m)
{
    int digits = pow(10, n-1); //huge number of n digits

    while (digits != 0){

        if (digits % m == 0){
            cout << digits << " is divisible by " << m << endl;
        }
        digits--;
    }
    return true;
}

这里有一个程序供您使用:

int main(void)
{
  int i = 0;
  cout << "Enter number: ";
  cin >> i;
  int m = 0;
  cout << "Enter divisor: ";
  cin >> m;
  if (m == 0)
  {
    cerr << "Can't divide by zero, aborting.\n";
    return EXIT_FAILURE;
  }
  cout << "Your number "
       << i;
  if ((i % m) == 0)
  {
    cout << " is divisible by " 
         << m
         << "\n";
  }
  else
  {
    cout << "not divisible by " << m << "\n";
  }
  return EXIT_SUCCESS;
}
int main(无效)
{
int i=0;
cout>i;
int m=0;
cout>m;
如果(m==0)
{

cerr如果你需要知道有多少N位数字可以被M除,那么这可以通过一个公式很容易地实现。假设我们想知道所有可以被17除的5位数字。我们要做的是找到可以被17除的最小的5位数字。如果我们这样做了

10000 % 17
我们得到
4
所以当我们这样做时

10000 - 4 + 17 
我们得到10013,这是第一个可以被17除的5位数字。现在我们需要知道17的多少倍数在[1001399999]范围内。我们需要找到最大的5位整数,我们可以通过简单的整数除法和乘法得到

99999 / 17 * 17 = 99994
得到我们取的倍数

(max   - min  ) / 17
(99994 - 10013) / 17
    89981       / 17 = 5293

这个问题有两种可能。
1.如果M(1)回答类型为“11111..”最多N-1位数字,后跟“0”。

这是否是家庭作业并不相关;重要的是在这个问题上付出的努力。在这种情况下,这似乎是零。我尝试了一些东西,但它是错误的,你能告诉我怎么做吗?如果你告诉我们你尝试了什么,告诉我们它是如何错误的,那么是的;这就是这个网站的目的。但是,如果你不做这两件事,我们就不会为你编写代码。为了增强@R_Kapp的评论,请提供一个。完成,现在请告诉我我做错了什么不,我需要找到任何N位数字可以被随机给定数整除的数!你不能生成随机数?我的解决方案是随机数,我相信这就是ea您可以自己添加最简单的部分。@Mas:这个答案对您很有用,您只需更改他使用的
n
部分,然后改为
10^n-1
)答案很好。我希望他能理解。老兄,我需要找到任何N位数字可被一个数字整除的数字!!@mas这可能更像是一个简单的数学问题,而不是一个编程问题。Thomas所展示的很容易推断到你想要的数字范围,在循环中使用。我们这里不提供现成的代码,也不作为一个函数操作在线调试服务。
10000 % 17
10000 - 4 + 17 
99999 / 17 * 17 = 99994
(max   - min  ) / 17
(99994 - 10013) / 17
    89981       / 17 = 5293