我的伪代码有意义吗?

时间:2014-01-29 16:07:59

标签: java pseudocode

i.stack.imgur.com/tOfrn.png

我相信我的代码现在是万无一失的。我现在会写出伪代码。但我确实有一个问题。为什么DRJava要求我在if语句之外返回一些东西?正如你所看到我为前写的:“返回1;”只是因为它问。但它永远不会返回那个值。有人可以向我解释一下吗?

public class assignment1question2test {
  public static void main(String[] args) {

    int[] a = new int[1];
    int l = 0;
    int r = a.length-1;

    for(int i=0; i<=r; i++) {
      a[i] = 1;
    }
    a[0] = 10;

    for (int i=0; i<=r; i++) { 
      System.out.println(a[i]);
    }

    System.out.print(recursiveSearch(a,l,r));

  }

  public static int recursiveSearch (int[] a, int l, int r) {

    int third1 = (r-l)/3 + l;
    int third2 = third1*2 - l + 1;

      if (r-l == 0) {
        return l;
      }

      System.out.println("i will be checking compare from " + l + " to " + third1 + " and " + (third1 + 1) + " to " + third2);
      int compareResult = compare(a,l,third1,third1 + 1, third2);

      if(r-l == 1) {
      if (compareResult == 1) {
        return l;
      }
      else {
        return r;
      }
      }

      if (compareResult == 0) {
        return recursiveSearch(a,third2 + 1, r);
      }
      if (compareResult == 1) {
        return recursiveSearch(a,l,third1);
      }
      if (compareResult == -1) {
        return recursiveSearch(a,third1 + 1, third2);
      }
      return 1;

  }
  public static int compare(int[] a, int i, int j, int k, int l) {

    int count1 = 0;
    int count2 = 0;

    for(int g=i; g<=j; g++) {
      count1 = count1 + a[g];
    }

    for(int g=k; g<=l; g++) {
      count2 = count2 + a[g];
    }

        if (count1 == count2) {
          return 0;
        }
        if (count1 > count2) {
          return 1;
        }
        if (count1 < count2) {
          return -1;
        }  

        return 0;
}
}

最终的假想法我认为

Algorithm: recursiveSearch (a,l,r)
Inputs: An array a, indices l and r which delimit the part of interest.
Output: The index that has the lead coin.
int third1 ← (r - l)/3
int third2 ← third1*2 - l + 1
if (r-l = 0) then
    return l
int compareResult  ← compare(a,l,third1,third1 + 1,third2)
if (r-l  = 1) then
    if (compareResult = 1) then
        return l
    else 
        return r
if (compareResult = 0) then
    return recursiveSearch(a, third2 + 1, r)
if (compareResult = "1") then
    return recursiveSearch(a,l,third1)
if (compareResult = "-1") then
    return recursiveSearch(a,third1 + 1,third2)

2 个答案:

答案 0 :(得分:0)

你应该更多地改进你的逻辑,它不考虑硬币数量是偶数的情况。

奇怪:取出一枚硬币,将剩余的硬币分成两半,然后进行比较。

偶数:将剩下的分成两半,然后进行比较。

对于递归函数,请同时定义基本情况:

当n = 1时,退还硬币。

当n = 2时,返回较重的硬币。

NumberOfCoin = r-l+1
if (NumberOfCoin = 1)
    return l;
if (NumberOfCoin = 2)
    compare(a,l,l,r,r)
        0:      Think it yourself
        -1:     Think it yourself
        1:      Think it yourself
if (NumberOfCoin is odd number)
    mid = Think it yourself
    compare(a, l, mid-1, mid+1, r)
        0:      Think it yourself
        -1:     Think it yourself
        1:      Think it yourself
if (NumberOfCoin is even number)
    mid = l+r/2
    compare(a, l, mid, mid+1, r)
        0:      Think it yourself
        -1:     Think it yourself
        1:      Think it yourself   

答案 1 :(得分:0)

String compareResult←compare(a,l,mid,mid,r)

在这里检查中间元素两次,使其成为:

String compareResult←compare(a,l,mid,mid + 1,r) 除此之外,你的算法对我来说似乎很公平。