我如何可以通过在C 3位数每个数字重复?

编程入门 行业动态 更新时间:2024-10-15 12:33:18
本文介绍了我如何可以通过在C 3位数每个数字重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

长度总是3,例子:

000 056 999

如何能得到一些这样我就可以在阵列中使用单一数字的值?

How can I get the number so I can use the value of the single digit in an array?

难道是比较容易通过它转换为字符,循环,转换回 INT 在需要的时候?

Would it be easier to convert to an char, loop through it and convert back to int when needed?

推荐答案

要获得在一个整数我的位置 P 十进制数你可以这样做:

To get the decimal digit at position p of an integer i you would do this:

(i / pow(p, 10)) % 10;

因此​​,从最后一位到第一个数字回路,你可以这样做:

So to loop from the last digit to the first digit, you would do this:

int n = 56; // 056 int digit; while(n) { digit = n % 10; n /= 10; // Do something with digit }

易于修改做准确的3倍。

Easy to modify to do it exactly 3 times.

更多推荐

我如何可以通过在C 3位数每个数字重复?

本文发布于:2023-11-29 16:01:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646877.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以通过   位数   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!