1 solutions
-
0
C++ :
#include<iostream> using namespace std; long long f(long long n) { if(n==1)return 1; else { long long k = f((n+1)/2); if(n%2) { if(k==1)return n; else return (k-1)*2; } else return 2*k; } } int main() { long long n; while(cin>>n) cout<<f(n)<<endl; return 0; }Java :
import java.io.BufferedInputStream; import java.util.Scanner; public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin=new Scanner(new BufferedInputStream(System.in)); la: while(cin.hasNext()){ int N=cin.nextInt(); if(N==1){System.out.println(1); continue la;} N-=1; int base=0; while(true) { if(N-Math.pow(2, base)<=0) { System.out.println(N*2); break; } else { N-=Math.pow(2, base); base++; } } // System.out.println(N2); } } }
- 1
Information
- ID
- 18783
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- (None)
- # Submissions
- 0
- Accepted
- 0
- Uploaded By