(1):字符串 反转 比如:I Like you 变成 you Like I
public class SwapString {
/**
* 思路:先把字符串的第一个字符与最后一个字符进行调换位置
*
* @param arr
* @param begin
* @param end
*/
public static void swap(char[]arr,int begin,int end){
while(begin < end){
char temp = arr[begin];
arr[begin] = arr[end];
arr[end] = temp;
begin ++;
end --;
}
}
public static String swapString(String str){
char []arr = str.toCharArray();
swap(arr,0,str.length()-1);
int begin = 0;
for(int i=1;i<arr.length;i++){
if(arr[i] ==' '){
swap(arr,begin,i-1);
begin = i + 1;
}
}
swap(arr,begin,arr.length - 1);
return new String(arr);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
System.out.println(swapString(str));
}
}
(
2):要求:输入二个字符串,从第一个字符串中删除第二个字符串中所有的字符,输出删除后第一个字符串。* 比如输入:They are students.”和”aeiou”,输出”Thy r stdnts.”
public class DelCommonStr { public static String delString(String str1,String str2){ String newString = ""; for(int i=0;i<str1.length();i++){ boolean flag = false; for(int j=0;j<str2.length();j++){ if(str1.charAt(i) == str2.c
public class ListAllNum { public static void printList(int m,int n,List<Integer>list){ if(m == 0){ System.out.println(list); return; } if(n<=0 || m<0){ return; } List list1 = new ArrayList<>(list); printList(m, n-1, list); list1.add(n); printList(m-n, n-1, list1); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); List<Integer> list = new ArrayList<>(); int up = n > m ? m : n; printList(m,up,list);
(3):要求:输入两个整数 n 和 m,从数列1,2,3.......n 中随意取几个数,使其和等于 m,要求将其中所有的可能组合列出来}
public class ListAllNum { public static void printList(int m,int n,List<Integer>list){ if(m == 0){ System.out.println(list); return; } if(n<=0 || m<0){ return; } List list1 = new ArrayList<>(list); printList(m, n-1, list); list1.add(n); printList(m-n, n-1, list1); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); List<Integer> list = new ArrayList<>(); int up = n > m ? m : n; printList(m,up,list); } }