写一个函数,例如:给你的 a b c 则输出 abc acb bac bca cab cba
import java.util.ArrayList;
import java.util.List;
public class NumTest {
public static void main(String[] args) {
String s=”ABCD”;//原字符串
List result = list(s, ”");//列出字符的组合,放入result
System.out.println(result.size());;
System.out.println(result);
}
/**
* 列出基础字符串(base)的所有组合
* @param base 以该字符串作为基础字符串,进行选择性组合。
* @param buff 所求字符串的临时结果
* @param result 存放所求结果
*/
public static List list(String base,String buff){
List result = new ArrayList();//存放结果信息。
if(base.length()<=0){
result.add(buff);
}
for(int i=0;i
List temp = list(new StringBuilder(base).deleteCharAt(i).toString(),buff+base.charAt(i));
result.addAll(temp);
}
return result;
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static void main(String[] args) {
String s=”ABCD”;//原字符串
List result = new ArrayList();//存放结果信息。
list(s, ”", result);//列出字符的组合,放入result
System.out.println(result.size());;
System.out.println(result);
}
/**
* 列出基础字符串(base)的所有组合
* @param base 以该字符串作为基础字符串,进行选择性组合。
* @param buff 所求字符串的临时结果
* @param result 存放所求结果
*/
public static void list(String base,String buff,List result){
if(base.length()<=0){
result.add(buff);
}
for(int i=0;i
list(new StringBuilder(base).deleteCharAt(i).toString(),buff+base.charAt(i),result);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
写一个函数,给你一个字符串 倒序输出来
public String getString(String str){
if(str!=null){
String newStr = “”;
for(int i=0;i
{
char c = str.charAt(str.length()-1-i);
newStr = newStr + c;
}
return newStr;
}else{
return null;
}
}
不使用中间变量 把两个变量的值互换
int a=10;
int b=100;
a=a*b;
b=a/b;
a=a/b;
System.out.print(“a=”+a+” b=”+b);
折半查找
public class Test
{
public static int[] data = { 12, 15, 20, 10, 19, 3, 89, 32, 39, 47, 55 }; // 原始数据
public static int counter = 1; // 计数器
public static int len = data.length;
public static void main(String args[])
{
//要查找的数
int keyValue = 89;
Test t = new Test();
boolean b = t.BinarySearch(keyValue);
if(b)
{
// 输出查找次数
System.out.println(“Search Time = ” + counter);
}else
{
// 输出没有找到数据
System.out.println(“No Found!!”);
}
}
// —————————————————
// 折半查找法
public static boolean BinarySearch(int keyValue)
{
int left; // 左边界变量
int right; // 右边界变量
int middle; // 中位数变量
System.out.println(“数据长度:”+len);
left = 0;
right = len – 1;
while (left <= right)
{
//由于源数据不是顺序的,需先进行排序
int temp;
for(int i=0;i
{
for(int j=0;j
{
if(data[j]>data[j+1])
{
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
}
}
}
middle = (left + right) / 2;
// 欲查找值较小
if (keyValue < data[middle])
{
right = middle – 1; // 查找前半段
}
// 欲查找值较大
else if (keyValue > data[middle])
{
left = middle + 1; // 查找后半段
}
// 查找到数据
else if (keyValue == data[middle])
{
System.out.println(“data[" + middle + "] = ” + data[middle]);
return true;
}
counter++;
}
return false;
} }
1. 将this is a test 转化为This Is A Test
String str=”this is a man”;
char c[]=new char[str.length()];
str.getChars(0,str.length(),c,0);
if(c[0]>=’a'&&c[0]<=’z')
{
c[0]=(char)(c[0]-32);
}
for(int i=1;i<=c.length-1;i++)
{
if(c[i]==’ ‘)
{
c[i+1]=(char)(c[i+1]-32);
}
}
str=new String(c);
System.out.print(str);
2. 将This is a test倒着输出tset a si sihT (用StringBuffer)
String str=”this is a man”;
StringBuffer sb=new StringBuffer(str.length());
for(int j = str.length()-1;j>=0;j–)
{
sb.append(str.charAt(j));
}
System.out.print(sb);
3. 用递归求N!=N*(N-1)*(N-2)*…*2*1;
long digui(int i)
{if(i==0||i==1)
{
return 1;
}
else
{
return i*digui(i-1);
}