一个数A,如果按2到A-1进制表达时,各个位数之和的均值是多少? 结果用最简分数表示。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public class Main{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
while(s.hasNextLine()){
int n = s.nextInt();
s.nextLine();
int sum = sum(n);
int m = n - 2;
int g = gcd(sum,m);
sum /= g;
m /= g;
System.out.println(sum+"/"+m);
}
}
//求从2至n-1进制各个位上数字之和
private static int sum(int n){
int sum = 0, h = n;
for(int i=2;i<n;i++){
//求i进制各个位上数字和
while(h>0){
sum += h%i;
h /= i;
}
h = n;
}
return sum;
}
//求最大公约数
private static int gcd(int m,int n){
while(n!=0){
int r = m%n;
m = n;
n = r;
}
return m;
}
}
|