试题 D: 数的分解

试题 D: 数的分解
【问题描述】
把 2019 分解成 3 个各不相同的正整数之和,并且要求每个正整数都不包
含数字 2 和 4,一共有多少种不同的分解方法?
注意交换 3 个整数的顺序被视为同一种方法,例如 1000+1001+18 和
1001+1000+18 被视为同一种。
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一
个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

思路

1.每个正整数都不包含数字 2 和 4。
2.交换 3 个整数的顺序被视为同一种方法。
对于条件1,可以自己写一个函数将数字逐位分解,在判断是否是2或4,
3.对于条件2,只要保证三个数是从小到大排列的,那么就不会出现重复问题。

 

Ans = 40785

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
#include<set>
using namespace std;
const int inf=0x3fffffff;
const int maxn=10010;

bool judge(int n){
	
	while(n){
		
		int a=n%10;
		
		if(a==2||a==4) return false;
		
		n/=10;
	}
	
	return true;
}

int main(){

	// ios::sync_with_stdio(false);
	// cin.tie(0),cout.tie(0);
	long long res=0;
	
	int n=2019;
	for(int a=1;a<n/3;a++){
		
		for(int b=a+1;b<2019/2;b++){
			
			for(int c=b+1;c<=2019/1;c++){
				
				if(a+b+c==2019){
//					cout<<a<<" "<<b<<" "<<c<<endl;	

					if(judge(a)&&judge(b)&&judge(c))
					res++;			
				}
			}
		}
	}
	
	cout<<res<<endl;
	
	return 0;
}
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
#include<set>
using namespace std;
const int inf=0x3fffffff;
const int maxn=10010;

bool judge(int n){
	
	while(n){
		
		int a=n%10;
		
		if(a==2||a==4) return false;
		
		n/=10;
	}
	
	return true;
}

int main(){

	// ios::sync_with_stdio(false);
	// cin.tie(0),cout.tie(0);
	long long res=0;
	
	int n=2019;
	for(int a=1;a<n/3;a++){
		
		if(judge(a)==false) continue;
		
		for(int b=a+1;b<2019/2;b++){
			
			if(judge(b)==false) continue;
				
			int c=2019-a-b;
				
				if(judge(c)==false) continue;
				if(c>b&&a+b+c==2019){
					res++;			
				}
			}
		}
	
	
	cout<<res<<endl;
	
	return 0;
}
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
#include<set>
using namespace std;
const int inf=0x3fffffff;
const int maxn=10010;

bool judge(int n){
	
	while(n){
		
		int a=n%10;
		
		if(a==2||a==4) return false;
		
		n/=10;
	}
	
	return true;
}

int main(){

	// ios::sync_with_stdio(false);
	// cin.tie(0),cout.tie(0);
	long long res=0;
	
	int n=2019;
	for(int a=1;a<n/3;a++){
		
		if(judge(a)==false) continue;
		
		for(int b=a+1;b<n/2;b++){
			
			if(judge(b)==false) continue;
				
			int c=2019-a-b;
				
				if(judge(c)==false) continue;
				if(c>b&&a+b+c==2019){
					res++;			
				}
			}
		}
	
	
	cout<<res<<endl;
	
	return 0;
}

wrong answer   

map 映射 b+c 的值可能有多个

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
#include<set>
using namespace std;
const int inf=0x3fffffff;
const int maxn=10010;

map<int,int> mp;

bool judge(int n){
	
	while(n){
		
		if( n%10==2 || n%10==4 ) return false;
		
		n/=10;
	}
	
	return true;
}

int main(){

	// ios::sync_with_stdio(false);
	// cin.tie(0),cout.tie(0);
	int n=2019;
	for(int b=2;b<n/2;b++){
		
		if(judge(b)==false) continue;
		for(int c=b+1;c<n;c++){
			
			if(judge(c)==false) continue;
			
			mp[b+c]=b;
			
			cout<<b<<" "<<c<<" "<<b+c<<endl;
		}
	}
	
	long long res=0;
	for(int a=1;a<n/3;a++){
		
		if(judge(a)==false) continue;
		
		int idx=n-a;
		if(mp.find(idx)==mp.end()) continue;
		
		int b=mp[idx];
		int c=mp[idx]-b;
		
//		cout<<a+b+c<<endl;
		
		res++;
	}
		
	cout<<res;

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值