[Blue Bridge Cup] Cargo Stacking C++

insert image description here

problem solving ideas

This is a given cube volume, how many possible cube length, width and height are there. At the beginning, I wanted to directly enumerate the triple cycle violence, but the number is too large to run out, so I want to optimize it. Since the volume is obtained by length × height × width, then the length, width and height must be the factors of the volume. We can first find out all the factors of the volume and put them in the vector container, and then find three factors from it In the case where the volume can be obtained by multiplying numbers, the time complexity is much smaller, and it can be run out quickly

#include<bits/stdc++.h>
using namespace std;
#include<vector>
int main()
{
    
    
	long long sum = 0,num = 2021041820210418;
	vector<long long> v1;
	for(long long i = 1 ; i <= sqrt(num) ; i++){
    
    
		if(num%i==0){
    
    
			v1.push_back(i);
			long long j = num/i;
			if(j != i){
    
    
				v1.push_back(j);
			}
		}
	}
	
	vector<long long>::iterator a,b,c;
	for(a = v1.begin();a !=v1.end(); a++)
		for(b = v1.begin(); b != v1.end(); b++)
			for(c = v1.begin(); c != v1.end(); c++){
    
    
				if((*a)*(*b)*(*c) == num){
    
    
					sum++;
				}
			}
	cout<<sum<<endl;
	return 0;
	
}

Guess you like

Origin blog.csdn.net/qq_63524016/article/details/129244967