作者:羊爱恋上狼 | 来源:互联网 | 2023-10-17 11:01
#include
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 < 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();
}
有人可以帮我吗?
它比数学更重要。
要获取整数的最后一位,我们可以使用模:
z = abc....xyz % 10
要从我们使用的号码中“删除”该数字
abc...xy = abc...xyz / 10
(其中/
表示整数除法,即34/10 == 3
)。
我认为这就是您已经知道的方法。与其深入研究代码,不如先对缺失的部分进行数学运算。
要在整数中添加数字
abc...xyz = (abc...xy * 10) + z
只有现在您具备了开始编写代码的所有必要条件:
int main() {
int inp;
int outp = 0;
std::cin >> inp; // read_input
while(inp > 0) { // reverse
int digit = ... // get_digit
inp = ... // remove_digit
outp = ... // add_digit
}
std::cout <}
很遗憾,不允许您使用函数。接下来的课程之一应该是,在命名事物并使代码清晰化方面,函数比注释要好得多
int read_input();
int remove_digit(int x);
int add_digit(int x,int digit);
int reverse(int x);
int main() {
int inp = read_input();
std::cout <}
,
可以在不使用功能pow
的情况下编写程序。
您在这里。
#include
int main()
{
while ( true )
{
std::cout <<"Enter a non-negative number (0 - exit): ";
unsigned int n;
if ( not ( std::cin >> n ) or n == 0 ) break;
std::cout <<'\n';
const unsigned int Base = 10;
unsigned int factor = 1;
while ( not ( n / factor unsigned int last_digit = n / factor;
unsigned int first_digit = n % Base;
n %= factor;
n = n - first_digit + last_digit;
first_digit *= factor;
n = first_digit + n;
std::cout < }
return 0;
}
程序输出看起来像
Enter a non-negative number (0 - exit): 987654321
187654329
Enter a non-negative number (0 - exit): 12345
52341
Enter a non-negative number (0 - exit): 100
1
Enter a non-negative number (0 - exit): 1
1
Enter a non-negative number (0 - exit): 0
如果这些语句
if ( not ( std::cin >> n ) or n == 0 ) break;
和
while ( not ( n / factor
包含尚不知道的符号,然后可以像这样重写它们
if ( !( std::cin >> n ) || n == 0 ) break;
和
while ( !( n / factor
,
您可以创建一个具有4个变量的程序-n,给定数字,cn-创建的数字,u-给定数字的最后一位,p-10 ^ p数字;
您将保存最后的n位数字,并在同时创建10 ^ p数字。然后,使用公式
cn = ((u * p + cn % p) /10) * 10 + n;
您将创建新号码;
#include
using namespace std;
int main() {
int n,cn,u,p;
cin>>n;
u = n % 10;
cn = n;
p = 1;
while(n>9) {
p = p * 10;
n = n / 10;
}
cn = ((u * p + cn % p) /10) * 10 + n;
cout<return 0;
}
,
要交换数字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<
示例:
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