Java第五次作業(yè)

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();

? ? }

}

#####################################################

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市棘利,隨后出現(xiàn)的幾起案子橱野,更是在濱河造成了極大的恐慌,老刑警劉巖善玫,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件水援,死亡現(xiàn)場離奇詭異密强,居然都是意外死亡,警方通過查閱死者的電腦和手機蜗元,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進店門或渤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人奕扣,你說我怎么就攤上這事薪鹦。” “怎么了惯豆?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵池磁,是天一觀的道長。 經(jīng)常有香客問我楷兽,道長地熄,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任芯杀,我火速辦了婚禮端考,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘揭厚。我一直安慰自己跛梗,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布棋弥。 她就那樣靜靜地躺著核偿,像睡著了一般。 火紅的嫁衣襯著肌膚如雪顽染。 梳的紋絲不亂的頭發(fā)上漾岳,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天,我揣著相機與錄音粉寞,去河邊找鬼尼荆。 笑死,一個胖子當(dāng)著我的面吹牛唧垦,可吹牛的內(nèi)容都是我干的捅儒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼振亮,長吁一口氣:“原來是場噩夢啊……” “哼巧还!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起坊秸,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤麸祷,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后褒搔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體阶牍,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡喷面,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了走孽。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片惧辈。...
    茶點故事閱讀 40,498評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖磕瓷,靈堂內(nèi)的尸體忽然破棺而出盒齿,到底是詐尸還是另有隱情,我是刑警寧澤生宛,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布县昂,位于F島的核電站,受9級特大地震影響陷舅,放射性物質(zhì)發(fā)生泄漏倒彰。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一莱睁、第九天 我趴在偏房一處隱蔽的房頂上張望待讳。 院中可真熱鬧,春花似錦仰剿、人聲如沸创淡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽琳彩。三九已至,卻和暖如春部凑,著一層夾襖步出監(jiān)牢的瞬間露乏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工涂邀, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留瘟仿,地道東北人。 一個月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓比勉,卻偏偏與公主長得像劳较,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子浩聋,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,507評論 2 359

推薦閱讀更多精彩內(nèi)容