The first line gives n and the second line n integers. Print their minimum, maximum and sum on one line, separated by single spaces. Try solving it with a function stats that takes the array, its length, and the addresses of three caller variables, and writes the minimum, maximum and sum into them (pointers int * in C; in C++ references int& also work). main calls the function once and then prints the three variables.
Input
1 ≤ n ≤ 100,000, every value is an integer from -1,000,000,000 to 1,000,000,000
Output
One line: minimum maximum sum
Example 1 · stdin
4
3 8 1 6
stdout
1 8 18
Example 2 · stdin
3
-5 -2 -9
stdout
-9 -2 -16
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 · 2 languages
This problem accepts only C and C++. Pointers, which let you pass the address of a caller's variable so the function can write a result into it, exist only in those two; Python, JavaScript, TypeScript, Java and Craykon have none (C# only inside unsafe). C declares void stats(const int *a, int n, int *mn, int *mx, long long *sum), passes addresses with & as in stats(a, n, &mn, &mx, &sum), and writes *mn = ... inside. C++ does the same more simply with references int& mn, and the call needs no &. Other languages return several values with Python return lo, hi, s, a C# tuple or out int mn, or an array or object in Java. Three traps: (1) taking int mn instead of int *mn makes the function write only into its copy, so the caller's variable keeps its initial value, with no error. (2) The sum can reach 1 billion × 100,000 = 10¹⁴, beyond int (about 2.1 billion); use long long and %lld. (3) Starting min and max at 0 gives a minimum of 0 when every value is positive and a maximum of 0 when every value is negative; start from the first element a[0].
C++main.cpp · 27 lines
#include <iostream>#include <vector>// C++는 같은 일을 참조(&)로 한다. 포인터 int* 로 받아도 되지만 참조는 null이 될 수 없다void stats(const std::vector<int>& a, int& mn, int& mx, long long& sum) { mn = mx = a[0]; sum = 0; for (int v : a) { if (v < mn) mn = v; if (v > mx) mx = v; sum += v; }}int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<int> a(n); for (int& v : a) std::cin >> v; int mn, mx; long long sum; stats(a, mn, mx, sum); std::cout << mn << " " << mx << " " << sum << "\n";}
Cmain.c · 28 lines
#include <stdio.h>#include <stdlib.h>/* 결과 세 개를 부른 쪽 변수의 주소로 받아서, 그 자리에 써 넣는다 */void stats(const int *a, int n, int *mn, int *mx, long long *sum) { *mn = *mx = a[0]; /* 첫 칸으로 시작해야 모두 양수·모두 음수일 때도 맞다 */ *sum = 0; for (int i = 0; i < n; i++) { if (a[i] < *mn) *mn = a[i]; if (a[i] > *mx) *mx = a[i]; *sum += a[i]; /* 합은 long long: 10억 × 10만은 int를 넘는다 */ }}int main(void) { int n; scanf("%d", &n); int *a = malloc(n * sizeof *a); for (int i = 0; i < n; i++) scanf("%d", &a[i]); int mn, mx; long long sum; stats(a, n, &mn, &mx, &sum); /* 주소를 넘긴다 */ printf("%d %d %lld\n", mn, mx, sum); free(a); return 0;}