[编程题]计数器
• 热度指数：1548 时间限制：C/C++ 1秒，其他语言2秒 空间限制：C/C++ 256M，其他语言512M
• 算法知识视频讲解

##### 输入描述:
`输入为时刻t，一个整形数字。0<t<1e12`

##### 输出描述:
`计数器显示的值。`

`4`

## 输出

`6`
```//非迭代版本
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long time = scanner.nextLong();
long value = 3;
while(time - value > 0){
time -= (value);
value <<= 1;
}
value -= time - 1;
System.out.println(value);
}
}

```

```#include <iostream>
using namespace std;
int main(void){
//1e12大于int类型范围
long long t, time = 3;
cin>>t;
while(t > time){
t -= time;
time *= 2;
}
cout<<time+1-t<<endl;
return 0;
}```

```#include <bits/stdc++.h>
using namespace std;
int main(){
long t,v=3,k=1;
cin>>t;
while(t>=k){
k += v;
v<<=1;
}
cout<<k-t<<endl;
return 0;
}```

```#include <bits/stdc++.h>
using namespace std;
int main(){
int t,velue=3,index=1;
cin>>t;
while(index<=t){
index+=velue;
velue<<=1;
}
cout<<index-t;
return 0;
}

```

```#include<bits/stdc++.h>
using namespace std;
int main()
{
long long t,n=3,m=1,f=3;
cin>>t;
while(m<t)
{
m=m+f;
f=2*f;
}
if(m==t)
cout<<f<<endl;
else
{
f=f/2;
m=m-f;
long long res=f-(t-m);
cout<<res<<endl;
}
return 0;
}```

```import java.io.BufferedReader;

public class Main {
public static void main(String[] args) throws Exception {
long sum = 4L, n = 6L;
while (t >= sum) {
sum += n;
n *= 2;
}
System.out.println(sum - t);
}
}```

```int main() {
long t = 0;
cin >> t;     long start =  1;
while (start<=t)
{
start = (start *2 +2);
}
printf("%ld", start - t);     return 0;
}
```

import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
long m=sc.nextLong();
double n= Math.ceil(Math.log(m/3.0+1)/Math.log(2));
System.out.println((long)(3*Math.pow(2, n-1)-m+3*(Math.pow(2, n-1)-1)+1));//里面可以合并
}
}

import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
long m=sc.nextLong();
double n= Math.ceil(Math.log(m/3.0+1)/Math.log(2));
System.out.println((long)(6*Math.pow(2, n-1)-m-2));
}
}

#include <iostream>

int main()
{
long int t,tTemp=1;
int n=1;
std::cin >> t;
while ((t-(((tTemp <<n)-1)*3))>0)
{
n++;
}
t = t - ((tTemp <<( n-1)) - 1) * 3;
std::cout << (3*(tTemp <<(n-1)))-t+1<< std::endl;
}

```#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
long t;
cin >> t;
long x = log(1 + float(t) / 3) / log(2);
long end = 3 * (pow(2, x + 1) - 1);
cout << (end - t + 1) << endl;
}
```

def init(n):
if n == 1:
return 1
else:
return 2*init(n-1)+2
m = int(input())
n = 1
while(True):
if m < init(n):
break
else:
n += 1
init_time = init(n-1)
init_value = init_time + 2
result = init_value - (m - init_time)
print(result)

#######################################################计数器
import math
s = int(input())
#先计算s属于第几个计数器
n = math.ceil(math.log(s/3+1,2))   #要向上取整
#再计算是第几个数
print(3*(2**(n-1)) - s+ 3*(2**(n-1))-2)

```n=int(input().strip())
i=1 count=3 k=0 while count<=n: if n<=count+3*(2**(i-1)):
k = 3*2 ** (i - 1)- n+count+1  count=3*(2**i-1)
i+=1 print(k)```

```import math
t = int(raw_input().strip())
n = math.ceil((math.log(t/3.0+1,2)))
start = 3*(2**(n-1))
t -= 3*(2**(n-1)-1)
print int(start-t+1)```

```import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long time = sc.nextLong();
calDisplayNum(time, 3);
}

private static void calDisplayNum(long time, long value) {
long result = time - value;
if (result <= 0) {
result = value - time + 1;
System.out.println(result);
} else {
calDisplayNum(time-value, value<<1);
}
}
}
```

16条回答 1525浏览

# 通过挑战的用户

• 2019-12-13 18:42:19
• 2019-12-11 19:27:15
• 2019-12-11 16:07:24
• 2019-12-07 15:11:45
• 2019-12-01 19:25:23

# 相关试题

• 扫描二维码，关注牛客网

• 下载牛客APP，随时随地刷题