繁体   English   中英

C-数字在数字中的位置

[英]C - Position of digit in a number

void main()
{
    int i, j = 0, p, q, N;    // N is the integer and p is the position needed
    printf("Enter two positive integers: ");
    scanf("%d%d", &N, &p);  // Taking the values of N and p

    for(i = 1; i <= p; i++) {

        j = N / 10;
        q = j % 10;
        i++;
        j = q;
    }

    printf("The digit of %d in the position %d is %d", N, p, j);
}

输入示例:

输入两个正整数:
123456789
3

输出:

The digit of 123456789 in the position 3 is 8 

上面的代码行实际上有2个问题:

  1. 如果我使用上述方法,则计算机通常会从右到左开始输入数字,而通常是从左到右开始计算数字
  2. 该程序仅在循环中重复自身,这意味着它不会使用j的新值来第二次运行
#include <stdio.h>

int main()
{
    int i=0;
    int j=0;
    int p=5;
    int n = 123456;
    int digits[12]={};

    j=n;
    while(j)
    {
        digits[i]=j%10;
        j = j/10;
        i++;
    }

     printf("The digit of %d in the position %d is %d",n,p,digits[i-p]);
     return 0;
}

对我而言,最简单的设计方法是将整数转换为数字并查找。

为什么这样:

为避免pow ,请使用log10因为它们需要浮点支持才能运行,而硬件浮点则需要快速运行。

并且要避免两个循环,因为这两个循环会进行很多重复的计算。

http://ideone.com/Vo9g6T

首先计算整数中的位数,然后说n 。然后将数字除以10 n-1即可得到第一个数字。然后将其放入循环中,将n1 ,将NN%= 10 n-1

int main()
{
    int i,j=0,p,N;
    printf("Enter two positive integers: ");
    scanf("%d%d",&N,&p);

    int pow=1,tmp=N;
    //counting power of 10 to divide
    while(tmp>10)
    {
        pow*=10;
        tmp/=10;
    }
    tmp=N;
    for(i=1; i<=p;i++){
       j = tmp/pow;
       tmp%=pow;
       pow/=10;
    }
    printf("The digit of %d in the position %d is %d\n",N,p,j);
}

有几种方法可以做到这一点。 正如一些评论所建议的,您可以执行以下任一操作:

方法1:将数字转换为字符串数组,可以通过sprintf完成。

char str[256];
int len = sprintf(str,"%d",N);
//traverse str using len

方法2:由于以b为底的N个数字为N的数字,您可以表示直到pow(b,n)-1任何数字。 要获得数字,可以使用pow的反函数,即以b为底的对数。 您可能不会获得整数值,因此可以使用floor并将结果强制转换为int。 完整程序如下所示:

#include <stdio.h>
#include <math.h>

int getDigit(int num, int p)
{
   return (num / (int)pow(10, floor(log10(num)) - p)) % 10;
}

int main()
{
    int i,j=0,p,q,N;// N is the integer and p is the position needed
    printf("Enter two positive integers: ");
    scanf("%d%d",&N,&p);// Taking the values of N and p
    j = getDigit(N,p);
    //The result is j if you count from 0, j+1 if you count from 1
    printf("The digit of %d in the position %d is %d\n",N,p,j+1);

    return 0;
}

不使用2个循环:

int main()
{
    int i,ans,p,N,no_of_digits=0,temp;
    printf("Enter two positive integers: ");
    scanf("%d%d",&N,&p);
    temp=N;
    no_of_digits=(int)log10(N) + 1;
    while(i<=(no_of_digits-p))
    {
        ans=N%10;
        N=N/10;
        i++;
    }
    printf("The digit of %d in the position %d is %d\n",temp,p,ans);
}

http://ideone.com/kQFISY

#include <stdio.h>

int getDgtLen (int n);

int main(void)
{
    // n is the integer and p is the position needed
    int i, q, n, p;

    // Prompt for taking the values of n and p
    do
    {
        printf("Enter two positive integers for number,  position: ");
        scanf("%d%d", &n, &p);
    }
    while (n < 0 || p <= 0);

    int nOrigin = n;
    int len = getDgtLen(n);

    if (p <= len)
    {
        for (i = 0; i < p; i++)
        {
            q = n % 10;
            n /= 10;
        }
        printf("The digit of %d in the position %d is %d\n", nOrigin, p, len - q + 1);
    }
    else
    {
        printf("position not found!\n");
    }
}

int getDgtLen (int n)
{
    int i = 0;
    while (n > 0)
    {
        n /= 10;
        i++;
    }
    return i;
}

暂无
暂无

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

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