JAVA 练习题

2024-11-26 01:24:02
推荐回答(4个)
回答1:

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;iif(ic[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");
}
}

回答2:

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 int indexOfChar = (byte)word.charAt(i)-97;
letterCount[indexOfChar]++;
if (letterCount[indexOfChar]>count || (letterCount[indexOfChar]==count && word.charAt(i) 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 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");

回答3:

做完了,例子都在main里,看吧。

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 int indexOfChar = (byte)word.charAt(i)-97;
letterCount[indexOfChar]++;
if (letterCount[indexOfChar]>count || (letterCount[indexOfChar]==count && word.charAt(i) 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 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");
}
}

回答4:

/*******************1044*******************/
public class S1044 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = Integer.parseInt(in.nextLine());
List strList = new ArrayList();
String input;
Map countMap = new HashMap();
Set countSet = null;
for (int i = 0; i < count; i++) {
countMap = new HashMap();
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 strList = new ArrayList();
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 strList = new ArrayList();
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 inputCode() {
String input;
List strList = new ArrayList();
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());
}
}
}