A
A
Abubakr Mirzoev2015-07-09 22:46:23
Programming
Abubakr Mirzoev, 2015-07-09 22:46:23

How to check the divisibility of numbers from the sequence 1,11,111,...,11..1 by their serial numbers?

Given a sequence of numbers (a sequence of units): 1, 11, 111, ..., 11..1. (up to N)
It ​​is required to determine the divisibility of a number by its serial number and write 0 or 1 to the array.
I wrote a solution, but with problems.
Here is my code:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <sstream>
 
using namespace std;
 
template <typename T>
string toStr(T val){
    ostringstream oss;
    oss<< val;
    return oss.str();
}
 
int main()
{
//-----------------ВВОД ВЫВОД ПОСЛЕДОВАТЕЛЬНОСТЕЙ------------------
    int i,j,N;
    cout<<"Vvedite N:";
    cin>>N;
    cout<<endl;
 
    string *a=new string[N];
    int *is_div = new int[N];
 
    a[0]="1";
    for(i=1;i<N;++i)
        for(j=0;j<=i;++j)
            a[i]+='1';
    for(i=0;i<N;++i)
        cout<<a[i]<<endl;
//--------АЛГОРИТМ ДЕЛЕНИЯ--------------------------
    string x;
    int dlina_a,x_ch,ost,result_ost;
 
    for(i=0;i<N;++i){
//N_a - Номер числа, kolpos_N - Кол.-во цифр в числе, dlina_a - длина числа
            int N_a=i+1,N1=N_a,kolpos_N=0,dlina_a=N_a;
 
            while(N1>0){
                N1=N1/10;
                kolpos_N++;
            }
        while(dlina_a>0){
            x=a[i].substr(0,kolpos_N);
                dlina_a-=kolpos_N;
                    a[i]=a[i].erase(0,kolpos_N);
            x_ch = atoi(x.c_str());
            if(x_ch<N_a){
                x+=a[i].substr(0,1);
                a[i]=a[i].erase(0,1);
                dlina_a-=1;
            }
            ost = x_ch%N_a;
            if(ost==0 && dlina_a>0)
                continue;
            else if(ost!=0 && dlina_a>0)
                a[i]=toStr(ost)+a[i];
            else
                result_ost=ost;
        }
        if(result_ost)
            is_div[i]=1;
        else
            is_div[i]=0;
    }
    for(int k=0;k<N;++k)
        cout<<is_div[k]<<"\n";
    return 0;
}

The is_div array should store 0-"no" or 1-"yes" responses.
It shows me that 1%1=0, 11%2=1, 111%3=1 is an error. those. after the first loop, only ones are stored in is_div. Help solve the problem.
Here's what I get:
6ef2b72b05ea45b08e740c27b6b4af06.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2015-07-10
@mr_abubakr

I apologize for php.

<?php

$n      = 5;
$array  = [];
$out    = [];

for ($i = 0; $i < $n; $i++) {
    $array[$i] = ($i > 0) ? $array[$i - 1] . "1" : "1";
    $out[$i] = ($array[$i] % ($i + 1) === 0) ? 1 : 0;
}

#    $array
#    Array
#    (
#        [0] => 1
#        [1] => 11
#        [2] => 111
#        [3] => 1111
#        [4] => 11111
#    )
#    $out
#    Array
#    (
#        [0] => 1
#        [1] => 0
#        [2] => 1
#        [3] => 0
#        [4] => 0
#    )

$out[$i] = $a % $b . Here is the implementation of long arithmetic . (Dividing a string by a number)

I
iSergios, 2015-07-15
@iSergios

Forgive me my python:

def func(n):
    result=[]
    m=[int('1'*x) for x in range(1,n+1)]
    for c in m:
        result.append(int(c%(m.index(c)+1)==0))
    return result

Где n - число элементов в последовательности.
Если добавить в функцию вывод заданной последовательности, получим:
>>> func(10)
[1, 11, 111, 1111, 11111, 111111, 1111111, 11111111, 111111111, 1111111111]
[1, 0, 1, 0, 0, 0, 0, 0, 1, 0]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question