1 条题解

  • 0
    @ 2025-4-7 21:41:58

    C++ :

    #include <iostream>
    using namespace std;
    unsigned long long josephus(unsigned long long n){
    	if(n==1)
    		return 1;
    	if(n%2==0)
    		return 2*josephus(n/2)-1;
    	else
    		return 2*josephus(n/2)+1;
    }
    int main(){
    	for(unsigned long long n;cin>>n;)
    		cout<<josephus(n)<<endl;
    	return 0;
    }
    

    Java :

    import java.util.*;
    import java.math.*;
    public class Main {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		 BigInteger two= new BigInteger("2");
    		 BigInteger one= new BigInteger("1");
    		BigInteger n=null;
    		BigInteger x=null;
    		while(in.hasNext()){
    			n = new BigInteger(in.next());
    			x= (n.subtract(mm(m(n)))).multiply(two).add(one);
    			System.out.println(x.toString());
    		}
    
    	}
      public static long m(BigInteger a){
    	  BigInteger z= new BigInteger("0");
    	  BigInteger two= new BigInteger("2");
        long s=0;
        while(a.divide(two).compareTo(z)!=0){
        	s++;
        	a=a.divide(two);
        }
      
    	return s;
    }
      public static BigInteger mm(long n){
    	  BigInteger sum= new BigInteger("1");
    	  BigInteger two= new BigInteger("2");
    	  for(int i=0;i<n;i++){
    		  sum = sum.multiply(two);
    	  }
    	  return sum;
      }
    }
    
    
    • 1

    信息

    ID
    2128
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者