编号变量产生另一个编号变量

时间:2016-12-29 00:44:55

标签: java math numbers

所以你从四个变量开始。

Variable A = Any #;
Variable B = Any #;
Variable C = 1;
Variable D = 1;

变量C& D总是等于1。

问题:

C& S需要多少次添加D与变量A和A相关联。 D当变量C可能将其自身添加到D计数而变量D可能将其自身添加到C计数。

例如

Variable A = 4;
Variable B = 7;

Output -> 4

这是因为模式(C = 1,D = 1):

C+=D        C+=D        D+=C        D+=C         / Equations    
C=2 D=1 (1) C=3 D=1 (2) C=3 D=4 (3) C=3 D=7 (4) / Output (4 in total)

如何获得进一步的结果?

嗯,我想出了一种方法,如果你倒退去弄清楚它会增加的总量。

count = 0;

if(A == B)
{
   "return impossible";
}
else if (A > B)
{
    A = A - B;
    count++;
}
else if (B > A)
{
    B = B - A;
    count++;
}

???令人困惑的问题???

这适用于较低的数字,但问题是程序必须能够处理高达10 ^ 50的数字。我的所有变量都存储在BigInteger变量中,我已经完成了4/5个测试用例。由于运行时错误,最后一个测试用例失败。测试案例未知,但我预感到它是10 ^ 50。我的问题是,是否有任何方法可以优化解决方案以更快地接收增加的数量,或者可能是用方程解决问题的另一种方法?提前谢谢!

进一步调试以上伪代码(我的代码):

    public static void main (String[] args) {
    System.out.println(answer("5000000000000000000000000000000000000000", "5")); //vA big number and a small number
}

public static String answer(String M, String F) 
{
    String str = testPossibilities(M, F);

    return str;
}

public static String testPossibilities(String M, String F)
{
      BigInteger nM = new BigInteger(M);
      BigInteger nF = new BigInteger(F);
      BigInteger inc = new BigInteger("1");
      BigInteger count = new BigInteger("0");

      while (BigInteger.valueOf(1).compareTo(nM) == -1 || BigInteger.valueOf(1).compareTo(nF) == -1) 
      {
          BigInteger offset = new BigInteger("" + (nM.divide(new BigInteger("2"))));

          System.out.print(nM + " " + nF + "\n"); // Print results

          if(nM.compareTo(nF) == 0 || BigInteger.valueOf(1).compareTo(nM) == 1 || BigInteger.valueOf(1).compareTo(nF) == 1) // If equal then not possible
          {
              return "impossible";
          }
          else if(nM.compareTo(nF) == 1)
          {
              if(nM.compareTo(nF.multiply(offset)) == 1)
              {
                  nM = nM.subtract(nF.multiply(offset));
                  count = count.add(nF.multiply(offset));                 
              }
              else
              {
                  nM = nM.subtract(nF);
                  count = count.add(inc);                     
              }
          }
          else if(nF.compareTo(nM) == 1)
          {
               nF = nF.subtract(nM);
               count = count.add(inc);
          }

      }

      if (BigInteger.valueOf(1).compareTo(nM) == 0 && BigInteger.valueOf(1).compareTo(nF) == 0) //If everything went ok then return the number
      {
          return "" + count;
      }

      return "impossible";

}

2 个答案:

答案 0 :(得分:1)

我想出了如何减少数字。

例如,如果您的目标是A = 24且目标B = 5,则答案为8。

如何?伪代码

count = 0;

A/B = 4; // as an integer
count += A/B; //Why? See how many times B can go into A w/ out overreaching A's value

Then 

A%B = 4; // as an integer

Now apply the subtraction method but with A being set equal to the modulus of A%B.

 A=4 B=5 // New subtraction values.

 count = 0;

 if(A == B)
 {
   "return impossible";
 }
 else if (A > B)
 {
    A = A - B;
    count++;
 }
 else if (B > A)
 {
    B = B - A;
    count++;
 }

这也适用于大数字。

解释是伪代码,因为问题是如何根据情况减少大数字。

答案 1 :(得分:1)

我参与了一个更快的算法,计算(5000000000000000000000000000000000000000,5)需要将近一秒钟,并且它基于mod

  1. ( A != B ) && ( A != 0 ) && ( B != 0 )时保持计算。
  2. count =除最大/最小值(从nF到nM)。如果它大于0,则返回该值,否则返回1.
  3. nM = nM mod nF。
  4. nF = nF mod nM。
  5. 然后,在迭代之后我们可以分析结果:

    1. 如果nF或nM达到0,但另一个数字是!= 1,则不可能。
    2. 否则将成功的计数返回给M和F.
    3. 方法:

      public static String answer( String M, String F )
      {
           BigInteger nM = new BigInteger(M);
           BigInteger nF = new BigInteger(F);
           long count = 0;
      
      
           while ( !nM.equals ( nF ) && !nM.equals ( new BigInteger ( "0" ) ) && !nF.equals ( new BigInteger ( "0" ) ) ) 
           {
               BigInteger divide = (nF.max ( nM )).divide ( nF.min ( nM ) );
               count += divide.compareTo ( new BigInteger ( "0" ) ) == 1 ? divide.longValue ( ) : 1;
               BigInteger originalNM = nM;
               BigInteger originalNF = nF;
               nM = originalNM.mod ( originalNF );
               nF = originalNF.mod ( originalNM );
               System.out.println(nM + " " + nF + " " + count); // Print results
           }
           if (nM.intValue ( ) == 0 && nF.intValue ( ) != 1) return "impossible " + (count-1);
           if (nM.intValue ( ) != 1  && nF.intValue ( ) == 0) return "impossible " + (count-1); 
      
           return "" + (count-1);
      
      }
      

      I / O示例:

      Input: 
      ( "5000000000000000000000000000000000000000" , "5" )
      
      Output: 
      0 5 6873995514006732800
      impossible 6873995514006732799
      
      Input:
      ( "123123123" , "43" )
      
      Output:
      19 43 2863328
      19 5 2863330
      4 5 2863333
      4 1 2863334
      0 1 2863338
      2863337
      
      Input:
      ( "4" , "7" )
      
      Output:
      4 3 1
      1 3 2
      1 0 5
      4
      

      注意:此算法会一直运行到0,因此正确的count将为count-1