PAT继续(3n+1)猜想

xiaoxiao2021-02-28  5

筛一下

注意数组不能开小,因为可能因为连续奇数而连乘

#include <stdio.h> #include <algorithm> using namespace std; int num[200], n; bool a[10005]; int main() { scanf("%d", &n); for(int i = 0; i < n; ++i) scanf("%d", &num[i]); sort(num, num + n);// for(int i = 0; i < n; ++i)printf("%d ", num[i]); for(int i = 0; i < n; ++i) { int m = num[i]; while( m != 1 ) { if( m & 1 ) { m = (3 * m + 1) / 2; a[m] = true;//筛 } else { m /= 2; a[m] = true;//筛 }//printf("%d %d\n", i, a[7]); } } int c(0); for(int i = n - 1; i >= 0; --i) { if( a[num[i]] == false ) { if(c) printf(" %d", num[i]); else { ++c; printf("%d", num[i]); } } } return 0; } 题目来源,PAT1005

转载请注明原文地址: https://www.6miu.com/read-2400184.html

最新回复(0)