codeforce 13 A && hdu 2031 有关进制的转化

http://acm.hdu.edu.cn/showproblem.php?pid=2031

http://codeforces.com/problemset/problem/13/A

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

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

Output

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

Sample test(s)
input
5
output
7/3
input
3
output
2/1
codeforce上的这个题目是要求将一个数A分别转化成2~A-1进制的数,每转化一次把各个数位的数加起来,而后把每一次转化的数在求和,最后求平均值(最间分数来表示)。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
using namespace std;
int getsum(int a,int t){
    int ans=0;
    while(a){
        ans+=a%t;
        a/=t;
    }
    return ans;

}
int gcd(int a,int b){
    while(b){
        int t=a%b;
        a=b;
        b=t;
    }
    return a;
}
int main(){
    int n;
    int ans=0;
    while(cin>>n){
        ans=0;
        for(int i=2;i<=n-1;i++)
            ans+=getsum(n,i);
        int tb=n-2;
        int tt=gcd(ans,tb);
        cout<<ans/tt<<'/'<<tb/tt<<endl;
    }
    return 0;
}

Problem Description
输入一个十进制数N,将它转换成R进制数输出。
 

Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。
 

Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
 

Sample Input
  
  
7 2 23 12 -4 3
 

Sample Output
  
  
111 1B -11
#include <stdio.h>
void jinzhi(int x,int y)
{
    if(x)
    {
        jinzhi(x/y,y);
        printf("%c",x%y>9 ? x%y-10+'A':x%y+'0');
    }
}
int main()
{
    int x,y;
    void jinzhi(int x,int y);
    while(~scanf("%d%d",&x,&y))
    {
        if(x>0)
            jinzhi(x,y);
        else if(x<0)
        {
            putchar('-');
            jinzhi(-x,y);
        }
        else
            printf('0');
        putchar('\n');
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值