The first line gives the count n and an integer k; the second line gives n integers. Scanning from the front, print the first number that is divisible by k. If there is no such number, print "none". Zero and negative numbers can be divisible by k (0 is divisible by every k, and -8 is divisible by 4).
Input
1 ≤ n ≤ 1000, 1 ≤ k ≤ 1000, each number between -1,000,000,000 and 1,000,000,000
Output
the first multiple of k, or "none"
Example 1 · stdin
4 3
4 9 6 12
stdout
9
Example 2 · stdin
3 7
1 2 3
stdout
none
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
Forgetting break keeps the loop going, leaving the last multiple. Another common mistake is setting "none" in an else inside the loop, which stops after looking at the first number. You only know "none" after the whole loop finishes, and languages express that differently: Python has for … else, whose else runs only when the loop ended without break. In C, keep the loop variable outside and treat i < n as "left by break". In Java, C#, C++ and JS, start a found variable at "none", change it when found, and stop. Negative remainders differ (Python -3 % 2 == 1, the others -1), but comparing with == 0 gives the same answer, so it does not matter here. Craykon stops with break; and iterates with foreach (x in a).
Pythonmain.py · 7 lines
n, k = map(int, input().split())for x in map(int, input().split()): if x % k == 0: print(x) breakelse: # break 없이 다 돌았을 때만 실행된다 print("none")
JavaScriptmain.js · 9 lines
const [n, k, ...a] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let found = "none";for (const x of a) { if (x % k === 0) { found = x; break; }}console.log(String(found));
TypeScriptmain.ts · 10 lines
import { readFileSync } from "node:fs";const [n, k, ...a]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let found: number | string = "none";for (const x of a) { if (x % k === 0) { found = x; break; }}console.log(String(found));
JavaMain.java · 17 lines
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), k = sc.nextInt(); String found = "none"; for (int i = 0; i < n; i++) { int x = sc.nextInt(); if (x % k == 0) { found = String.valueOf(x); break; } } System.out.println(found); }}
C#Main.cs · 20 lines
using System;class Program{ static void Main() { int k = int.Parse(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries)[1]); string found = "none"; foreach (string s in Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries)) { int x = int.Parse(s); if (x % k == 0) { found = x.ToString(); break; } } Console.WriteLine(found); }}
C++main.cpp · 17 lines
#include <iostream>#include <string>int main() { int n, k; std::cin >> n >> k; std::string found = "none"; for (int i = 0; i < n; i++) { int x; std::cin >> x; if (x % k == 0) { found = std::to_string(x); break; } } std::cout << found << "\n";}
Cmain.c · 14 lines
#include <stdio.h>int main(void) { int n, k, x, i; scanf("%d %d", &n, &k); for (i = 0; i < n; i++) { scanf("%d", &x); if (x % k == 0) break; } /* break로 나왔으면 i < n */ if (i < n) printf("%d\n", x); else puts("none"); return 0;}
Craykonmain.cray · 13 lines
first = Console.ReadNumbers();k = first[1];a = Console.ReadNumbers();found = "none";foreach (x in a){ if (x % k == 0) { found = x; break; }}Console.WriteLine(found);