#include <iostream>
using namespace std;
int main()
{
// Here I seperate my number because at first I have to seperate and then I have to change the first digit and last digit.
int numri, shifrat, i = 0, a, shuma = 0, m, d;
cout << "Jep nje numer \n";
cin >> numri;
m = numri;
while (numri != 0) {
i++;
numri = numri / 10;
}
d = pow(10, i - 1);
numri = m;
while (numri != 0) {
shifrat = numri / d;
cout << shifrat << " ";
numri = numri % d;
d = d / 10;
}
//Now after that I have to change the last digit and first digit. Please help me.
//But I'm not allowed to use functions or swap only integers and loops or conditions.
cin.get();
}
有人能帮助我吗?
最佳答案
交换数字ABCDEF中的第一位和最后一位--FBCDEA
num = 12345
res = 52341
这个想法是:
52341 = 12345-10005 + 50001
第一位数fd = num%10
小数点后乘数df = 1
until num is not zero we do
last digit ld = num
num = num / 10
if num != 0 decimal places multiplier df = df*10
结果= ABCDEF-F-A * 100000 + A + F * 100000
int m = numri;
int ld = 0; // last digit(most)
int fd = numri % 10; // first digit(least)
int df = 1; // last digit multiplier 10^n where n is decimal places
while (numri != 0) {
ld = numri;// this will be the last digit in last iteration
numri = numri / 10;
if(numri) df = df * 10;
}
int res = m - fd - ld * df + fd * df + ld;
cout<<res;
例:
if the num = 12345
fd = 12345 % 10 =5
df = 1
num = 12345 / 10 = 1234
df = df*10 = 10
num = 1234 / 10 = 123
df = df*10 = 100
num = 123 / 10 = 12
df = df*10 = 1000
fd = num = 12
num = 12 / 10 = 1
df = df*10 = 10000
fd = num = 1
num = 1/10 =0
df not changed
loop exit
result = 12345 - 5 - 1*10000 + 1 + 5*10000 = 62341
关于c++ - 我必须更改我的数字的最后一位和第一位,但不能仅对整数或循环使用函数。例如从12345到52341,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58778697/