您的位置:首页 > 其它

Jason and Number

2014-04-13 17:38 405 查看

Problem Description

  Given a number N, Jason will connect from the first prime number to the Nth prime to form a new number.

  Unfortunately, Jason has gone crazy, and he will repeat operation:

  1、delete the number in the even position;

  2、delete the number in the odd position.

  Now, Jason wants to know what the last number is.

  

  For example, if N is equal to 6, the new number is 23571113.

  After deleting the number in even position, the new number is 2511

  After deleting the number in odd position, the new number is 51

  After deleting the number in even position, the new number is 5

  Therefore, the last number is 5.



Input

  The input will consist of a series of test cases, and each test case contains an integers N (N <= 20000).



Output

  For each test case, output what the last number is.



Sample Input

6



Sample Output

5

//解题报告:

在字符串长度为[1,3)时 输出第1个子符;

在字符串长度为[3,11)时 输出第3(1+2^1)个子符;

在字符串长度为[11,23)时
输出第11(3+2^3)个子符;

在字符串长度为[43,171)时
输出第43(11+2^5)个子符;

在字符串长度为[173,683)时
输出第171(43+2^7)个子符;

......;

//标程:

#include<stdio.h>

#include<cmath>

#include<algorithm>

#include<string>

#include<iostream>

#include<string.h>

#define Max 300010

using namespace std;

int prime[Max],cnt,vis[Max];

void prim()

{

cnt=0;

int i,j;

memset(vis,0,sizeof(vis));

for(i=2;;i++)

{

if(!vis[i])

{

prime[++cnt]=i;

if(cnt>20010) return;

for(j=2*i;j<=Max;j+=i)

vis[j]=1;

}

}

}

void tostr(string &str,int x)

{

while(x)

{

str+=(x%10+'0');

x/=10;

}

}

int main()

{

// freopen("a.txt","r",stdin);

prim();

int n,i,j;

int a[15];

a[0] = 1;

for(i = 1; i <= 10; i ++)

a[i] = a[i-1] + (1<<(2*i-1));

while(scanf("%d",&n)==1)

{

string str="";

for(i=n;i>=1;i--)

tostr(str,prime[i]);

reverse(str.begin(),str.end());

int len = str.size();

for(i = 0; i <= 10; i ++)

if(len < a[i]) break;

cout << *(str.begin() +a[i-1] - 1) <<endl;

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: