题目要求:双倍超立方数是指一个正整数可以正好被拆分为两种不同的a^3+b^3的方式,其中a,b均为整数且0<a<=b。对于任何一个指定的 int n, 返回所有的小于等于n的双倍超立方数的个数。 #include <iostream> #include <string> #include <map> #include <vector> #include <iterator> using namespace std; struct Cubic { Cubic(){} Cubic(long rd, long cub):radix(rd), cubic(cub){} long radix; long cubic; }; int main() { long N; cout<<"Please Input n: "<<endl; cin >> N; //N = 475574; vector<Cubic> CubicVec; for (int i = 1, cub = 1; cub < N; ) { Cubic temp(i, cub); CubicVec.push_back(temp); ++i; cub = i * i * i; } map<long, vector<Cubic> > CubicMap; int len = CubicVec.size(); for (int i = 1; i < len; ++i) { for (int j = i + 1; j < len; ++j) { long temp = CubicVec[i].cubic + CubicVec[j].cubic; if (temp > N) continue; CubicMap[temp].push_back(CubicVec[i]); CubicMap[temp].push_back(CubicVec[j]); } } int cnt = 0; for (map<long, vector<Cubic> >::iterator it = CubicMap.begin(); it != CubicMap.end(); ++it) { if (it->second.size() > 2) { const vector<Cubic>& tempVec = it->second; for (vector<Cubic>::const_iterator vit = tempVec.begin(); vit < tempVec.end(); ) { Cubic temp1 = *vit++; Cubic temp2 = *vit++; cout<<temp1.radix<<"^3 + "<<temp2.radix<<"^3 = "<< it->first<<endl; } cout<<endl; ++cnt; } } cout<<"Total Twice Super Cubic Number: "<<cnt<<endl; return 0; }