0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paizaラーニングレベルアップ問題集の「最大値と最小値」をやってみた。

Posted at

paizaラーニングレベルアップ問題集の最大値と最小値をやってみました。


問題


普通はmax(a, b, c) - min(a, b, c)と書く、と思いますが、
今回はmax(abs(b-a), abs(c-a), abs(c-b))で書いてみたいと思います。
(特に深い理由はありません)


C
#include <stdio.h>
#include <stdlib.h>

int chmax(int* a, int b) {
	if (b > *a) *a = b;
	return *a;
}

int main() {
	int a, b, c;
	scanf("%d %d %d", &a, &b, &c);
	int ans = abs(b - a);
	chmax(&ans, abs(c - a));
	chmax(&ans, abs(c - b));
	printf("%d\n", ans);
	return 0;
}

C++
#include <iostream>
using namespace std;

int main() {
	int a, b, c;
	cin >> a >> b >> c;
	cout << max(abs(b - a), max(abs(c - a), abs(c - b))) << endl;
	return 0;
}

C#
using System;

class Program
{
	public static void Main()
	{
		int[] abc = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int a = abc[0], b = abc[1], c = abc[2];
		Console.WriteLine(Math.Max(Math.Abs(b - a), Math.Max(Math.Abs(c - a), Math.Abs(c - b))));
	}
}

Go
package main
import (
	"fmt"
	"math"
)

func main() {
	var a, b, c int
	fmt.Scan(&a, &b, &c)
	fmt.Println(int(math.Max(math.Abs(float64(b - a)), math.Max(math.Abs(float64(c - a)), math.Abs(float64(c - b))))))
}

Java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		sc.close();
		System.out.println(Math.max(Math.abs(b - a), Math.max(Math.abs(c - a), Math.abs(c - b))));
	}
}

JavaScript
const [a, b, c] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(Math.max(Math.abs(b - a), Math.abs(c - a), Math.abs(c - b)));

Kotlin
fun main() {
	val (a, b, c) = readln().split(' ').map { it.toInt() }
	println(Math.max(Math.abs(b - a), Math.max(Math.abs(c - a), Math.abs(c - b))))
}

PHP
<?php
	[$a, $b, $c] = array_map("intval", explode(' ', fgets(STDIN)));
	echo max(abs($b - $a), abs($c - $a), abs($c - $b)), PHP_EOL;
?>

Perl
use List::Util qw(max);

my ($a, $b, $c) = map { int($_) } split ' ', <STDIN>;
print max(abs($b - $a), abs($c - $a), abs($c - $b)), $/;

Python3
a, b, c = map(int, input().split())
print(max(abs(b - a), abs(c - b), abs(c - a)))

Ruby
a, b, c = gets.split.map(&:to_i)
p [(b - a).abs, (c - a).abs, (c - b).abs].max

Scala
import scala.io.StdIn._

object Main extends App{
	val Array(a, b, c) = readLine().split(' ').map { _.toInt }
	println(Math.max(Math.abs(b - a), Math.max(Math.abs(c - a), Math.abs(c - b))))
}

Swift
let abc = readLine()!.split(separator: " ").compactMap { Int($0) }
let (a, b, c) = (abc[0], abc[1], abc[2])
print(max(abs(b - a), abs(c - a), abs(c - b)))
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?