将一个 9 位数字转换为三个 3 位数字

标签 c math logic

好的,大家好,我输入了一个 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/

相关文章:

sql - 简化 SQL 语句的一般规则

c - 重放文件系统调用

C99 的固定宽度整数类型

我可以将 NULL 传递给 modf/modff 吗?

java - 我的解决方案是否存在逻辑流程问题?

java - 为什么我的程序跳过 if 语句

c - OS-9 C语言编程

c - 如何确保 strtol() 已成功返回?

math - 使用 Coq 证明有限集的幂集是有限的

python - 输入数字总和