编写以下两个函数
/以空格(单个或多个)为分隔符,将line中的元素抽取出来,放入一个List/
public static List convertStringToList(String line)
/在list中移除掉与str内容相同的元素/
public static void remove(List list, String str)
裁判测试程序:
public class Main {
/*covnertStringToList函数代码*/
/*remove函数代码*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
List<String> list = convertStringToList(sc.nextLine());
System.out.println(list);
String word = sc.nextLine();
remove(list,word);
System.out.println(list);
}
sc.close();
}
}
样例说明: 底下展示了4组测试数据。
输入样例
1 2 1 2 1 1 1 2
1
11 1 11 1 11
11
2 2 2
1
1 2 3 4 1 3 1
1
输出样例
[1, 2, 1, 2, 1, 1, 1, 2]
[2, 2, 2]
[11, 1, 11, 1, 11]
[1, 1]
[2, 2, 2]
[2, 2, 2]
[1, 2, 3, 4, 1, 3, 1]
[2, 3, 4, 3]
AC代码
/*以空格(单个或多个)为分隔符,将line中的元素抽取出来,放入一个List*/
public static List<String> convertStringToList(String line){
String[] s = line.split("\\s+");
List<String> l = new ArrayList<String>();
for(int i = 0; i < s.length; i++){
if(!s.equals(" "))
l.add(s[i]);
}
return l;
}
/*在list中移除掉与str内容相同的元素*/
public static void remove(List<String> list, String str){
Iterator l = list.iterator();
int i;
while(l.hasNext()){
String s = (String)l.next();
if(s.equals(str))
l.remove();
}
}
找出给定n个整数中的次大值(第二大)。
输入格式:
输入分为两行,第一行一个整数n,表示数据的个数,第二行是由空格分隔的n个整数。
输出格式:
n个数据中的次大值。
输入样例:
6
23 34 8 26 17 9
输出样例:
26
AC代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, i, num;
n = sc.nextInt();
int max1, max2;
max1 = sc.nextInt();
max2 = max1;
for(i = 1; i < n; i++){
num = sc.nextInt();
if(num >= max1){
max2 = max1;
max1 = num;
}
else{
if(num > max2)
max2 = num;
}
}
System.out.println(max2);
sc.close();
}
}
完成数组元素的移动功能:假设数组有n个元素,输入一个数x,把数组的第x个位置的元素删除了,后面的元素依次前进一个位置。 重复若干次这样的删除,得到最后的结果。
输入格式:
第一行包括一个整数n(1<=n<=100),表示数组元素的个数。 第二行输入n个数组元素,均为整数,用空格隔开。 第三行输入一个数k(1<=k<=100),表示要进行k次删除。 接下来k行,每行一个数x,表示要删除第x个元素。
输出格式:
输出经过k次删除后的数组,每两个元素之间用空格隔开。
输入样例:
10
1 2 3 4 5 6 7 8 9 10
4
3
2
4
6
输出样例:
1 4 5 7 8 10
AC代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, k, m;
n = sc.nextInt();
int[] a = new int[n];
int i, j;
for(i = 0; i < n; i++)
a[i] = sc.nextInt();
k = sc.nextInt();
for(i = 0; i < k; i++){
m = sc.nextInt();
for(j = m - 1; j < n - 1; j++){
a[j] = a[j + 1];
}
n--;
}
System.out.print(a[0]);
for(i = 1; i < n; i++){
System.out.print(" " + a[i]);
}
sc.close();
}
}
设计一个BankAccount类,这个类包括:
(1)一个int型的balance表时账户余额。
(2)一个无参构造方法,将账户余额初始化为0。
(3)一个带一个参数的构造方法,将账户余额初始化为该输入的参数。
(4)一个getBlance()方法,返回账户余额。
(5)一个withdraw()方法:带一个amount参数,并从账户余额中提取amount指定的款额。
(6)一个deposit()方法:带一个amount参数,并将amount指定的款额存储到该银行账户上。
设计一个Main类进行测试,分别输入账户余额、提取额度以及存款额度,并分别输出账户余额。
输入格式:
依次输入账户余额、提取额度、存款额度
输出格式:
依次输出初始账户余额、提取amount额度后的账户余额、存入amount后的账户余额
输入样例:
在这里给出一组输入。例如:
700
70
7
输出样例:
在这里给出相应的输出。例如:
700
630
637
AC代码
import java.util.*;
class BankAccount{
int balance;
BankAccount(){
balance = 0;
}
BankAccount(int balance){
this.balance = balance;
}
void setBalance(int balance){
this.balance = balance;
}
int getBlance(){
return balance;
}
int withdraw(int amount){
balance -= amount;
return balance;
}
int deposit(int amount){
balance += amount;
return balance;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BankAccount ba = new BankAccount();
int b, a1, a2;
b = sc.nextInt();
a1 = sc.nextInt();
a2 = sc.nextInt();
ba.setBalance(b);
System.out.println(ba.getBlance());
System.out.println(ba.withdraw(a1));
System.out.println(ba.deposit(a2));
sc.close();
}
}
编写程序,实现字符串大小写的转换并倒序输出。
输入格式:
输入一行字符串
输出格式:
字符串大小写的转换,并倒序输出
输入样例:
Hello World!
输出样例:
!DLROw OLLEh
AC代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s;
s = sc.nextLine();
int len = s.length();
char[] n = new char[len];
int i;
for(i = 0; i < len; i++)
{
char c = s.charAt(i);
if(c >= 'a' && c <= 'z')
c -= 32;
else if(c >= 'A' && c <= 'Z')
c += 32;
n[i] = c;
}
for(i = len - 1; i >= 0; i--){
System.out.printf("%c", n[i]);
}
sc.close();
}
}
从键盘接收N个英文字符串(其中不同的字符串数量大于10),从头开始取5个不同的字符串放入一个集合S1,然后接着取5个不同的字符串放入另一个集合S2,按照字母顺序输出S1和S2的并集中的每个字符串(字符串区分大小写)
输入格式:
一行以空格分开的英文字符串(不同的字符串数量大于10)。
输出格式:
按照字母顺序(先比较字符串首字母,首字母相同的比较字符串第二个字母,以此类推)输出的S1和S2并集的字符串。
输入样例:
android python java javaee javase database java jsp servlet java algorithm junit
输出样例:
algorithm
android
database
java
javaee
javase
jsp
python
servlet
AC代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s[] = new String[12];
Set<String> a = new TreeSet<String>();
Set<String> b = new TreeSet<String>();
String s1 = sc.nextLine();
for(String i:s1.split(" ")){
if(a.size() < 5){
a.add(i);
continue;
}
if(b.size() < 5){
b.add(i);
continue;
}
}
a.addAll(b);
for(String i:a){
System.out.println(i);
}
sc.close();
}
}