Discuz!NT|BBS|论坛

注册

 

发新话题 回复该主题

[求助]急急急,虫窝有没有C++高手 [复制链接]

1#

[求助]急急急,虫窝有没有C++高手

刚学C++,要编写一个ARRAY的简单程式,就是要看看ARRAY中有没有相同的数值,例如,

Enter integers (-1 to finish):
list[0]: 1
list[1]: 2
list[2]: 3
list[3]: 4
list[4]: 5
list[5]: 6
list[6]: 5
list[7]: 4
list[8]: 3
list[9]: 2
list[10]: 1
list[11]: -1
The array has 11 elements: 1 2 3 4 5 6 5 4 3 2 1
There is a matching number in the array.
There is no matching number in the 1st half.
Press any key to continue . . .


Enter integers (-1 to finish):
list[0]: 1
list[1]: 2
list[2]: 3
list[3]: 4
list[4]: 5
list[5]: 6
list[6]: 7
list[7]: 8
list[8]: 9
list[9]: 10
list[10]: 11
list[11]: -1
The array has 11 elements: 1 2 3 4 5 6 7 8 9 10 11
There is no matching number in the array.
Press any key to continue . . .

以下是我的code
  1. #include
  2. using namespace std;

  3. void display( const int list[], int n );
  4. void read( int list[], int &n );
  5. bool isMatch(const int a[], int length);

  6. int main()
  7. {
  8.     const int MAXSIZE = 100;
  9.     int a[MAXSIZE] = { 0 }, size;
  10.     
  11.     read(a, size);
  12.     
  13.     cout << "The array has " << size << " elements: " << flush;
  14.     display(a, size);
  15.     
  16.     if (size > 0) {
  17.         if (!(isMatch(a, size))){
  18.              cout << "There is no matching numbers in the array." << endl;
  19.         }
  20.         else {
  21.              cout << "There is a matching number in the array." << endl;
  22.              if (size > 4){
  23.                 if (!(isMatch(a, size/2)))
  24.                    cout << "There is no matching number in the 1st half."
  25.                         << endl;
  26.                 else
  27.                    cout << "There is a matching number in the 1st half."
  28.                         << endl;  
  29.              }
  30.         }      
  31.     }
  32.         
  33.     system("pause" );
  34.     return 0;
  35. }//end main

  36. void display( const int list[], int n )
  37. {
  38.   for( int i = 0; i < n; i++ )
  39.      cout << list[i] << " ";
  40.   cout << endl;    
  41. }

  42. void read( int list[], int &n )
  43. {
  44.   cout << "Enter integers (-1 to finish): " << endl;
  45.   n = 0;
  46.   do
  47.   {    
  48.     cout << "list[" << n << "]: ";  
  49.     cin >> list[n];
  50.   }
  51.   while( list[n++] != -1 );
  52.   --n;  // don't count the last element
  53. }

  54. // Check if there is a match in the array up to length
  55. bool isMatch(const int a[], int length)
  56. {

  57.      if (length > 1)
  58.         {
  59.              for( int i = 0; i < length - 1; i++ )
  60.              for( int j = 0; j < length - 1; j++ )
  61.                    if( a [i] == a [j]) return true;    
  62.       }      
  63.       return false;
  64. }
复制代码
但是出来的程式有问题,我想应该是最后的bool isMatch 出了问题,有没有人可以告诉我怎么写才对?? 谢谢了:c::c::c::c::c::c::c::c:

[ 本帖最后由 SEELE 于 2007-6-1 06:40 PM 编辑 ]
分享 转发
TOP
2#
这是C style的写法,C++应该写成class

C++有STL,直接用Vector就好。

有现成的没必要自己重复造车轮
TOP
3#
#include
using namespace std;

void display( const int list[], int n );
void read( int list[], int &n );
bool isMatch(const int a[], int length);

int main()
{
    const int MAXSIZE = 100;
    int a[MAXSIZE] = { 0 }, size;
    
    read(a, size);
    
    cout << "The array has " << size << " elements: " << flush;
    display(a, size);
    
    if (size > 0) {
        if (!(isMatch(a, size))){
             cout << "There is no matching numbers in the array." << endl;
        }
        else {
             cout << "There is a matching number in the array." << endl;
             if (size > 4){
                if (!(isMatch(a, size/2)))
                   cout << "There is no matching number in the 1st half."
                        << endl;
                else
                   cout << "There is a matching number in the 1st half."
                        << endl;  
             }
        }      
    }
        
    system("pause" );
    return 0;
}//end main

void display( const int list[], int n )
{
  for( int i = 0; i < n; i++ )
     cout << list << " ";
  cout << endl;    
}

void read( int list[], int &n )
{
  cout << "Enter integers (-1 to finish): " << endl;
  n = 0;
  do
  {    
    cout << "list[" << n << "]: ";  
    cin >> list[n];
  }
  while( list[n++] != -1 );
  --n;  // don't count the last element
}

// Check if there is a match in the array up to length
bool isMatch(const int a[], int length)
{

     if (length > 1)
        {
             for( int i = 0; i < length ; i++ )
             {
             for( int j = 0; j < length ; j++ )
             {
                   if(i != j)
                   {
                   if( a == a [j]) return true;
                   }
             }
                  
             }
      }      
      return false;
}
TOP
4#
谢谢各位,我已经找到问题所在了,现在的CODE是每次只要一找到不同的数就return false, 而且inner loop 应该从 i+1 开始, 要找的是重复的数,所以 if(a==a[j]) return ture,所有loop完了以后再return false 应该就可以了
  1. bool isMatch(const int a[], int length)
  2. {
  3.      if (length > 1)
  4.      {
  5.        for( int i = 0; i < length - 1; i++ )
  6.        {
  7.          for( int j = i+1; j < length - 1; j++ )
  8.          {              
  9.            if (a[i] == a[j]) return true;
  10.          }
  11.        }      
  12.      }
  13.      return false;
  14. }
复制代码
我想这样应该就可以了,谢谢各位
TOP
发新话题 回复该主题