如何抓住从一些数字些? [英] How to grab some digits from a number?

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

问题描述

让我们说我们有以下一些。

Let us say we have the following number.

数= 2001000200030

Number = 2001000200030

我怎么能抢到第一位,并将其存储到一个变量?然后
 抓住下一个四位数字,并将其存储到另一个变量?
那么接下来的四个数字...

How can I grab the first digit and store it to a variable? Then grab the next four digits and store them to another variable? Then the next four digits ...

所以输出应该是这样的。

So the output should be like this.

第一= 2;

firstfour = 0010

firstfour = 0010

secondfour = 0020

secondfour = 0020

thirdfour = 0030

thirdfour = 0030

感谢你和我AP preciate你的时间。

Thank you and I appreciate your time.

推荐答案

为什么要使用字符串时,你可以处理数字的坚持?您可以通过使用10次幂取模和除法的数字拆分。

Why use strings when you can stick with processing numbers? You can split by digits using modulo and division in powers of 10.

#include <stdio.h>

int main(void) {
    long long number = 2001000200030LL;

    /* Find power of 10 for top digit */
    long long currentDigit = 1LL;
    while((currentDigit * 10LL) < number) currentDigit *= 10LL;

    printf("First digit = %lld\n", number / currentDigit);
    number %= currentDigit;

    /* Read off groups of four digits */
    while(currentDigit >= 10000LL)
    {
        long long nextFour;
        currentDigit /= 10000LL;
        nextFour = number / currentDigit;
        number %= currentDigit;
        printf("Next four = %04lld\n", nextFour);
    }

    /* Output any remaining digits not covered by a group of four */
    if (currentDigit > 1LL)
    {
        printf("Remaining digits = ");
        for(currentDigit /= 10LL; currentDigit > 1LL; currentDigit /= 10LL)
        {
            printf("%lld", (number / currentDigit) % 10LL);
        }
        printf("%lld\n", number % 10LL);
    }

    return 0;
}

这篇关于如何抓住从一些数字些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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