簡體   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