CIVIC DILL MIX

本文介绍了一个程序,该程序能够将罗马数字转换为阿拉伯数字进行求和运算,并将结果再次转换回罗马数字格式输出。主要涉及罗马数字的基本规则、转换算法及其实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CIVIC DILL MIX

Time Limit: 1000MS Memory limit: 65536K

题目描述

Roman numerals are an ancient numbering system used extensively throughout Europe through the 13th century (where it was eventually replaced by our current positional system). Vestiges of this system still exist today on clock faces, building cornerstones, Super Bowls and Star Wars episodes. The system uses the following 7 symbols:

Symbols I, X, C and M can be repeated as needed (though never more than three times for I, X and C), so that 3 is represented as III, 27 as XXVII and 4865 as MMMMDCCCLXV. The symbols are always written from the highest value to the lowest, but for one exception: if a lower symbol precedes a higher one, it is subtracted from the higher. Thus 4 is written not as IIII but as IV, and 900 is written as CM.
The rules for this subtractive behavior are the following:
1. Only I, X and C can be subtracted.
2. These numbers can only appear once in their subtractive versions (e.g., you can’t write 8 as IIX).
3. Each can only come before symbols that are no larger than 10 times their value. Thus we can not write IC for 99 or XD for 490 (these would be XCIX and CDXC, respectively). Note that the first two words in this problem title are invalid Roman numerals, but the third is fine.
Your task for this problem is simple: read in a set of Roman numeral values and output their sum as a Roman numeral.

输入

Input will consist of multiple test cases. Each test case starts with a positive integer n indicating the number of values to add. After this will come n values (potentially several on a line), all valid Roman numerals with whitespace only coming between values. A value of n = 0 will indicate end of input. All sums will be less than 5000.

输出

For each test case, output the case number and the sum, both as Roman numerals, using the format shown below. Case numbers should start at I.

示例输入

2
XII MDL
4
I I I
I
0

示例输出

Case I: MDLXII
Case II: IV
这个题大体思路先换成数字再转换
#include<stdio.h>
#include<string.h>
void f1(int s);
int f(char a)
{
	if(a=='I') return 1;
	if(a=='V')  return 5;
	if(a=='X')  return 10;
	if(a=='L') return 50;
	if(a=='C')  return 100;
	if(a=='D')  return 500;
    if(a=='M') return 1000;
}
void f1(int s)
{
	int s1,s2,s3,s4,s21,s31,s41,j;
	 s1=s/1000;
		for(j=1;j<=s1;j++) printf("M");
		s2=(s-s1*1000)/100;
		if(s2==4) printf("CD");
		else if(s2==9) printf("CM");
		else if(s2==5) printf("D");
		else  if(s2>5&&s2<9) {printf("D");s21=s2-5;s21=s2-5;
		                      for(j=1;j<=s21;j++) printf("C");}
		else    for(j=1;j<=s2;j++) printf("C");
		s3=(s-s1*1000-s2*100)/10;
			if(s3==4) printf("XL");
		else if(s3==9) printf("XC");
		else if(s3==5) printf("L");
		else  if(s3>5&&s3<9) {printf("L");s31=s3-5;s31=s3-5;
		                      for(j=1;j<=s31;j++) printf("X");}
		else    for(j=1;j<=s3;j++) printf("X");
		s4=s%10;
			if(s4==4) printf("IV");
		else if(s4==9) printf("IX");
		else if(s4==5) printf("V");
		else  if(s4>5&&s4<9) {printf("V");s41=s4-5;s41=s4-5;
		                      for(j=1;j<=s41;j++) printf("I");}
		else    for(j=1;j<=s4;j++) printf("I");
}
int main()
{
	int n,i,j,k1,s,num,s1,s2,s3,s4,s5,s21,s31,s41,count=0;
	char st1[1000];
	while(scanf("%d",&n)!=EOF&&n!=0)
	{
		count++;
		s=0;
		for(i=0;i<=n-1;i++)
		{
		    scanf("%s",st1);
	     	k1=strlen(st1);
	     	for(j=0;j<=k1-1;j++)
			{      
		    	
			       if(st1[j]=='I'&&(st1[j+1]=='V'||st1[j+1]=='X'))
				   {num=f(st1[j+1])-f(st1[j]);j++;}
				   else if(st1[j]=='X'&&(st1[j+1]=='L'||st1[j+1]=='C'))
	               {num=f(st1[j+1])-f(st1[j]);j++;}
				   else if(st1[j]=='C'&&(st1[j+1]=='D'||st1[j+1]=='M'))
	               {num=f(st1[j+1])-f(st1[j]);j++;}
				   else
				    num=f(st1[j]); 
					s+=num;
			}
		  
		}
		printf("Case ");
		f1(count);
		printf(": ");
        f1(s);
    
            printf("\n");
	}
	return 0;
}

资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 在 Linux 系统中,查找域名或主机名对应的 IP 地址是网络管理中的一项基础且关键任务,对于排查网络故障、调试网络问题以及监控网络服务是否正常运行等场景都非常重要。本文将介绍五种在 Linux 终端查询域名 IP 地址的方法。 首先,dig 命令(全称 Domain Information Groper)是一个功能强大的 DNS 查询工具,能够向 DNS 服务器发送查询请求并获取详细的响应信息。如果需要查询单个域名的 IP 地址,可以使用命令 dig 2daygeek.com +short 。此外,还可以通过编写 bash 脚本,将包含域名的文本文件中的域名逐个读取,然后利用 dig 命令进行查询,从而实现批量查询域名 IP 地址的功能。 其次,host 命令是一个简单易用的 DNS 查询工具,主要用于将域名解析为 IP 地址。要获取某个域名的 IP 地址,直接使用 host 2daygeek.com 即可。如果只想显示 IP 地址部分,可以通过管道结合 grep 和 sed 命令来实现,例如:host 2daygeek.com | grep "has address" | sed s/has address/-/g 。 再者,nslookup 命令也是一种常用的 DNS 查询工具,它支持交互式查询 DNS 信息。通过 nslookup 2daygeek.com 可以查询域名的 IP 地址。若要以非交互式的方式只显示 IP 地址,可以使用命令 nslookup 2daygeek.com | awk /^Address:/ {print $2} 。 另外,fping 命令与传统的 ping 命令不同,它不会直接进行 DNS 查询,而是通过发送 ICMP Echo Request(pi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值