试题D:最少刷题数
设一个例子是12 10 15 20 6
(1)先排序:6 10 12 15 20
(2)设一个例子是:后一半(12、15、16)的人是不需要动的,他们是及格的;
而前面一半的(6、10)的则要争抢及格的名额,刷题数目就要超过刚好及格的人的刷题数;
也就是不及格的人要刷到题目数超过最后一个及格的人(最少刷13题);
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
int n = scan.nextInt();
int []arrs = new int[n];
//hhh数组复制arrs里面的
//也可以用int[] hhh = Arrays.copyOfRange(arrs, 0, arrs.length);
int []hhh = new int[n];
for (int i = 0; i < n ; i++) {
arrs[i] = scan.nextInt();
hhh[i] = arrs[i] ;
System.out.println(hhh[i]);
}
//这个快速排序,为了练习才写的
//可以使用Arrays.sort(arrs); Java自带的快速排序
QuickSort(0,n-1,arrs);
int mid = arrs[n/2]; //mid是最后一个及格的人 答题数目
//再循环一遍没排序的数组,即可输出答案
for (int i = 0; i < n; i++) {
if(mid > hhh[i]){
System.out.println(mid - hhh[i] + 1);
}else if(mid<=hhh[i]){
System.out.println(0);
}
}
scan.close();
}
//快速排序
public static void QuickSort(int left,int right,int[] num) {
if(left<right){
int p = partition(left,right,num);
QuickSort(left,p-1,num);
QuickSort(p+1,right,num);
}
}
public static int partition(int left,int right,int[] num){
int p = num[left];
while(left<right){
while(left<right && num[right]>=p) --right;
num[left] = num[right];
while(left<right && num[left]<=p) ++left;
num[right] = num[left];
}
num[left] = p;
return left;
}
试题E:求阶乘
此题关键点在于:
①看有多少个0,取决于我们阶乘里面有多少个5
②通过一个数学公式可得,其中(x / 5^n < 1)的我们直接相当于他等于0
因为5!里面有一个是5的倍数,25!里面有一个是25的倍数,125!有一个是125的倍数
本来x/25 = 2,而在x/5已经包含了一次,故只需要加上一次x/25即可
当阶乘是25!,我们就有
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
int n = nextInt();
//five保存当前阶乘i/five => 5的个数,25的个数,125的个数
int five,ans = 0;
//temp保存当前i!/5 i!/25 i!/125...
int temp;
//i是当前阶乘的数目5!,10!,15!,20!,25!....
int i=5;
while(ans != n){
//five保存了5,25,125,625....
five = 5;
ans = 0;
//第二个循环:判断i除5,25,125等5的幂次至少有1个,如果没有就没必要进行
while(i/five != 0){
//temp 接受 i!/5 + i!/25 +i!/125
temp = i/five;
ans = ans + temp;
five *=five;
}
//结果会多加5
i = i+5;
}
System.out.println(i-5);
scan.close();
}
}