The first line gives n and the second line a list A of n integers. Make a list B as a copy of A. The third line gives the number of edits m, and each of the next m lines gives "i v", meaning: set the i-th item of B (counting from 1) to v. After applying all edits in order, print A on the first line and B on the second, separated by single spaces. A must not change. If the same item is edited several times, the last value stays.
Input
1 ≤ n ≤ 1,000, 0 ≤ m ≤ 1,000, 1 ≤ i ≤ n, every value and v is an integer from -1,000,000,000 to 1,000,000,000
Output
Two lines: the original list A, then the edited list B
Example 1 · stdin
4
1 2 3 4
2
2 20
4 40
stdout
1 2 3 4
1 20 3 40
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
b = a is not a copy. For Python lists, JavaScript/TypeScript arrays, and Java/C# arrays, assignment just gives the same list a second name, so changing b changes a too. How to copy: Python a.copy(), a[:] or list(a); JavaScript/TypeScript [...a] or a.slice(); Java a.clone() or Arrays.copyOf(a, n); C# (int[])a.Clone() (for a List, new List<int>(a)); Craykon a.GetRange(0, a.Count). C++ is the other way round: std::vector<int> b = a;copies the whole thing, and to share you must write auto& b = a; (a reference). In C, int *b = a; is only a pointer to the same array; use a separate array and memcpy(b, a, n * sizeof a[0]) with the right byte count. sizeof a on a pointer gives the pointer's size (usually 8 bytes), not the array's. All of these copies are one level deep: if the list holds lists, the inner lists are still shared.
Pythonmain.py · 9 lines
n = int(input())a = list(map(int, input().split()))b = a.copy() # b = a 라면 같은 목록에 이름만 둘m = int(input())for _ in range(m): i, v = map(int, input().split()) b[i - 1] = vprint(*a)print(*b)
JavaScriptmain.js · 11 lines
const nums = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const n = nums[0];const a = nums.slice(1, 1 + n);const b = [...a]; // b = a 라면 같은 배열을 가리킨다const m = nums[1 + n];for (let k = 0; k < m; k++) { const i = nums[2 + n + 2 * k]; const v = nums[3 + n + 2 * k]; b[i - 1] = v;}console.log(a.join(" ") + "\n" + b.join(" "));
TypeScriptmain.ts · 12 lines
import { readFileSync } from "node:fs";const nums: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const n: number = nums[0];const a: number[] = nums.slice(1, 1 + n);const b: number[] = [...a]; // b = a 라면 같은 배열을 가리킨다const m: number = nums[1 + n];for (let k = 0; k < m; k++) { const i: number = nums[2 + n + 2 * k]; const v: number = nums[3 + n + 2 * k]; b[i - 1] = v;}console.log(a.join(" ") + "\n" + b.join(" "));
JavaMain.java · 25 lines
import java.util.Scanner;import java.util.StringJoiner;public class Main { static String join(int[] arr) { StringJoiner out = new StringJoiner(" "); for (int v : arr) out.add(String.valueOf(v)); return out.toString(); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); int[] b = a.clone(); // int[] b = a; 라면 같은 배열을 가리킨다 int m = sc.nextInt(); for (int k = 0; k < m; k++) { int i = sc.nextInt(); b[i - 1] = sc.nextInt(); } System.out.println(join(a)); System.out.println(join(b)); }}
C#Main.cs · 20 lines
using System;using System.Linq;class Program{ static void Main() { Console.ReadLine(); int[] a = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); int[] b = (int[])a.Clone(); // int[] b = a; 라면 같은 배열을 가리킨다 int m = int.Parse(Console.ReadLine()); for (int k = 0; k < m; k++) { int[] iv = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); b[iv[0] - 1] = iv[1]; } Console.WriteLine(string.Join(" ", a)); Console.WriteLine(string.Join(" ", b)); }}
C++main.cpp · 22 lines
#include <iostream>#include <vector>void print_all(const std::vector<int>& v) { for (size_t i = 0; i < v.size(); i++) std::cout << (i ? " " : "") << v[i]; std::cout << "\n";}int main() { int n, m, i, v; std::cin >> n; std::vector<int> a(n); for (int& x : a) std::cin >> x; std::vector<int> b = a; // vector는 대입하면 통째로 복사된다 std::cin >> m; for (int k = 0; k < m; k++) { std::cin >> i >> v; b[i - 1] = v; } print_all(a); print_all(b);}
Cmain.c · 22 lines
#include <stdio.h>#include <string.h>void print_all(const int *arr, int n) { for (int i = 0; i < n; i++) printf(i ? " %d" : "%d", arr[i]); putchar('\n');}int main(void) { int n, m, i, v, a[1000], b[1000]; scanf("%d", &n); for (int k = 0; k < n; k++) scanf("%d", &a[k]); memcpy(b, a, n * sizeof a[0]); /* 바이트 수 = 개수 × 한 칸의 크기 */ scanf("%d", &m); for (int k = 0; k < m; k++) { scanf("%d %d", &i, &v); b[i - 1] = v; } print_all(a, n); print_all(b, n); return 0;}
Craykonmain.cray · 12 lines
n = Console.ReadLine().ToNumber();a = Console.ReadNumbers();// b = a; 라면 같은 목록에 이름만 둘이다. 부분목록은 새 목록을 만든다b = a.GetRange(0, a.Count);m = Console.ReadLine().ToNumber();for (k = 0; k < m; k++){ iv = Console.ReadNumbers(); b[iv[0] - 1] = iv[1];}Console.WriteLine(a);Console.WriteLine(b);