{ }codelper
swapping valuesP0344

The shorter one stands in front

In PE class the students stood in pairs, one in front of the other. The first line gives n, the number of pairs. Each of the next n lines gives one pair: the front person's name, the front person's height, the back person's name and the back person's height, in this order, separated by single spaces. If the front person is taller than the back person, the two swap places. If the two have the same height, they do not swap. For each pair, print on one line the front person's name, the front person's height, the back person's name and the back person's height as they stand after this, separated by single spaces. On the last line, print the number of pairs that swapped places.

Input
1 ≤ n ≤ 100, each name is 1 to 10 lowercase English letters, 80 ≤ height ≤ 200 (integers)
Output
n lines, one per pair, with name, height, name, height separated by single spaces, then one line with the number of pairs that swapped
Example 1 · stdin
3
mina 150 jun 142
bora 131 seo 131
tae 95 hana 120
stdout
jun 142 mina 150
bora 131 seo 131
tae 95 hana 120
1
Example 2 · stdin
1
yuna 99 dojun 100
stdout
yuna 99 dojun 100
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

Each language has its own way to swap the values of two variables.
Python a, b = b, a, JS/TS [a, b] = [b, a], C# (a, b) = (b, a) and C++ std::swap(a, b) swap in one line, and the same form works for names (strings) and heights (numbers).
Java and Craykon keep one side in a temporary variable and swap in three lines, as in tmp = a; a = b; b = tmp;.
C also uses a temporary, but a character array holding a name cannot be assigned with =, so the characters are moved with strcpy(tmp, a); strcpy(a, b); strcpy(b, tmp);.
Trap one: if you write a = b; b = a; without a temporary, the original value of a is lost in the first statement.
The first line of example 1 becomes jun 142 jun 142.
Trap two: a person's name and height must move together.
If you swap only the names and leave the heights, the first line of example 1 becomes jun 150 mina 142.
Trap three: equal heights do not swap, so the condition is >.
With >= the second line of example 1 becomes seo 131 bora 131 and the last line becomes 2.
Trap four: convert the heights to numbers before comparing.
In Python and JS, comparing the strings as they were read compares them in character order, so "95" > "120" is true.
The third line of example 1 becomes hana 120 tae 95, and example 2 also changes to dojun 100 yuna 99.

Pythonmain.py · 12 lines
n = int(input())swapped = 0for _ in range(n):    a, ha, b, hb = input().split()    ha, hb = int(ha), int(hb)   # 수로 바꿔야 키를 수로 견준다    if ha > hb:        # 오른쪽을 먼저 다 계산한 뒤에 넣으므로 임시 변수가 필요 없다        a, b = b, a        ha, hb = hb, ha        swapped += 1    print(a, ha, b, hb)print(swapped)
JavaScriptmain.js · 18 lines
const lines = require("fs").readFileSync(0, "utf8").trim().split("\n");const n = Number(lines[0]);const out = [];let swapped = 0;for (let i = 1; i <= n; i++) {  let [a, ha, b, hb] = lines[i].trim().split(/\s+/);  ha = Number(ha);  hb = Number(hb);  if (ha > hb) {    // 구조 분해로 한 줄에 맞바꾼다    [a, b] = [b, a];    [ha, hb] = [hb, ha];    swapped++;  }  out.push(`${a} ${ha} ${b} ${hb}`);}out.push(swapped);console.log(out.join("\n"));
TypeScriptmain.ts · 21 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").trim().split("\n");const n: number = Number(lines[0]);const out: string[] = [];let swapped: number = 0;for (let i = 1; i <= n; i++) {  const t: string[] = lines[i].trim().split(/\s+/);  let a: string = t[0];  let ha: number = Number(t[1]);  let b: string = t[2];  let hb: number = Number(t[3]);  if (ha > hb) {    // 구조 분해로 한 줄에 맞바꾼다    [a, b] = [b, a];    [ha, hb] = [hb, ha];    swapped++;  }  out.push(`${a} ${ha} ${b} ${hb}`);}out.push(String(swapped));console.log(out.join("\n"));
JavaMain.java · 29 lines
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();        StringBuilder sb = new StringBuilder();        int swapped = 0;        for (int i = 0; i < n; i++) {            String a = sc.next();            int ha = sc.nextInt();            String b = sc.next();            int hb = sc.nextInt();            if (ha > hb) {                // Java에는 한 줄 맞바꾸기가 없어 임시 변수에 한쪽을 맡겨 둔다                String tmpName = a;                a = b;                b = tmpName;                int tmpHeight = ha;                ha = hb;                hb = tmpHeight;                swapped++;            }            sb.append(a).append(' ').append(ha).append(' ').append(b).append(' ').append(hb).append('\n');        }        sb.append(swapped).append('\n');        System.out.print(sb);    }}
C#Main.cs · 25 lines
using System; class Program{    static void Main()    {        int n = int.Parse(Console.ReadLine());        int swapped = 0;        for (int i = 0; i < n; i++)        {            string[] t = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);            string a = t[0], b = t[2];            int ha = int.Parse(t[1]), hb = int.Parse(t[3]);            if (ha > hb)            {                // 튜플로 한 줄에 맞바꾼다                (a, b) = (b, a);                (ha, hb) = (hb, ha);                swapped++;            }            Console.WriteLine($"{a} {ha} {b} {hb}");        }        Console.WriteLine(swapped);    }}
C++main.cpp · 21 lines
#include <iostream>#include <string>#include <utility> int main() {    int n, swapped = 0;    std::cin >> n;    for (int i = 0; i < n; i++) {        std::string a, b;        int ha, hb;        std::cin >> a >> ha >> b >> hb;        if (ha > hb) {            // std::swap은 문자열도 수도 한 줄에 맞바꾼다            std::swap(a, b);            std::swap(ha, hb);            swapped++;        }        std::cout << a << " " << ha << " " << b << " " << hb << "\n";    }    std::cout << swapped << "\n";}
Cmain.c · 26 lines
#include <stdio.h>#include <string.h> int main(void) {    int n, swapped = 0;    scanf("%d", &n);    for (int i = 0; i < n; i++) {        char a[11], b[11];   /* 이름 10글자 + 끝을 알리는 '\0' */        int ha, hb;        scanf("%10s %d %10s %d", a, &ha, b, &hb);        if (ha > hb) {            /* 배열은 =로 넣을 수 없어서 strcpy로 글자를 옮긴다 */            char tmp[11];            strcpy(tmp, a);            strcpy(a, b);            strcpy(b, tmp);            int t = ha;            ha = hb;            hb = t;            swapped++;        }        printf("%s %d %s %d\n", a, ha, b, hb);    }    printf("%d\n", swapped);    return 0;}
Craykonmain.cray · 23 lines
n = Console.ReadLine().ToNumber();바꾼수 = 0;for (i = 0; i < n; i++){    칸 = Console.ReadLine().Split(" ");    앞이름 = 칸[0];    앞키 = 칸[1].ToNumber();    뒤이름 = 칸[2];    뒤키 = 칸[3].ToNumber();    if (앞키 > 뒤키)    {        // 한쪽을 먼저 임시로 맡겨 둔다        임시 = 앞이름;        앞이름 = 뒤이름;        뒤이름 = 임시;        임시 = 앞키;        앞키 = 뒤키;        뒤키 = 임시;        바꾼수 += 1;    }    Console.WriteLine(앞이름 + " " + 앞키 + " " + 뒤이름 + " " + 뒤키);}Console.WriteLine(바꾼수);