输入包括一行,四个整数a,b,c,d(1 ≤ a,b,c,d ≤ 10^6), 以空格分割
输出一个整数,表示牛牛最少需要支付的硬币
4 1 5 4
4
arr = sorted(map(int, input().split()))
print(arr[3] + arr[2] - arr[1] - arr[0])
根据数学推导:
所以总共要进行len(max) + len(max2) - len(min2)- len(min)
次操作
#include<iostream> #include<algorithm>using namespace std;int main() { int arr[4]; cin >> arr[0] >> arr[1] >> arr[2] >> arr[3]; sort(arr, arr + 4); int sum = (arr[3] - arr[0]) + (arr[2] - arr[1]); cout << sum << endl; return 0; }
有四个数字,a, b, c, d,从小到大排序,假设边长定为 x,则需要改变的次数为
total =
四个数,能构成五个区域,两端的不考虑,不做解释。
x 在 a 和 b 之间时,结果为 ,由于 ,x 取b最小,为
x 在 b 和 c 之间时,结果为
x 在 c 和 d 之间时,结果为 ,由于 ,x取c最小,为
import java.io.*; import java.util.Arrays; public class Main{ public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; while((line = br.readLine()) != null){ int[] a = new int[4]; String[] s = line.trim().split(" "); for(int i=0;i<4;i++){ a[i] = Integer.parseInt(s[i]); } Arrays.sort(a); int money = a[2]-a[0]+a[3]-a[1]; System.out.println(money); } } }
arr=list(map(int,input().split())) pay=[] for i in range(min(arr),max(arr)+1): sum=0 for j in range(4): sum+=abs(arr[j]-i) pay.append(sum) print(min(pay))
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int a, b, c, d, tmp, ans; while(scanf("%d%d%d%d", &a, &b, &c, &d) == 4) { ans = abs(a - b) + abs(a - c) + abs(a - d); tmp = abs(b - a) + abs(b - c) + abs(b - d); ans = (ans < tmp) ? ans : tmp; tmp = abs(c - a) + abs(c - b) + abs(c - d); ans = (ans < tmp) ? ans : tmp; tmp = abs(d - a) + abs(d - b) + abs(d - c); ans = (ans < tmp) ? ans : tmp; printf("%d\n", ans); } return 0; }
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin=new Scanner (System.in); int a[]=new int[4]; for(int i=0;i<4;i++) { a[i]=cin.nextInt(); } Arrays.sort(a); int min=0; min=min+(a[1]-a[0])+(a[3]-a[2])+2*(a[2]-a[1]); System.out.print(min); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] arr = new int[4]; for (int i = 0; i < 4; i++) { arr[i] = sc.nextInt(); } int min = Integer.MAX_VALUE; for (int i = 0; i < 4; i++) { int cur = Math.abs(arr[i] - arr[0]) + Math.abs(arr[i] - arr[1]) + Math.abs(arr[i] - arr[2]) + Math.abs(arr[i] - arr[3]); if (cur < min) { min = cur; } } System.out.println(min); } }
//无脑做法
#include<iostream> #include<algorithm> #include<cmath> using namespace std; int main(int argc, char** argv) { int a, b, c, d, v[4] = { 0 }; cin >> a >> b >> c >> d; v[0] = ab***-a) + abs(c-a) + abs(d-a); v[1] = abs(a-b) + abs(c-b) + abs(d-b); v[2] = abs(a - c) + abs(c - b) + abs(d - c); v[3] = abs(a - d) + abs(d - b) + abs(d - c); sort(v, v + 4); cout << v[0] << endl; system("pause"); }
lst=list(map(int, input().split())) print(min([sum([abs(j-i) for j in lst]) for i in range(min(lst),max(lst)+1)]))
lst=list(map(int, input().split())) a1=min(lst) a2=max(lst) cs=[] for i in range(a1,a2+1): k=0 #完全可以用sum加abs加列表推导式的,不想用太多高级函数 #k=sum([abs(j-i) for j in lst]) for j in lst: #k=k+abs(j-i) m=j-i if m<0: k-=m else: k+=m cs.append(k) print(min(cs))
#include<iostream> #include<stdio.h> #include<algorithm> #include<cstring> #include<vector> #include<math.h> using namespace std; int main(){ //木棍拼凑正方形,四个木棍长度相等 int a, b, c, d; cin >> a >> b >> c >> d; //最后的正方形的边长必为初始给的四个数之一 int m1, m2, m3, m4; m1 = abs(b - a) + abs(c - a) + abs(d - a); m2 = abs(a - b) + abs(c - b) + abs(d - b); m3 = abs(a - c) + abs(b - c) + abs(d - c); m4 = abs(a - d) + abs(b - d) + abs(c - d); cout << min(min(m1, m2),min(m3, m4)) << endl; return 0; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] lens = new int[4]; for (int i=0; i!=4; i++) { lens[i] = sc.nextInt(); } Arrays.sort(lens); int ans = 0; for (int i=0; i!=4; i++) { ans += Math.abs(lens[i] - lens[2]); } System.out.println(ans); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; /** * @author wylu */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] strs = br.readLine().split(" "); int[] a = new int[strs.length]; for (int i = 0; i < strs.length; i++) a[i] = Integer.parseInt(strs[i]); Arrays.sort(a); System.out.println((a[3] - a[0]) + (a[2] - a[1])); } }
//JS*** var str = readline(); var arr = str.split(" "); arr.sort(function(a,b){ return b-a; }) var num = []; for(var i =0 ;i<arr.length;i++){ num[i] = 0; for(var j = 0 ; j< arr.length ;j++){ var gap = (parseInt(arr[i])-parseInt(arr[j])); num[i] +=parseInt(Math.abs(gap)); } } print(Math.min.apply(Math,num));