题解 | #憧憬#

憧憬

https://ac.nowcoder.com/acm/contest/11216/A

#include<iostream>
#define ll long long 
using namespace std;
ll a[10005];
ll n,m;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
    ll ans2=0;
	while(n--){
		cin>>m;
		ll k=0;
		int s=-1;
		while(m){
        	//转为二进制数并记录
			a[k++]=m%2;
            //记录第一个非0的位置,后面不用再从头找了
			if(s==-1&&a[k-1])	s=k-1;
			m/=2;
		} 
		ll ans=0;
        //s==-1说明,答案是0,不用算了
		if(s!=-1){
        	//二进制转为十进制
			while(s<k){
				ans=2*ans+a[s++];
			}
			ans2+=ans;
		}
	}
    cout<<ans2<<endl;
	return 0;
}
全部评论

相关推荐

投递长鑫存储等公司8个岗位
点赞 评论 收藏
分享
白火同学:大二有这水平很牛了,可以适当对关键信息加粗一点,比如关键技术、性能指标之类的。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务