Numeral System

From Progteam

Revision as of 02:45, 23 June 2008 by Kerry (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Checkmark.jpg This problem has been solved by mlc413.
Checkmark.jpg This problem has been solved by kerry.


Numeral System
Problem Number 2685
Sorter: mlc413
Source: Unknown
Link: http://acm.pku.edu.cn/JudgeOnline/problem?id=2685



Numeral System is problem number 2685 on the Peking University ACM site.


import java.util.*;

public class Main{

    public static Scanner in;
 
    public static void main(String[] args){
        in=new Scanner(System.in);

        doStuff();
    }

    public static void doStuff(){
        int N=in.nextInt();

        for(int i=0;i<N;i++){
	    String s1 = in.next();
	    String s2 = in.next();
            solve(s1, s2);
        }
    }

    public static void solve(String s1, String s2){
		System.out.println( getMCXI(getDig(s1)+getDig(s2)));
    
	//	System.out.println( getMCXI(1204));
    
    }
    
    public static int getDig(String num){
	int ans = 0;
	int len = num.length();
	int pos = 0;
	while(pos < len){
	    char ch = num.charAt(pos);
	    if(ch >= '0' && ch <='9'){
		pos++;
		ans += (int)(ch - '0')*getVal(num.charAt(pos));
		pos++;
	    }
	    else{
		pos++;
		ans += getVal(ch);
	    }
	}
	
	return ans;
	
    }

    public static int getVal(char ch){
	switch(ch){
	case 'm': return 1000;
	case 'c' : return 100;
	case 'x' : return 10;
	case 'i': return 1;
	    
	}
	return 0;
    }
    
        public static char getChar(int n){
	switch(n){
	case 1000: return 'm';
	case 100: return 'c';
	case 10: return 'x';
	case 1: return 'i';
	    
	}
	
	return 'Y';
    }

    
    public static String getMCXI(int num){
	String ans = "";
	int pos = 1000;
	
	while(num!=0){
	    int n = num/pos;
	    if(n>1){
		ans = ans + (n) ;
		//	System.out.println((char)n);
		ans = ans  + getChar(pos);
	    }
	    else if(n==1){
		ans = ans + getChar(pos);
	    }
	    num = num%pos;
	    pos = pos/10;
	}
	
	
	return ans;
    
    }
    
}


Kerry's C answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



int main() {
int i, j, total, num, tmp, answer;
char a,last;
        scanf("%d", &total);
        getchar();
        for(i=0;i<total;i++) {
                num=0;
                tmp=1;
                while ((a=getchar())!=' ') {
                        if((a>'A') && (last> 'A'))
                                tmp=1;
                        if (a<'A') {
                                tmp=(a-'0');
                        }
                        else {
                        switch (a) {
                                case 'm':
                                        num+=(1000*tmp);
                                        break;
                                case 'c' :
                                        num+=(100*tmp);
                                        break;
                                case 'x' :
                                        num+=(10*tmp);
                                        break;
                                case 'i' :
                                        num+=(tmp*1);
                                        break;
                        }
                        }
                        last=a;
                }
                tmp=1;
                while ((a=getchar())!='\n') {
                        if((a>'A') && (last> 'A'))
                                tmp=1;
                        if (a<'A') {
                                tmp=(a-'0');
                        }
                        else {
                        switch (a) {
                                case 'm':
                                        num+=(1000*tmp);
                                        break;
                                case 'c' :
                                        num+=(100*tmp);
                                        break;
                                case 'x' :
                                        num+=(10*tmp);
                                        break;
                                case 'i' :
                                        num+=(tmp*1);
                                        break;
                        }
                        }
                        last=a;
                }

                if (num>999) {
                        if (num>1999) {
                                printf("%dm", num/1000);
                        }
                        else printf("m");
                        num = num%1000;
                }
                if (num>99) {
                        if (num>199)
                                printf("%dc", num/100);
                        else printf("c");
                        num = num %100;
                }
                if (num>9) {
                        if (num>19)
                                printf("%dx", num/10);
                        else printf("x");
                        num = num %10;
                }
                if (num>0) {
                        if (num>1)
                                printf("%di", num);
                        else printf("i");
                }
                printf("\n");
        }

}

Personal tools