153 - 判斷回文
Time Limit: 1000? Memory Limit: 65535
Submit: 187? Solved: 124
Description
用戶從鍵盤輸入一個整數(shù)亭病,程序?qū)⑴袛噙@個數(shù)是幾位數(shù)并輸出其位數(shù),并判斷這個數(shù)是否是回文數(shù),是則輸出Y断国,否則輸出N柜去√猎遥回文數(shù)是指將該數(shù)含有的數(shù)字逆序排列后得到的數(shù)和原數(shù)相同足淆,例如12121尝胆、3223都是回文數(shù)丧裁。
Input
整數(shù)
Output
幾位數(shù)
是否是回文數(shù)
Sample Input
12121
Sample Output
5
Y
162 - 字符串
Time Limit: 1000? Memory Limit: 65535
Submit: 128? Solved: 74
Description
對于輸入字符串s(假設(shè)字符串只包含字母構(gòu)成的單詞和空格),完成如下功能:
1. 統(tǒng)計該字符串中字母c出現(xiàn)的次數(shù)
2. 求該字符串的逆
3. 輸出該字符串中子串str的所有位置(無需考慮子串疊加現(xiàn)象)
4. 將字符串中每個單詞的第一個字母變成大寫并輸出
Input
字符串s
字母c
子串str
Output
c在s中出現(xiàn)的次數(shù)
s的逆
str在s中的所有位置
所有單詞首字母大寫后的字符串
Sample Input
I scream you scream we all scream for icecream
m
eam
Sample Output
4
maerceci rof maercs lla ew maercs uoy maercs I
5 16 30 43
I Scream You Scream We All Scream For Icecream
__________________________________________________
import java.util.*;
public class Main {
? ? public static void main(String[] args) {
? ? ? ? // TODO Auto-generated method stub
? ? ? ? Scanner scan=new Scanner(System.in);
? ? ? ? String s=scan.nextLine();
? ? ? ? //1. 統(tǒng)計該字符串中字母c出現(xiàn)的次數(shù)
? ? ? ? String c=scan.nextLine();
? ? ? ? int count=0;
? ? ? ? for(int i=0;i<s.length();i++){
? ? ? ? ? ? char tmp=s.charAt(i);
? ? ? ? ? ? if(tmp==c.charAt(0)){
? ? ? ? ? ? ? ? count++;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? System.out.println(count);
? ? ? ? //2. 求該字符串的逆
? ? ? ? StringBuffer sb=new StringBuffer(s);
? ? ? ? System.out.println(sb.reverse());
? ? ? ? //3. 輸出該字符串中子串str的所有位置(無需考慮子串疊加現(xiàn)象)
? ? ? ? String str=scan.nextLine();
? ? ? ? int i=0;
? ? ? ? while(s.indexOf(str,i)!=-1){
? ? ? ? ? ? if(i!=0){
? ? ? ? ? ? ? ? System.out.print(" ");
? ? ? ? ? ? }
? ? ? ? ? ? System.out.print(s.indexOf(str,i));
? ? ? ? ? ? i=str.length()+s.indexOf(str,i);
? ? ? ? }
? ? ? ? System.out.println();
? ? ? ? //4. 將字符串中每個單詞的第一個字母變成大寫并輸出
? ? ? ? String[] split=s.split(" ");
? ? ? ? for(int ii=0;ii<split.length;ii++){
? ? ? ? ? ? String ss=split[ii].substring(0,1).toUpperCase()+split[ii].substring(1);
? ? ? ? ? ? System.out.print(ss+" ");
? ? ? ? }
? ? }
}
###############################################
163 - 各類字符數(shù)
Time Limit: 1000? Memory Limit: 65535
Submit: 97? Solved: 76
Description
從鍵盤輸入一個字符串含衔,程序輸出該字符串中的大寫英文字母數(shù)煎娇,小寫英文字母數(shù)以及非英文字母數(shù)
Input
字符串
Output
大寫英文字母數(shù)
小寫英文字母數(shù)
非英文字母數(shù)
Sample Input
Hello My Dear Friend, I Miss You Very Much!
Sample Output
9
24
10
____________________________________________
import java.util.*;
public class Main {
? ? public static void main(String[] args) {
? ? ? ? // TODO Auto-generated method stub
? ? ? ? Scanner scan=new Scanner(System.in);
? ? ? ? String s=scan.nextLine();
? ? ? ? int counta=0;
? ? ? ? int countA=0;
? ? ? ? int count_=0;
? ? ? ? for(int i=0;i<s.length();i++){
? ? ? ? ? ? if(s.charAt(i)>='A'&&s.charAt(i)<='Z'){
? ? ? ? ? ? ? ? countA++;
? ? ? ? ? ? }
? ? ? ? ? ? else if(s.charAt(i)>='a'&&s.charAt(i)<='z'){
? ? ? ? ? ? ? ? counta++;
? ? ? ? ? ? }
? ? ? ? ? ? else count_++;
? ? ? ? }
? ? ? ? System.out.println(countA);
? ? ? ? System.out.println(counta);
? ? ? ? System.out.println(count_);
? ? }
}
#########################################################
164 - 解析二維數(shù)組
Time Limit: 1000? Memory Limit: 65535
Submit: 117? Solved: 61
Description
讀入一個字符串,該字符串表示一個整型二維數(shù)組d贪染,數(shù)組中的元素通過解析字符串參數(shù)獲得缓呛。例如,字符串參數(shù):“1,2;3,4,5;6,7,8”杭隙,對應(yīng)的數(shù)組為:
d[0,0] = 1 d[0,1] = 2?
d[1,0] = 3 d[1,1] = 4 d[1,2] = 5
d[2,0] = 6 d[2,1] = 7 d[2,2] = 8
打印這個數(shù)組各元素的內(nèi)容
Input
字符串
Output
二維數(shù)組各元素
Sample Input
1,2;3,4,5;6,7,8
Sample Output
d[0,0] = 1 d[0,1] = 2
d[1,0] = 3 d[1,1] = 4 d[1,2] = 5
d[2,0] = 6 d[2,1] = 7 d[2,2] = 8
____________________________________
import java.util.*;
public class Main{
public static void main(String[] args) {
? ? ? ? Scanner scan = new Scanner(System.in);
? ? ? ? String s=scan.next();
? ? ? ? String[] st=s.split(";");
? ? ? ? for(int i=0;i<st.length;i++) {
? ? ? ? String[] st2=st[i].split(",");
? ? ? ? for(int j=0;j<st2.length;j++) {
? ? ? ? if(j!=0) System.out.print(" ");
? ? ? ? System.out.print("d["+i+","+j+"] = "+st2[j]);
? ? ? ? }
? ? ? ? System.out.println();
? ? ? ? }
? ? }
}
###############################################
165 - 數(shù)據(jù)類型判斷
Time Limit: 1000? Memory Limit: 65535
Submit: 102? Solved: 67
Description
從鍵盤分別輸入通過空格分割的整型(int)哟绊、浮點型(double)、字符型(String)痰憎、布爾型(boolean)票髓,根據(jù)讀取的內(nèi)容判斷他們的類型并將他們解析為正確的對象,并都放到一個數(shù)組中铣耘。輸出各個對象的類型
Input
字符串
Output
數(shù)據(jù)類型
Sample Input
2.1 true 123 abcde
Sample Output
double boolean int String
____________________________________________
import java.util.*;
public class Main {
? ? public static void main(String[] args) {
? ? ? ? // TODO Auto-generated method stub
? ? ? ? Scanner scan=new Scanner(System.in);
? ? ? ? int f=0;
? ? ? ? while(scan.hasNext()){
? ? ? ? ? ? if(f!=0)
? ? ? ? ? ? ? ? System.out.print(' ');
? ? ? ? ? ? if(scan.hasNextInt())
? ? ? ? ? ? ? ? System.out.print("int");
? ? ? ? ? ? else if(scan.hasNextDouble())
? ? ? ? ? ? ? ? System.out.print("double");
? ? ? ? ? ? else if(scan.hasNextBoolean())
? ? ? ? ? ? ? ? System.out.print("boolean");
? ? ? ? ? ? else
? ? ? ? ? ? ? ? System.out.print("String");
? ? ? ? ? ? scan.next();
? ? ? ? ? ? f++;
? ? ? ? }
? ? }
}
##################################################
158 - 打印雙休日
Time Limit: 1000? Memory Limit: 65535
Submit: 100? Solved: 57
Description
輸入年份和月份洽沟,打印當(dāng)月所有雙休日日期,打印格式為:“2018-06-16”
Input
年份和月份
Output
雙休日日期
Sample Input
2018 6
Sample Output
2018-06-02
2018-06-03
2018-06-09
2018-06-10
2018-06-16
2018-06-17
2018-06-23
2018-06-24
2018-06-30
___________________________
import java.util.*;
public class Main{
? ? public static void main(String[] args) {
? ? ? ? Scanner scan = new Scanner(System.in);
? ? ? ? int year=scan.nextInt();
? ? ? ? int month=scan.nextInt();
? ? ? ? Calendar c = Calendar.getInstance();
? ? ? ? c.set(Calendar.YEAR, year);
? ? ? ? c.set(Calendar.MONTH, month - 1);
? ? ? ? int max = c.getActualMaximum(Calendar.DAY_OF_MONTH);
? ? ? ? int start = 1;
? ? ? ? while (start <= max) {
? ? ? ? ? ? c.set(Calendar.DAY_OF_MONTH, start);
? ? ? ? ? ? if (isWeekenday(c)) {
? ? ? ? ? ? ? ? System.out.println(year+"-"+String.format("%02d",month)+"-"+String.format("%02d",start));
? ? ? ? ? ? }
? ? ? ? ? ? start++;
? ? ? ? }
? ? }
? ? public static boolean isWeekenday(Calendar c) {
? ? ? ? return c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || c.get(Calendar.DAY_OF_WEEK)? == Calendar.SATURDAY;
? ? }
}
##################################################
166 - 比較日期
Time Limit: 1000? Memory Limit: 65535
Submit: 99? Solved: 54
Description
從命令行輸入兩個日期(格式為MM,dd,yyyy)涡拘,程序解析日期玲躯,判斷兩個日期的大小,以及兩個日期的間隔天數(shù)鳄乏。
Input
兩個日期
Output
日期大小關(guān)系
間隔天數(shù)(正數(shù))
Sample Input
04,12,2012 04,21,2012
Sample Output
<
9
HINT
月份是從0開始
_________________________________________________
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main{
? ? public static void main(String[] args) {
? ? ? ? Scanner scan = new Scanner(System.in);
? ? ? ? String d1=scan.next();
? ? ? ? String d2=scan.next();
? ? ? ? String[] st1=d1.split(",");
? ? ? ? String[] st2=d2.split(",");
? ? ? ? Date date1 = new Date(Integer.parseInt(st1[2]),Integer.parseInt(st1[0]),Integer.parseInt(st1[1]));
? ? ? ? Date date2 = new Date(Integer.parseInt(st2[2]),Integer.parseInt(st2[0]),Integer.parseInt(st2[1]));
? ? ? ? /* Calendar c1 = Calendar.getInstance();
? ? ? ? c1.set(Calendar.YEAR, Integer.parseInt(st1[2]));
? ? ? ? c1.set(Calendar.MONTH, Integer.parseInt(st1[0]));
? ? ? ? c1.set(Calendar.DAY_OF_MONTH, Integer.parseInt(st1[1]));
? ? ? ? Calendar c2 = Calendar.getInstance();
? ? ? ? c2.set(Calendar.YEAR, Integer.parseInt(st2[2]));
? ? ? ? c2.set(Calendar.MONTH, Integer.parseInt(st2[0]));
? ? ? ? c2.set(Calendar.DAY_OF_MONTH, Integer.parseInt(st2[1]));*/
? ? ? ? if (date1.equals(date2)) {
? ? ? ? ? ? System.out.println("=");
? ? ? ? } else if (date1.before(date2)) {
? ? ? ? ? ? System.out.println("<");
? ? ? ? } else System.out.println(">");
? ? ? ? long d = (date2.getTime()-date1.getTime())/86400000;
? ? ? ? System.out.println(Math.abs(d));
? ? ? ? scan.close();
? ? }
}
#####################################################