poj 2591

原题:

                                                                                         Set Definition
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 4496Accepted: 1944

Description

Set S is defined as follows:
(1) 1 is in S;
(2) If x is in S, then 2x + 1 and 3x + 1 are also in S;
(3) No other element belongs to S.

Find the N-th element of set S, if we sort the elements in S by increasing order.

Input

Input will contain several test cases; each contains a single positive integer N (1 <= N <= 10000000), which has been described above.

Output

For each test case, output the corresponding element in S.

Sample Input

100
254

Sample Output

418
1461

分析:

    又是一道简单题, 我用的是打表的方法,将答案存在一个数组a 中. 在计算时用 i 和 j 分别记录下一个2*x + 1 和3*x + 1 的x 的下标, 比较2*x + 1 和3*x + 1 的大小, 小的存入a 数组, 这样a 数组中的数就已经从大到小排好序了. 最后根据输入只要输出答案就可以了!

参考代码:

     //2591
#include <iostream>
using namespace std;
unsigned int a[10000002];
int n;
int main(void)
{
    int i,j,k,t1,t2;
    a[1] = 1;
    i = j = 1;
    for(k=2;k<=10000001;k++)
      {
          t1 = 2*a[i]+1;
          t2 = 3*a[j]+1;

          a[k] = min(t1,t2);
          if(a[k]==t1)
           {
              i++;
           }
          if(a[k]==t2)
           {
              j++;
           }
      }

    while(cin>>n)
     {
        cout<<a[n]<<endl;
     }

    system("pause");
    return 0;
}

我用了157MS,看到有的高手30MS, 请到访的高手赐教,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值