从C中从最不重要开始的数字中获取数字非常简单:
#include <stdio.h>
int main()
{
int num = 1024;
while(num != 0)
{
int digit = num % 10;
num = num / 10;
printf("%d\n", digit);
}
return 0;
}
但是如何从第一个数字(此处为1)开始提取数字,该解决方案可以应用于任何数字?
使用数组会很简单,但我不想使用数组,我也不想使用逻辑运算符。
答案 0 :(得分:5)
以下程序可以满足您的需求:
#include <stdio.h>
int main()
{
int num =0;
int power=1;
printf("Enter any number:");
scanf("%d",&num);
while(num>power)
power*=10;
power/=10;
while(num != 0)
{
int digit = num /power;
printf("%d\n", digit);
if(digit!=0)
num=num-digit*power;
if(power!=1)
power/=10;
}
return 0;
}
答案 1 :(得分:2)
假设32位有符号数是正数,那么作为一个简单的例子:
#include <stdio.h>
int main()
{
int rmdr;
int dvsr;
int quot;
scanf("%d", &rmdr); // read in number
dvsr = 1000000000;
while(0 >= (quot = rmdr / dvsr)){ // skip leading zeroes
rmdr %= dvsr;
dvsr /= 10;
if(dvsr == 1)
break;
}
while(dvsr){ // display number
quot = rmdr / dvsr;
printf("%1d", quot);
rmdr %= dvsr;
dvsr /= 10;
}
printf("\n");
return(0);
}
或略微优化:
int main()
{
int rmdr;
int rm10;
int dvsr;
int quot;
scanf("%d", &rmdr); // read in number
rm10 = rmdr/10;
dvsr = 1;
while(dvsr <= rm10) // skip to 1st digit
dvsr *= 10;
while(dvsr){ // display number
quot = rmdr / dvsr;
printf("%1d", quot);
rmdr %= dvsr;
dvsr /= 10;
}
printf("\n");
return(0);
}
答案 2 :(得分:1)
尝试将返回值存储为整数或字符数组。
所以我可以按照我们的要求打印这个值。
如果我们使用字符数组,我们可以轻松找到长度并轻松获得结果。
#include <stdio.h>
#include<string.h>
int main()
{
int num = 1024;
char a[10];
int i=0;
while(num != 0)
{
a[i++] = (num % 10)+'0';
num = num / 10;
}
a[i]='\0';
printf("%s",a);
return 0;
}
答案 3 :(得分:1)
如果您不想使用数组,一个简单的解决方案就是......
从用户那里获取输入。
撤消号码。
然后打印数字。
#include<stdio.h>
#include <math.h>
int reversDigits(int num)
{
int rev_num = 0;
while(num > 0)
{
rev_num = rev_num*10 + num%10;
num = num/10;
}
return rev_num;
}
int main() {
int i = 1024;
int number = reversDigits(i);
while(number != 0)
{
int digit = number % 10;
number = number/ 10;
printf("%d\n", digit);
}
return 0;
}
答案 4 :(得分:0)
我知道这个问题已得到解答,但我会发布这个答案,以防有人发现它有用。
#include<stdio.h>
//get_int_first gets each digit and multiplies it by it's placeholder value.
// the number is reconstructed and returned to main
int get_int_first(int num){
int power = 1,len = 0,first = 0,i = 0;
int number = 0;
while (num>power){
power=power*10; //calculating number of zeroes in number. for 789, power = 10 -> 100 -> 1000
}
power = power/10; // to multiply with power directly and get highest placeholder, we divide by 10. now power = 100
while (power>1){
first = (num/power); //get digits from the leftmost(100th placeholder/nth placeholder) f = 7 f = 8
number = number + first*power; //first*power = gives number as nb = 0 +7*100 = 700 nb = 700 + 80
num = num - (power*(first)); //change number to get next significant digit from left n = 789 - (100*7) = 89 n = 89-10*8=9
power = power/10; //change power to divide number by appropriate power. p = 100/10 = 10 p = 1
}
number = number + num; //number is missing the unit's digit and it is stored in num. thus, it is added to number
return number;
}
int main() {
printf("digits are %d\n",get_int_first(789));
return 0;
}
答案 5 :(得分:0)
这是我的尝试。仅适用于正数。最大范围2 ^ 64(unsigned long long)
#include <iostream>
#include <cmath>
using namespace std;
using bignum = unsigned long long;
inline
bignum Power(unsigned x, unsigned y) {
return y>0 ? x*Power(x,y-1) : 1;
}
// return digits count in a number
inline
int Numlen(bignum num) {
return num<10 ? 1 : floor(log10(num))+1;
}
// get the starting divisor for our calculation
inline
bignum Getdivisor(unsigned factor) {
return Power(10, factor);
}
int main()
{
bignum num{3252198};
//cin >> num;
unsigned numdigits = Numlen(num);
auto divisor = Getdivisor(numdigits-1);
while(num > 0) {
cout << "digit = " << (num/divisor) << "\n";
num %= divisor;
divisor /= 10;
}
}
/*
output:
digit = 3
digit = 2
digit = 5
digit = 2
digit = 1
digit = 9
digit = 8
*/