ios:求解线性丢番图方程 (参见示例说明)

关于里番ios的问题,在diophantine equation solver中经常遇到, 让我先澄清一下 (在你们解雇我之前),这不是家庭作业问题,我也不是大学生。:)

让我先澄清一下 (在你们解雇我之前),这不是家庭作业问题,我也不是大学生。:)

编辑感谢 @ Klas 和其他人,我的问题现在归结为一个需要以编程方式解决的数学方程。

我正在寻找一个解决Linear Diophantine Equation的算法 / 代码。对于像我这样的较小的凡人,这里是这样的等式看起来像:

示例 1:3x + 4y + 5z = 25(找到 x,y,z 的所有可能值)

示例 2:10p + 5q + 6r + 11s = 224(找到 p,q,r,s 的所有可能值)

示例 3:8p + 9q + 10r + 11s + 12t = 1012(找到 p,q,r,s,t 的所有可能值)

我试着谷歌搜索无济于事。我本来以为已经写了一些代码来解决这个问题。如果你们遇到了某种已经实现了这个的库,请告诉我。如果解决方案是在 Java 中,没有什么可以更酷!。算法 / 伪代码也会做。非常感谢。

3

蛮力递归是一种选择,具体取决于您将允许值或值的数量。

假设:用户输入(被乘数)始终是不同的正整数。要找到的系数必须是非负整数。

算法:

Of the multiplicands, let M be the largest.
Calculate C=floor(F/M).
If F=M*C, output solution of the form (0,0,...,C) and decrement C
If M is the only multiplicand, terminate processing
Loop from C down to 0 (call that value i)
  Let F' = F - i*M
  Recursively invoke this algorithm:
    The multiplicands will be the current set minus M
    The goal value will be F'
  For each solution output by the recursive call:
     append i to the coefficient list and output the solution
2

我碰巧为此编写了 Java 代码。请帮助自己。解决方案没有经过广泛的测试,但到目前为止似乎运行良好。

package expt.qp;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class LinearDiophantine {
    private Map<Integer, Integer> sol = new LinkedHashMap<Integer, Integer>();
    private Map<Integer, Integer> coeff = new HashMap<Integer, Integer>();
    /**
     * @param args
     */
    public static void main(String[] args) {
        // Fill up the data
        // 3x + 4y + 5z + 3a = 25
        LinearDiophantine ld = new LinearDiophantine();
        ld.coeff.put(1, 1);ld.coeff.put(2, 2);ld.coeff.put(3, 3);ld.coeff.put(4, 4);
        Map<Integer, Integer> coeffCopy = new HashMap<Integer, Integer>(ld.coeff);
        int total=30;
        // Real algo begins here
        ld.findPossibleSolutions(total, coeffCopy);
    }
    private void findPossibleSolutions(int total, Map<Integer, Integer> coeff) {
        int index=returnLargestIndex(coeff);
        int range = (int) Math.floor(total/coeff.get(index));
        if(range*coeff.get(index) == total) {
            sol.put(index, range);
            displaySolution();
            //System.out.println();
            range--;
        }
        if(coeff.size() == 1) {
            return;
        }
        while(range>=0) {
            int remTotal = total - range*coeff.get(index);
            Map<Integer, Integer> coeffCopy = new HashMap<Integer, Integer>(coeff);
            coeffCopy.remove(index);
            sol.put(index, range);
            findPossibleSolutions(remTotal, coeffCopy);
            range--;
        }
    }
    private void displaySolution() {
        int total = 0;
        for(int i : sol.keySet()) {
            //System.out.print(coeff.get(i)+"("+sol.get(i)+"), ");
            total = total + (coeff.get(i)*sol.get(i));
        }
        if(total != 30)
            System.out.print(total+",");
    }
    /**
     * @param coeff
     */
    private int returnLargestIndex(Map<Integer, Integer> coeff) {
        int largestKey = coeff.keySet().iterator().next();
        for(int i : coeff.keySet()) {
            if(coeff.get(i)>coeff.get(largestKey)) {
                largestKey=i;
            }
        }
        return largestKey;
    }
}
2

这是一个数学问题,而不是一个编程问题。一旦你有了一个合适的算法,推动它不应该太难。

我建议你谷歌在丢番图方程。

我找到了一个explanation给你。

1

加上 Klas 非常准确的答案:

Hilbert ’ s 10th problem asked if an algorithm existed for determining whether an arbitrary Diophantine equation have a solution.Such an algorithm does exist for the solution of first-order Diophantine equations.However,the impossib

取自:Wolfram MathWorld

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(623)
Calm an:如何以编程方式在Android应用程序中切换两种语言
上一篇
Ios手机升级和电脑升级的区别:自动升级 iOS应用程序(upgrading ios)
下一篇

相关推荐

  • ios翻译屏幕轻松实现精准翻译

    iOS翻译屏幕是一种可以帮助用户在不同语言之间进行翻译的功能。它可以帮助用户在不同语言之间进行交流,从而更好地理解他们的对话。要实现iOS翻译屏幕,需要使用Apple的Natural Language API,它可以帮助您识别文本中的语言,并将其转换为另一种语言。…

    2023-04-25 12:30:26
    0 10 28
  • ios手机解压软件最佳iOS解压软件,轻松解压文件!

    iOS手机解压软件是指能够将压缩文件(如ZIP,RAR,7z等)解压缩到iOS手机上的软件。目前市面上有很多iOS手机解压软件,比如WinZip,iZip,Unzip,Zip Browser,iUnarchive等。以WinZip为例,它可以帮助用户在iOS设备上快速解压缩ZIP、RAR、7z、TAR、GZIP和BZIP2文件。它还可以将文件加密,并将其传输到其他设备。…

    2023-06-16 06:59:41
    0 20 63
  • ios系统最新版本是多少全新体验,更强大的功能

    iOS系统最新版本是iOS 代码为14.0。iOS 14支持iPhone 6s及以上机型,提供了全新的App Library、Widgets、Picture in Picture、Siri等功能。…

    2023-07-15 02:04:29
    0 63 39
  • ios移动应用开发:如何使用iOS开发构建出色的移动应用

    iOS移动应用开发是指使用iOS开发工具(如Xcode)和iOS SDK(Software Development Kit)开发iOS应用程序。iOS SDK提供了一系列的API,可以帮助开发者构建出丰富多彩的iOS应用程序。…

    2023-11-03 05:17:37
    0 96 82
  • taptap有ios版的吗?在iOS上享受精彩游戏体验

    答:Taptap是一款Android和iOS平台上的游戏应用,它提供了大量的游戏下载、评论和发现功能。Taptap有iOS版本,可以在App Store上进行下载。代码如下:…

    2023-04-15 14:18:08
    0 15 99
  • ios什么时候可以微信双开:iOS 11及以上版本可以实现微信双开

    iOS微信双开可以在iOS 3及以上版本使用,下面是实现双开的代码:// 判断是否支持双开…

    2023-04-30 16:08:25
    0 29 43
  • ios如何关闭家庭邀请:如何在iOS上关闭家庭共享邀请

    iOS家庭邀请可以通过以下代码来关闭:// 关闭家庭邀请…

    2023-04-28 03:24:47
    0 63 28
  • ios主题链接从入门到精通

    iOS主题链接是一种可以让用户在应用之间进行快速切换的功能,可以让用户在不同的应用中使用相同的主题。下面是一段示例代码,它可以帮助你实现iOS主题链接:…

    2023-12-16 15:46:49
    0 39 10

发表评论

登录 后才能评论

评论列表(53条)