poj1338(啊这题我要死了)

来源:互联网 发布:成都网络外包公司 编辑:程序博客网 时间:2024/04/30 02:25


第一次的代码 存在很大的问题 因为没办法控制规模 所以产生了溢出


#include<stdio.h>#include<iostream>#include<algorithm>#include<math.h>using namespace std;int main(){int a, b, c, i,n;double num[2000000];double sum;//首先找到所有的 只有2 3 5为因子的数字 一直找到1500num[0] = 1;i = 1;for (a = 0; a <= 30; a++){for (b = 0; b <= 17; b++){for (c = 0; c <= 12; c++){sum = pow((double)2, a)*pow((double)3, b)*pow((double)5, c);if (sum != 1){num[i] = sum;i++;}}}}//排序sort(num, num + i);//下面读取数字while (true){cin >> n;if (n == 0){break;}cout << num[n - 1] << endl;}return 0;}

第二个方法吧 按顺序来的 但是似乎超时了 网上有建议打表的  可是那样很low啊!

算法还是没变化 就是投机取巧了吧

#include<stdio.h>#include<iostream>#include<algorithm>#include<math.h>using namespace std;int main(){int num[1501];int i = 1;int currentNum = 1,tmp,n;num[0] = 1;while (i <= 1500){if (((currentNum % 2) == 0) || ((currentNum % 3) == 0) || ((currentNum % 5) == 0)){//判定是否是dirtyNumbertmp = currentNum;while (tmp % 2 == 0){tmp = tmp / 2;}while(tmp % 5 == 0){tmp = tmp / 5;}while(tmp % 3 == 0){tmp = tmp / 3;}if (tmp == 1)//除开了{num[i] = currentNum;i++;}}currentNum++;}while (true){cin >> n;if (n == 0){break;}cout << num[n - 1] << endl;}return 0;}

第三次的代码

 似乎是第一种方法  还是用因子为主 但是我之前的第一种方法好乱啊!主要是不按照顺序来 数字最终无法排序 造成了 时间的炒鸡复杂度!

看了网上大神的方法 得出了新的思路。

下标玩的是心跳啊!!!!!!!!真的是心跳啊!!!大神的世界我不懂啊!!!!!!!!!!!!!!!!!


#include<stdio.h>#include<iostream>#include<algorithm>#include<math.h>using namespace std;int min(int a, int b, int c);int min(int a, int b, int c){int t = a;if (b < t){t = b;}if (c < t){t = c;}return t;}int main(){int ans[1501];int a = 1, b = 1, c = 1,i=2,n;ans[1] = 1;while (i <= 1500){ans[i] = min(ans[a] * 2, ans[b] * 3, ans[c] * 5);if (ans[i] == (ans[a] * 2)){a++;}if (ans[i] == (ans[b] * 3)){b++;}if (ans[i] == (ans[c] * 5)){c++;}i++;}while (true){cin >> n;if (n == 0){break;}cout << ans[n] << endl;}return 0;}








这里要注意啊!!!!!!!!!!!

我觉得这题太tm难啊!!!!!

好过分的啊!!!!!!!!!!

这里必须是if不能使else啊!!!!!!!

因为你要考虑是不是重复的情况啊!!!

刚好相等的时候 就要++啊!!!!!!

if (ans[i] == (ans[a] * 2)){a++;}else if (ans[i] == (ans[b] * 3)){b++;}else{c++;}


0 0
原创粉丝点击