It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                                已关闭8年。
            
                    
我在一次采访中被要求使用位运算符将整数的最后一位和第一位交换。尝试了很多,但我找不到解决方案。我怎样才能做到这一点?

最佳答案

使用int digits = log10(x)获取位数。

使用int first = x / pow(10,digits)获取第一个数字。

使用int last = x % 10获取最后一位数字。

放在一起就可以了

int swapped = x + (last - first) * pow(10,digits) + (first - last)

关于c - 使用按位运算符将第一位和最后一位交换为整数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6476132/

10-11 04:22