蓝桥杯2020年真题:字符串编码

题目

时间限制: 1.0s 内存限制: 512.0MB 本题总分:20 分
【问题描述】
小明发明了一种给由全大写字母组成的字符串编码的方法。
对于每一个大写字母,小明将它转换成它在 26 个英文字母中序号,
即 A→1, B→2, ... Z→ 26。 这样一个字符串就能被转化成一个数字序列: 
比如 ABCXYZ → 123242526。 现在给定一个转换后的数字序列,
小明想还原出原本的字符串。当然这样 的还原有可能存在多个符合条件的字符串。
小明希望找出其中字典序最大的字符串。
【输入格式】
一个数字序列。
【输出格式】
一个只包含大写字母的字符串,代表答案
【样例输入】 
123242526
【样例输出】 
LCXYZ
【评测用例规模与约定】 
对于 20% 的评测用例,输入的长度不超过 20。 
对于所有评测用例,输入的长度不超过 200000

答案

package competition4;

import java.util.Scanner;

public class StringCode
{
    
    
	public static void main(String[] args)
	{
    
    
		//System.out.println((int)'A');//65
		Scanner in = new Scanner(System.in);
		String str=in.nextLine();
		String result="";
		String temp="";
		for(int x=0;x<str.length();)
		{
    
    
			if(str.charAt(x)=='1'&&x+1<str.length())
			{
    
    
				temp +=str.charAt(x);
				x++;
				temp +=str.charAt(x);
				result +=(char)(Integer.valueOf(temp)+65-1);
				temp="";
				x++;
			}
			else if(str.charAt(x)=='2'&&x+1<str.length())
			{
    
    
				if(str.charAt(x+1)>='0'&&str.charAt(x+1)<='6')
				{
    
    
					temp +=str.charAt(x);
					x++;
					temp +=str.charAt(x);
					result +=(char)(Integer.valueOf(temp)+65-1);
					temp="";
					x++;
				}
			}
			else if(x<str.length())
			{
    
    
				temp +=str.charAt(x);
				result +=(char)(Integer.valueOf(temp)+65-1);
				temp="";
				x++;
			}
		}
		System.out.println(result);
		in.close();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_43416157/article/details/109006207