天天看点

蓝桥杯算法训练(java)--Anagrams问题&&前缀表达式&&大小写转换

Anagrams问题

Anagrams指的是具有如下特性的两个单词:在这两个单词当中,每一个英文字母(不区分大小写)所出现的次数都是相同的。例如,“Unclear”和“Nuclear”、“Rimon”和“MinOR”都是Anagrams。编写一个程序,输入两个单词,然后判断一下,这两个单词是否是Anagrams。每一个单词的长度不会超过80个字符,而且是大小写无关的。

输入格式:输入有两行,分别为两个单词。

输出格式:输出只有一个字母Y或N,分别表示Yes和No

思路:可以利用

map

存储每个字符和其出现的次数。然后比较两个

map

的内容。

细节注意:大小写是无关的,所以我们可以先将字符串转化成小写的。

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scn=new Scanner(System.in);
		String s1=scn.next();
		String s2=scn.next();
		if(s1.length()==s1.length())
		{
			Map<Character,Integer> map1=new HashMap<Character,Integer>();
			Map<Character,Integer> map2=new HashMap<Character,Integer>();
			s1=s1.toLowerCase();
			s2=s2.toLowerCase();
			for (int i = 0; i < s1.length(); i++) {
				if(map1.containsKey(s1.charAt(i)))
				{
					Integer in=map1.get(s1.charAt(i));
					in=in+1;
				}
				else {
					map1.put(s1.charAt(i), 1);
				}
				if(map2.containsKey(s2.charAt(i)))
				{
					Integer inn=map2.get(s2.charAt(i));
					inn=inn+1;
				}
				else {
					map2.put(s2.charAt(i), 1);
				}		
			}
			if(map1.equals(map2))
				System.out.println("Y");
			else
				System.out.println("N");
		}
		else {
			System.out.println("N");
		}
		
	}

}
           

前缀表达式

编写一个程序,以字符串方式输入一个前缀表达式,然后计算它的值。

输入格式为:“运算符 对象1 对象2”。

运算符为“+”(加法)、“-”(减法)、“*”(乘法)或“/”(除法),运算对象为不超过10的整数,它们之间用一个空格隔开。

要求:对于加、减、乘、除这四种运算,分别设计相应的函数来实现。

输入格式:输入只有一行,即一个前缀表达式字符串。

输出格式:输出相应的计算结果(如果是除法,直接采用c语言的“/”运算符,结果为整数)。

思路: 这道题不难,主要记住如何在字符串中获取数字。

可以利用Character.isDigit()判断是不是数字。

另外,由于它的输入是有空格隔开的,所以获取字符串时不能使用

scn.next()

,而是要使用

scn.nextLine()

.

import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		String s = scn.nextLine();//会在输入enter的时候停止。而scn.next()会在输入空格的时候停止。
		int num1 = Integer.parseInt(s.substring(2, 3));
		int num2 = 0;
		for (int i = 3; i < s.length(); i++) {
			while (Character.isDigit(s.charAt(i))) {
				num1 = num1 * 10 + Integer.parseInt(s.substring(i, i + 1));
				i++;
			}
			i++;
			num2 = Integer.parseInt(s.substring(i, i + 1));
			i++;
			while (i < s.length() && Character.isDigit(s.charAt(i))) {
				num2 = num2 * 10 + Integer.parseInt(s.substring(i, i + 1));
				i++;
			}

		}
		if (s.charAt(0) == '+')
			System.out.println(num1 + num2);
		else if (s.charAt(0) == '-')
			System.out.println(num1 - num2);
		else if (s.charAt(0) == '*')
			System.out.println(num1 * num2);
		else if (s.charAt(0) == '/')
			System.out.println(num1 / num2);
	}
}
           

另外一种方法:在比赛中观察输入和输出格式的特点很重要!

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner scn=new Scanner(System.in);
		char c=scn.next().charAt(0);
		int a,b,sum;
		a=scn.nextInt();
		b=scn.nextInt();
		if(c=='+')
			System.out.println(a+b);
		else if(c=='-')
			System.out.println(a-b);
		else if(c=='*')
			System.out.println(a*b);
		else if(c=='/')
			System.out.println(a/b);
	}
           

大小写转换

编写一个程序,输入一个字符串(长度不超过20),然后把这个字符串内的每一个字符进行大小写变换,即将大写字母变成小写,小写字母变成大写,然后把这个新的字符串输出。

  输入格式:输入一个字符串,而且这个字符串当中只包含英文字母,不包含其他类型的字符,也没有空格。

  输出格式:输出经过转换后的字符串。

思路:

蓝桥杯算法训练(java)--Anagrams问题&amp;&amp;前缀表达式&amp;&amp;大小写转换

用字符串S1用于存储原字符串S大写的结果。

用字符串S2用于存储原字符串S小写的结果。

用字符串res用于存储结果。

遍历s中每个字符,如果s中字符和s1中的字符相同,则说明s中的字符是大写的,那么我们把它改成小写的,即把s2中该处的字符添加到res中。

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scn=new Scanner(System.in);
		String s=scn.next();
		String s1=s.toUpperCase();
		String s2=s.toLowerCase();
		String res="";
		for (int i = 0; i < s.length(); i++) {
			if(s.substring(i, i+1).equals(s1.substring(i, i+1)))
				res+=s2.substring(i, i+1);
			else
				res+=s1.substring(i, i+1);
		}
		System.out.println(res);
	}
}