-4

程序没有输入它应该输入的 if 语句。例如,当 sentence1 是 oguzhan 并且 sentence2 是第一个字符的 bugrahan 时,它应该输入第一个 if 语句结束替换应该是 4,但它没有。

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cmath>

using namespace std;

int main() {
  char sentence1[50];
  char sentence2[50];
  int m, n, k, l;
  int i, j, substitution;
  cout << "Enter the first word:" << endl;
  cin >> sentence1;
  cout << "Enter the second word:" << endl;
  cin >> sentence2;
  m = strlen(sentence1);
  n = strlen(sentence2);
  int cost[m + 1][n + 1];
  cost[0][0] = 0;

  for (i = 1; i < m + 1; i++) {
    cost[i][0] = cost[i - 1][0] + 2;

  }
  for (j = 1; j < n + 1; j++) {
    cost[0][j] = cost[0][j - 1] + 2;

  }

  for (i = 1; i < m + 1; i++) {
    for (j = 1; j < n + 1; j++) {

      if ((sentence1[i - 1] == 'a' || sentence1[i - 1] == 'u' ||
           sentence1[i - 1] == 'e' || sentence1[i - 1] == 'i' ||
           sentence1[i - 1] == 'o') &&
          (sentence2[j - 1] != 'a' || sentence2[j - 1] != 'u' ||
           sentence2[j - 1] != 'e' || sentence2[j - 1] != 'i' ||
           sentence2[j - 1] != 'o')) {
        substitution = 4;
      }

      if ((sentence1[i - 1] != 'a' || sentence1[i - 1] != 'u' ||
           sentence1[i - 1] != 'e' || sentence1[i - 1] != 'i' ||
           sentence1[i - 1] != 'o') &&
          (sentence2[j - 1] == 'a' || sentence1[i - 1] != 'u' ||
           sentence1[i - 1] != 'e' || sentence1[i - 1] != 'i' ||
           sentence1[i - 1] != 'o')) {
        substitution = 4;
      }

      if (sentence1[i - 1] == sentence2[j - 1]) {
        substitution = 0;
      }

      if ((sentence1[i - 1] == 'a' || sentence1[i - 1] == 'u' ||
           sentence1[i - 1] == 'e' || sentence1[i - 1] == 'i' ||
           sentence1[i - 1] == 'o') &&
          (sentence2[j - 1] == 'a' || sentence2[j - 1] == 'u' ||
           sentence2[j - 1] == 'e' || sentence2[j - 1] == 'i' ||
           sentence2[j - 1] == 'o')) {
        substitution = 3;
      }
      if ((sentence1[i - 1] != 'a' || sentence1[i - 1] != 'u' ||
           sentence1[i - 1] != 'e' || sentence1[i - 1] != 'i' ||
           sentence1[i - 1] != 'o') &&
          (sentence2[j - 1] != 'a' || sentence2[j - 1] != 'u' ||
           sentence2[j - 1] != 'e' || sentence2[j - 1] != 'i' ||
           sentence2[j - 1] != 'o')) {
        substitution = 3;
      }

      cost[i][j] = min(min(cost[i - 1][j] + 2, cost[i][j - 1] + 2),
                       cost[i - 1][j - 1] + substitution);
    }
  }

  for (i = 0; i < m + 1; i++) {
    for (j = 0; j < n + 1; j++) {

      cout << cost[i][j] << "  ";
    }
    cout << endl;
  }

  cout << sentence1[0];
  return 0;
}
4

1 回答 1

7

像这样的条件:sentence2[j-1]!='a'||sentence2[j-1]!='u'始终为真 - 没有单个字符可以同时等于au,因此其中一个必须为真。

如果您正在使用!=,它必须几乎总是由&&, not加入||,否则结果将始终为真,无论输入如何。

于 2013-04-24T14:32:49.363 回答