枚举—区间划分求双和数组

题目描述:
在这里插入图片描述
算法思想:

  • 等式变形:变原式为def*(bc+ca+ab)=abc(ef+fd+d*e) ,若用原式则可能需要进行浮点数的运算,由于浮点数不好操作,通过变形,进行整数运算
  • 划分区间:根据条件划分区间

在这里插入图片描述
全部代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int a,b,c,d,e,f,s,cnt=0;

int main(){
    
    
	scanf("%d",&s);
	for(a=1;a<=(s-3)/3;a++)
	for(b=a+1;b<=(s-a-1)/2;b++)		 
	for(d=a+1;d<=(s-3)/3;d++)
	for(e=d+1;e<=(s-d-1)/2;e++){
    
    
		c = s - a - b;
		f = s - d - e;
		int tmp0 = d*e*f*(b*c+c*a+a*b);
		int tmp1 = a*b*c*(e*f+f*d+d*e);
		if(tmp0==tmp1){
    
    
			cnt++;
			printf("%d:(%d,%d,%d) ",cnt,a,b,c);
			printf("(%d,%d,%d)\n",d,e,f);
		}
	}
	if(cnt==0)	printf("无解");
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/weixin_45666249/article/details/114758100