二进制表示中数字的偶数和奇数位的1的数量是多少?(What is the number of 1's at even and odd places of a number in binary representation?)
无论是按位还是64位范围内的整数函数,它们都是更快的实现方式。除了我实现的那个。
/* Find F(i)=abs(a(i)-b(i)) a(i)=number of 1's in even position b(i)=number of 1's in odd position for an integer i, where i fits in 64-bit */ //function calculate the above equation //returns the answer long long int F(long long int k) { //size of array is taken for 64-bit number int a[64]={0},i,a,b; long long int m; m=k; //convert long long int into binary for(i=63;i>-1;i--) { if(m==1||m==0) { a[i]=m; break; //exit the for loop } a[i]=m%2; //storing bit by bit m/=2; } // initialized with a value of zero a=0; b=0; //find first bit having 1 int f; for(i=0;i<64;i++) { if(a[i]==1) { f=i; break; } } //calculating the number of 1's in even and odd positions for(i=f;i<64;i++) { if(a[i]==1) { if((63-f)%2==0) { a++; //1's in even positions } else { b++; //1's in odd positions } } } //return the answer return abs(a-b); }
所以基本上我要做的是通过使用mod 2的简单方法转换其二进制表示中的整数。然后执行任务以从左到右找到其二进制表示中的第一个1,并且我们的指针在第一个数。 现在使用第一个索引计算奇数和偶数位置的1的数量。最后返回总偶数和奇数位置1的绝对差值。
Is their is any faster way to implement it whether it is bitwise or any function for an integer in the range of 64-bit.Except the one I've implemented.
/* Find F(i)=abs(a(i)-b(i)) a(i)=number of 1's in even position b(i)=number of 1's in odd position for an integer i, where i fits in 64-bit */ //function calculate the above equation //returns the answer long long int F(long long int k) { //size of array is taken for 64-bit number int a[64]={0},i,a,b; long long int m; m=k; //convert long long int into binary for(i=63;i>-1;i--) { if(m==1||m==0) { a[i]=m; break; //exit the for loop } a[i]=m%2; //storing bit by bit m/=2; } // initialized with a value of zero a=0; b=0; //find first bit having 1 int f; for(i=0;i<64;i++) { if(a[i]==1) { f=i; break; } } //calculating the number of 1's in even and odd positions for(i=f;i<64;i++) { if(a[i]==1) { if((63-f)%2==0) { a++; //1's in even positions } else { b++; //1's in odd positions } } } //return the answer return abs(a-b); }
So basically what I am trying to do is to convert the integer in its binary representation by simple method of using mod 2. Then a task is performed to find the first 1 in its binary representation for left to right and our pointer is on the first number. Now count the number of 1's in odd and even position using the index of first 1.Finally return the absolute difference of total even and odd place 1's.
原文:https://stackoverflow.com/questions/25814786
最满意答案
firebaseAuth.fetchProvidersForEmail(userEmail). addOnCompleteListener(new OnCompleteListener<ProviderQueryResult>() { @Override public void onComplete(@NonNull Task<ProviderQueryResult> task) { List<String> providerList = task.getResult().getProviders(); if (providerList.isEmpty()) { //Empty List means new user } else if (providerList.contains("facebook.com")) { //Facebook is the provider } } }
使用上面的代码来获取提供者。
firebaseAuth.fetchProvidersForEmail(userEmail). addOnCompleteListener(new OnCompleteListener<ProviderQueryResult>() { @Override public void onComplete(@NonNull Task<ProviderQueryResult> task) { List<String> providerList = task.getResult().getProviders(); if (providerList.isEmpty()) { //Empty List means new user } else if (providerList.contains("facebook.com")) { //Facebook is the provider } } }
Use above code to get providers.
相关问答
更多-
由于Facebook关闭了API 2.2,该插件于2017年3月27日停止工作。 此插件的最新版本已更新,以支持Facebook 2.8 API。 请参考: https : //moodle.org/plugins/auth_googleoauth2希望这会有所帮助。 It is a problem caused by a change in Facebook. Facebook deprecated their Graph API version 2.2. I got the answer from he ...
-
问题源于在我的部署脚本中使用不正确的环境变量url。 The problem originated in the use of an incorrect environment variable url within my deployment script.
-
使用第三方身份验证时,您将获得一个身份验证令牌,然后您可以使用该令牌将用户签入您的应用程序。 使用此令牌,Firebase Auth能够提取用户的电子邮件地址。 但是对于您的情况,如果您希望在FB登录后获取用户的电子邮件地址,则需要使用Facebook的Graph API。 您只需要创建一个GraphRequest,其中包含您要求的信息,如下所示。 斯威夫特2 struct FacebookPermission { static let ID: String = "id" static le ...
-
我认为在应用程序中(_ application:UIApplication,open url:URL,sourceApplication:String?,annotation:Any) - > Bool,你需要得到GIDSignIn.sharedInstance()。handle的结果(url,sourceApplication:sourceApplication,annotation) :注释)并将其与FB一起返回。 所以它应该是这样的: return handled || GIDSignIn.share ...
-
有一个codeigniter auth类允许facebook登录吗?(Is there a codeigniter auth class which allows facebook login?)[2023-05-22]
我不确定该项目是否仍处于开发阶段,但A3M(帐户认证和授权)模块看起来可能是一个很好的匹配。 CI论坛帖子在这里 ,谷歌代码页在这里 。 主要特征 使用Facebook,Twitter和OpenID登录/注册(谷歌,雅虎,美国在线,MySpace,其他) 传统注册,登录(记住我)和退出 管理帐户/档案详情(包括档案图片) 管理关联帐户 忘记/重置本机帐户的密码 reCAPTCHA支持 SSL支持 i18n的语言文件(加上所有必要的i18n表,国家,语言,国家/地区的IP等) 语义XHTML视图 I am n ... -
根据文档 , providerData是FIRUserInfo结构的数组。 您发布的Android代码的(大部分)等效Swift代码如下所示: if let providerData = FIRAuth.auth()?.currentUser?.providerData { for userInfo in providerData { switch userInfo.providerID { case "facebook.com": print ...
-
所以基本上我所做的是在后端实现gae-sessions https://github.com/dound/gae-sessions/wiki 。 然后我在前端实现了Facebook ios SDK认证。 然后我将access_token发送给GAE,在那里我对facebook进行facebook图形API调用,并创建包含用户和设备信息的会话,并确保请求是由我的ios应用程序完成的。 然后在随后的请求中,我验证用户在会话中与尝试访问数据的用户相同,并且该通话也来自我的iOS应用程序。 如果任何人有任何指示,使 ...
-
firebaseAuth.fetchProvidersForEmail(userEmail). addOnCompleteListener(new OnCompleteListener
() { @Override public void onComplete(@NonNull Task task) { List providerList = task.getResult().g ... -
如何使用Firebase Auth访问Facebook范围数据(How do you access Facebook scope data with Firebase Auth)[2021-04-16]
进行身份验证时,onAuthStateChange将为您提供令牌。 这是我的多个帐户链接。 你能找到吗? 您使用的是Firebase v3吗? When you authenticate, onAuthStateChange will give you a token. This is mine with multiple account linked. Can you find it? Are you using Firebase v3? -
这个问题是由Apple“创新”引起的。 使用Xcode 8 beta 6,我必须使用 @available(iOS 9.0, *) func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { The problem was caused by Apple "innovations". With Xcode 8 beta 6 ...