PTA-字符串处理(1061/1073/1001/1005/1035/1077/1082)

PTA-1061 Dating (20 分)

Sherlock Holmes received a note with some strange strings: Let’s date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm. It took him only a minute to figure out that those strange strings are actually referring to the coded time Thursday 14:04 – since the first common capital English letter (case sensitive) shared by the first two strings is the 4th capital letter D, representing the 4th day in a week; the second common character is the 5th capital letter E, representing the 14th hour (hence the hours from 0 to 23 in a day are represented by the numbers from 0 to 9 and the capital letters from A to N, respectively); and the English letter shared by the last two strings is s at the 4th position, representing the 4th minute. Now given two pairs of strings, you are supposed to help Sherlock decode the dating time.

Input Specification:

Each input file contains one test case. Each case gives 4 non-empty strings of no more than 60 characters without white space in 4 lines.

Output Specification:

For each test case, print the decoded time in one line, in the format DAY HH:MM, where DAY is a 3-character abbreviation for the days in a week – that is, MON for Monday, TUE for Tuesday, WED for Wednesday, THU for Thursday, FRI for Friday, SAT for Saturday, and SUN for Sunday. It is guaranteed that the result is unique for each case.

Sample Input:

3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm
Sample Output:
THU 14:04

解决代码

模拟,注意一些边界条件即可。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	char week[7][5]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
	string a,b,c,d;
	cin>>a>>b>>c>>d;
	vector<char> x;
	for(int i=0;i<min(a.size(),b.size());i++)
	{
		if(a[i]==b[i]&&a[i]>='A'&&a[i]<='G'&&x.empty())
		{
			x.push_back(a[i]);
			continue;
		}
		if(a[i]==b[i]&&!x.empty()&&((a[i]>='A'&&a[i]<='N')||(a[i]>='0'&&a[i]<='9')))
		{
			x.push_back(a[i]);
			break;
		}
	}
	printf("%s ",week[x[0]-65]);
	if(x[1]<='9') printf("0%d:",x[1]-48);
	else printf("%d:",x[1]-64+9);
	for(int i=0;i<min(c.size(),d.size());i++)
	{
		if(c[i]==d[i]&&((c[i]>=65&&c[i]<=90)||(c[i]>=97&&a[i]<=122)))
		{
			x.push_back(i);
			break;
		}
	}
	if(x[2]<10) printf("0%d",x[2]);
	else printf("%d",x[2]);
	cout<<endl;
	return 0;
}

PTA-1073 Scientific Notation (20 分)

Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [±][1-9].[0-9]+E[±][0-9]+ which means that the integer portion has exactly one digit, there is at least one digit in the fractional portion, and the number and its exponent’s signs are always provided even when they are positive.

Now given a real number A in scientific notation, you are supposed to print A in the conventional notation while keeping all the significant figures.

Input Specification:

Each input contains one test case. For each case, there is one line containing the real number A in scientific notation. The number is no more than 9999 bytes in length and the exponent’s absolute value is no more than 9999.

Output Specification:

For each test case, print in one line the input number A in the conventional notation, with all the significant figures kept, including trailing zeros.

Sample Input 1:

+1.23400E-03

Sample Output 1:

0.00123400

Sample Input 2:

-1.2E+10

Sample Output 2:

-12000000000

解决代码

模拟分情况讨论即可

#include<iostream>
using namespace std;
int main()
{
	string a,ans;
	long long int x;
	cin>>a;
	bool plus;
	for(int i=1;i<a.size();i++)
	{
		if(a[i]=='+'||a[i]=='-')
		{
			ans=a.substr(1,i-2);
			string str=a.substr(i+1,a.size()-1);
			x=stoi(str);
			if(a[i]=='+') plus=true;
			else plus=false;
			break;
		}
	}
	if(a[0]=='-') printf("-");
	if(x==0)
	{
		printf("%s",ans.c_str());
		return 0;
	}
	if(plus)
	{
		printf("%c",ans[0]);
		int num=ans.size()-2;
		if(x<num)
		{
			int i;
			for(i=2;i<2+x;i++)
			{
				printf("%c",ans[i]);
			}
			printf(".");
			for(i;i<ans.size();i++)
			{
				printf("%c",ans[i]);
			}
		}
		else
		{
			for(int i=2;i<ans.size();i++)
			{
				printf("%c",ans[i]);
			}
			for(int i=0;i<x-num;i++)
			{
				printf("0");
			}
		}
	}
	else
	{
		printf("0.");
		for(int i=0;i<x-1;i++)
		{
			printf("0");
		}
		printf("%c",ans[0]);
		for(int i=2;i<ans.size();i++)
		{
			printf("%c",ans[i]);
		}
	}
    cout<<endl;
	return 0;
}

PTA-1001 A+B Format (20 分)

Calculate a+b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −10^6≤a,b≤10 ^6. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input:

-1000000 9

Sample Output:

-999,991

解决代码

加法之和可以用long long存储,按照逗号出现的条件分情况处理

#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long int a,b;
	cin>>a>>b;
	long long int sum=a+b;
	string str=to_string(abs(sum));
	if(sum<0) printf("-");
	if(str.size()<=3)
	{
		printf("%s",str.c_str());
	}
	else if(str.size()<=6)
	{
		for(int i=0;i<str.size();i++)
		{
			printf("%c",str[i]);
			if(i==str.size()-1) break;
			if((str.size()-i-1)%3==0) printf(",");
		}
	}
	else
	{
		for(int i=0;i<str.size();i++)
		{
			printf("%c",str[i]);
			if(i==str.size()-1) break;
			if(i%3==0) printf(",");
		}
	}
    cout<<endl;
	return 0;
}

PTA-1005 Spell It Right (20 分)

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (≤10^100).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:

12345

Sample Output:

one five

解决代码

直接模拟即可

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string a;
	char num[10][8]={"zero","one","two","three","four","five","six","seven","eight","nine"};
	cin>>a;
	long long int sum=0;
	for(int i=0;i<a.size();i++)
	{
		sum+=a[i]-'0';		
	}
	string tot=to_string(sum);
	for(int i=0;i<tot.size();i++)
	{
		printf("%s",num[tot[i]-'0']);
		if(i!=tot.size()-1) printf(" ");
	}
    cout<<endl;
	return 0;
}

PTA-1035 Password (20 分)

To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) from O (o in uppercase). One solution is to replace 1 (one) by @, 0 (zero) by %, l by L, and O by o. Now it is your job to write a program to check the accounts generated by the judge, and to help the juge modify the confusing passwords.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N (≤1000), followed by N lines of accounts. Each account consists of a user name and a password, both are strings of no more than 10 characters with no space.

Output Specification:

For each test case, first print the number M of accounts that have been modified, then print in the following M lines the modified accounts info, that is, the user names and the corresponding modified passwords. The accounts must be printed in the same order as they are read in. If no account is modified, print in one line There are N accounts and no account is modified where N is the total number of accounts. However, if N is one, you must print There is 1 account and no account is modified instead.

Sample Input 1:

3
Team000002 Rlsp0dfa
Team000003 perfectpwd
Team000001 R1spOdfa

Sample Output 1:

2
Team000002 RLsp%dfa
Team000001 R@spodfa

Sample Input 2:

1
team110 abcdefg332

Sample Output 2:

There is 1 account and no account is modified

Sample Input 3:

2
team110 abcdefg222
team220 abcdefg333

Sample Output 3:

There are 2 accounts and no account is modified

解决方案

#include<bits/stdc++.h>
using namespace std;
struct N
{
	string name;
	string pass;
	bool change;
};
N ans[1005];
int main()
{
	int n;
	cin>>n;
	int num=0;
	for(int i=0;i<n;i++)
	{
		cin>>ans[i].name>>ans[i].pass;
		ans[i].change=false;
		for(int j=0;j<ans[i].pass.size();j++)
		{
			char q=ans[i].pass[j];
			if(ans[i].pass[j]=='1') ans[i].pass[j]='@';
			if(ans[i].pass[j]=='0') ans[i].pass[j]='%';
			if(ans[i].pass[j]=='l') ans[i].pass[j]='L';
			if(ans[i].pass[j]=='O') ans[i].pass[j]='o';
			if(q!=ans[i].pass[j])
			{
				ans[i].change=true;	
			} 
		}
		if(ans[i].change) num++;
	}
	if(!num)
	{
		if(n==1) printf("There is 1 account and no account is modified\n");
		else printf("There are %d accounts and no account is modified\n",n);
		return 0;
	}
	printf("%d\n",num);
	for(int i=0;i<n;i++)
	{
		if(ans[i].change)
		{
			printf("%s %s\n",ans[i].name.c_str(),ans[i].pass.c_str());
		}
	}
	return 0;
}

PTA-1077 Kuchiguse (20 分)

The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker’s personality. Such a preference is called “Kuchiguse” and is often exaggerated artistically in Anime and Manga. For example, the artificial sentence ending particle “nyan~” is often used as a stereotype for characters with a cat-like personality:

Itai nyan~ (It hurts, nyan~)

Ninjin wa iyada nyan~ (I hate carrots, nyan~)

Now given a few lines spoken by the same character, can you find her Kuchiguse?

Input Specification:
Each input file contains one test case. For each case, the first line is an integer N (2≤N≤100). Following are N file lines of 0~256 (inclusive) characters in length, each representing a character’s spoken line. The spoken lines are case sensitive.

Output Specification:
For each test case, print in one line the kuchiguse of the character, i.e., the longest common suffix of all N lines. If there is no such suffix, write nai.

Sample Input 1:

3
Itai nyan~
Ninjin wa iyadanyan~
uhhh nyan~

Sample Output 1:

nyan~

Sample Input 2:

3
Itai!
Ninjinnwaiyada T_T
T_T

Sample Output 2:

nai

解决代码

将求后缀和转化为求前缀和可大大降低问题的复杂程度,转化之后每次从第一个字符串从左至右取出字符与其他串相应位置进行比对,若遇到失配则退出输出已经匹配的子串即可。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	getchar();
	string ans[105];
	int mini=555; 
	for(int i=0;i<n;i++)
	{
		getline(cin,ans[i]);
		reverse(ans[i].begin(),ans[i].end());
		int l=ans[i].size();
		mini=min(mini,l);
	}
	int len=0;
	for(int i=0;i<mini;i++)
	{
		char ch=ans[0][i];
		bool same=true;
		for(int j=1;j<n;j++)
		{
			if(ch!=ans[j][i])
			{
				same=false;
				break;
			}
		}
		if(same) len=i+1;
		else break;
	}
	if(!len) printf("nai\n");
	else
	{
		string b=ans[0];
		string a=b.substr(0,len);
		reverse(a.begin(),a.end());
		printf("%s\n",a.c_str());
	}
	return 0;
}

PTA-1082 Read Number in Chinese (25 分)

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fu first if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu. Note: zero (ling) must be handled correctly according to the Chinese tradition. For example, 100800 is yi Shi Wan ling ba Bai.

Input Specification:

Each input file contains one test case, which gives an integer with no more than 9 digits.

Output Specification:

For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.

Sample Input 1:

-123456789

Sample Output 1:

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

Sample Input 2:

100800

Sample Output 2:

yi Shi Wan ling ba Bai

解决代码

本题是字符串处理中最复杂的一道题,直接模拟即可,但模拟中的细节较多。
注意输出“ling”的条件,除了“Shi”之外的进位之后都有可能输出“ling”,且需要在每段(相对应4位)之间单独判断:
80080008是“ba Qian ling ba Wan ling ba”,
80000008是“ba Qian Wan ling ba”,
80000000是“ba Qian Wan”.
注意边界条件“0”时单独输出“ling”。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	char ans[10][10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
	string str;
	cin>>str;
	long long int tot=abs(stoi(str));
	if(!tot)
	{
		printf("ling");
		return 0;
	} 
	if(str[0]=='-')
	{
		printf("Fu ");
		str=str.substr(1,str.size()-1);
	}
	int len=str.size();
	int cnt=0;
	bool pan=false;
	bool is=false;
	for(int i=0;i<str.size();i++)
	{
		cnt++;
		if(str[i]-'0'==0)
		{
			if(pan)
			{
				bool bo=false;
				int right=len-1;
				if(i+4<=right) right-=4;
				for(int j=i;j<=right;j++)
				{
					if(str[j]!='0')	bo=true;
				}
				if(bo)
				{
					printf(" ling");
					pan=false;
				}
			}
		}
		else
		{
			if(i!=0) printf(" ");
			printf("%s",ans[str[i]-'0']);
			is=true;
		} 
		if((len-cnt)%4==3&&str[i]!='0')
		{
			printf(" Qian");
			pan=true;
		} 
		else if((len-cnt)%4==2&&str[i]!='0')
		{
			printf(" Bai");
			pan=true;
		} 
		else if((len-cnt)%4==1&&str[i]!='0')
		{
			printf(" Shi");
		} 
		if(len-cnt==8)
		{
			printf(" Yi");
			pan=true;
			is=false;
		} 
		if(len-cnt==4&&is)
		{
			printf(" Wan");
			pan=true;
			is=false;
		} 
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新西兰做的饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值