一,统计数字位数
1.给定一个整数可能为正,0,负数,统计这个数据的位数.例如1234567输出7位; -12345678输出8位;0 输出1位
int main()
{
long long n;
long long m;
scanf("%lld",&n);
int count = 0;//位数
m = n;
do
{
count++;
n /= 10;//丢弃个位
} while (n != 0);
printf("%lld是个%d位数字\n",m,count);
return 0;
}
二、顺序输出
2.给定一个整数可能为正,0,负数,顺序输出这个数字的每一位.
例如1234567输出1 2 3 4 5 6 7 例如-123456789输出 -1 2 3 4 5 6 7 8 9
int main()
{
long long n;
scanf("%lld",&n);
if (n == 0)
{
printf("0\n");
return 0;
}
int count = 0;//位数
long long m = n;
while (n != 0)
{
n /= 10;//丢弃个位
count++;
}
int power = pow(10,count-1);
n = m;
if (n < 0)
{
printf("-");
n = -n;
}
while (n != 0)
{
printf("%d ",n/power);
n %= power;
power /= 10;
}
return 0;}
三、逆序输出
3.给定一个整数可能为正,0,负数,从右往左输出这个数据的每一位. 例如1234567输出7 6 5 4 3 2 1 例如-12345678输出 -8 7 6 5 4 3 2 1
int main()
{
long long n;
scanf("%lld",&n);
if (n < 0)
{
printf("-");
n = -n;
}
do
{
printf("%d ",n%10);//得到个位,并输出
n /= 10;
} while (n!=0);
return 0;}
日常学习的总结,贵在坚持,与诸君共勉!