我需要做所謂的「十進制右循環移位」。例如,如果輸入爲8652
,輸出將爲2865
。cpp中的十進制右循環移位
有沒有辦法在CPP中執行此操作而不轉換爲字符串和字符串操作?只需使用算術運算,循環並轉換爲二進制。
我需要做所謂的「十進制右循環移位」。例如,如果輸入爲8652
,輸出將爲2865
。cpp中的十進制右循環移位
有沒有辦法在CPP中執行此操作而不轉換爲字符串和字符串操作?只需使用算術運算,循環並轉換爲二進制。
其實它可以方便,快捷的方式來完成這樣的任何整型數字:
#include <iostream>
using namespace std;
int concatenate (int x, int y)
{
int pow = 10;
while (y >= pow)
pow *= 10;
return x * pow + y;
}
int main()
{
int rpm = 8652;
// Cyclic Right Shift
int lastDigit = rpm % 10; //getting the last digit
int otherDigits = (rpm - lastDigit)/10; //rest of the digits
int CRS = concatenate(lastDigit, otherDigits); //concatenation
}
您可以搜索*扭轉C++ *一個整數,並得到各種答案。 – NathanOliver
「」中的操作是否算作算術運算? –
AndyG