简体   繁体   English

三个连续数字的C乘积

[英]C product of three consecutive numbers

I was trying to solve this tricky question, but for some reason my code is doing something wrong... I don't exactly know why, but I'll try to explain as much as I can. 我试图解决这个棘手的问题,但是由于某种原因,我的代码做错了什么……我不知道为什么,但是我会尽力解释。

Consecutive products: Write a program that reads a positive integer from standard input and verifies if it's equal to the product of three natural and consecutive numbers. 连续积:编写一个程序,该程序从标准输入中读取一个正整数,并验证其是否等于三个自然数和连续数的乘积。 For example, the number 120 is equal to 4x5x6, as for number 90 there aren't any three consecutive natural numbers whose product is 90. Your program should generate as output 'S' if there are 3 consecutive natural numbers whose product is the value read, or 'N' if none. 例如,数字120等于4x5x6,因为对于数字90,没有任何三个连续的自然数,其乘积为90。如果存在三个连续的自然数,其乘积为值,则程序应生成“ S”作为输出读取,如果没有则为“ N”。

Input

120

Expected Output

"S"


Input

60

Expected Output

"S"


Input

80

Expected Output

"N"


Input

120

Expected Output

"S"

And this is my code: 这是我的代码:

 #include <stdio.h>
int main(){
    int int1,i,count=10,j,k,w=0;
    scanf("%i",&int1);
    for (i = 1; i <= count; ++i)
    {
        for (j = 1; j <= count+1; ++j)
        {
            for ( k = 1; k <= count+2; ++k)
            {
                if ((i==j+1 && i==k+2) && (i*j*k==int1)){
                    w=1;
                }
            }
        }
    }
    if (w==0)
    {
        printf("N");
    }
    else{
        printf("S");
    }
}

So basically what this does is I have 3 loops that will generate random numbers in a k*i*j form... and it checks if we are getting what we want(the product of three natural and consecutive numbers) . 所以基本上这是我有3个循环,它们将以k*i*j形式生成随机数...并且它检查我们是否得到了想要的(三个自然数和连续数的乘积)。 This is for an assignment. 这是一项任务。

You don't need 3 loops. 您不需要3个循环。 One trival approach would be: 一种简单的方法是:

int test(int num)
{
    for (int i = 1; i < num; i++)
    {
        int product = i * (i + 1) * (i + 2);
        if ( product == num )
            return true;
        else if (product > num)
            break;
    }
    return false;
}

I modified your code. 我修改了您的代码。 Please let me know if the problem still exists. 请让我知道问题是否仍然存在。 The change made is exactly as WDS said. 所做的更改与WDS所说的完全一样。

#include <stdio.h>
int main(){
int int1,i,count=10,j,k,w=0,comp;
scanf("%i",&int1);
for (i = 1; i <= count; ++i)
{
    comp = i*(i+1)*(i+2);
    if(comp==int1)
    {
        w = 1;
    }
}
if (w==0)
{
    printf("N");
}
else
{
    printf("S");
}
return 0;
}

You might want to add the algorithm tag on this question. 您可能要在此问题上添加算法标签。 That said, my approach would be to consider what the product of 3 consecutive numbers is. 也就是说,我的方法是考虑3个连续数字的乘积是什么。 You could write it as x * (x+1) * (x+2). 您可以将其写为x *(x + 1)*(x + 2)。 But there is a better way. 但是有更好的方法。

Write it as (x-1) * x * (x+1). 将其写为(x-1)* x *(x + 1)。 Then multiply and simplify. 然后相乘并简化。 The result is x^3-x. 结果是x ^ 3-x。

Now for any given number, start a single loop on x from x = 2 (because if x=1 then x-1=0 and this will never be a solution) and incrementing by 1 each loop. 现在,对于任何给定的数字,从x = 2开始x上的单个循环(因为如果x = 1则x-1 = 0,这将永远不是一个解决方案),并每个循环增加1。 Check on each loop for a match with the input number. 检查每个循环是否与输入编号匹配。 If it is a match, return true. 如果匹配,则返回true。 If it is not a match and exceeds the input number return false. 如果不匹配,并且超过输入数字,则返回false。 If it is not a match and is less than the input number, loop again. 如果不是匹配项并且小于输入数字,请再次循环。

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

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