c - 找到方程的 (x,y) 解

标签 c equation

程序从用户那里接收到一个正数k,并且应该检查方程有多少解

3*x+5*y=k

在许多解决方案的情况下,该函数采用所有解决方案中 |x-y| 的较大绝对值。如果只有一种解决方案,它会打印出来。例如:

  • 如果用户输入 k=34(x,y) 有两种解决方案:(3,5)(8,2)。因此,程序计算 |8-2||3-5| 有两个解,取较大的一个,6。

  • 例如,如果用户输入 k=8,则只有一个解决方案,(1,1)

可悲的是,我的老师要求我只使用循环、ifelse 语句。没有递归,没有辅助函数,也没有数组。她还希望程序高效,这样我就不能在循环中使用循环。

我尝试编写代码,但程序没有响应。我定义了 counter 来计算方程的可能解的数量和 distance 较大的绝对值:

void check_how_many_solutions(int n) {
    int  y = 0, counter = 0, distance = 0, equation1 = 0, equation2 = 0, equation3 = 0; 
    while (equation3 <= n) {
        equation1 = (n - (5 * y)) / 3;
        equation2 = (n - (3 * equation1)) / 5; 
        equation3 = (3 * equation1) + (5 * equation2);
        if (equation3 == n) { 
            counter++;  
            if (fabs(equation1 - equation2) > distance)
                distance = fabs(equation1 - equation2);
        }
        y++;
    }   
    if (counter > 1)
        printf("The values of x and y are (%d,%d)\n", equation1, equation2); 
    else
        printf("The greater absolute value of |x-y| is %d\n", distance);
}

代码确实运行但没有结果。

最佳答案

这是我制作的一个程序,我相信它可以回答您提出的问题。您只想遍历 x 的整数值并检查计算出的 y 是否也是整数。我还跟踪找到的解决方案总数以及 x 和 y 之间距离最大的解决方案,并根据这些给出最终答案。

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

void count_solns(double);

int main(int argc, char *argv[])
{
    if (argc == 2 && argv[1]) {
        int k = atoi(argv[1]);
        count_solns(k);
    }
    else {
        puts("Usage: count_solns <positive_integer>");
        return 1;
    }
    return 0;
}

void count_solns(double k)
{
    printf("Print positive integer solutions to 3x + 5y = %.3f\n", k);
    int solns_found = 0;
    int distance = -1;
    int final_x, final_y;
    double x, y;
    for (x = 0; x <= (k/3); x++) {
        y = (k-3*x)/5;
        if (y == floor(y)) {
            printf("Solution found at (x, y) == (%d, %d)\n", (int) x, (int) y);
            solns_found++;
            if (fabs(x-y) > distance) {
                final_x = x;
                final_y = y;
            }
        }
    }
    if (solns_found == 0)
        printf("No whole number solutions found for 3x + 5y = %.3f", k);
    else if (solns_found == 1)
        puts("This is the only solution where x and y are both whole numbers");
    else
        printf("The whole number solution with the highest distance between x and y is (x, y) == (%d, %d)", final_x, final_y);
    return;
}

用法看起来像这样:

$ ./count_solns 70
Print positive integer solutions to 3x + 5y = 70.000
Solution found at (x, y) == (0, 14)
Solution found at (x, y) == (5, 11)
Solution found at (x, y) == (10, 8)
Solution found at (x, y) == (15, 5)
Solution found at (x, y) == (20, 2)
The solution with the highest distance between x and y is (x, y) == (20, 2)

关于c - 找到方程的 (x,y) 解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54028663/

相关文章:

如果我使用 QtCreator 构建,从共享对象到主应用程序的调用会中断

c++ - 在 Visual Studio 中的同一解决方案中构建 C 项目以调用 C++ 库中的函数

c - NSURLConnection 失败,错误代码为 -1001

C# dll 求解简单方程

c - 我在这个 C 程序中遇到两个错误。处理结构体、数组和冒泡排序

c - 在编译时是否有任何通用的方法来计算 N 个整数常量的最大值(MACROS)-

java - 格式化方程?

python - Sympify() 和 Eval() 方程不在函数内执行

python - 最大方程长度

Java 数学函数像这样的数学方程吗?