/**
* 打印1-19个数中,所有和等于20的可能性
*/
@Test
public void calcTwenty() {
for(int i=1;i<20/2;i++) {
calcTwenty("",0,i);
}
System.out.println("总共"+total);
}
public void calcTwenty(String prefix,int count,int n) {
int t=20-count-n;
if(t<=n) return;
if(prefix.equals("")) {
prefix=n+"";
} else {
prefix+="+"+n;
}
System.out.printf("%s+%d%n",prefix,t);
total++;
for(int i=n+1;i<20;i++) {
calcTwenty(String.format("%s", prefix),count+n,i);
}
}
/**
*截取字符串字节数
*/
@Test
public void substrbyte() {
String str="hi中国hello";
int length=5;
StringBuilder sb=new StringBuilder();
int count=0;
for(int i=0;i<str.length();i++) {
String c=str.substring(i,i+1);
count+=c.getBytes().length;
if(count<=length) {
sb.append(c);
} else {
break;
}
}
System.out.println(sb.toString());
}
/**
* 编写一个程序,输入一个 n, 输出从1到这个数字之间的出现的1的个数,比如f(13)等于6; f(9)等于1;
*/
@Test
public void countDigitOne () {
int n=20;
int count=0;
for(int i=1;i<=n;i++) {
int t=i;
while(t>0) {
int m=t%10;
if(m==1) count++;
t=t/10;
}
}
System.out.println(count);
}
/**
* 大整数加法
*/
@Test
public void bigInt() {
String num1="123456789987654321123456789987654321";
String num2="987654321123456789123456789987654321";
String sum=add(num1,num2);
System.out.printf("%s + %s = %s",num1,num2,sum);
}
private String add(String num1,String num2) {
int n1,n2,n,c=0;
StringBuilder result=new StringBuilder();
for(int i=0,j=0;i<num1.length() || j<num2.length();i++,j++) {
if(i<num1.length()) {
n1=num1.charAt(num1.length()-i-1)-'0';
} else {
n1=0;
}
if(j<num2.length()) {
n2=num2.charAt(num2.length()-i-1)-'0';
} else {
n2=0;
}
n=(n1+n2+c)%10;
c=(n1+n2+c)/10;
result.append(n);
}
if(c!=0) result.append(c);
return result.reverse().toString();
}
/***
* 大整数乘法
*/
@Test
public void bigMultiply() {
String num1="1111187986711567567111143511234234324343424";
String num2="11346798111111575673453535398796535534511111";
int n1,n2,n;
int c=0;
String sum="";
StringBuilder result=new StringBuilder();
for(int i=0;i<num2.length();i++) {
n1=num2.charAt(num2.length()-i-1)-'0';
for(int j=0;j<num1.length();j++) {
n2=num1.charAt(num1.length()-j-1)-'0';
n=(n1*n2+c)%10;
c=(n1*n2+c)/10;
result.append(n);
}
if(c!=0) {
result.append(c);
c=0;
}
result.reverse();
for(int k=0;k<i;k++) {
result.append(0);
}
sum=add(sum,result.toString());
result.delete(0, result.length());
}
System.out.printf("%s * %s = %s ",num1,num2,sum);
}
/**
* 全排列1,2,2,3,4,5 --4不能在第三位,3与5不能相邻
*/
@Test
public void pailei() {
long begin = System.currentTimeMillis();
int count = 0;
int[] a = new int[5];
outer: for (int i = 122345; i <= 543221; i++) {
for (int j = 0; j < 5; j++) {
a[j] = 0;
}
int k = 100000, m = 1000000, n, t = 0;
for (int j = 0; j < 6; j++) {
n = (i % m) / k;
if (n > 5 || n == 0) {
i=i+k-1;
continue outer;
}
if (j == 2 && n == 4)
continue outer;
if (n == 3 && t == 5 || n == 5 && t == 3)
continue outer;
t = n;
a[n - 1]++;
k /= 10;
m /= 10;
}
for (int j = 0; j < 5; j++) {
if (a[j] == 0)
continue outer;
}
if (a[1] != 2)
continue;
System.out.println(i);
count++;
}
System.out.println("总共:"+count);
long end = System.currentTimeMillis();
System.out.println("耗时"+(end - begin) + "ms");
}