Topic 1118: The C++/java of the numeral conversion

Source: Internet
Author: User

Title Description:

The conversion of any two different binary nonnegative integers (2 binary ~16), the given integer within the range expressed by long can be obtained.
The representation symbol for the different binary is (0,1,...,9,a,b,...,f) or (0,1,...,9,a,b,...,f).

Input:

The input has only one row and contains three integer a,n,b. A indicates that the following n is a binary integer, and b indicates that a binary integer n is to be converted to a B-binary integer. A, B is a decimal integer, 2 =<, b <= 16.

The data may exist with leading zeros.

Output:

There may be multiple sets of test data, and for each set of data, the output contains a row with an integer that is the converted B-binary number. The letter symbols are all capitalized, i.e. (0,1,...,9,a,b,...,f) when output.

Sample input:
7 AAB3
Sample output:
210306
Tips:

You can use strings to represent different binary integers.



C + + code:

#include <stdio.h> #include <math.h> #include <string.h> #define MAX. int main () {int data[16]={0,1    , 2,3,4,5,6,7,8,9,10,11,12,13,14,15};    Char suju[16]={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};    int A, B;    Char n[100];    Char Stack[max];    int i,j;        while (scanf ("%d%s%d", &a,n,&b)!=eof) {int inda=0;        int len = strlen (n);        int top=-1;            for (i=0;i<len;i++) {switch (N[i]) {case ' 0 ': break;            Case ' 1 ': Inda + = (int) pow (a,len-i-1) *data[1];break;            Case ' 2 ': Inda + = (int) pow (a,len-i-1) *data[2];break;            Case ' 3 ': Inda + = (int) pow (a,len-i-1) *data[3];break;            Case ' 4 ': Inda + = (int) pow (a,len-i-1) *data[4];break;            Case ' 5 ': Inda + = (int) pow (a,len-i-1) *data[5];break;            Case ' 6 ': Inda + = (int) pow (a,len-i-1) *data[6];break;            Case ' 7 ': Inda + = (int) pow (a,len-i-1) *data[7];break; Case ' 8 ': Inda + = (int) POW(a,len-i-1) *data[8];break;            Case ' 9 ': Inda + = (int) pow (a,len-i-1) *data[9];break;            Case ' A ': Case ' a ': Inda + = (int) pow (a,len-i-1) *data[10];break;            Case ' B ': case ' B ': Inda + = (int) pow (a,len-i-1) *data[11];break;            Case ' C ': Case ' C ': Inda + = (int) pow (a,len-i-1) *data[12];break;            Case ' d ': Case ' d ': Inda + = (int) pow (a,len-i-1) *data[13];break;            Case ' E ': Case ' E ': Inda + = (int) pow (a,len-i-1) *data[14];break;            Case ' F ': Case ' F ': Inda + = (int) pow (a,len-i-1) *data[15];break;            Default:break;        }} if (inda==0) stack[++top]= ' 0 ';            while (Inda) {stack[++top]=suju[inda%b];        inda=inda/b;        } while (top>=0) printf ("%c", stack[top--]);    printf ("\ n"); } return 0;} /************************************************************** problem:1118 User:carvin LangUage:c++ result:accepted time:10 Ms memory:1108 kb************************************************************* ***/

Java code (RE, I don't know why!) Posted here, providing a way:)

Import Java.util.scanner;import java.math.*;p ublic class main{public static void Main (string[] args) {int radix        DATA[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};        Char radixarray[]={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};        Long A, B;        int i,j,k;        String str;        Scanner cin=new Scanner (system.in);            while (Cin.hasnext ()) {A=cin.nextlong ();            Str=cin.nextline ();            B=cin.nextlong ();            String result= "";            Char Strarray[]=str.tochararray ();            int len=strarray.length;            Long radix=0;                for (i=0;i<len;i++) {switch (Strarray[i]) {case ' 0 ': break;                    Case ' 1 ': radix+= (int) Math.pow (a,len-i-1) *radixdata[1];break;                tenradix+= (int) pow (a,x) *data[1];break;                Case ' 2 ': radix+= (int) Math.pow (a,len-i-1) *radixdata[2];break; CasE ' 3 ': radix+= (int) Math.pow (a,len-i-1) *radixdata[3];break;                Case ' 4 ': radix+= (int) Math.pow (a,len-i-1) *radixdata[4];break;                Case ' 5 ': radix+= (int) Math.pow (a,len-i-1) *radixdata[5];break;                Case ' 6 ': radix+= (int) Math.pow (a,len-i-1) *radixdata[6];break;                Case ' 7 ': radix+= (int) Math.pow (a,len-i-1) *radixdata[7];break;                Case ' 8 ': radix+= (int) Math.pow (a,len-i-1) *radixdata[8];break;                Case ' 9 ': radix+= (int) Math.pow (a,len-i-1) *radixdata[9];break;                Case ' A ': Case ' a ': radix+= (int) Math.pow (a,len-i-1) *radixdata[10];break;                Case ' B ': case ' B ': radix+= (int) Math.pow (a,len-i-1) *radixdata[11];break; Case ' C ': Case ' C ': radix+= (int) Math.pow (a,len-i-1) *rAdixdata[12];break;                Case ' d ': Case ' d ': radix+= (int) Math.pow (a,len-i-1) *radixdata[13];break;                Case ' E ': Case ' E ': radix+= (int) Math.pow (a,len-i-1) *radixdata[14];break;                Case ' F ': Case ' F ': radix+= (int) Math.pow (a,len-i-1) *radixdata[15];break;                Default:break;            }//switch}//for if (0==radix) System.out.print (0);                while (radix>0) {//system.out.print (radix%b);                result=radixarray[(int) (radix%b)]+result;            Radix/=b;            } System.out.println (Result);        System.out.println ();     }//while}//main}//main/************************************************************** problem:1118 User:carvin Language:java Result:runtime error****************************************************************/ 

Post a code of God (Java) on the Web:

Import static java.lang.System.out;    Import Java.io.BufferedInputStream;    Import Java.util.Scanner;    public class Main {            static Scanner in = new Scanner (new Bufferedinputstream (system.in));            private static string string;            private static int A, B;            public static void Main (String args[]) {                    while (In.hasnext ()) {                            a = In.nextint ();                            string = In.next ();                            b = In.nextint ();                            Out.println (Integer.tostring (integer.valueof (String, a), b)                                            . toUpperCase ());}}    /**************************************************************    problem:1118    User:carvin    Language:java    result:accepted    time:300 ms    memory:27516 kb**************************************** ************************/



Topic 1118: The C++/java of the numeral conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.