代码包含右圆弧移动,例如,数字 12345 将为 51234 。
问题是我如何进行向左移动,例如 12345 将为 23451 ,我的电话号码在3位数之间10不仅是5位数字。请帮忙吗?
谢谢。
#include <stdio.h>
int mPow(long n, long b);
long Counts(long);
int main() {
long q;
printf("Please Enter a number \n");
scanf("%ld",&q);
printf("Right Rotate:%ld \n",(q%10)*(mPow(10,Counts(q)-1))+q/10);
return 0;
}
long Counts(long q) {
int count=0;
while(q!=0) {
q/=10;
count++;
}
return count;
}
long mPow(long n,long b) { //n=>Base, b=>power
long ret=1;
while(b!=0) {
ret = ret*n;
b--;
}
}
答案 0 :(得分:3)
#include <iostream>
using namespace std;
// steps:
// number example : 12345
// get the first digit : 5
// get the rest number : 1234
// get the number of digit in rest number : 4
// shifted number : rest number + firstDigit x 10^(num of digit)
// : 1234 + 5 x 10^4
// : 1234 + 50000 = 51234
int rotateRight(int num) {
int firstDigit = num % 10;
int restNumber = num / 10;
int numberOfDigit = 0;
int temp = restNumber;
while (temp != 0) {
temp /= 10;
numberOfDigit++;
}
return restNumber + firstDigit * pow(10, numberOfDigit);
}
int main() {
int num = 12345;
cout << rotateRight(num) << endl;
return 0;
}
答案 1 :(得分:2)
#include <stdio.h>
#include <math.h>
int getdig(int n){ //to get the no of digits in a number
int count=0;
while(n%10>0){
count ++;
n=n/10;
}
return count;
}
int RightRotateDigit(int num) { //exmpl 12345
int rightmost=num%10; //get the last digit 5
num=num-rightmost; // num is now 12340
num=num/10; //num is now 1234
int dig=getdig(num) //get the no of digits in 1234 i.e 4
int add=rightmost*pow(10,dig) ; //5*10000=50000
num=num+add; //num is now 51234
return num;
}
答案 2 :(得分:-2)
嗨,这种逻辑可能会帮助您解决问题
int r=0,num = 12345;
r = num % 10;
num = num / 10;
num = r * 10000 + num;
/* print num and your answer will be 51234 */