Codeforces Beta Round #13 13A Numbers

Codeforces Beta Round #13 13A Numbers

题目:

Little Petya likes numbers a lot. He found that number 123 in base 16 consists of two digits: the first is 7 and the second is 11. So the sum of digits of 123 in base 16 is equal to 18.

Now he wonders what is an average value of sum of digits of the number A written in all bases from 2 to A - 1.

Note that all computations should be done in base 10. You should find the result as an irreducible fraction, written in base 10.

输入:

Input contains one integer number A (3 ≤ A ≤ 1000).

输出:

Output should contain required average value in format «X/Y», where X is the numerator and Y is the denominator.

样例:

输入:

5

输出:

7/3

输入:

3

输出:

2/1

分析:

首先分析如何去计算各个进制数字每一位的和,首先想到的就是数组存储然后求和。存储的每一位是可以通过取余得到的。这是基本的思路。

但是还要考虑最简分数的问题,要化简分数实质上是找两个数的最大公约数,就可以使用gcd递归,辗转相除法即可。

代码:

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>

using namespace std;

int sum = 0;

int gcd(int x,int y)
{
    
    
    if(!y)
    {
    
    
        return x;
    }
    else
    {
    
    
        return gcd(y,x%y);
    }
}

void trans(int n,int x)
{
    
    
    int q = 0;
    int a[1000] = {
    
    0};
    while(n)
    {
    
    
        a[q++] = n%x;
        n /= x;
    }
    for(int i = 0;i < q;i++)
    {
    
    
        sum += a[i];
    }
}

int main()
{
    
    
    int n;

    cin>>n;
    for(int i = 2;i <= n-1;i++)
    {
    
    
        trans(n,i);
    }
    cout<<sum/gcd(sum,n - 2)<<"/"<<(n - 2)/gcd(sum,n - 2)<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46052886/article/details/113402486