好的,大家好,我输入了一个 9 位数字 (123456789
),我需要将其分解为第一个中间和最后三个数字 (123,456,789
)。
我知道我可以使用模来获得最后三位数字。
first_group_of_three = input number % 1000;
但我不知道如何获得其他两组。 任何帮助将不胜感激。
更新! 感谢 Milevyo 的回答,我设法弄清楚了。现在对于那里的代码怪胎,我是一名初学者程序员,我想问什么是编写此代码的更有效方法(如下),也许使用更少的变量或一些其他技术。我没有将其转换为 char,因为我们仍然“没有学会它”,所以我们不被允许这样做
scanf("%d" , &input_number);
last_group_of_three = input_number % 1000;
temp = (input_number-last_group_of_three)/1000;
middle_group_of_three = temp % 1000;
first_group_of_three = (temp-middle_group_of_three)/1000;
最佳答案
first_group_of_three = input number % 1000;
shift=(input number-first_group_of_three)/1000
second_group_of_three = shift % 1000;
and so on
关于将一个 9 位数字转换为三个 3 位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33154241/