我怎样才能得到“3”又名第二位?
另外,如果n类似于245836,我怎么能逐一提取每个数字呢?
#include <stdio.h> int main(int argc, char *argv[]) { int n = 234; // Gets first digit int digit = n / 100; putchar('0' + digit); // gets 3rd digit digit = n % 10; putchar('0' + digit); }How can I get "3" aka 2nd digit?
Also If the n was something like 245836 how could I extract each digit one by one?
最满意答案
您可以通过除以10然后将除法除数除以10来实现:
int second = (number / 10) % 10;一般来说,你可以把整数除以k的十次幂除以k最低有效数字。 将剩余除数除以k的十次幂相当于保持最后的k位数。
我怎么能一个一个地提取每个数字?
您可以使用%10取最后一位数字,然后将该数字从结尾删除/= 10 。
You can do it by dividing by ten and then taking the remainder of division by ten:
int second = (number / 10) % 10;In general, you could think of integer-dividing by a k-th power of ten as of dropping k least significant digits. Taking a remainder of division by k-th power of ten is equivalent to keeping the last k digits.
how could I extract each digit one by one?
You can take the last digit with %10 and then dropping that digit off the end by /= 10.
更多推荐
发布评论