public static int myFindABC(){int total = 0;
int sum = 0;
//HashMaphashMap = new HashMap<>();
for (int i=100;i<999;i++){int a = i/100;//百位
int b = i%100/10;//十位
int c = i%100%10;//个位
if(a==b||b==c||a==c){continue;
}
for (int j=999;j>99&&i+j<10000&&i+j>999;j--){// if(j// break;
// }
int a2 = j/100;//百位
int b2 = j%100/10;//十位
int c2 = j%100%10;//个位
if(a2==b2||b2==c2||a2==c2||a2==a||a2==b||a2==c||b2==a||b2==b||b2==c||c2==a||c2==b||c2==c){continue;
}
//if(i+j>999&&i+j<10000){sum = i+j;
int a3 = sum/1000;
int b3 = sum%1000/100;
int c3 = sum%100/10;
int d3 = sum%100%10;
if(a3==b3||a3==c3||a3==d3||b3==c3||b3==d3||c3==d3||
a3==a||a3==b||a3==c||a3==a2||a3==b2||a3==c2||
b3==a||b3==b||b3==c||b3==a2||b3==b2||b3==c2||
c3==a||c3==b||c3==c||c3==a2||c3==b2||c3==c2||
d3==a||d3==b||d3==c||d3==a2||d3==b2||d3==c2
){continue;
}
System.out.print(i+" + "+j+" = "+sum);
total ++;
System.out.println();
}
}
return total;
}
测试方法:
public static void main(String[] args) {System.out.println(myFindABC());
}
C++实现#include#includeusing namespace std;
int s[10];
bool get_Clength(int x)
{int leng=0;
int d = 0;
while(x)
{d = x % 10;
x /= 10;
if(s[d] == 0){s[d] = 1;
}else{break;
}
leng++;
}
if(leng != 4){return false;
}
return true;
}
bool get_ABlength(int x)
{int leng=0;
int d = 0;
while(x)
{d = x % 10;
x /= 10;
if(s[d] == 0){s[d] = 1;
}else{break;
}
leng++;
}
if(leng != 3){return false;
}
return true;
}
int main(){int total = 0;
for(int i = 100; i< 1000; i ++){
for(int j = 1000 - i; j< 1000; j ++){memset(s, 0, sizeof(s));
if (get_ABlength(i) && get_ABlength(j) && get_Clength(i + j)){printf("%d + %d = %d\n", i, j, i + j);
total++;
}
}
}
cout<<"total number:"<
int s[10]用来确定数字处于0到9之间不重复
确保C是四位数,并且b是三位数,所以j从1000-i到1000
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧