1.找出足球赛对阵方 (10分)
2014年世界杯足球赛在6月24日星期二小组赛赛程如下所示: Australian VS Spain; Holland VS Chile; Cameroon VS Brazil; Croatia VS Mexico 请你编写一个简单的C++程序,当输入其中任何一个国家的名字就能找出与其对阵的国家名。例如,输入“Holland”,就能找到对阵方是“Chile”。
输入格式:
Holland
输出格式:
Holland team's rival is Chile.
输入样例:
Germany
输出样例:
Germany's team has no match today.
答案:
//找出足球赛对阵方
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<String,String> map=new HashMap<>();
map.put("Australian","Spain");
map.put("Spain","Australian");
map.put("Holland","Chile");
map.put("Chile","Holland");
map.put("Cameroon","Brazil");
map.put("Brazil","Cameroon");
map.put("Croatia","Mexico");
map.put("Mexico","Croatia");
Scanner input=new Scanner(System.in);
String str=input.next();
if(map.containsKey(str)){
System.out.println(str+" team's rival is "+map.get(str)+".");
}else{
System.out.println(str+"'s team has no match today.");
}
}
}
2.找素数
请编写程序,从键盘输入两个整数m,n,找出等于或大于m的前n个素数。
输入格式:
第一个整数为m,第二个整数为n;中间使用空格隔开。例如:
103 3
输出格式:
从小到大输出找到的等于或大于m的n个素数,每个一行。例如:
103
107
109
输入样例:
9223372036854775839 2
输出样例:
9223372036854775907
9223372036854775931
答案:
//找素数
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
BigInteger m = input.nextBigInteger();
int n = input.nextInt();
int temp = n;
for (BigInteger i = m; temp > 0; i = i.add(BigInteger.ONE)) {
if (isPrime(i)) {
temp--;
System.out.println(i);
}
}
}
public static boolean isPrime(BigInteger a) {
return a.isProbablePrime(50);
}
}
3.字符串替换
将文本文件中指定的字符串替换成新字符串。 由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的内容,当输入的一行为end时,表示结束。end后面有两个字符串,要求用第二个字符串替换文本中所有的第一个字符串。
输入格式:
Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology. The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
end (表示结束)
Institute (第一个字符串,要求用第二个字符串替换)
University (第二个字符串)
输出格式:
Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
输入样例:
Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.
The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
end
Institute
University
输出样例:
Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
答案:
//字符串替换
import java.util.*;
public class Main{
public static void main(String []args){
ArrayList<String> ls = new ArrayList<String>();
Scanner in = new Scanner(System.in);
int len = 0;
ls.add(in.nextLine());
while(ls.get(len).compareTo("end") != 0){
len++;
ls.add(in.nextLine());
}
String a = in.nextLine();
String b = in.nextLine();
String []ss = new String[len];
for(int i = 0; i < len;i++){
ss[i] = ls.get(i).replace(a,b);
}
for(int i = 0;i<len;i++){
System.out.println(ss[i]);
}
in.close();
}
}
4.电话号码同步(Java)
文件phonebook1.txt和phonebook2.txt中有若干联系人的姓名和电话号码。请你设计一个程序,将这两个文件中的电话号码同步。(所谓同步,就是将两个文件中的电话号码合并后剔除相同的人名和电话号码。请将同步后的电话号码按照姓名拼音顺序排序后保存到文件phonebook3.txt中。)
由于目前的OJ系统暂时不能支持用户读入文件和写文件,我们编写程序从键盘输入文件中的姓名和电话号码,当输入的单词为end时,表示文件结束