The first line gives the count n and the window size k, and the second line gives n numbers separated by single spaces. A window is k consecutive numbers. Find the window with the largest sum. If several windows have the largest sum, choose the leftmost one. On the first line print the position where that window starts (counting from 1). On the second line print the k numbers of that window separated by single spaces. On the third line print the window's average to two decimal places. Do not round the average; take its exact value (sum ÷ k) and drop everything from the third decimal place on (e.g. 11 / 3 = 3.666… → 3.66, 7 / 2 → 3.50).
Input
1 ≤ k ≤ n ≤ 10,000, 0 ≤ each number ≤ 1000 (integers)
Output
Three lines: the start position, the k numbers of the window, and the average truncated to two decimal places
Example 1 · stdin
7 3
1 5 2 4 4 1 2
stdout
2
5 2 4
3.66
Example 2 · stdin
5 2
4 3 1 2 5
stdout
1
4 3
3.50
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
Adding k numbers again for every window costs n × k steps. Instead keep a running sum: when the window slides, add the number coming in and subtract the one going out (total += a[i] - a[i - k]), and n steps are enough. Cutting out the chosen window differs in what the second argument means: Python a[s:s + k], JS/TS a.slice(s, s + k), Java Arrays.copyOfRange(a, s, s + k) and C++ vector(a.begin() + s, a.begin() + s + k) take the end position (excluded), while C# GetRange(s, k) and Craykon a.GetRange(s, k) (a.부분목록(s, k)) take a count. C has no slicing, so you loop over k items from s. The first trap is passing a count to JS, as in a.slice(s, k), which gives the wrong window. The second trap is truncation: C printf("%.2f"), Python f"{x:.2f}" and Craykon Str.Fixed(x, 2) all round, so 2/3 becomes 0.67. Multiply the sum by 100, truncate with integer division (best * 100 / k, Python //, Craykon Mathf.Quot), and build 3.66 from the quotient and remainder. Truncating a floating-point value, as in Math.floor(best / k * 100), fails because 0.29 × 100 is 28.999…, which gives 0.28, so stay with integers (sum × 100 is at most 10⁹, which fits in an int). The third trap is ties: switching to >= keeps the right-hand window. There are n − k + 1 windows, so looping one time too few misses the last one.
Pythonmain.py · 15 lines
n, k = map(int, input().split())a = list(map(int, input().split()))# 첫 창의 합을 구한 뒤, 한 칸씩 밀며 새로 들어온 수를 더하고 빠진 수를 뺀다total = sum(a[:k])best, start = total, 0for i in range(k, n): total += a[i] - a[i - k] if total > best: # 같으면 바꾸지 않는다 → 맨 왼쪽 창이 남는다 best, start = total, i - k + 1print(start + 1)print(*a[start:start + k])t = best * 100 // k # 소수 둘째 자리까지 버림(정수로 계산)print(f"{t // 100}.{t % 100:02d}")
JavaScriptmain.js · 17 lines
const [n, k, ...a] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let total = 0;for (let i = 0; i < k; i++) total += a[i];let best = total, start = 0;for (let i = k; i < n; i++) { total += a[i] - a[i - k]; if (total > best) { best = total; start = i - k + 1; }}const t = Math.floor((best * 100) / k); // 소수 둘째 자리까지 버림console.log(start + 1);console.log(a.slice(start, start + k).join(" ")); // slice(시작, 끝) — 끝은 빼고console.log(`${Math.floor(t / 100)}.${String(t % 100).padStart(2, "0")}`);
TypeScriptmain.ts · 18 lines
import { readFileSync } from "node:fs";const [n, k, ...a]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let total = 0;for (let i = 0; i < k; i++) total += a[i];let best = total, start = 0;for (let i = k; i < n; i++) { total += a[i] - a[i - k]; if (total > best) { best = total; start = i - k + 1; }}const t: number = Math.floor((best * 100) / k); // 소수 둘째 자리까지 버림console.log(start + 1);console.log(a.slice(start, start + k).join(" ")); // slice(시작, 끝) — 끝은 빼고console.log(`${Math.floor(t / 100)}.${String(t % 100).padStart(2, "0")}`);
JavaMain.java · 33 lines
import java.io.*;import java.util.*;public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()), k = Integer.parseInt(st.nextToken()); int[] a = new int[n]; st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) a[i] = Integer.parseInt(st.nextToken()); int total = 0; for (int i = 0; i < k; i++) total += a[i]; int best = total, start = 0; for (int i = k; i < n; i++) { total += a[i] - a[i - k]; if (total > best) { best = total; start = i - k + 1; } } // copyOfRange(배열, 시작, 끝) — 끝은 빼고 int[] window = Arrays.copyOfRange(a, start, start + k); StringJoiner sj = new StringJoiner(" "); for (int x : window) sj.add(String.valueOf(x)); int t = best * 100 / k; // 정수 나눗셈 = 버림(값이 음수가 아니므로) System.out.println(start + 1); System.out.println(sj); System.out.printf("%d.%02d%n", t / 100, t % 100); }}
C#Main.cs · 31 lines
using System;using System.Collections.Generic;using System.Linq;class Program{ static void Main() { int[] nk = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); int n = nk[0], k = nk[1]; List<int> a = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList(); int total = 0; for (int i = 0; i < k; i++) total += a[i]; int best = total, start = 0; for (int i = k; i < n; i++) { total += a[i] - a[i - k]; if (total > best) { best = total; start = i - k + 1; } } int t = best * 100 / k; // 정수 나눗셈 = 버림(값이 음수가 아니므로) Console.WriteLine(start + 1); Console.WriteLine(string.Join(" ", a.GetRange(start, k))); // GetRange(시작, 개수) Console.WriteLine($"{t / 100}.{t % 100:D2}"); }}
C++main.cpp · 29 lines
#include <iomanip>#include <iostream>#include <vector>int main() { int n, k; std::cin >> n >> k; std::vector<int> a(n); for (int& x : a) std::cin >> x; int total = 0; for (int i = 0; i < k; i++) total += a[i]; int best = total, start = 0; for (int i = k; i < n; i++) { total += a[i] - a[i - k]; if (total > best) { best = total; start = i - k + 1; } } // 반복자 두 개(시작, 끝)로 잘라 새 vector를 만든다 std::vector<int> window(a.begin() + start, a.begin() + start + k); std::cout << start + 1 << "\n"; for (size_t i = 0; i < window.size(); i++) std::cout << (i ? " " : "") << window[i]; std::cout << "\n"; int t = best * 100 / k; // 정수 나눗셈 = 버림 std::cout << t / 100 << "." << std::setw(2) << std::setfill('0') << t % 100 << "\n";}
Cmain.c · 28 lines
#include <stdio.h>static int a[10000];int main(void) { int n, k; scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) scanf("%d", &a[i]); int total = 0; for (int i = 0; i < k; i++) total += a[i]; int best = total, start = 0; for (int i = k; i < n; i++) { total += a[i] - a[i - k]; if (total > best) { best = total; start = i - k + 1; } } printf("%d\n", start + 1); /* C에는 자르기가 없다: 시작 자리부터 k개를 돈다 */ for (int i = start; i < start + k; i++) printf(i > start ? " %d" : "%d", a[i]); printf("\n"); int t = best * 100 / k; /* 정수 나눗셈 = 버림 */ printf("%d.%02d\n", t / 100, t % 100); return 0;}
Craykonmain.cray · 28 lines
첫줄 = Console.ReadNumbers();n = 첫줄[0];k = 첫줄[1];a = Console.ReadNumbers();// 첫 창의 합을 구한 뒤, 한 칸씩 밀며 새로 들어온 수를 더하고 빠진 수를 뺀다합 = 0;for (i = 0; i < k; i++){ 합 += a[i];}best = 합;start = 0;for (i = k; i < n; i++){ 합 += a[i] - a[i - k]; if (합 > best) { best = 합; start = i - k + 1; }}Console.WriteLine(start + 1);Console.WriteLine(a.GetRange(start, k));t = Mathf.Quot(best * 100, k);소수부 = "" + t % 100;Console.WriteLine(Mathf.Quot(t, 100) + "." + 소수부.PadLeft(2, "0"));