比赛 |
EYOI暨SBOI暑假快乐赛2nd |
评测结果 |
WWWWWWWWWW |
题目名称 |
幂次计算 |
最终得分 |
0 |
用户昵称 |
┭┮﹏┭┮ |
运行时间 |
0.000 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2022-06-26 11:02:36 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
int n,n1,s,s1,s2;
long long a[30] = {1,2,4};
int main(){
freopen("mico.in","r",stdin);
freopen("mico.out","w",stdout);
for(int i = 3;i <= 29;i++){
a[i] = a[i-1] * 2;
}
while(cin>>n){
if(n == 0)break;
s = 0,s1 = 0,s2 = 0;
if(n == 1){
cout<<0<<endl;
continue;
}
for(int i = 0;i < 29;i++){
if(n >= a[i] && n <= a[i+1]){
n1 = n - a[i];
n = a[i+1] - n;
s1 = s+i+1;
s2 = s+i;
break;
}
}
if(n == 0){
cout<<s1<<endl;
continue;
}
if(n1 == 0){
cout<<s2<<endl;
continue;
}
for(int i = 29;i >= 0;i--){
if(n >= a[i]){
n -= a[i];
s1++;
}
}
for(int i = 29;i >= 0;i--){
if(n1 >= a[i]){
n1 -= a[i];
s2++;
}
}
cout<<min(s1,s2)<<endl;
}
return 0;
}