1 条题解
-
0
C :
#include<stdio.h> int main() { int t; scanf("%d",&t); while(t--) { int n,cnt=0; scanf("%d",&n); while(n!=1) { cnt++; if(n%2==1) cnt++; n/=2; } cnt++; printf("%d\n",cnt); } return 0; }C++ :
#include <bits/stdc++.h> #define int long long using namespace std; const int N=1e6+10; typedef pair<int,int> PII; typedef priority_queue<int , vector<int>, greater<int>> minqueue; //从小到大 queue typedef priority_queue<int, vector<int>, less<int>> maxqueue; //从大到小 queue int gcd(int a, int b){ //最大公因数 return b ? gcd(b, a % b) : a; } int lcm(int a, int b){ //最小公倍数 return a * b / __gcd(a, b); } int qmi(int base, int power, int p) //快速幂求余 { int result = 1; while (power > 0) { if (power & 1) result = result * base % p; base = base * base % p ; power >>= 1; } return result % p; } void solve() { int n; cin>>n; int ans=0; while(n>0) { if(n%2==0) n/=2; else n-=1; ans++; } cout<<ans<<'\n'; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; t=1; cin>>t; while(t--) { solve(); } return 0; }
- 1
信息
- ID
- 721
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 10
- 标签
- 提交数
- 7
- 已通过
- 4
- 上传者