{ }codelper
appendingP0374

Numbers of the students who retake

n students took a quiz. The students are numbered 1, 2, …, n in the order their scores are given. A student whose score is below c retakes the quiz. The first line gives n and c separated by a single space. The second line gives the n scores separated by single spaces. On the first line, print how many students retake the quiz. On the second line, print the numbers of those students from smallest to largest, separated by single spaces. If no student retakes the quiz, print only 0 on the first line and do not print a second line.

Input
1 ≤ n ≤ 100, 0 ≤ c ≤ 100, 0 ≤ score ≤ 100 (all integers)
Output
First line: how many students retake; second line: their numbers (first line only if there are none)
Example 1 · stdin
5 60
70 45 60 59 90
stdout
2
2 4
Example 2 · stdin
3 50
50 80 100
stdout
0
main.py
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

The count has to be printed before the numbers, so you cannot print each number as soon as you find it; you collect them one at a time in an empty list.
The name for putting one item at the end of a list differs by language.
Python append, JS/TS push, Java add on an ArrayList, C# Add on a List, C++ push_back on a vector, Craykon Add.
A C array does not grow, so the solution sets aside 100 slots and counts separately how many are filled.
Trap one: storing the result of the call back into the list loses the list.
Python retake = retake.append(…) stops with an AttributeError when the second student of example 1 is added, because append returns None; JS retake = retake.push(…) stops with retake.push is not a function, because push returns the new length.
Trap two: positions count from 0, so you must add i + 1.
Adding i makes the second line of example 1 1 3.
Trap three: "below" is <.
With <=, student 3 with exactly 60 in example 1 is included too, giving 3 and 2 3 4.
Trap four: adding the score instead of the number makes the second line of example 1 45 59.

Pythonmain.py · 9 lines
n, c = map(int, input().split())scores = list(map(int, input().split()))retake = []                      # 빈 목록에서 시작한다for i in range(n):    if scores[i] < c:        retake.append(i + 1)     # 학생 번호는 1번부터print(len(retake))if retake:    print(*retake)
JavaScriptmain.js · 13 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const [n, c] = lines[0].trim().split(/\s+/).map(Number);const scores = lines[1].trim().split(/\s+/).map(Number);const retake = []; // 빈 배열에서 시작한다for (let i = 0; i < n; i++) {  if (scores[i] < c) {    retake.push(i + 1); // 학생 번호는 1번부터  }}console.log(retake.length);if (retake.length > 0) {  console.log(retake.join(" "));}
TypeScriptmain.ts · 14 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const [n, c]: number[] = lines[0].trim().split(/\s+/).map(Number);const scores: number[] = lines[1].trim().split(/\s+/).map(Number);const retake: number[] = []; // 빈 배열에서 시작한다for (let i = 0; i < n; i++) {  if (scores[i] < c) {    retake.push(i + 1); // 학생 번호는 1번부터  }}console.log(retake.length);if (retake.length > 0) {  console.log(retake.join(" "));}
JavaMain.java · 26 lines
import java.util.ArrayList;import java.util.List;import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt(), c = sc.nextInt();        List<Integer> retake = new ArrayList<>();   // 빈 목록에서 시작한다        for (int i = 0; i < n; i++) {            int score = sc.nextInt();            if (score < c) {                retake.add(i + 1);   // 학생 번호는 1번부터            }        }        System.out.println(retake.size());        if (!retake.isEmpty()) {            StringBuilder line = new StringBuilder();            for (int number : retake) {                if (line.length() > 0) line.append(' ');                line.append(number);            }            System.out.println(line);        }    }}
C#Main.cs · 25 lines
using System;using System.Collections.Generic; class Program{    static void Main()    {        string[] head = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        int n = int.Parse(head[0]), c = int.Parse(head[1]);        string[] parts = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        var retake = new List<int>();   // 빈 목록에서 시작한다        for (int i = 0; i < n; i++)        {            if (int.Parse(parts[i]) < c)            {                retake.Add(i + 1);   // 학생 번호는 1번부터            }        }        Console.WriteLine(retake.Count);        if (retake.Count > 0)        {            Console.WriteLine(string.Join(" ", retake));        }    }}
C++main.cpp · 23 lines
#include <iostream>#include <vector> int main() {    int n, c;    std::cin >> n >> c;    std::vector<int> retake;   // 빈 vector에서 시작한다    for (int i = 0; i < n; i++) {        int score;        std::cin >> score;        if (score < c) {            retake.push_back(i + 1);   // 학생 번호는 1번부터        }    }    std::cout << retake.size() << "\n";    if (!retake.empty()) {        for (std::size_t k = 0; k < retake.size(); k++) {            if (k > 0) std::cout << " ";            std::cout << retake[k];        }        std::cout << "\n";    }}
Cmain.c · 26 lines
#include <stdio.h> int main(void) {    int n, c;    if (scanf("%d %d", &n, &c) != 2) return 0;    /* C의 배열은 늘어나지 않는다: 가장 많을 때(100명)만큼 잡아 두고, 몇 칸 찼는지 따로 센다 */    int retake[100];    int count = 0;    for (int i = 0; i < n; i++) {        int score;        if (scanf("%d", &score) != 1) return 0;        if (score < c) {            retake[count] = i + 1;   /* 학생 번호는 1번부터 */            count++;        }    }    printf("%d\n", count);    if (count > 0) {        for (int k = 0; k < count; k++) {            if (k > 0) printf(" ");            printf("%d", retake[k]);        }        printf("\n");    }    return 0;}
Craykonmain.cray · 17 lines
head = Console.ReadNumbers();n = head[0];c = head[1];scores = Console.ReadNumbers();retake = []; // 빈 목록에서 시작한다for (i = 0; i < n; i++){    if (scores[i] < c)    {        retake.Add(i + 1); // 학생 번호는 1번부터    }}Console.WriteLine(retake.Count);if (retake.Count > 0){    Console.WriteLine(retake); // 목록은 빈칸으로 이어서 쓴다}