{ }codelper
joined outputP0373

Bonus-added scores joined with commas

The same bonus b is to be added to the scores of the n students in a group. The first line gives n and b separated by a single space. The second line gives the n scores separated by single spaces. Print the n scores with the bonus added, in the given order, on one line. Put one comma and one space (", ") between scores. Put nothing before the first score or after the last score. If a score with the bonus goes over 100, print it as it is.

Input
1 ≤ n ≤ 100, 1 ≤ b ≤ 100, 0 ≤ score ≤ 100 (all integers)
Output
One line: the n scores with the bonus added, joined with ", "
Example 1 · stdin
3 5
70 85 90
stdout
75, 90, 95
Example 2 · stdin
1 10
100
stdout
110
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

Each language has its own tool for joining several values into one line with something only in between.
Python ", ".join(…), JS/TS array.join(", "), Java StringJoiner, C# string.Join(", ", array), Craykon list.Join(", ").
The C and C++ solutions write "put , in front unless it is the first score" by hand.
Trap one: writing , after every score leaves a comma after the last one (example 1 becomes 75, 90, 95,).
Trap two: printing the list as it is gives whatever shape the language likes.
Python print(list) and Java Arrays.toString(array) add square brackets, as in [75, 90, 95]; JS join() with no separator joins with no space, 75,90,95; and Craykon Console.WriteLine(list) joins with spaces only, 75 90 95.
Trap three: Python's join joins strings only.
Passing numbers stops with a TypeError, so pass str(x + b).
JS/TS join, C# string.Join and Craykon Join turn numbers into strings for you.
Trap four: in JS, if the scores are not converted with Number, "70" + 5 is not addition but concatenation and gives 705 (example 1 becomes 705, 855, 905).

Pythonmain.py · 4 lines
n, b = map(int, input().split())scores = list(map(int, input().split()))# join은 문자열만 이어 주므로 수를 str로 바꿔서 넘긴다print(", ".join(str(x + b) for x in scores))
JavaScriptmain.js · 5 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const [n, b] = lines[0].trim().split(/\s+/).map(Number);const scores = lines[1].trim().split(/\s+/).map(Number);// join(", ")은 사이에만 ", "을 넣는다. 수는 알아서 문자열로 바뀐다console.log(scores.map((x) => x + b).join(", "));
TypeScriptmain.ts · 6 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const [n, b]: number[] = lines[0].trim().split(/\s+/).map(Number);const scores: number[] = lines[1].trim().split(/\s+/).map(Number);// join(", ")은 사이에만 ", "을 넣는다. 수는 알아서 문자열로 바뀐다console.log(scores.map((x: number) => x + b).join(", "));
JavaMain.java · 16 lines
import java.util.Scanner;import java.util.StringJoiner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt(), b = sc.nextInt();        // StringJoiner는 사이에만 ", "을 넣는다. 문자열만 받으므로 수를 바꿔서 넣는다        StringJoiner line = new StringJoiner(", ");        for (int i = 0; i < n; i++) {            int x = sc.nextInt();            line.add(String.valueOf(x + b));        }        System.out.println(line);    }}
C#Main.cs · 18 lines
using System; class Program{    static void Main()    {        string[] head = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        int n = int.Parse(head[0]), b = int.Parse(head[1]);        string[] parts = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        int[] result = new int[n];        for (int i = 0; i < n; i++)        {            result[i] = int.Parse(parts[i]) + b;        }        // string.Join은 사이에만 ", "을 넣는다. 수 배열도 그대로 받는다        Console.WriteLine(string.Join(", ", result));    }}
C++main.cpp · 14 lines
#include <iostream> int main() {    int n, b;    std::cin >> n >> b;    for (int i = 0; i < n; i++) {        int x;        std::cin >> x;        // 첫 점수가 아닐 때만 앞에 ", "을 쓴다        if (i > 0) std::cout << ", ";        std::cout << x + b;    }    std::cout << "\n";}
Cmain.c · 15 lines
#include <stdio.h> int main(void) {    int n, b;    if (scanf("%d %d", &n, &b) != 2) return 0;    for (int i = 0; i < n; i++) {        int x;        if (scanf("%d", &x) != 1) return 0;        /* C에는 이어 주는 도구가 없다: 첫 점수가 아닐 때만 앞에 ", "을 쓴다 */        if (i > 0) printf(", ");        printf("%d", x + b);    }    printf("\n");    return 0;}
Craykonmain.cray · 11 lines
head = Console.ReadNumbers();n = head[0];b = head[1];scores = Console.ReadNumbers();result = [];foreach (x in scores){    result.Add(x + b);}// 잇기(", ")는 사이에만 ", "을 넣는다Console.WriteLine(result.Join(", "));