文章目录
-
- 2352、Verdis Quo[将罗马数字转化为十进制数字]
- 2549、让你算出小数点后第n位是什么
- 2564、词组缩写
- 2565、放大的X[画图]
- 2567、将第二个字符串插入第一个字符串中央
- 2572、查找包含子串的字符串
- 2609、How many[计数多少相同的字符串]
- 2607、Let the Balloon Rise II[异或运算]
- 2707、Steganography[字符串解密]
- 2708、Vertical Histogram[直方图]
- 2719、The Seven Percent Solution[字符串]
- 2721、Persistent Bits[字符串]
- 2723、Electronic Document Security[字符串处理]
2352、Verdis Quo[将罗马数字转化为十进制数字]
The Romans used letters from their Latin alphabet to represent each of the seven numerals in their number system. The list below shows which
letters they used and what numeric value each of those letters represents:
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000
Using these seven numerals, any desired number can be formed by following the two basic additive and subtractive rules. To form a number using
the additive rule the Roman numerals are simply written from left to right in descending order, and the value of each roman numeral is added
together. For example, the number MMCLVII has the value 1000 + 1000 + 100 + 50 + 5 + 1 + 1 = 2157. Using the addition rule alone could lead to
very long strings of letters, so the subtraction rule was invented as a result. Using this rule, a smaller Roman numeral to the left of a larger one is
subtracted from the total. In other words, the number MCMXIV is interpreted as 1000 - 100 + 1000 + 10 - 1 + 5 = 1914.
Over time the Roman number writing system became more standardized and several additional rules were developed. The additional rules used today
are:
- The I, X, or C Roman numerals may only be repeated up to three times in succession. In other words, the number 4 must be represented as IV
and not as IIII. - The V, L, or D numerals may never be repeated in succession, and the M numeral may be repeated as many 2. times as necessary.
- Only one smaller numeral can be placed to the left of another. For example, the number 18 is represented as XVIII but not as XIIX.
- Only the I, X, or C can be used as subtractive numerals.
- A subtractive I can only be used to the left of a V or X. Likewise a X can only appear to the left of a L or C, and a C can only be used to the
left of a D or M. For example, 49 must be written as XLIX and not as IL.
Your goal is to write a program which converts Roman numbers to base 10 integers.
Input
The input to this problem will consist of the following:
A line with a single integer “N” (1 ≤ N ≤ 1000), where N indicates how many Roman numbers are to be converted.
A series of N lines of input with each line containing one Roman number. Each Roman number will be in the range of 1 to 10,000 (inclusive)
and will obey all of the rules laid out in the problem’s introduction.
Output
For each of the N Roman numbers, print the equivalent base 10 integer, one per line.
Sample Input
3
IX
MMDCII
DXII
Sample Output
9
2602
512
Code:
/*将罗马数字转化为十进制数
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000
注意当输入的罗马数字,左边的数字比右边小时为负数
*/
#include<iostream>
#include<cstring>
using namespace std;
char c[10000];
int num[10000];
int main(){
int t;
cin>>t;
while(t--){
cin>>c;
int len=strlen(c);
int j=0;
//将输入的各个罗马数字存储在int数组
for(int i=0;i<len;i++){
switch(c[i]){
case 'I':
num[j++]=1;
break;
case 'V':
num[j++]=5;
break;
case 'X':
num[j++]=10;
break;
case 'L':
num[j++]=50;
break;
case 'C':
num[j++]=100;
break;
case 'D':
num[j++]=500;
break;
case 'M':
num[j++]=1000;
break;
}
}
//遍历num[]数组
int sum=0;
if(j==1){
cout<<num[0]<<endl;
continue;
}
for(int i=0;i<j-1;i++){
if(num[i]<num[i+1]){
sum+=(-1)*num[i];
}
if(num[i]>=num[i+1]){
sum+=num[i];
}
if(i+1==j-1){
sum+=num[i+1];
}
}
cout<<sum<<endl;
}
return 0;
}
2549、让你算出小数点后第n位是什么
问题是这样的:给你一个小数x,让你算出小数点后第n位是什么,(1 <= n <= 6)
Input
首先输入一个t,表示有t组数据,跟着t行:
每行输入一个小数(输入数据保证一定是a.b的形式,为了简单化问题,没有循环小数的情况)
然后跟一个n,表示小数点后第几位
Output
输出一个数表示小数点后第n位的数
Sample Input
3
1.234 1
2.345 2
3.456 3
Sample Output
2
4
6
Code
//输出小数点后的第n位数字
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
char a[100];
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>a>>n;
string s="";
s=strstr(a,".");//返回a字符串中.首次出现的地址,此处返回.(包含)之后的字符串
int len=s.size();
if(len>=7)
cout<<s[n]<<endl;
//小数位数不足6位以0补足
else if(n<=len-1){
cout<<s[n]<<endl;
}
else{
cout<<0<<endl;
}
}
return 0;
}
2564、词组缩写
定义:一个词组中每个单词的首字母的大写组合称为该词组的缩写。
比如,C语言里常用的EOF就是end of file的缩写。
Input
输入的第一行是一个整数T,表示一共有T组测试数据;
接下来有T行,每组测试数据占一行,每行有一个词组,每个词组由一个或多个单词组成;每组的单词个数不超过10个,每个单词有一个或多个大写或小写字母组成;
单词长度不超过10,由一个或多个空格分隔这些单词。
Output
请为每组测试数据输出规定的缩写,每组输出占一行。
Sample Input
1
end of file
Sample Output
EOF
Code
//词组缩写-一个词组中每个单词的首字母的大写组合称为该词组的缩写
#include<iostream>
#include<string>
#include<string.h>
#include<cstring>
using namespace std;
char b[100];
int main(){
int t;
cin>>t;
getchar();//吸收回车符
while(t--){
string a;
getline(cin,a);
int len=a.size();
bool flag=true;
//将缩写后的字符存在b[]中
int num=0;
for(int i=0,j=0;i<len;i++){
if(a[i]==' '){
flag=true;
continue;
}
else if(flag==true){
num++;
if(a[i]>='a'&&a[i]<='z')
b[j]=a[i]-32;
else
b[j]=a[i];
j++;
flag=false;
}
}
for(int i=0;i<num;i++){
cout<<b[i];
}
cout<<endl;
}
return 0;
}
2565、放大的X[画图]
请你编程画一个放大的’X’。
如3*3的’X’应如下所示:
X X
X
X X
5*5的’X’如下所示:
X X
X X
X
X X
X X
Input
输入数据第一行是一个整数T,表示有T组测试数据;
接下来有T行,每行有一个正奇数n(3 <= n <= 79),表示放大的规格。
Output
对于每一个n打印一个规格为n * n放大的’X’;每组输出后面空一行。
Sample Input
2
3
5
Sample Output
X X
X
X X
X X
X X
X
X X
X X
Code
//输出大大的X
#include<iostream>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,num=0;
cin>>n;
for(int i=0;i<n;i++){
num=0;
for(int j=0;j<n;j++){
if(i==j||(i+j)==n-1){
cout<<"X";
num++;
//控制每行最后一个X后无空格
if(i!=n/2&&num==2){
break;
}
else if(i==n/2&&num==1){
break;
}
}
else{
cout<<" ";
}
}
cout<<endl;
}
cout<<endl;
}
return 0;
}
2567、将第二个字符串插入第一个字符串中央
有2行字符串,其中第一个串的长度为偶数,现在要求把第2个串插入到第一个串的正中央,如此便能开启墓碑进入墓中。
Input
输入数据首先给出一个整数n,表示测试数据的组数。
然后是n组数据,每组数据2行,每行一个字符串,长度大于0,小于50,并且第一个串的长度必为偶数。
Output
请为每组数据输出一个能开启古墓的字符串,每组输出占一行。
Sample Input
2
HDCM
UA
Aw
CFlo
Sample Output
HDUACM
ACFlow
Code
方法一
//输入两个字符串,将第二个字符串插入第一个字符串正中央
#include<iostream>
#include<string>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
string s1,s2,str1="",str2="",s="";
cin>>s1>>s2;
int len=s1.size();
for(int i=0;i<len;i++){
if(i<len/2){
str1+=s1[i];
}
else{
str2+=s1[i];
}
}
s=str1+s2+str2;
cout<<s<<endl;
}
return 0;
}
方法二:利用sbustr()函数
//输入两个字符串,将第二个字符串插入第一个字符串正中央
#include<iostream>
#include<string>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
string s1,s2,s="";
cin>>s1>>s2;</