// 判断一个字符串中字符两两不等 private static boolean diff(String str) { char[] cs = str.toCharArray(); for (int i = 0; i < cs.length - 1; i++) { for (int j = i + 1; j < cs.length; j++) { if (cs[i] == cs[j]) { return false; } } } return true; }---------------这样写复杂了。
本身数字很大算起来很久。public class Math1 { int [] array = new int[2000]; public Math1() { for(int i = 0;i < array.length; i++){ array[i] = 0; } } public boolean hundredRepeated(int num){ int x; int y; int z; x = num / 100; y = num /10 % 10; z = num % 10; if(x == y | x == z | y == z){ return false; } else{ return true; } } public boolean thousandRepeated(int num){ int p; int x; int y; int z; p = num / 1000; x = num / 100 % 10; y = num /10 % 10; z = num % 10; if(p == x | p == y | p == z | x == y | x == z | y == z){ return false; } else{ return true; } } public static void main (String[] args) { Math1 math = new Math1(); for(int i = 100;i < 200;i++){ for(int j = 100;j < 200;j++){ if(math.hundredRepeated(i) && math.hundredRepeated(j) & math.array[i + j] != 0){ System.out.println (i+"与"+j+":符合要求" + " 其和:"+ (i+j)); } else{ if(math.hundredRepeated(i) && math.hundredRepeated(j) && math.thousandRepeated(i+j)){ System.out.println (i+"与"+j+":符合要求" + " 其和:"+ (i+j)); } math.array[i + j] = 1; } } } } }
import java.util.ArrayList;
import java.util.List;
public class Test { /**
* @param args
*/
public static void main(String[] args) {
List<Integer> intList = new ArrayList<Integer>();
int g,s,b,q,sum;
for(int i=102;i<=987;i++){
g=i/1%10;//个位
s=i/10%10;//十位
b=i/100%10;//百位
if(g==s||g==b||s==b){
continue;
}
intList.add(i);
}
for(int i=0;i<intList.size()-1;i++){
for(int j=i+1;j<intList.size();j++){
sum=intList.get(i)+intList.get(j);
if(sum>=1234){
g=sum/1%10;//个位
s=sum/10%10;//十位
b=sum/100%10;//百位
q=sum/1000%10;//千位
if(g==s||g==b||g==q||s==b||s==q||b==q){
continue;
}
System.out.println(intList.get(i)+" + "+intList.get(j)+" = "+sum);
}else{
continue;
}
}
} }}
import java.util.ArrayList;
import java.util.List;
public class Test { /**
* @param args
*/
public static void main(String[] args) {
List<Integer> intList = new ArrayList<Integer>();
int g,s,b,q,sum;
for(int i=102;i<=987;i++){
g=i/1%10;//个位
s=i/10%10;//十位
b=i/100%10;//百位
if(g==s||g==b||s==b){
continue;
}
intList.add(i);
}
for(int i=0;i<intList.size()-1;i++){
for(int j=i+1;j<intList.size();j++){
sum=intList.get(i)+intList.get(j);
if(sum>=1023){
g=sum/1%10;//个位
s=sum/10%10;//十位
b=sum/100%10;//百位
q=sum/1000%10;//千位
if(g==s||g==b||g==q||s==b||s==q||b==q){
continue;
}
System.out.println(intList.get(i)+" + "+intList.get(j)+" = "+sum);
}else{
continue;
}
}
} }}
import java.io.FileWriter;public class NumTest {
/**
* 1、2个三位数相加,结果是一个4位数
*
* 2、2个三位数,必须每一个数字都不同
*
* 3、相加的结果,每一位数,也都不同
*
* 4、打印所有的可能
*
* @throws Exception
*/
public static void main(String[] args) throws Exception {
for (int num1 = 102; num1 <= 987; num1++) {
for (int num2 = (1023 - num1) >= 102 ? (1023 - num1) : 102; num2 <= 987; num2++) {
if (diff(num1 + "" + num2) && diff(String.valueOf(num1 + num2))) {
// System.out.println(num1 + " + " + num2 + " = " + (num1 +
// num2));
log(num1 + " + " + num2 + " = " + (num1 + num2) + "\r\n");
}
}
}
finish();
} private static File f = new File("text.txt"); private static FileWriter fw = null; // 记录到文件
private static void log(String str) throws Exception {
if (fw == null) {
fw = new FileWriter(f);
} fw.append(str);
} private static void finish() throws Exception {
fw.close();
} // 判断一个字符串中字符两两不等
private static boolean diff(String str) {
char[] cs = str.toCharArray();
for (int i = 0; i < cs.length - 1; i++) {
for (int j = i + 1; j < cs.length; j++) {
if (cs[i] == cs[j]) {
return false;
}
}
}
return true;
}
}
private static boolean diff(String str) {
char[] cs = str.toCharArray();
for (int i = 0; i < cs.length - 1; i++) {
for (int j = i + 1; j < cs.length; j++) {
if (cs[i] == cs[j]) {
return false;
}
}
}
return true;
}---------------这样写复杂了。
public Math1() {
for(int i = 0;i < array.length; i++){
array[i] = 0;
}
} public boolean hundredRepeated(int num){
int x;
int y;
int z;
x = num / 100;
y = num /10 % 10;
z = num % 10;
if(x == y | x == z | y == z){
return false;
}
else{
return true;
}
}
public boolean thousandRepeated(int num){
int p;
int x;
int y;
int z;
p = num / 1000;
x = num / 100 % 10;
y = num /10 % 10;
z = num % 10;
if(p == x | p == y | p == z | x == y | x == z | y == z){
return false;
}
else{
return true;
}
}
public static void main (String[] args) {
Math1 math = new Math1();
for(int i = 100;i < 200;i++){
for(int j = 100;j < 200;j++){
if(math.hundredRepeated(i) && math.hundredRepeated(j) & math.array[i + j] != 0){
System.out.println (i+"与"+j+":符合要求" + " 其和:"+ (i+j));
}
else{
if(math.hundredRepeated(i) && math.hundredRepeated(j) && math.thousandRepeated(i+j)){
System.out.println (i+"与"+j+":符合要求" + " 其和:"+ (i+j));
}
math.array[i + j] = 1;
}
}
}
}
}
只要把main里面的200改为1000就可以达到功效
请在CMD中运行否则卡死你。。