Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
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
# )
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
>>> 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 questionAsk a Question
731 491 924 answers to any question