JAVA 练习题
Description
判断一个由a~z这26个字符组成的字符串中哪个字符出现的次数最多。
Input
第一行是测试数据的组数n,每组测试数据占1行,是一个由a~z这26个字符组成的字符串,每组测试数据之间有一个空行,每行数据不超过1000个字符且非空
Output
n行,每行输出对应一个输入,包括出现次数最多的字符和该字符出现的次数,中间是一个空格。如果有多个字符出现的次数相同且最多,那么输出ASCII码最小的那个字符。
Sample Input
2
abbccc
adfadffasdf
Sample Output
c 3
f 4
Source
1047
Description
在我们现在使用的日历中, 闰年被定义为能被4整除的年份,但是能被100整除而不能被400整除的年是例外,它们不是闰年。例如:1700, 1800, 1900 和 2100 不是闰年,而 1600, 2000 和 2400是闰年。 给定从公元2000年1月1日开始逝去的天数,你的任务是给出这一天是哪年哪月哪日星期几。
Input
输入包含若干行,每行包含一个正整数,表示从2000年1月1日开始逝去的天数。输入最后一行是?1, 不必处理。可以假设结果的年份不会超过9999。
Output
对每个测试样例,输出一行,该行包含对应的日期和星期几。格式为“YYYY-MM-DD DayOfWeek”, 其中 “DayOfWeek” 必须是下面中的一个: "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" 或 "Saturday“。
Sample Input
1730
1740
1750
1751
-1
Sample Output
2004-09-26 Sunday
2004-10-06 Wednesday
2004-10-16 Saturday
2004-10-17 Sunday
Source
1048
Description
Julius Caesar 生活在充满危险和阴谋的年代。为了生存,他首次发明了密码,用于军队的消息传递。假设你是Caesar 军团中的一名军官,需要把Caesar 发送的消息破译出来、并提供给你的将军。消息加密的办法是:对消息原文中的每个字母,分别用该字母之后的第5个字母替换(例如:消息原文中的每个字母A都分别替换成字母F),其他字符不 变,并且消息原文的所有字母都是大写的。
密码字母:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
原文字母:V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
Input
最多不超过100个数据集组成。每个数据集由3部分组成:
起始行:START
密码消息:由1到200个字符组成一行,表示Caesar发出的一条消息
结束行:END
在最后一个数据集之后,是另一行:ENDOFINPUT
Output
每个数据集对应一行,是Caesar 的原始消息。
Sample Input
START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT
Sample Output
IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE
Source 展开
import java.util.Arrays;
public class ArrayTest {
public static void main(String[] args) {
int[] a={10,69,90,65,10};
int[] b={65,65,11};
int[] c=new int[a.length+b.length];
sop("a",a);
sop("b",b);
//浅复制!
for(int i=0;i<c.length;i++)
if(i<a.length)
c[i]=a[i];
else
c[i]=b[i-a.length];
sop("c",c);
for (int i = 0; i < c.length-1; i++)
for (int j = 0; j < c.length-i-1; j++) {
if(c[j]>c[j+1]) {
int tem=c[j];
c[j]=c[j+1];
c[j+1]=tem;
}
}
sop("c",c);
//补充下:以下为深复制方法,这种方法初学时候还是算了吧!没什么算法可学!
//合并加排序三行代码就完事了,你能学到什么?
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
Arrays.sort(c);
sop("深复制:",c);
}
private static void sop(String str,int...arr) {
System.out.print(str+"=>");
for(int a:arr)
System.out.print(a+",");
System.out.println("\tlength=>"+arr.length+"\n");
}
}
public static void charInt(String chaine){ //1044
String[] charInt = new String[2];
int count = -1;
char maxChar = 'A';
int[] letterCount = new int[26];
String word = chaine.toLowerCase();
for (int i=0;i<word.length();i++) {
int indexOfChar = (byte)word.charAt(i)-97;
letterCount[indexOfChar]++;
if (letterCount[indexOfChar]>count || (letterCount[indexOfChar]==count && word.charAt(i)<maxChar)){
count = letterCount[indexOfChar];
maxChar = word.charAt(i);
}
}
charInt[0] = String.valueOf(maxChar);
charInt[1] = ""+count;
System.out.println(charInt[0]+" "+charInt[1]);
}
public static void getDate(int n){ //1047 这题如果给1,其实是指2000年1月2号.
n++;
int[] getYear = getYear(n);
int year = getYear[0];
int[] getMonth = getMonth(year,getYear[1]);
int month = getMonth[0];
String monthString ;
if(month<10) monthString = "0"+String.valueOf(month);
else monthString = String.valueOf(month);
int day = getMonth[1];
System.out.println(year+"-"+monthString+"-"+day+" "+getDayOfWeek(n));
}
private static boolean isBissextile(int n){
if (n%4==0 && !(n%100==0&&n%400!=0))
return true;
else
return false;
}
private static int[] getYear(int n){
int[] getYear = new int[2];
int year = 2000;
while(n>0){
if(isBissextile(year)) n -= 366;
else n -= 365;
if (n>0) year++;
}
if(isBissextile(year)) n+=366;
else n += 365;
getYear[0] = year;
getYear[1] = n;
return getYear;
}
private static int[] getMonth(int year, int n){
int[] getMonth = new int[2];
int month = 1;
while(n>0){
if(month<=7 && month%2 != 0) n -= 31;
else if (month==2 && isBissextile(year) ) n -= 29;
else if (month==2 && !isBissextile(year)) n -= 28;
else if(month<=7 && month%2==0) n -= 30;
else if(month%2==0) n-=31;
else n -= 30;
if (n>0) month++;
}
if(month<=7 && month%2 != 0) n += 31;
else if (isBissextile(year) && month==2) n += 29;
else if (!isBissextile(year) && month==2) n += 28;
else if(month<=7 && month%2==0) n += 30;
else if(month%2==0) n+=31;
else n += 30;
getMonth[0] = month;
getMonth[1] = n;
return getMonth;
}
private static String getDayOfWeek(int n){
int quotient = n/7;
int remainder = n -= quotient*7;
switch(remainder){
case 0 : return "Sunday";
case 1 : return "Monday";
case 2 : return "Tuesday";
case 3 : return "Wednesday";
case 4 : return "Thursday";
case 5 : return "Friday";
case 6 : return "Saturday";
default : return "Never arrive";
}
}
public static void getCode(String chaine){ //1048
chaine = chaine.toUpperCase();
System.out.println("START");
System.out.println(chaine);
System.out.println("END");
System.out.println();
for(int i=0;i<chaine.length();i++){
System.out.print((changChar(chaine.charAt(i))));
}
System.out.println();
}
private static char changChar(char c){
if(c>=65 && c<=90 && c-5<65) return (char)(c+26-5);
else if(c>=65 && c<=90) return (char)(c-5);
else return c;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JavaExos.charInt("adfadffasdfda");
JavaExos.getDate(1751);
JavaExos.getCode("NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX");
public class JavaExos {
public static void charInt(String chaine){ //1044
String[] charInt = new String[2];
int count = -1;
char maxChar = 'A';
int[] letterCount = new int[26];
String word = chaine.toLowerCase();
for (int i=0;i<word.length();i++) {
int indexOfChar = (byte)word.charAt(i)-97;
letterCount[indexOfChar]++;
if (letterCount[indexOfChar]>count || (letterCount[indexOfChar]==count && word.charAt(i)<maxChar)){
count = letterCount[indexOfChar];
maxChar = word.charAt(i);
}
}
charInt[0] = String.valueOf(maxChar);
charInt[1] = ""+count;
System.out.println(charInt[0]+" "+charInt[1]);
}
public static void getDate(int n){ //1047 这题如果给1,其实是指2000年1月2号.
n++;
int[] getYear = getYear(n);
int year = getYear[0];
int[] getMonth = getMonth(year,getYear[1]);
int month = getMonth[0];
String monthString ;
if(month<10) monthString = "0"+String.valueOf(month);
else monthString = String.valueOf(month);
int day = getMonth[1];
System.out.println(year+"-"+monthString+"-"+day+" "+getDayOfWeek(n));
}
private static boolean isBissextile(int n){
if (n%4==0 && !(n%100==0&&n%400!=0))
return true;
else
return false;
}
private static int[] getYear(int n){
int[] getYear = new int[2];
int year = 2000;
while(n>0){
if(isBissextile(year)) n -= 366;
else n -= 365;
if (n>0) year++;
}
if(isBissextile(year)) n+=366;
else n += 365;
getYear[0] = year;
getYear[1] = n;
return getYear;
}
private static int[] getMonth(int year, int n){
int[] getMonth = new int[2];
int month = 1;
while(n>0){
if(month<=7 && month%2 != 0) n -= 31;
else if (month==2 && isBissextile(year) ) n -= 29;
else if (month==2 && !isBissextile(year)) n -= 28;
else if(month<=7 && month%2==0) n -= 30;
else if(month%2==0) n-=31;
else n -= 30;
if (n>0) month++;
}
if(month<=7 && month%2 != 0) n += 31;
else if (isBissextile(year) && month==2) n += 29;
else if (!isBissextile(year) && month==2) n += 28;
else if(month<=7 && month%2==0) n += 30;
else if(month%2==0) n+=31;
else n += 30;
getMonth[0] = month;
getMonth[1] = n;
return getMonth;
}
private static String getDayOfWeek(int n){
int quotient = n/7;
int remainder = n -= quotient*7;
switch(remainder){
case 0 : return "Sunday";
case 1 : return "Monday";
case 2 : return "Tuesday";
case 3 : return "Wednesday";
case 4 : return "Thursday";
case 5 : return "Friday";
case 6 : return "Saturday";
default : return "Never arrive";
}
}
public static void getCode(String chaine){ //1048
chaine = chaine.toUpperCase();
System.out.println("START");
System.out.println(chaine);
System.out.println("END");
System.out.println();
for(int i=0;i<chaine.length();i++){
System.out.print((changChar(chaine.charAt(i))));
}
System.out.println();
}
private static char changChar(char c){
if(c>=65 && c<=90 && c-5<65) return (char)(c+26-5);
else if(c>=65 && c<=90) return (char)(c-5);
else return c;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JavaExos.charInt("adfadffasdfda");
JavaExos.getDate(1751);
JavaExos.getCode("NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX");
}
}
public class S1044 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = Integer.parseInt(in.nextLine());
List<String> strList = new ArrayList<String>();
String input;
Map<Character, Integer> countMap = new HashMap<Character, Integer>();
Set<Character> countSet = null;
for (int i = 0; i < count; i++) {
countMap = new HashMap<Character, Integer>();
input = in.nextLine();
int length = input.length();
for (int j = 0; j < length; j++) { // 统计各个字符的数量
char c = input.charAt(j);
if (countMap.containsKey(c)) {
countMap.put(c, countMap.get(c) + 1);
} else {
countMap.put(c, 1);
}
}
char maxChar = '\u0000';
int maxCount = -1;
countSet = countMap.keySet();
for (Character ch : countSet) {
if (countMap.get(ch) > maxCount) {
maxCount = countMap.get(ch);
maxChar = ch;
}
}
strList.add(maxChar + " : " + maxCount);
}
for (String str : strList) {
System.out.println(str);
}
}
}
/*******************1047*******************/
public class S1047 {
public static void main(String[] args) throws Throwable {
Scanner in = new Scanner(System.in);
SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat weekFormat = new SimpleDateFormat("E");
long standardTime = new SimpleDateFormat("yyyyMMdd").parse("20000101").getTime();
long tmpTime;
Date date;
List<String> strList = new ArrayList<String>();
while (true) {
int dayCount = Integer.parseInt(in.nextLine().trim());
if (dayCount == -1) { // 为-1 就退出
break;
}
tmpTime = standardTime + dayCount * 86400000l;
date = new Date(tmpTime);
strList.add(dayFormat.format(date) + " " + getWeekDay(weekFormat.format(date)));
}
for (String str : strList) {
System.out.println(str);
}
}
private static String getWeekDay(String name) {
if (name.equals("星期一")) {
return "Monday";
}else if (name.equals("星期二")) {
return "Tuesday";
}else if (name.equals("星期三")) {
return "Wednesday";
}else if (name.equals("星期四")) {
return "Thursday";
}else if (name.equals("星期五")) {
return "Friday";
}else if (name.equals("星期六")) {
return "Saturday";
}else if (name.equals("星期日")) {
return "Sunday";
} else {
return name;
}
}
}
/*******************1048*******************/
public class S1048 {
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) { // 65 - 90
List<String> strList = new ArrayList<String>();
String input;
while (true) {
input = in.nextLine().trim();
if (input.equals("START")) {
strList.addAll(inputCode());
} else if (input.equals("ENDOFINPUT")){
break;
}
}
for (String str : strList) {
System.out.println(str);
}
}
public static List<String> inputCode() {
String input;
List<String> strList = new ArrayList<String>();
StringBuffer tempStr = null;
while (true) {
input = in.nextLine();
if (input.equals("END")) {
return strList;
}
tempStr = new StringBuffer(input);
int length = tempStr.length();
for (int i = 0; i < length; i++) {
char ch = tempStr.charAt(i);
if (ch >= 65 && ch <= 90) {
ch -= 5;
if (ch < 65) {
ch += 26;
}
}
tempStr.setCharAt(i, ch);
}
strList.add(tempStr.toString());
}
}
}
广告 您可能关注的内容 |