|
发表于 2022-1-17 13:56:20
|
显示全部楼层
- /*
- 答案:8581146
- 耗时:0.314319秒(八核)
- */
- #include <iostream>
- #include <algorithm>
- #include <omp.h>
- using namespace std;
- int v1[5] = { 1,10,13,32,44 };
- int v89[9] = { 4,16,20,37,42,58,85,89,145 };
- int S[10] = { 0,1,4,9,16,25,36,49,64,81 };
- int main(void)
- {
- double t = omp_get_wtime();
- int nCount = 0;
- #pragma omp parallel for shared(v1,v89,S) reduction(+:nCount)
- for (int n = 1; n <= 10000000; ++n)
- {
- int k = n;
- bool bFind = false;
- while (true)
- {
- int nSum = 0;
- while (k != 0)
- {
- nSum += S[k % 10];
- k /= 10;
- }
- if (binary_search(v1, v1 + 5, nSum))
- break;
- if (binary_search(v89, v89 + 9, nSum))
- {
- bFind = true;
- break;
- }
- k = nSum;
- }
- if (bFind)
- ++nCount;
- }
- t = omp_get_wtime() - t;
- cout << nCount << endl << t << endl;
- return 0;
- }
复制代码 |
|