第一行有一个整数n(1<= n <= 100),表示学生的人数。其后n行每行有1个整数,取值为15到25。
可能有多组测试数据,对于每组数据, 输出一行,该行包含一个浮点数,为要求的平均年龄,保留到小数点后两位。 要输出浮点数、双精度数小数点后2位数字,可以用下面这种形式: printf("%.2f", num);
2 18 17
17.50
import java.text.DecimalFormat; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); double sum=0; while (scanner.hasNext()){ int i = scanner.nextInt(); sum+=i; } System.out.println(new DecimalFormat(".00").format(sum/n)); } }
#include<iostream> #include<iomanip> using namespace std; int main(){ int n; while(cin>>n){ int* stu=new int[n]; int sum=0; for(int i=0;i<n;i++){ cin>>stu[i]; sum+=stu[i]; } cout<<fixed<<setprecision(2)<<(double)sum/n<<endl; } }
def avg(a): print('%.2f'%(sum(a)/len(a))) while True: try: age = [] n = int(input()) for i in range(n): a = int(input()) age.append(a) avg(age) except: break
#include <iomanip> #include <iostream> using namespace std; int main() { int n, num; while (cin >> n) { double sum = 0; for (int i = 0; i < n; i++) { cin >> num; sum += num; } cout << setiosflags(ios_base::fixed) << setprecision(2) << sum / n << endl; } return 0; }
#include "stdio.h" int main(){ int n; while(scanf("%d",&n)!=EOF){ int age,sum = 0; for(int i=0; i<n; ++i){ scanf("%d",&age); sum += age; } double average = sum*1.0/n; printf("%.2lf\n",average); } }
#include<iostream> #include<cstdio> using namespace std; int MAX_N = 100; int main(){ int n; while(scanf("%d", &n) != EOF){ int arr[n]; int sum = 0; for(int i = 0; i < n; i++){ scanf("%d", &arr[i]); sum += arr[i]; } printf("%.2f", (float) sum / n); } return 0; }
#include<stdlib.h> #include<stdio.h> int main(){ int n; while(scanf("%d",&n)!=EOF){ int *num=(int *)malloc(sizeof(int)*n); float sum=0; for(int i=0;i<n;i++){ int temp; scanf("%d",&temp); num[i]=temp; } for(int i=0;i<n;i++) sum+=num[i]; printf("%.2f",sum/n); printf("\n"); } }
#include <iostream> using namespace std; int main() { int N;float m[100]; while(cin>>N) { if(N<1||N>100)cout<<"你的输入有误"<<endl; float sum=0; for(int i=0;i<N;i++) { cin>>m[i]; if(m[i]<15||m[i]>25){ cout<<"您的输入有误"<<endl; break; } sum+=m[i]; } printf("%.2f",sum/N); } return 0; }
#include<iostream> using namespace std; int main(){ int n; int arr[101]; while(cin >> n){ float sum = 0; for(int i = 1; i <= n; i++){ cin >> arr[i]; sum += arr[i]; } printf("%.2f",sum/n); } }
#include<stdio.h> (737)#include<math.h> int main() { int n; float age[100]={0},sum_age=0; while(scanf("%d",&n)!=EOF) { int i=0; for(;i<n;i++) { scanf("%f",&age[i]); sum_age+=age[i]; } printf("%.2f",sum_age/n); } return 0; }
#include<stdio.h> int main() { int n,i,a,sum=0; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a); sum=sum+a; } printf("%.2f",sum*1.0/n); return 0; }