Official

B - 326-like Numbers Editorial by en_translator


The most straightforward approach is like:

for i=n to 919:
  if i is a 326-like number:
    print(i)
    exit

In order to determine if a given number is a 326-like number, one has to obtain the digit at each decimal place. There are two ways:

  1. Finding by computation.
    Given an integer \(n\), the \(100\)s place can be found as \(\left\lfloor \frac{n}{100}\right\rfloor \bmod 10\); the tens as \(\left\lfloor \frac{n}{10} \right\rfloor \bmod 10\); the ones as \(n \bmod 10\).
  2. Finding by string conversion.
    Given a three-digit integer \(n\), one can convert \(n\) to a string \(s\) so that the \(100\)s place is obtained as the first character of \(s\), tens as the second, and ones as the third. Note that in most languages, this operation yields a character instead of a digit.

By implementing it, one can determine if an integer is a 326-like number.

Sample code (C++) by computation

#include<bits/stdc++.h>
using namespace std;

bool check(int n){
  int c100=n/100;
  int c10=n/10%10;
  int c1=n%10;
  return c100*c10==c1;
}

int main(){
  int n;
  cin >> n;
  for(int i=n;i<=919;i++){
    if(check(i)){
      cout << i << endl;
      return 0;
    }
  }
}

Sample code (Python) by string conversion

def check(n):
  s=str(n)
  c100=int(s[0])
  c10=int(s[1])
  c1=int(s[2])
  return c100*c10==c1

N=int(input())
for i in range(N,919+1):
  if check(i):
    print(i)
    exit()

One can also iterate over the digits for each decimal place:

Sample code (Python)

N=int(input())
for c100 in range(1,10):
  for c10 in range(10):
    for c1 in range(10):
      x=c100*100+c10*10+c1
      if x>=N and c100*c10==c1:
        print(x)
        exit()

posted:
last update: