1 条题解
-
0
C++ :
#include <bits/stdc++.h> using namespace std; int main(){ int arr[1000],n,i,a = 0,b = 0,c = 0,d = 0; cin>>n; for(i = 0;i < n;i++){ cin>>arr[i]; if(arr[i] <= 18){ a++; }else if(arr[i] <= 35){ b++; }else if(arr[i] <= 60){ c++; }else{ d++; } } cout<<a<<" "<<fixed<<setprecision(2)<<a*100.0/n<<"%"<<endl; cout<<b<<" "<<fixed<<setprecision(2)<<b*100.0/n<<"%"<<endl; cout<<c<<" "<<fixed<<setprecision(2)<<c*100.0/n<<"%"<<endl; cout<<d<<" "<<fixed<<setprecision(2)<<d*100.0/n<<"%"<<endl; }Java :
import java.text.NumberFormat; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i] = sc.nextInt(); } getCount(a); } public static void getCount(int[] a){ int count1 = 0;int count2 = 0;int count3 = 0;int count4 = 0; double per1 = 0;double per2 = 0;double per3 = 0;double per4= 0; NumberFormat nt = NumberFormat.getPercentInstance(); nt.setMinimumFractionDigits(2); for(int i = 0;i<a.length;i++){ if(a[i] >=0 && a[i]<=18){ count1++; per1 = count1*1.0/a.length; }else if(a[i] >=19 && a[i]<=35){ count2++; per2 = count2*1.0/a.length; }if(a[i] >=36 && a[i]<=60){ count3++; per3 = count3*1.0/a.length; }if( a[i]>=61){ count4++; per4 = count4*1.0/a.length; } } System.out.println(count1 + " " +nt.format(per1)); System.out.println(count2 + " " +nt.format(per2)); System.out.println(count3 + " " +nt.format(per3)); System.out.println(count4 + " " +nt.format(per4)); } }Python :
n=int(input()) s1=list(map(int,input().split())) alen=len([item for item in s1 if 0<=item<=18]) print("%d %.2lf%%"%(alen,100*alen/len(s1))) alen=len([item for item in s1 if 19<=item<=35]) print("%d %.2lf%%"%(alen,100*alen/len(s1))) alen=len([item for item in s1 if 36<=item<=60]) print("%d %.2lf%%"%(alen,100*alen/len(s1))) alen=len([item for item in s1 if 61<=item]) print("%d %.2lf%%"%(alen,100*alen/len(s1)))
- 1
信息
- ID
- 406
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者