洛谷P1028数的计算
题目描述: 我们要求找出具有下列性质数的个数(包含输入的自然数n): 先输入一个自然数n(n≤1000),然后对此自然数按照如下方法进行处理: 1.不作任何处理; 2.在它的左边加上一个自然数,但该自然数不能超过原数的一半; 3.加上数后,继续按此规则进行处理,直到不能再加自然数为止. 输入: 6 输出 6 个人思路: 基本思路是递归应该没啥好说的。但是提交过程中有这样的情况: #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; long long ans = 0; void judge(int k){ if(k != 0){ for(int i = k;i > 0;i--){ ans++; judge(i/2); } } else return; } int main() { int n; cin >> n; int k = n / 2; judge(k); cout << ans + 1 << endl; return 0; } 这样暴力遍历枚举的话大概500可以过,但是题目给了1000…所以前五个数据AC后面就全给T了。 AC代码: #include<iostream> #include<cstring> #include<cstdio>