2017-01-20 1 views
-3

これは問題Codeforces-D2A-129 Cookiesの解決方法です。プログラム全体のロジックが正常に動作しているようですが、間違った答えです。

43 44 96 33 23 42 33 66 53 87 8 90 43 91 40 88 51 18 48 62 59 10 22 20 54 6 13 63:プログラムであるテストケース11に間違った答えを出力します2 56 31 52 98 42 54 32 26 77 9 24 33 91 16 30 39 34 78 82 73 90 12 15 67 76 30 18 44 86 84 98 65 54 100 79 28 34 40 56 11 43 72 35 86 59 89 40 30 33 7 19 44 15

私のプログラム:

#include <cstdio> 
#include <algorithm> // for count() and sort() 
#include <vector> 
using namespace std; 

// function prototype 
void erase_duplicates(vector<int> &, int, int); 

int main() 
{ 
    int num; // number of bags of cookies 
    int total = 0; // total number of cookies 
    int ways = 0; // number of ways Olga can take a bag of cookies 
    int duplicates = 0; // number of duplicates of an element 
    scanf("%i", &num); // getting number of bags of cookies 

    vector<int> cookies(num); // number of cookies in the ith bag 
    // getting number of cookies in each bag 
    for(int i = 0; i < num; i++) 
     scanf("%i", &cookies[i]); 

    for(int j = 0; j < num; j++) // calculating total number of cookies 
     total += cookies[j]; 

    // sorting the input 
    sort(cookies.begin(), cookies.end()); 

    for(int k = 0; k < cookies.size(); k++) 
    { 
     if((total - cookies[k]) % 2 == 0) 
     { 
      // adding number of duplicates of the current element to the number of ways 
      duplicates = count(cookies.begin(), cookies.end(), cookies[k]); 
      ways += duplicates; 
      // erasing the duplicates of that element 
      erase_duplicates(cookies, cookies[k], k); 
     } 
    } 
    //printing the possible number of ways 
    printf("%i", ways); 
    return 0; 
} 

// This function erases the duplicates of the element passed as the second argument. 
// Parameters are: vector of integers, element, index of the element. 
void erase_duplicates(vector<int> &cookies, int value, int k){ 
    for(int i = k; i < cookies.size(); i++){ 
     if(cookies[i] == value) // if it is a duplicate, remove it. 
      cookies.erase(cookies.begin() + i); 
    } 
} 

私のコードが悪いのか?

+3

です* - ?コードをデバッグないのはなぜ?デバッグはプログラム全体の作成プロセスの一部です。また、重複を消去するには、 'std :: unique'を使います。 – PaulMcKenzie

+0

デバッガを使ってコードをステップ実行する方法を学ぶ必要があるかもしれません。良いデバッガを使用すると、プログラムを1行ずつ実行し、どこからずれているかを確認することができます。これはプログラミングをする場合に不可欠なツールです。詳しい読書:** [小さなプログラムをデバッグする方法](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver

+0

ありがとう、ありがとう。学んだ教訓。 –

答えて

0

実際にソートする必要はありません。 問題は次のように解決できます。 最初にすべての配列要素を合計し、偶数要素と奇数要素の数を求めます。 次に、配列要素の合計が奇数の場合、奇数のクッキーを持つバッグは盗まれる可能性があるので、答えは奇数の要素の数になり、合計が偶数であれば偶数のクッキーを持つ袋の数になります。

ロジック - 2つの奇数の違いは、2つの偶数番号の
違いは、私のコードで間違って何*さえも、

#include<iostream> 
#include<vector> 

using namespace std; 
int main() 
{ 
    int n; 
    vector<int> num; 
    cin>>n; 
    num.resize(n); 
    int even=0,odd=0,sum=0; 
    for(int i=0;i<n;i++) 
    { 
     cin>>num[i]; 
     sum+=num[i]; 
     if(num[i]%2) 
     odd++; 
     else even++; 
    } 
    if(sum%2) 
     cout<<odd<<endl; 
    else cout<<even<<endl; 
    return 0; 
} 
関連する問題