简体   繁体   English

如何从 C 中的数字中获取每个数字?

[英]How to get every digit from a number in C?

I have these variables:我有这些变量:

 int dividend;
 int divider;

And I have the function:我有 function:

Divisibility7 (int num);

Those two variables will be at the main function, and the user will be asked to enter the dividend and divider, but in case the user enter the divider 7 , then, the function above will be called.这两个变量将在主 function 处,用户将被要求输入股息和除数,但如果用户输入除数7 ,则将调用上面的 function。

The problem is that I have to follow specific criteria to do this.问题是我必须遵循特定的标准才能做到这一点。 So let's say the user will enter with the dividend 7203 .因此,假设用户将以红利7203输入。 This happen:发生这种情况:

I. Get the last digit of the number . I.获取数字的最后一位

Last digit: 3最后一位:3

Ii.二。 Multiply the last digit by 2将最后一位数字乘以 2

3 x 2 = 6 3×2 = 6

Iii.三. Get the value of the initial number , without the last digit.获取初始数字的值,没有最后一位。

720 720

Iv.四. Subtract the initial value without the last digit from the multiplication result.从乘法结果中减去没有最后一位的初始值

fabs (720 - 6) = 714晶圆厂 (720 - 6) = 714

V. Repeat the process until the result is a value less than or equal to 70、重复这个过程,直到结果是一个小于等于70的值

Vi.六。 Compare the result with the table of contents (0, 7, 14, 21, 28, 35, 42, 49, 54, 63, 70) for Determine whether or not the number is divisible by 7将结果与目录(0, 7, 14, 21, 28, 35, 42, 49, 54, 63, 70) 进行比较,判断该数是否能被7整除

Code:代码:

int res;
int x; 
int y;

int Divisibility7(int num) {

    int res;
int x;
int y;
int z;

    while(num > 70) {

    x = num % 10; // get the last digit from the number
    y = x * 2; // multiply the last digit by 2;
    z = num/10; // get the first digits from the number
    fabs(z - y); // subtract the first digits with the last digits;

    }

} }

In the part of the while, the final fabs(zy) returns what I want, to be the first digits subtracting the last number, but the problem is that the while stop there, I have to do something to make this while go till 70 or less.在 while 的一部分,最后的 fabs(zy) 返回我想要的,是第一个数字减去最后一个数字,但问题是 while 停在那里,我必须做一些事情来使这个 while go 直到 70或更少。

PS: I need to check if the final number from the iterations, it's a number multiplied by 7, how can I do that? PS:我需要检查迭代的最终数字是否是乘以 7 的数字,我该怎么做? And I can't use mod for this.我不能为此使用 mod。

You have not changed num in your while loop . 您没有在while循环中更改num。 Also you do no return the value . 同样,您不返回任何值。 Hope the following code will be ok for you . 希望以下代码适合您。

int Divisibility7(int num) {

    int res,x,y,z;
    while(num > 70) {

    x = num % 10; // get the last digit from the number
    y = x * 2; // multiply the last digit by 2;
    z = num/10; // get the first digits from the number
    num = abs(z - y); // subtract the first digits with the last digits;

    }

 if(num == 0 || num == 7 || num == 14 || num == 21 || num == 28 || num == 35 || num == 42 || num == 49 || num == 54 || num == 63 || num == 70) {
    return 1;
}
else { 
    return 0;
}

}

Not sure, but I think this is what are you trying to do: 不确定,但是我认为这是您想要做的事情:

int main (void)
{
    int number, lastDigitMultiplied;
    scanf("%d", &number);
    while(number > 70){
        //get the last digit and multiply it by 2
        lastDigitMultiplied = (number % 10) * 2;
        //subtract the initial value without the last digit from the multiplication result.
        number = number / 10 - lastDigitMultiplied;
    }
    if(abs(number) % 7 == 0)
        printf("The result is %d and it is a multiple of 7", number);
    else
        printf("The result is %d and it is not a multiple of 7", number);
    return 0;
}

for divisibility against 7 , if you have a positive large bigint already formatted as a string , don't waste time with the regular method doing it 1 digit at a time.对于7的整除性,如果你有一个正的大bigint已经格式化为string ,不要浪费时间用常规方法一次做 1 位数字。

powers of 10, mod 7 , repeats every 6 rounds: 10 的幂,模 7 ,每 6 轮重复一次:

10^1  %  7  =  3  10^7   %  7  =  3  10^13  %  7  =  3
10^2  %  7  =  2  10^8   %  7  =  2  10^14  %  7  =  2
10^3  %  7  =  6  10^9   %  7  =  6  10^15  %  7  =  6

10^4  %  7  =  4  10^10  %  7  =  4  10^16  %  7  =  4
10^5  %  7  =  5  10^11  %  7  =  5  10^17  %  7  =  5
10^6  %  7  =  1  10^12  %  7  =  1  10^18  %  7  =  1

meaning, the effects of the digits mod 7 stay identical as long as they're in chunks of 6 digits at a time.意思是,只要数字mod 7一次以 6 位为一组,它们的效果就保持不变。

the largest multiple of 6-digits supported by double precision FP to full integer precision would be 12-digits at a time. double precision FP支持的6-digits的最大倍数达到完整的 integer 精度一次将是12-digits

So the way to do it is to right-align the digits by padding leading edge zeros to ensure string length is a multiple of 12 .所以这样做的方法是通过填充前缘零来右对齐数字,以确保字符串长度是12 的倍数 Then simply add the digits onto each other, performing a single mod % 7 operation once every 9000 or so rounds of the addition loop然后简单将数字相加,每9000轮左右的加法循环执行一次mod % 7运算

—- (that's when you run up against 2^53 limit; 9007 rounds if u wanna be pedantic about it) —-(那是你遇到2^53限制的时候;如果你想迂腐的话,9007 轮)

example:例子:

   x = 71400535477047763120175175402859619447790
       02233464423375355339031113233806609150957

x % 7 = 4

now try summing up chunks of 12:现在尝试总结 12 个块:

007140053547704776312017517540285961944779
002233464423375355339031113233806609150957

007140053547      7140053547
704776312017    711916365564
517540285961   1229456651525
944779002233   2174235653758
464423375355   2638659029113
339031113233   2977690142346
806609150957   3784299293303
----------------------------
               3784299293303 % 7 = 4

it works exactly the same for any multiples of 6: eg 6, 18, 24, and 30 --对于 6 的任何倍数,它的工作原理完全相同:例如 6、18、24 和 30 --

00714005354770477631201751754
02859619447790022334644233753
55339031113233806609150957

312017   312017
517540   829557
285961  1115518
944779  2060297
002233  2062530
464423  2526953
375355  2902308
339031  3241339
113233  3354572
806609  4161181
150957  4312138
007140  4319278
053547  4372825
704776  5077601
-----   -------
_       5077601 % 7 = 4
00000000714005354770477631201
75175402859619447790022334644
23375355339031113233806609150957

464423375355339031   464423375355339031
113233806609150957   577657181964489988
000000007140053547   577657189104543535
704776312017517540  1282433501122061075
285961944779002233  1568395445901063308
--------------------------------------- 
                    1568395445901063308 % 7 = 4
00000000000000714005354770477
63120175175402859619447790022
33464423375355339031113233806
609150957

339031113233806609150957   339031113233806609150957
000000000000007140053547   339031113233813749204504
704776312017517540285961  1043807425251331289490465
944779002233464423375355  1988586427484795712865820
---------------------------------------------------
                          1988586427484795712865820  % 7 = 4
000000007140053547704776312017517
540285961944779002233464423375355
339031113233806609150957

000000007140053547704776312017          7140053547704776312017
517540285961944779002233464423  517540293101998326707009776440
375355339031113233806609150957  892895632133111560513618927397

                                892895632133111560513618927397  % 7 = 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM