如何检查一个数字是否乘以某物,但会增加某物

发布于 2025-02-04 03:25:42 字数 1067 浏览 2 评论 0原文

因此,我一直在尝试制作某种计算器工具。功能之一是检查一个因子是否乘以某物,但同时又增加了其他因素。这有助于考虑三项官方。我找到了这些因素,但我不知道该如何进行。到目前为止,这就是

public static void MA() {
     int sum = 0;
     int product = 0;
     ArrayList<Integer> Factors = new ArrayList<Integer>();

     
     
     System.out.println("Enter the sum of the number");
     sum = sc.nextInt();
     System.out.println("Enter the number it should multiply to");
     product = sc.nextInt();

if(product < 0){
  for(int i = product; i <= Math.abs(product); ++i) {

      // skips the iteration for i = 0
      if(i == 0) {
        continue;
      }
      else {
        if (product % i == 0) {
          Factors.add(i);
        }
}
    }
  }
  else{
    for (int i = 1; i <= product; ++i) {
  
      // if number is divided by i
      // i is the factor
      if (product % i == 0) {
        Factors.add(i);
      }
    }
  }  

  System.out.println(Factors);
   }

我的程序架构是一个主要类别,它呼吁所有其他功能。

示例:x^2 + 9x + 20 =(x + 5)(x + 4)

4*5 = 20 4+5 = 9

So I've been trying to make some sort of a calculator tool. One of the function is to check if a factor of a number multiplies to something but at the same time adds to something else. This is helpful for factoring a trinomial. I found the factors but I don't know how to proceed. This is what I have so far

public static void MA() {
     int sum = 0;
     int product = 0;
     ArrayList<Integer> Factors = new ArrayList<Integer>();

     
     
     System.out.println("Enter the sum of the number");
     sum = sc.nextInt();
     System.out.println("Enter the number it should multiply to");
     product = sc.nextInt();

if(product < 0){
  for(int i = product; i <= Math.abs(product); ++i) {

      // skips the iteration for i = 0
      if(i == 0) {
        continue;
      }
      else {
        if (product % i == 0) {
          Factors.add(i);
        }
}
    }
  }
  else{
    for (int i = 1; i <= product; ++i) {
  
      // if number is divided by i
      // i is the factor
      if (product % i == 0) {
        Factors.add(i);
      }
    }
  }  

  System.out.println(Factors);
   }

Keep in mind the architecture of my program is a main class that calls on all other functions.

EXAMPLE: X^2 + 9x + 20 = (x+5)(x+4)

4*5 = 20
4+5 = 9

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖不如嘢 2025-02-11 03:25:42

您的问题可以简洁地表示如下:

给定list&lt; integer&gt;,找到其2个元素,总计为给定数字。

一个简单的解决方案是:

List<Integer> factors = new ArrayList<>(); // populated elsewhere
int sum = ?; // whatever
Integer a = null;
Integer b = null;

outer:
for (int i = 0; i < factors.size() - 1; i++) {
    for (int j = i + 1; j < factors.size(); j++) {
        if (factors.get(i) + factors.get(j) == sum) {
            a = factors.get(i);
            b = factors.get(j);
            break outer;
        }
    }
}

如果a和b不是null,则找到了sum的一对。

Your problem can be succinctly expressed as follows:

Given a List<Integer>, find 2 of its elements that sum to a given number.

A simple solution to which is:

List<Integer> factors = new ArrayList<>(); // populated elsewhere
int sum = ?; // whatever
Integer a = null;
Integer b = null;

outer:
for (int i = 0; i < factors.size() - 1; i++) {
    for (int j = i + 1; j < factors.size(); j++) {
        if (factors.get(i) + factors.get(j) == sum) {
            a = factors.get(i);
            b = factors.get(j);
            break outer;
        }
    }
}

If a and b are not null, a pair that summed to sum was found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文