PAT_(string处理)A1082 Read Number in Chinese A1077 Kuchiguse

目录

1082 Read Number in Chinese (25分)

1077 Kuchiguse (20分)


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

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

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

yi Shi Wan ling ba Bai

Sample Output 2:

yi Shi Wan ling ba Bai
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
#include<unordered_map>
#include<map>
#include<stack>
#define pb push_back
using namespace std;

string num[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
string wei[5]={"Shi","Bai","Qian","Wan","Yi"};

int main(){
	//char str[20];
	string str;
	cin>>str;
	int len=str.length();
	int left=0;int right=len-1;
	if(str[0]=='-'){cout<<"Fu"; left++;}
	
	while(left+4<=right){
		right-=4;
	}//until 他们在同一节
	
	while(left<len){
		bool flag=false; //前面有没有积累的0 
		bool isPrint=false;//本节有没有输出过
		while(left<=right){ // [left,right]这一节
			 if(left>0 && str[left]=='0'){
			 	flag=true; // 有积累的0 
			 }else{
			 	
			 	if(flag==true){
			 		cout<<" ling"; flag=false;
			 	}
			 	//首位不是0
				 if(left>0) cout<<" ";
				 cout<<num[str[left]-'0'];
				 isPrint=true;
				 if(left!=right){
				 	cout<<" "<<wei[right-left-1];
				 }
			 }
			left++;
		} 
		if(isPrint=true && right!=len-1){
			cout<<" "<<wei[(len-1-right)/4+2]; //还剩(len-1-right)/4节 , +2 
		}
		right+=4;//输出下一节 	
	} 	
	return 0;
}
/**
123456789
yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
*/

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<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
#include<unordered_map>
#include<map>
#include<stack>
#define pb push_back
using namespace std;

vector<string> str;

int main(){
	int n;		
	cin>>n;getchar();
  int len=n;string tmp;	
	for(int i=0;i<n;i++){
		getline(cin,tmp);
		str.push_back(tmp);
	}
	//if(str[0][10-1]==str[1][20-1])cout<<1;
	
	int max_same=0x3f3f3f;
	int index=-1;
	for(int i=1;i<len;i++){
		int len1=str[i-1].length();
		int len2=str[i].length();
    if(len1==0||len2==0) break;
		int temp=0;
		for(int j=0; j<len1 && j<len2; j++){
			if(str[i-1][len1-1-j]==str[i][len2-1-j]){temp++;}
			else{
                if(max_same>temp){
                   max_same=temp;
				   index=i;
                }
				break;
			}	
		}
		if(max_same==0) break;	
	}

	if(max_same==0 || max_same==0x3f3f3f){
		cout<<"nai";
	}else{
		int l=str[index].length();
		for(int i=l-max_same;i<l;i++){
			cout<<str[index][i];
		}
	}
	
	return 0; 
}

// 20 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<map> 
#include<set>
#include<cmath>
#define pb push_back 
using namespace std;

vector<string> str;

int main(){
	int n; string s;
	cin>>n; getchar();
	for (int i = 0; i < n; ++i)
	{
		getline(cin,s); //s.reverse();
		reverse(s.begin(),s.end());
		//cout<<s<<endl;
		str.pb(s);
	}
	int maxlen=0x3f3f3f3f;
	int curlen;
	for (int i = 1; i < n; ++i){
		int len1=str[i-1].length();
		int len2=str[i].length();
		if(len1==0 || len2==0) break;

		curlen=0;
		for(int j=0;j<len1 && j<len2; j++){
			if(str[i-1][j]==str[i][j]) {
				curlen++; 
			}
			else break;
		}
		if(curlen<maxlen) maxlen=curlen;
		if (maxlen==0) break;
		
	}
	if(maxlen==0 || maxlen==0x3f3f3f3f){
		cout<<"nai";
	}else{
		for(int i=maxlen-1;i>=0;i--){
			cout<<str[0][i];
		}
	}
	return 0;
}

c++ string reverse

string str="12345";
reverse(str.begin(),str.end());
cout<<str;//输出str结果为"54321"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值