目錄
前言:
R7-5 Count the letters in a string (統(tǒng)計(jì)字符串中的字符)
R7-1 找素?cái)?shù)
R7-3 電話號碼同步(Java)
總結(jié):
前言:
????????臨近期末,我也更新一下PTA上的JAVA大題,希望各位都可以考出一個好的成績。
R7-5 Count the letters in a string (統(tǒng)計(jì)字符串中的字符)
(Count the letters in a string) (統(tǒng)計(jì)字符串中的字符)
Write a method that counts the number of letters in a string using the following header:
public static int countLetters(String s)
?Write a test program that prompts the user to enter a string and displays the number of letters in the string.
(統(tǒng)計(jì)字符串中的字符) 使用以下標(biāo)題編寫一個計(jì)算字符串中字母數(shù)的方法:
public static int countLetters(String s)
編寫一個程序,提示用戶輸入字符串并顯示字符串中的字母數(shù)。
input style :
Enter a string
output style:
Displays the number of the letters in the string.
input sample:
qwe&&&,,;#@23qer
output sample:
The number of letters inside the string is: 6
代碼:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.next();
System.out.println("The number of letters inside the string is: "+countLetters(s));
}
public static int countLetters(String s){
int c=0;
for (int i = 0; i < s.length(); i++) {
if(Character.isLetter(s.charAt(i))){
c++;
}
}
return c;
}
}
統(tǒng)計(jì)字符串中的字母其實(shí)非常簡單,主要就是調(diào)用JAVA中的方法:
Character.isLetter(s.charAt(i))
根據(jù)它的返回值來判斷字符串S中第i個字符是否是一個字母。
R7-1 找素?cái)?shù)
請編寫程序,從鍵盤輸入兩個整數(shù)m,n,找出等于或大于m的前n個素?cái)?shù)。
輸入格式:
第一個整數(shù)為m,第二個整數(shù)為n;中間使用空格隔開。例如:
103 3
輸出格式:
從小到大輸出找到的等于或大于m的n個素?cái)?shù),每個一行。例如:
103
107
109
輸入樣例:
9223372036854775839 2
輸出樣例:
9223372036854775907
9223372036854775931
這道題主要是需要調(diào)用JAVA? MATH庫中的BigInteger ,因?yàn)檫@里的例子都是超大數(shù),因此需要用BigInteger來存儲。
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
BigInteger m = input.nextBigInteger();
BigInteger n = input.nextBigInteger();
input.close();
BigInteger count = BigInteger.ZERO;
BigInteger num = m;
while (count.compareTo(n) < 0) {
if (num.isProbablePrime(100)) {
System.out.println(num);
count = count.add(BigInteger.ONE);
}
num = num.add(BigInteger.ONE);
}
}
}
R7-3 電話號碼同步(Java)
文件phonebook1.txt和phonebook2.txt中有若干聯(lián)系人的姓名和電話號碼。請你設(shè)計(jì)一個程序,將這兩個文件中的電話號碼同步。(所謂同步,就是將兩個文件中的電話號碼合并后剔除相同的人名和電話號碼。請將同步后的電話號碼按照姓名拼音順序排序后保存到文件phonebook3.txt中。)
由于目前的OJ系統(tǒng)暫時不能支持用戶讀入文件和寫文件,我們編寫程序從鍵盤輸入文件中的姓名和電話號碼,當(dāng)輸入的單詞為end時,表示文件結(jié)束。將同步后的電話號碼按照姓名拼音順序排序后輸出。
輸入格式:
張三 13012345678
李四 13112340000
王五 13212341111
馬六 13312342222
陳七 13412343333
孫悟空 13512345555
end (表示文件phonebook1.txt結(jié)束)
張三 13012345678
孫悟空 13512345555
王五 13212341111
陳七 13412343333
唐三藏 13612346666
豬悟能 13712347777
沙悟凈 13812348888
end (表示文件phonebook2.txt結(jié)束)
輸出格式:
陳七 13412343333
李四 13112340000
馬六 13312342222
沙悟凈 13812348888
孫悟空 13512345555
唐三藏 13612346666
王五 13212341111
張三 13012345678
豬悟能 13712347777
本題主要是考察我們對樹這種數(shù)據(jù)結(jié)構(gòu)的利用,其實(shí)不是很難。
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String args[]){
Scanner in=new Scanner(System.in);
String string;
String string2;
Set<String> set=new TreeSet<String>();
Set<String> set2=new TreeSet<String>();
Set<String> set3=new TreeSet<String>();
while(in.hasNextLine()){
string=in.nextLine();
if(string.equals("end")){
break;
}
else{
set.add(string);
}
}
while(in.hasNextLine()){
string2=in.nextLine();
if(string2.equals("end")){
break;
}
else{
set2.add(string2);
}
}
set3.addAll(set);
set3.addAll(set2);
Iterator iterator=set3.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
總結(jié):
????????祝愿各位可以考出一個如意的成績。暑假期間我會更新前端三件套,有興趣的小伙伴可以和我一起學(xué)習(xí),共同進(jìn)步。
如果我的內(nèi)容對你有幫助,請點(diǎn)贊,評論,收藏。創(chuàng)作不易,大家的支持就是我堅(jiān)持下去的動力!
文章來源:http://www.zghlxwxcb.cn/news/detail-566293.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-566293.html
到了這里,關(guān)于【從零開始學(xué)習(xí)JAVA | 第二十七篇】JAVA期末練習(xí)(PTA)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!