There is a balance whose two arms may have different lengths.
The left arm has length a and the right arm has length b.
The balance is level when the weight on the left pan multiplied by a equals the weight on the right pan multiplied by b.
There are n weights, numbered from 1 to n.
The first line gives n, a and b separated by single spaces.
The second line gives the n weights in order of their numbers, separated by single spaces.
You want to choose two different weights, put one on the left pan and one on the right pan, and make the balance level.
Weights with different numbers are different weights even if they weigh the same.
Print the number of the weight for the left pan and the number of the weight for the right pan, in this order, separated by a single space.
If there are several ways, print the one with the smallest left number, and if there are still several, the one with the smallest right number.
If there is no way, print -1.
Input
1 ≤ n ≤ 200, 1 ≤ a ≤ 100, 1 ≤ b ≤ 100, 1 ≤ weight ≤ 1,000 (all integers, given as digits only, without commas)
Output
One line with the left weight's number and the right weight's number separated by a single space, or -1 if there is no way
Example 1 · stdin
5 2 3
4 6 9 2 3
stdout
2 1
Example 2 · stdin
3 3 3
5 7 9
stdout
-1
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
Try every left weight against every right weight with two nested loops and stop at the first pair that fits: that pair is the answer. Each language has its own way to leave two nested loops at once. Java, JS and TS put a label such as search: on the outer loop and leave with break search;. C jumps out of the loops with goto done;. The C++ solution puts a flag variable in both loop conditions, as in i < n && !found. The Python, C# and Craykon solutions move the search into a function and leave with return. C# has no labeled break. Trap one: a plain break leaves only the inner loop. The outer loop keeps running and a later pair overwrites the answer. Example 1 prints 5 4 instead of 2 1. Trap two: left and right are different places, so (i, j) and (j, i) are different ways. That is why j must also run from the beginning to the end. If you start j at i + 1, as is common when choosing two items, example 1 never sees 2 1, where the right number is smaller, and prints -1. Trap three: the same weight cannot be on both pans, so you must check that i and j differ. Without the check, example 2, where a equals b, prints 1 1. Trap four: multiply the left weight by a and the right weight by b. With them the other way round, example 1 prints 1 2.
Pythonmain.py · 11 lines
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 lines
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 lines
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 lines
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 lines
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 lines
#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 lines
#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 lines
// 돌려주기 하나로 두 겹의 반복을 모두 벗어난다찾기(n, a, b, w){ for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i != j && w[i] * a == w[j] * b) { return i + 1 + " " + (j + 1); } } } return "-1";}첫줄 = Console.ReadNumbers();w = Console.ReadNumbers();Console.WriteLine(찾기(첫줄[0], 첫줄[1], 첫줄[2], w));