如何编写swapdigits函数。 [英] How to write a swapdigits function.

查看:64
本文介绍了如何编写swapdigits函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写一个函数:swapDigits(m,n,x){},它有3个非负整数参数,并且必须返回通过交换数字x的第m和第n位得到的数字。对于前swapDigits(0,3,123456)返回126453,数字的第0位是其最低有效位。或另一个前。 swapDigits(2,3,1234)返回1324.我不能使用字符串或数组。这是我的工作,但是如果m或n = 0,如果x = 10a + b,则程序不起作用,对于其他一些数字,例如swapDigits(2,4,2412),程序不起作用。



我尝试了什么:



Write a function: swapDigits(m,n,x){} that has 3 nonnegative integer arguments, and must return the number that is obtained by swapping the mth and nth digit of a number x. for ex. swapDigits(0,3,123456) returns 126453, the 0th digit of a number is its least significant digit. or another ex. swapDigits(2,3,1234) returns 1324. I must not use strings or arrays. This is my work, but the program doesn't work if m or n=0, if x=10a+b, and for some other numbers like swapDigits(2,4,2412).

What I have tried:

int numberOfDigits(int x) {
	int count=0;
	while(x!=0){
		x/=10;
		count++;
	}
	return count;
}

int swapDigits(int m, int n, int x) {
	int tempa, tempb, temp, temp1,  temp2, a, b, c, g, d;
	temp=x;
	temp1=x;
	temp2=x;
	a= numberOfDigits(x) - m ;
	b= numberOfDigits(x) - n ;
	tempa=a;
	tempb=b;
	int mthDigit, nthDigit;
	while(tempa>=0){
		tempa--;
		temp1/=10;
		mthDigit=temp1%10;
	}
	while(tempb>=0){
		tempb--;
		temp2/=10;
		nthDigit=temp2%10;
	}

	d = (mthDigit*pow(10, a)) + (nthDigit*pow(10, b)) ;
	g = (nthDigit*pow(10, a)) + (mthDigit*pow(10, b)) ;

	return x - d + g;
}

推荐答案

首先考虑一下:什么是十进制数字?如何删除一个?我如何添加一个?

嗯,这个数字的第3位数字:654321是4,我将3转换为10的幂数将其删除:1000。 />
如果我将基数除以1000 * 10,我得到65.如果我乘以1000 * 10,我得到650000.

如果我取原始数字的剩余部分, 1000,我得到321.

所以将这两个加在一起已经删除了第三个数字:

Start by thinking about it: what is a decimal digit? How do I remove one? How do I add one?
Well, to the 3rd digit of this number: 654321 is "4", and I'd remove it by converting the 3 to a power of ten: 1000.
If I divide the base number by 1000 * 10 I get 65. If I multiply that by 1000 * 10 I get 650000.
If I take the remainder of the original number and 1000, I get 321.
So adding those two together has removed the third digit:
removed = (x / 10000) * 10000 + (x % 1000)



同样,我可以很容易地提取数字:


Similarly, I can extract the digit very easily:

digit = (x / 1000) % 10





和只需更改十次幂值,我就可以对第一个数字做同样的事情:



And just by changing the power-of-ten value I can do the same for the first digit:

removed = (x / 10) * 10 + (x % 1)
digit = (x / 1) % 10





所以...如果你写两个函数来删除一个数字并返回一个给定的数字 x 和十位数的所有你需要做的就是每次调用它们两次然后相乘并添加。



有意义吗?



So... if you write a two functions to remove a digit and return a digit given x and the digit power-of-ten all you have to do is call each of them twice and then multiply and add.

Does that make sense?


你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你已经接近了一个错误。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


这篇关于如何编写swapdigits函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆