【问题标题】:Implement a sum formula in C在 C 中实现求和公式
【发布时间】:2015-11-26 11:45:41
【问题描述】:

所以,我需要在 C 中实现这个公式:

其中 n 由用户输入,并且必须 >= 1。Ci 是数字 nk的第 i 位> 位,而 Ci 与该行是 Ci 的补码(数字 + 补码 = 9)。 例如,数字 21262 满足这个等式,因为:

21262 = 7^5 + 8^4 + 7^3 + 3^2 + 7^1 = 16807 + 4096 + 343 + 9 + 7 = 21262

我尝试过制作算法并将其转换为 C 程序,但是当我执行时,出现了问题。我无法制定执行 k-i+1 幂的循环。

#include <stdio.h>

int main() {
int n, t, k, c, i, s, power, j;
do {
    printf("Enter n = ");
    scanf("%d", &n);
} while (n < 1);

t = n;                    // Here we save the value of n, and operate with t
k = 0;                          // k counts the number of digits 
while (t > 0) {
    t = t / 10;      
    k++;                        // k = number of digits
}                                
t = n;
s = 0;

for (i = 1; i <= k; i++) {            // Starts the sum from i to k
    c = 9 - t % 10;                   // Complements the digits
    power = 1;

    for (j = 1; j <= k-i+1; j++)      // Start of loop that powers th number
        power = c * power;
    s = s + power;
    t = t / 10;
}

if (s == n)
    printf("The number fulfills the equation");
else
    printf("The number doesn't fulfill the equation");
return 0;
}

如您所见,我尝试通过将 c 补码乘以自身 k-i+1 次的循环来解决功率问题。但有些不对劲。请帮忙!

【问题讨论】:

  • 你从左到右而不是从右到左阅读数字
  • 为了我们人类的可读性,请始终缩进代码。通常,在每个左大括号 '{' 之后缩进,在每个右大括号 '}' 之前不缩进

标签: c algorithm sum formula


【解决方案1】:

从右到左阅读时,幂应该是i 而不是k - i + 1

#include<stdio.h>


int main() {
    int n, t, k, c, i, s, power, j;
    do {
        printf("Enter n = ");
        scanf("%d", &n);
    } while (n < 1);

    t = n;                    // Here we save the value of n, and operate with t
    k = 0;                          // k counts the number of digits
    while (t > 0) {
        t = t / 10;
        k++;                        // k = number of digits
    }
    t = n;
    s = 0;

    for (i = 1; i <= k; i++) {            // Starts the sum from i to k
        c = 9 - (t % 10);                   // Complements the digits
        power = 1;

        for (j = 1; j <= i; j++)      // Start of loop that powers th number
            power = c * power;
        s = s + power;
        t = t / 10;
    }

    if (s == n)
        printf("The number fulfills the equation");
    else
        printf("The number doesn't fulfill the equation");
    return 0;
}

【讨论】:

  • 非常感谢,现在可以使用了。但我不明白它是怎样的 i,而不是 k-i+1。我应该如何解释这个公式?
  • 例如如果 n 是 12345,对于 i = 1 你有 t % 10 = 5,你的循环是从右边而不是从左边
  • 哦,对了。但我怎么知道是我?我想即使我知道我应该改变它,我也无法得出结论是我
  • 如果它可以帮助您更好地理解它,您可以制作外循环for (i = k; i &gt;= 1; i--)和内循环for (j = 1; j &lt;= k-i+1; j++)
【解决方案2】:

你我在&lt;math.h&gt; 头文件下使用 pow(b,p) 函数。它可能会减少你的工作量。您不需要使用内循环,使用此代码代替内循环

power = pow(c,i); 

这段代码可能是这样的-

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


int main() {
    int n, t, k, c, i, s, power, j;
    do {
        printf("Enter n = ");
        scanf("%d", &n);
    } while (n < 1);

    t = n;                    // Here we save the value of n, and operate with t
    k = 0;                          // k counts the number of digits
    while (t > 0) {
        t = t / 10;
        k++;                        // k = number of digits
    }
    t = n;
    s = 0;

    for (i = 1; i <= k; i++) {            // Starts the sum from i to k
        c = 9 - (t % 10);                   // Complements the digits
        power = 1;

        power = pow(c,i);       // multiply the c by i th time by the pow(b,p) function under math.h.
        s = s + power;
        t = t / 10;
    }

    if (s == n)
        printf("The number fulfills the equation");
    else
        printf("The number doesn't fulfill the equation");
    return 0;
}

【讨论】:

    【解决方案3】:

    我尝试了一种不同的方法来解决您的问题,并且效果很好...我认为它可能对您有用,所以我将其发布:

    注意:我使用了一个函数来计算输入值中的位数,还包含math.h文件以使用函数pow()

    这是我的代码:

    #include <stdio.h>
    #include <math.h>//math.h to use pow() function
    
    int num_digits(int n); //the sub function to caluclate number of digits
    
    int main()
    {
        int n,c,t,t1,to_add,ans=0,d,i;
    
        printf("enter number: ");
        scanf("%d",&n);
    
        t=t1=n;
        d=num_digits(n); //function called
    
        for(i=1;i<=d;i++)
        {
            t=t1%10;
            c=9-t;
            to_add=pow(c,i); //pow()  used here
            ans=ans+to_add;
            t1=t1/10;
            printf("%d\n",t1);
        }
    
        if(ans==n)
            printf("condition fulfilled!\n");
        else
            printf("condition not fulfilled");
    
        return 0;
    } //main function ends here
    
    int num_digits(int n) //function to caluclate number of digits
    {
        int i,c;
        c=n;
        for(i=1;c>10;i=i+1)
            {
                c=n/pow(10,i); //pow() even used here
            }
        return i;
    }
    

    顺便说一句,你的问题很有挑战性:)

    -谢谢你

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多