1 条题解

  • 0
    @ 2021-6-14 23:25:18

    C :

    #include<stdio.h>
    #include<string.h>
    
    
    int main()
    {
       int n,p,sum=0; 
       unsigned int i=0,j,k=0,l=0;
       char m[30],a[2],b[2],res[1000],ch;
       memset(res,0,sizeof(res));
       scanf("%s",m);
       for(i=strlen(m)-1;i>=0;i--)
       {
           if(m[i]!='>')
    	   {
    		   a[k]=m[i];
    		   k++;
    	   }
    	   else
    		   break;
       }
       a[k]='\0';
       if(k==1)
    	   p=a[0]-'0';
       else
    	   p=a[0]-'0'+(a[1]-'0')*10;
       for(i=strlen(m)-2-k;i>=0;i--)
       {
           
           if(m[i]!='<')
    	   {
    		   b[l]=m[i];
    		   l++;
    	   }
    	   else
    		   break; 
       }
       b[l]='\0';
       if(l==1)
    	   n=b[0]-'0';
       else
    	   n=b[0]-'0'+(b[1]-'0')*10;
       m[strlen(m)-k-l-2]='\0';
       for(i=0;i<strlen(m);i++)
       {
           sum=sum*n+m[i]-'0';
       }
       while(sum)
       {
       
    	   res[i]=sum%p+'0';
    	   sum/=p;
    	   i++;
       }
       res[i]='\0';
       for(j=0,--i;j<i;i--,j++)
       {
         ch=res[j];
    	 res[j]=res[i];
    	 res[i]=ch;
       }
       if(strlen(m)==0)
    	   strcmp(res,"0");
       printf("%s<%d>=%s<%d>",m,n,res,p);
       return 0;
    }
    

    C++ :

    #include <iostream>
    #include <string.h>
    #include <stack>
    #include <queue>
    using namespace std;
    int main()
    {
    	char T[30];
    	cin>>T;
    	queue <int> Q;
    	int ans=0,n=0,p=0;
    	stack <int> S;
    	int len=strlen(T);
    	int i=0;
    	while(T[i]>='0'&&T[i]<='9')
    			Q.push(T[i++]-'0');
    	i++;
    	while(T[i]>='0'&&T[i]<='9')
    			n=n*10+T[i++]-'0';
    	i++;
    	while(T[i]>='0'&&T[i]<='9'&&i<len)
    			p=p*10+T[i++]-'0';
    	while(!Q.empty())
    	{
    		ans=ans*n+Q.front();
    		cout<<Q.front();
    		Q.pop();
    	}
    	while(ans)
    	{
    		S.push(ans%p);
    		ans/=p;
    	}
    	cout<<"<"<<n<<">=";
    	while(!S.empty())
    	{
    		cout<<S.top();
    		S.pop();
    	}
    	cout<<"<"<<p<<">"<<endl;
    	return 0;
    }
    

    Pascal :

    var s,strm,strm1,strn:string;x,y,i,j:longint;n,m,m1,w:int64;
        z:array[1..10000]of integer;
    begin
      readln(s);
      x:=pos('<',s);
      for i:=1 to x-1 do strn:=strn+s[i];
      y:=pos('>',s);
      for i:=x+1 to y-1 do strm:=strm+s[i];
      val(strm,m);
      for i:=y+1 to length(s) do strm1:=strm1+s[i];
      val(strm1,m1);
      w:=1;
      for i:=length(strn) downto 1 do
        begin
          n:=n+(ord(strn[i])-48)*w;
          w:=w*m;
        end;
      i:=0;
      repeat
        i:=i+1;
        z[i]:=n mod m1;
        n:=n div m1;
      until n=0;
      write(strn,'<',strm,'>=');
      for j:=i downto 1 do write(z[j]);
      writeln('<',m1,'>');
    end.
    

    Java :

    import java.math.BigDecimal;
    import java.util.Scanner;
    
    public class Main {
    	public static String str;
    	public static int n, m;
     
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		str = sc.next();
    		int ids = str.indexOf("<");
    		String tmp = str.substring(ids);
    		char[] tm = tmp.toCharArray();
    		String num = "";
     
    		for (int i = 0; i < tm.length; i++) {
    			if (tm[i] >= '0' && tm[i] <= '9') {
    				num += tm[i];
    			}
    			if (tm[i] == '>') {
    				n = Integer.parseInt(num);
    				num = "";
    			}
    		}
    		m = Integer.parseInt(num);
    		// System.out.println(n + " " + m);
    		// 左边的数字
    		str = str.substring(0, ids);
    		BigDecimal result = new BigDecimal("0");
    		// n进制底数
    		BigDecimal ndis = new BigDecimal("" + n);
    		// 假如进制不是10进制,则转化为十进制
    		if (n != 10) {
    			char[] nums = str.toCharArray();
    			for (int i = nums.length - 1; i >= 0; i--) {
    				result = result.add(new BigDecimal(nums[i] + "").multiply(ndis
    						.pow(nums.length - 1 - i)));
    				// System.out.println(result + " ");
    				// result += Math.pow(n, nums.length - 1 - i);
    			}
    		} else { // 进制是10进制
    			result = new BigDecimal(str);
    		}
    		// 转化为m进制
    		if (n == m) {
    			System.out
    					.println(result + "<" + n + ">=" + result + "<" + m + ">");
    		} else {
    			long yus;
    			String mizji = "";
    			// m进制底数
    			BigDecimal mdis = new BigDecimal("" + m);
    			while (result.compareTo(new BigDecimal("0")) > 0) {
    				mizji = result.remainder(mdis) + mizji;
    				result = result.divide(mdis, BigDecimal.ROUND_DOWN);
    			}
    			System.out.println(str + "<" + n + ">=" + mizji + "<" + m + ">");
    		}
     
    	}
    }
    
    
    • 1

    信息

    ID
    183
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者