本文主要是介绍Jason and Number,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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
Sample Output
#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;
}
这篇关于Jason and Number的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!