您的位置:首页 > 其它

杭电ACM HDOJ 1924 CIVIC DILL MIX

2013-02-07 21:56 260 查看
#include <cstdio>
#include <iostream>
using namespace std;
char word[20];
int getvalue(char word){
	switch(word){
	case 'I':return 1;
	case 'V':return 5;
	case 'X':return 10;
	case 'L':return 50;
	case 'C':return 100;
	case 'D':return 500;
	case 'M':return 1000;
	}
}
//符号转化为数字
int SymtoNum(char word[]){
	int i,sum;
	i=sum=0;
	while(word[i+1]){
		if(getvalue(word[i])<getvalue(word[i+1])){
			sum-=getvalue(word[i]);
		}
		else
			sum+=getvalue(word[i]);
		i++;
	}
	sum+=getvalue(word[i]);
	return sum;
}
//数字转化为符号
void NumtoSym(char word[],int num){
	int i=0;
	while(num){
		if(num>=1000){
			word[i++]='M';num-=1000;}
		else if(num>=900){
			word[i++]='C';num+=100;}
		else if(num>=500){
			word[i++]='D';num-=500;}
		else if(num>=400){
			word[i++]='C';num+=100;}
		else if(num>=100){
			word[i++]='C';num-=100;}
		else if(num>=90){
			word[i++]='X';num+=10;}
		else if(num>=50){
			word[i++]='L';num-=50;}
		else if(num>=40){
			word[i++]='X';num+=10;}
		else if(num>=10){
			word[i++]='X';num-=10;}
		else if(num>=9){
			word[i++]='I';num+=1;}
		else if(num>=5){
			word[i++]='V';num-=5;}
		else if(num>=4){
			word[i++]='I';num+=1;}
		else if(num>=0){
			word[i++]='I';num-=1;}
	}
	word[i]='\0';
}
int main(){
	int n;
	int i,ans;
	int count=0;
	while(cin>>n,n){
		ans=0;
		count++;
		for(i=0;i<n;i++){
			cin>>word;
			ans+=SymtoNum(word);
		}
		NumtoSym(word,count);
		printf("Case %s: ",word);
		NumtoSym(word,ans);
		printf("%s\n",word);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: