数据结构---填数字

在这里插入图片描述

JAVA实现

public static int  myFindABC(){
        int total = 0;
        int sum = 0;
        //HashMap<Integer,Integer> hashMap = 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<i){
//                    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 <iostream>
#include <string.h>
using 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:"<<total;

}

int s[10]用来确定数字处于0到9之间不重复
确保C是四位数,并且b是三位数,所以j从1000-i到1000

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值