1 条题解
-
0
C++ :
#include<bits/stdc++.h> using namespace std; int main() { int a[1010],n,i,s = 0,r; double v,mi; cin>>n; for(i = 0;i < n;i++){ cin>>a[i]; s = s + a[i]; } v = s * 1.0 / n; mi = fabs(a[0] - v); r = a[0]; // cout<<v<<endl; for(i = 1;i < n;i++){ if(abs(a[i] - v) <= mi){ r = a[i]; mi = fabs(a[i] - v); // cout<<r<<" "<<mi<<endl; } } cout<<r<<endl; return 0; }
Java :
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num1 = scanner.nextInt(); // int num2 = scanner.nextInt(); int[] a = new int[num1]; double s = 0,avg,x = 0; for (int i = 0; i < num1; i++) { a[i] = scanner.nextInt(); x = x + a[i]; } avg = x * 1.0 / num1; double min = Math.abs(avg - a[0]); for (int i = 0; i < num1; i++) { if(min > Math.abs(avg - a[i]))min = Math.abs(avg - a[i]); } for (int i = 0; i < num1; i++) { if(min == Math.abs(avg - a[i])) System.out.println(a[i]); } } public static boolean judge(int i,int j){ int a,s = 0; while(i != 0){ a = i % 10; s = s + a; i /= 10; } if(s == j)return true; else return false; } public static void paiXu(int[] a){ int temp; for(int i = 0;i < a.length - 1;i++){ for(int k = 0;k < a.length - i - 1;k++){ if(a[k] > a[k + 1]){ temp = a[k]; a[k] = a[k + 1]; a[k + 1] = temp; } } } } public static int MAX(int[] a){ int max = a[0]; for (int i = 0; i < a.length; i++) { if(max < a[i]){ max = a[i]; } } return max; } public static int MIN(int[] a){ int min = a[0]; for (int i = 0; i < a.length; i++) { if(min > a[i]){ min = a[i]; } } return min; } }
Python :
n=int(input()) a=input().split() a=list(map(int,a)) s=sum(a) av=s/n cha=abs(a[0]-av) chai=0 for i in range(1,n): if(abs(a[i]-av)<cha): cha=abs(a[i]-av) chai=i print(a[chai])
- 1
信息
- ID
- 2498
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者