I meet a problem when I do the exercise in UVa Online Judge.
Here is the link to this problem. http://ift.tt/1bD5Sc7
To solve this problem, like 13 3(number of different values) 9 2 1, I need to solve the problem like 9x + 2y + 1z = 13(x >= y >= z), which is equal to the problem 9x + 11y + 12z = 13, is it have non-negative integer roots?
I use the algorithm to calculate all the possibilities of sum 9x + 11y + 12z with the changing the value of x, y, z.
Here is my code,
#include <cstdio>
#include <vector>
using namespace std;
int main(){
int nb_case;
int k;
scanf("%d", &nb_case);
vector<int> right;
for (k = 0; k < nb_case; k++){
int i, j;
int total = 0;
int nb_value = 0;
int cache_coin = 0;
scanf("%d %d", &total, &nb_value);
vector<int> coins;
scanf("%d", &cache_coin);
coins.push_back(cache_coin);
for (i = 1; i < nb_value; i++){
scanf("%d", &cache_coin);
coins.push_back(cache_coin + coins[i - 1]);
}
vector<bool> possible;
possible.push_back(true);
for (i = 1; i <= total; i++){
possible.push_back(false);
}
for (i = 0; i < nb_value; i++){
for (j = coins[i]; j <= total; j++){
possible[j] = possible[j] | possible[j - coins[i]];
}
}
if (possible[total]){
right.push_back(1);
} else{
right.push_back(0);
}
}
for (k = 0; k < nb_case; k++){
if (right[k] == 1){
printf("YES\n\n");
}
else{
printf("NO\n\n");
}
}
}
The judge system told me that it is wrong answer.
But I find a similar problem in SPOJ. http://ift.tt/1H61qAF
These two problems just have slight differences which is not related to the core algorithm. I change the code.
#include <cstdio>
#include <vector>
using namespace std;
int main(){
int nb_case = 1;
int k;
vector<int> right;
for (k = 0; k < nb_case; k++){
int i, j;
int total = 0;
int nb_value = 0;
int cache_coin = 0;
scanf("%d %d", &total, &nb_value);
vector<int> coins;
scanf("%d", &cache_coin);
coins.push_back(cache_coin);
for (i = 1; i < nb_value; i++){
scanf("%d", &cache_coin);
coins.push_back(cache_coin + coins[i - 1]);
}
vector<bool> possible;
possible.push_back(true);
for (i = 1; i <= total; i++){
possible.push_back(false);
}
for (i = 0; i < nb_value; i++){
for (j = coins[i]; j <= total; j++){
possible[j] = possible[j] | possible[j - coins[i]];
}
}
if (possible[total]){
right.push_back(1);
} else{
right.push_back(0);
}
}
for (k = 0; k < nb_case; k++){
if (right[k] == 1){
printf("YES\n\n");
}
else{
printf("NO\n\n");
}
}
}
my code is correct in SPOJ.
I cannot figure out why it is not correct with UVa Online Judge.
I test many example for my algorithm but I cannot find an error.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire