acm JNOJ 大数

1107: Josephus

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 35   Solved: 12
[ Submit][ Status][ Web Board]

Description

Josephus problem is named after Flavius Josephus, a Jewish historian living in the  1st century. According to Josephus' account of the siege of Yodfat, he and his 40  soldiers were trapped in a cave by Roman soldiers. They chose suicide over  capture, and settled on a serial method of committing suicide by drawing lots.  Josephus states that by luck or possibly by the hand of God, he and another man  remained until the end and surrendered to the Romans rather than killing  themselves. This is the story given in Book 3, Chapter 8, part 7 of Josephus' The  Jewish War (writing of himself in the third person).  In this case, we simplify the problem that the prisoners are label from 1 to n, in each  circle, we will kill every second remaining person until one survived. We want to you  to output the number label of remaining person.

Input

First line contains a single integer t, indicates the number of cases.  Followed by T lines indicates t strings contains of less than 100 characters. Indicates the binary
form of n.

Output

For each test case output a single line indicates the label of the lucky man!

Sample Input

2
1010
10

Sample Output

5
1

HINT

Source

#include<cstdio>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;

#define  N  100;
char person[100];
long Bigsum[10]={0};
int main(){
	int t,length,i,j;
	cin>>t;
	getchar();
	while(t--){
		for(i=0;i<10;i++){
			Bigsum[i]=0;
		}
		gets(person);
		length=strlen(person);
		//printf("%d,%d,%d,%d\n",person[0]-'0',person[1]-'0','0','1');
		j=0;
		for(i=1;i<length;i++){
			for(j=0;j<9;j++){
				Bigsum[j]*=2;
				//printf("chen%d\n",Bigsum[j]);
			}
				Bigsum[0]+=(person[i]-'0');
				//printf("000%d\n",Bigsum[0]);
			for(j=0;j<9;j++){
				while(Bigsum[j]>=100000000){
					//printf("Jin%d\n",Bigsum[j]);
				Bigsum[j]-=100000000;
				Bigsum[j+1]+=1;
				}
			}		
		}
		for(j=0;j<9;j++){
				Bigsum[j]*=2;
			}
		Bigsum[0]+=(person[0]-'0');	
		for(j=0;j<9;j++){
				while(Bigsum[j]>=100000000){
				Bigsum[j]-=100000000;
				Bigsum[j+1]+=1;
				}
			}	
		//printf("%d\n",Bigsum[0]);
		for(j=9;j>=0&&Bigsum[j]==0;j--){
			//printf("%d",Bigsum[i]);
		}
		printf("%d",Bigsum[j--]);
		for(;j>=0;j--)printf("%08d",Bigsum[j]);
		printf("\n");
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值