def find(n, a, b, w): for i in range(n): for j in range(n): if i != j and w[i] * a == w[j] * b: return f"{i + 1} {j + 1}" # return 하나로 두 겹의 반복을 모두 벗어난다 return "-1"n, a, b = map(int, input().split())w = list(map(int, input().split()))print(find(n, a, b, w))
JavaScriptmain.js · 12 行
const [n, a, b, ...w] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let answer = "-1";// 바깥 반복에 이름표를 붙이면 break 이름표로 두 겹을 한 번에 벗어난다search: for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { if (i !== j && w[i] * a === w[j] * b) { answer = `${i + 1} ${j + 1}`; break search; } }}console.log(answer);
TypeScriptmain.ts · 13 行
import { readFileSync } from "node:fs";const [n, a, b, ...w]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let answer: string = "-1";// 바깥 반복에 이름표를 붙이면 break 이름표로 두 겹을 한 번에 벗어난다search: for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { if (i !== j && w[i] * a === w[j] * b) { answer = `${i + 1} ${j + 1}`; break search; } }}console.log(answer);
JavaMain.java · 22 行
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt(); int[] w = new int[n]; for (int i = 0; i < n; i++) w[i] = sc.nextInt(); String answer = "-1"; // 바깥 반복에 이름표를 붙이면 break 이름표로 두 겹을 한 번에 벗어난다 search: for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j && w[i] * a == w[j] * b) { answer = (i + 1) + " " + (j + 1); break search; } } } System.out.println(answer); }}
C#Main.cs · 24 行
using System;class Program{ // C#에는 이름표 break가 없어서 찾기를 함수로 빼고 return으로 벗어난다 static string Find(int n, int a, int b, int[] w) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j && w[i] * a == w[j] * b) return $"{i + 1} {j + 1}"; } } return "-1"; } static void Main() { int[] first = Array.ConvertAll(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries), int.Parse); int[] w = Array.ConvertAll(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries), int.Parse); Console.WriteLine(Find(first[0], first[1], first[2], w)); }}
C++main.cpp · 23 行
#include <iostream>#include <vector>int main() { int n, a, b; std::cin >> n >> a >> b; std::vector<int> w(n); for (int& x : w) std::cin >> x; int left = -1, right = -1; bool found = false; // 두 반복의 조건에 모두 !found를 넣어, 찾는 순간 두 겹이 다 멈추게 한다 for (int i = 0; i < n && !found; i++) { for (int j = 0; j < n && !found; j++) { if (i != j && w[i] * a == w[j] * b) { left = i + 1; right = j + 1; found = true; } } } if (found) std::cout << left << " " << right << "\n"; else std::cout << -1 << "\n";}
Cmain.c · 18 行
#include <stdio.h>int main(void) { int n, a, b, w[200]; scanf("%d %d %d", &n, &a, &b); for (int i = 0; i < n; i++) scanf("%d", &w[i]); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j && w[i] * a == w[j] * b) { printf("%d %d\n", i + 1, j + 1); goto done; /* break는 안쪽 반복만 벗어나므로 goto로 두 겹을 벗어난다 */ } } } printf("-1\n");done: return 0;}
Craykonmain.cray · 19 行
// 돌려주기 하나로 두 겹의 반복을 모두 벗어난다찾기(n, a, b, w){ 循环 (i = 0; i < n; i++) { 循环 (j = 0; j < n; j++) { 如果 (i != j && w[i] * a == w[j] * b) { 返回 i + 1 + " " + (j + 1); } } } 返回 "-1";}첫줄 = 控制台.读数字();w = 控制台.读数字();控制台.输出(찾기(첫줄[0], 첫줄[1], 첫줄[2], w));