假设我在 C 中有一个多位数整数。我想将它分解成一位数整数。
123
会变成 1
, 2
, 和 3
.
我该怎么做,特别是如果我不知道整数有多少位?
int value = 123;
while (value > 0) {
int digit = value % 10;
// do something with digit
value /= 10;
}
首先,数一下数字:
unsigned int count(unsigned int i) {
unsigned int ret=1;
while (i/=10) ret++;
return ret;
}
然后,您可以将它们存储在一个数组中:
unsigned int num=123; //for example
unsigned int dig=count(num);
char arr[dig];
while (dig--) {
arr[dig]=num%10;
num/=10;
}
作为提示,获取数字中的第 n 个数字非常容易;除以 10 n 次,然后模 10,或在 C:
int nthdig(int n, int k){
while(n--)
k/=10;
return k%10;
}
123 的最后一位是 123 % 10。 您可以通过执行 123/10 来删除 123 的最后一位数字——使用整数除法这将得到 12。 回答你关于“我怎么知道你有多少位数”的问题—— 尝试按照上述方法进行操作,您将了解如何知道何时停止。
我认为下面的代码会有所帮助....
temp = num;
while(temp)
{
temp=temp/10;
factor = factor*10;
}
printf("\n%d\n", factor);
printf("Each digits of given number are:\n");
while(factor>1)
{
factor = factor/10;
printf("%d\t",num/factor);
i++;
num = num % factor;
}
//Based on Tony's answer
#include <stdio.h>
int nthdig(int n, int k){
while(n--)
k/=10;
return k%10;
}
int main() {
int numberToSplit = 987;
printf("Hundreds = %i\n",nthdig(2, numberToSplit));
printf("Tens = %i\n",nthdig(1, numberToSplit));
printf("Units = %i\n",nthdig(0, numberToSplit));
}
这导致以下打印输出:
数百 = 9
十位 = 8
单位 = 7
我根据@asaelr 的代码做了这个:
typedef struct digitsArrayPlusNumber {
uint32_t *baseAddress;
uint32_t number;
} digitsArrayPlusNumber;
digitsArrayPlusNumber *splitDigits (uint32_t inValue) {
// based on code from [email protected]
uint32_t inputValue = inValue;
//Count digits
uint32_t theCount = 1;
while (inputValue /= 10)
theCount++;
// put in array
uint32_t *arr = malloc(sizeof(uint32_t) * theCount);
uint32_t dig = theCount;
while (dig--) {
arr[dig]=inValue % 10;
inValue /= 10;
// printf ("%d\n", arr[dig]);
}
digitsArrayPlusNumber *dandn = malloc (sizeof(digitsArrayPlusNumber));
dandn->baseAddress = arr;
dandn->number = theCount;
return dandn;
}
int main(int argc, const char * argv[]) {
for (int d = 0; d < splitDigits(12345678)->number; d++)
printf ("%u\n", (splitDigits(12345678)->baseAddress)[d]);
}
效果很好,谢谢!
可以用%10,表示除以后的余数。所以
123 % 10
是3,因为余数是3,所以123减去3就是120,然后120除以10就是12。做同样的过程。
//This is c#, but you can see how easy it would be to convert
//to another language.
int myNumber = 123456;
string myWord = Convert.ToString(myNumber);
string partOne = myWord.Substring(0, 2);
string partTwo = myWord.Substring(2, 2);
string partThree = myWord.Substring(4, 2);
int num01 = Convert.ToInt32(partOne);
int num02 = Convert.ToInt32(partTwo);
int num03 = Convert.ToInt32(partThree);
Console.WriteLine(num01);
Console.WriteLine(num02);
Console.WriteLine(num03);
Output:
12
34
56
我们可以将此程序用作具有 3 个参数的函数。这里在“while(a++<2)", 2 is the number of digits you need(can give as one argument)replace 2 with no of digits you need. Here we can use "z/=pow(10,6)" if we don't need last certain digits ,replace 6 by the no of digits you don't need(can give as another argument),and the third argument is the number you need to break.
int main(){
long signed c=0,z,a=0,b=1,d=1;
scanf("%ld",&z);
while(a++<2){
if(d++==1)
z/=pow(10,6);
c+=(z%10)*b;
z/=10;
b*=10;}
return c;}
你可以分而治之,但你已经重写了所有的算术库。我建议使用多精度库 https://gmplib.org 但当然这是很好的做法