题解

46 条题解

  • 2
    @ 2018-05-06 23:03:54

    一道进制转换我竟然敲了一个小时……(捂脸)主要是第五组数据太坑了……(强行推脱)
    思路和普通进制转换类似,特殊之处是当余为负数时要加上一个模数的绝对值变为正数,然后商加一即可。还要注意我这个代码只循环到倒数第二次处理,最后一次要再敲一遍代码(没办法,本蒟蒻不会合成一个)还有,n=0特判!!!n=0特判!!!n=0特判!!!重要的事情说三遍,否则你会感受到被第五个点支配的绝望~(我觉得代码还有一定可读性)

    #include<iostream>
    #include<cmath>
    using namespace std;
    int ans,now,a,b,a1;
    bool bj;
    char cl[105];
    int main()
    {
        while(cin>>a>>b)
        {   
            now=0;
            bj=0;
            a1=a;
            ans=a%b;
            a/=b;
            while(a!=0)
            {
                while(ans<0)
                {
                    ans+=abs(b);
                    a++;
                }
                if(ans<=9)
                    cl[++now]=ans+'0';
                else
                    cl[++now]=ans-10+'A';
                ans=a%b;
                a/=b;
            }
            while(ans<0)
            {
                ans+=abs(b);
                bj=1;
            }
            if(ans<=9)
                cl[++now]=ans+'0';
            else
                cl[++now]=ans-10+'A';
            if(bj)
                cl[++now]=1+'0';
            if(a1==0)
            cout<<"0=(base "<<b<<")"<<endl;
            else
            { 
                cout<<a1<<"=";
                for(int i=now;i>=1;--i)
                    cout<<cl[i];
                cout<<"(base"<<" "<<b<<")"<<endl;
            }
        }
        return 0;
    }
    
  • 0
    @ 2016-05-29 20:30:13

    #include<iostream>
    #include<vector>
    #include<cstdio>
    using namespace std;
    vector<int> h;
    int main()
    {
    int x,y;
    while(cin>>x>>y)
    {
    h.clear() ;
    cout<<x<<'=';
    while(x)
    {
    int n=x%y;
    x/=y;
    if(n<0)
    {
    n-=y; x++;
    }
    h.push_back(n);

    }
    for(int i=h.size()-1;i>=0;i--)
    if(h[i]<10)cout<<h[i];
    else printf("%c",h[i]+'A'-10);
    cout<<"(base "<<y<<')';
    cout<<endl;
    }
    return 0;
    }

  • 0
    @ 2015-10-04 15:17:39

    记录信息
    评测状态 Accepted
    题目 P1465 进制转换
    递交时间 2015-10-04 15:17:18
    代码语言 C++
    评测机 VijosEx
    消耗时间 245 ms
    消耗内存 528 KiB
    评测时间 2015-10-04 15:17:18
    评测结果
    编译成功

    测试数据 #0: Accepted, time = 46 ms, mem = 524 KiB, score = 10
    测试数据 #1: Accepted, time = 31 ms, mem = 528 KiB, score = 10
    测试数据 #2: Accepted, time = 15 ms, mem = 524 KiB, score = 10
    测试数据 #3: Accepted, time = 15 ms, mem = 520 KiB, score = 10
    测试数据 #4: Accepted, time = 31 ms, mem = 524 KiB, score = 10
    测试数据 #5: Accepted, time = 15 ms, mem = 524 KiB, score = 10
    测试数据 #6: Accepted, time = 31 ms, mem = 520 KiB, score = 10
    测试数据 #7: Accepted, time = 46 ms, mem = 524 KiB, score = 10
    测试数据 #8: Accepted, time = 15 ms, mem = 528 KiB, score = 10
    测试数据 #9: Accepted, time = 0 ms, mem = 520 KiB, score = 10
    Accepted, time = 245 ms, mem = 528 KiB, score = 100
    代码
    #include <iostream>
    #include <stdio.h>
    using namespace std;
    char num[20]={'0','1','2','3',
    '4','5','6','7','8','9','A',
    'B','C','D','E','F','G','H','I','J'};
    int main()
    {
    int a,b;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
    char stack[100];
    int cnt=1;
    printf("%d=",a);
    for(;a!=0;){
    for(int i=0;i<-b;i++){
    if((a-i)%(-b)==0){
    stack[cnt++]=num[i];
    a-=i;
    break;
    }
    }
    a/=b;
    }
    for(--cnt;cnt>0;cnt--)printf("%c",stack[cnt]);
    printf("(base %d)\n",b);
    }
    }

  • 0
    @ 2015-10-03 16:50:12

    ´´´C
    #include "stdio.h"
    #include "math.h"
    int num[25];
    char number[25]={'0','1','2','3','4','5','6','7','8','9',
    'A','B','C','D','E','F','G','H','I','J'};
    int main()
    {
    int n,r,i,len;
    while(scanf("%d%d",&n,&r)!=EOF) //check EOF
    {
    for(i=0;i<25;i++) num[i]=0;//initialize the array
    len=0;
    printf("%d=",n);
    while(n!=0)
    {
    if(n%r<0)
    {
    num[len++]=n%r-r;
    n=n/r+1;
    }
    else
    {
    num[len++]=n%r;
    n/=r;
    }
    }
    while(num[len]==0&&len>0) len--; //especially pay attention to 0!!!
    if(len==0&&num[0]==0) printf("(base %d)\n",r);
    else
    {
    for(i=len;i>=0;i--)
    printf("%c",number[num[i]]);
    printf("(base %d)\n",r);
    }
    }
    return 0;
    }
    ´´´

  • 0
    @ 2015-04-07 16:23:56

    var i,j,k,m,n:longint;
    a:array[0..32] of longint;
    begin
    assign(input,'1465.in');reset(input);
    //assign(output,'.out');rewrite(output);

    while not eof do begin
    readln(n,m);
    write(n,'=');
    while n<>0 do begin
    j:=n mod m;
    if j<0 then begin
    j:=n-((n div m+1)*m);
    n:=n div m+1;
    end else n:=n div m;
    inc(a[0]);
    a[a[0]]:=j;
    end;
    while a[0]>0 do begin
    if a[a[0]]>9 then write(chr(a[a[0]]+55))
    else
    write(a[a[0]]);
    dec(a[0]);
    end;
    writeln('(base ',m,')');
    end;
    end.

  • 0
    @ 2013-10-01 18:49:37

    //NOIP2000进制转换
    #include <iostream>
    #include <cstdlib>
    #include <cmath>

    using namespace std;
    int a[100000000];

    int main()
    {
    char b[21];
    b[0]='0';
    b[1]='1';
    b[2]='2';
    b[3]='3';
    b[4]='4';
    b[5]='5';
    b[6]='6';
    b[7]='7';
    b[8]='8';
    b[9]='9';
    b[10]='A';
    b[11]='B';
    b[12]='C';
    b[13]='D';
    b[14]='E';
    b[15]='F';
    b[16]='G';
    b[17]='H';
    b[18]='I';
    b[19]='J';
    int n,x,y;
    while(cin >>n>>x)
    {
    cout <<n<<"=";
    a[0]=0;
    while(n!=0)
    {
    a[0]++;
    if(n % x<0)//负进制处理
    {
    y=n/x+1;
    }
    if(n % x>=0)
    {
    y=n/x;
    }
    a[a[0]]=n-y*x;
    n=y;
    }
    for(int i=a[0];i>0;i--)
    cout <<b[a[i]];
    cout <<"(base "<<x<<")\n";
    }
    return 0;
    }

  • 0
    @ 2009-11-19 09:45:39

    const d:array[0..20] of char=('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K');

    var n,m,i:longint;

    procedure work(n,m:longint);

    var a,b,i:longint;

    str:ansistring;

    begin

    i:=1; a:=n;

    repeat

    b:=a mod m;

    a:=a div m;

    if b

  • 0
    @ 2009-10-27 18:59:36

    编译通过...

    ├ 测试数据 01:答案正确... 0ms

    ├ 测试数据 02:答案正确... 0ms

    ├ 测试数据 03:答案正确... 0ms

    ├ 测试数据 04:答案正确... 0ms

    ├ 测试数据 05:答案正确... 0ms

    ├ 测试数据 06:答案正确... 0ms

    ├ 测试数据 07:答案正确... 0ms

    ├ 测试数据 08:答案正确... 0ms

    ├ 测试数据 09:答案正确... 0ms

    ├ 测试数据 10:答案正确... 0ms

    ---|---|---|---|---|---|---|---|-

    Accepted 有效得分:100 有效耗时:0ms

    program P1465;

    var n,r:longint;

    s,ss:string;

    procedure f(a,b:longint);

    var t:longint;

    begin

    write(a,'=');

    while a0 do

    begin

    t:=a mod b;

    a:=a div b;

    if t

  • 0
    @ 2009-09-25 13:28:23

    0的时候是

    0=(base ...

    无语了

  • 0
    @ 2009-08-08 01:11:51

    C/C++的人注意了,本人声明此题的输入格式为:

    while((scanf("%d",&n))!=EOF)

    {

    cin >> k;

    ……

    }

    本人那小号的通过率换来的,本人不想让大家费通过率

  • 0
    @ 2009-07-19 09:50:43

    大家可以看看这里:

    这一题是关于进制转换的,只不过进制是负的,所以我们还是以正常的进制转换为框架进行解题。(如果正常的都不会就不要继续看了)

    首先,我们可以把N,S都取绝对值,做一遍正常的进制转换。显然这个不能得到正解,但是我们可以通过一系列操作使它变为正确的解。

    我们先来观察一下正进制与负进制在哪些数位上有区别,不难发现,当数位数MOD2等于0的数位是表示负数的。举个例子12345(-10进制)中,4在从右往左数第二位上表示-40,2在从右往左第四位上表示-2000。

    现在知道了其实正进制与负进制的区别其实不是很大以后(仅在偶数位上会产生区别),我们完全可以想办法消除这些区别,对正常的进制转换的得数进行修改即可。

    为了分析的方便,我们不妨先分析N>0的情况。当N大于0时,由于只有偶数位会产生区别,所以我们从低位到高位逐个修改偶数位。假设我们修改到了第M位,第M位上正常转换过来时的系数为A[M],从正常的转换的角度来说这个A[M]表示的是A[M]*S^M,但是由于是负进制,所以这个A[M]实际上表示的是-A[M]*S^M,为了让它再负进制中实际上也表示A[M]*S^M,我们不妨把A[M]*S^M看作(S-A[M])*S^M。仔细观察一下,也许你就明白了为什么要这样表示。(我们把A[M+1]即第M+1位上的数加1,把A[M]改成S-A[M]不就实现了上面式子的表达吗?)如此不断进行修改,就可以得到最后结果。

    当然如果N_0 then now:=0 else now:=1;

    n:=abs(n);

    l:=0;

    while n>0 do

    begin

    inc(l);

    a[l]:=n mod k;

    n:=n div k;

    end;

    i:=1;

    while i0 then

    begin

    a[i]:=k-a[i];

    inc(a);

    t:=i+1;

    while a[t]>=k do

    begin

    a[t]:=a[t] mod k;

    inc(t);

    inc(a[t]);

    end;

    if a[l+1]>0 then inc(l);

    end;

    inc(i);

    end;

    for i:=l downto 1 do write(stri[a[i]+1]);

    writeln('(base ',-k,')');

    end;

    end;

    end.

    (*^__^*) 嘻嘻……有没有觉得晕人啊 我写的时候也有点晕啊

    ....

  • 0
    @ 2009-05-15 19:59:30

    CmYKRgB123大牛精辟的结论:

    当 a / b * b a 时,a 整除 b 结果为 a / b + 1。

    有了这个,这道题目就没什么特殊的了。

  • 0
    @ 2008-11-11 01:09:15

    重复三个过程。

    1. 去摸

    2. 除商

    3. 借位

  • 0
    @ 2008-11-10 18:33:13

    C/C++

    -4 / 3

    标准规定余数非负,但机器实现可能是和被除数同号

    如果 n 为 0,请输出 “0=(base ……)”(第五组数据)

  • 0
    @ 2008-11-08 08:41:50

    真莫名其妙的第5组

    自己用0测试对了 不过

    加了if n=0 then begin writeln('(base ',base,')'); continue; end;

    就AC了

  • 0
    @ 2008-11-01 20:07:15

    一次AC,简单的小程序。

    主要部分:

    readln(n,r);

    k:=n;

    f:='0123456789ABCDEFGHIJ';

    while k0 do

    begin

    b:=k mod r;

    k:=k div r;

    if b

  • 0
    @ 2008-10-23 20:24:25

    出题人是疯子!!!

    第五组数据答案怎么是0=(base -R)???

    WA了3次!!这组数据就是错的!!!

    希望出题人以后不要开这样的玩笑,通过率又降了一点!!!

    ├ 测试数据 01:答案正确... 0ms

    ├ 测试数据 02:答案正确... 0ms

    ├ 测试数据 03:答案正确... 0ms

    ├ 测试数据 04:答案正确... 0ms

    ├ 测试数据 05:答案正确... 0ms

    ├ 测试数据 06:答案正确... 0ms

    ├ 测试数据 07:答案正确... 0ms

    ├ 测试数据 08:答案正确... 0ms

    ├ 测试数据 09:答案正确... 0ms

    ├ 测试数据 10:答案正确... 0ms

    ---|---|---|---|---|---|---|---|-

    Accepted 有效得分:100 有效耗时:0ms

  • 0
    @ 2008-10-23 20:18:30

    样例有误,输入为

    30000 -2

    -20000 -2

    28800 -16

    -25000 -16

  • 0
    @ 2008-10-23 15:42:48

    NND第5组数据是错的。

    • -。
  • 0
    @ 2008-10-21 22:02:07

    用eof即可AC 用seekeof会WA的,冤死了

    太简单了。。。。LX的许多代码都太麻烦了。。。

信息

ID
1465
难度
6
分类
其他 | 数学 点击显示
标签
递交数
1227
已通过
330
通过率
27%
被复制
10
上传者