我如何解决以下问题 [英] How do I solve the following question

查看:81
本文介绍了我如何解决以下问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个数字n找出给定数字的多少片段可以被11整除:



输入:



1215598

输出:



4

数字55,121,12155,15598是可以被11整除的数字的连续片段。



我想用C ++编写一个程序来做同样的事情,但我无法找出一个有效的合适的方式。



我的尝试:



Given a number n find out how many fragments of the given number is divisible by 11 for example:

Input:

1215598
Output:

4
The numbers 55,121,12155,15598 are continuous fragments of the number which are divisible by 11.

I want to write a program in C++ to do the same but I am unable to figure out an efficient and suitable way.

What I have tried:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int DivisibilityByEleven(int num);
int num;
int main()
{
   int result=DivisibilityByEleven(num);
   return 0;
}

int DivisibilityByEleven(int num)
{
    int counter=0;
    cin>>num;   
    vector<int> arr;
    while(num!=0)
    {
        int temp=num%10;
        num=num/10;
        arr.push_back(temp);
    }     
    reverse(arr.begin(),arr.end());
    for(int i=0;i<arr.size();i++)
    {
        cout<<arr[i];
    }

    if(num%11==0)
    {
        counter++;
    }
}

推荐答案

我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



想想任务:有多少连续的碎片输入数字是否存在?

片段的最小大小为2(或者它不能被11整除),最大值是7的整数长度(如果不允许则为少一点 - 6)

这意味着所有连续的片段都是:

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Think about the task: how many "contiguous fragments" of the input number are there?
The minimum size of a fragment is 2 (or it's not divisible by 11) and the maximum is the whole number length of 7 (or if that is not allowed then one less - 6)
Which means that all the contiguous fragments are:
12, 21, 15, 55, 59, 98,
121, 215, 155, 559, 598,
1215, 2155, 1559, 5598,
12155, 21559, 15598,
121559, 215598,
1215598

使用一对嵌套循环很容易解决问题。

然后你需要做的就是检查每个片段的可分性,这是微不足道的:模数运算符%将为你做到这一点!

亲自尝试,你可能会发现它并不像你想象的那么困难!



如果您遇到特定问题,请询问相关问题,我们会尽力提供帮助。但是我们不打算为你做这一切!

They are easy to work out with a pair of nested loops.
Then all you have to do is check each fragment for divisibility, and that's trivial: the modulus operator "%" will do that for you!
Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


我会使用数字的字符串表示:

I would use the string representation of the number:
#include <iostream>
#include <sstream>

using namespace std;

// get the integer corresponding to the substring starting at 'pos', having length 'len'
int slice_to_int(const string & str, size_t pos, size_t len)
{
  istringstream iss(str.substr(pos, len));
  int n;
  iss >> n;
  return n;
}

int main()
{
  int n = 1215598;

  ostringstream oss;
  oss << n;

  string s = oss.str(); // get the string representation of the number

  size_t len = s.length();
  size_t count = 0;

  for (size_t i = 0; i<len-1; ++i)
  {
    for (size_t j = 2; j <= (len - i) ; ++j)
    {
      int k = slice_to_int( s, i, j);
      if ( (k % 11) == 0)
      {
        ++count;
      }
    }
  }

  cout << "count = " << count << endl;
}


这篇关于我如何解决以下问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆