Buongiorno a tutti, ho un programma in c++ da svolgere, io ho trovato una soluzione, ma ho qualche problema nella lettura dell'input: se inserisco un numero alla volta funziona, ma se inserisco tutto in una volta sola, facendo il copia-incolla dal Sample, si blocca dopo il caso 2... (nel codice riga 58) e devo interrompere manualmente l'esecuzione.
Testo (in inglese, se serve lo traduco...):
Problem
There are N houses for sale. The i-th house costs Ai dollars to buy. You have a budget of B dollars to spend.
What is the maximum number of houses you can buy?
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a single line containing the two integers N and B. The second line contains N integers. The i-th integer is Ai, the cost of the i-th house.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum number of houses you can buy.
Sample
In Sample Case #1, you have a budget of 100 dollars. You can buy the 1st and 3rd houses for 20 + 40 = 60 dollars.
In Sample Case #2, you have a budget of 50 dollars. You can buy the 1st, 3rd and 4th houses for 30 + 10 + 10 = 50 dollars.
In Sample Case #3, you have a budget of 300 dollars. You cannot buy any houses (so the answer is 0).
Soluzione da me svolta:
Allora, copiando l'esempio intero da questa domanda, funziona!
Il testo del problema arriva da qui: https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc7/00000000001d3f56
Se copio l'esempio dalla pagina del link non funziona, aiutatemi a capire...
Testo (in inglese, se serve lo traduco...):
Problem
There are N houses for sale. The i-th house costs Ai dollars to buy. You have a budget of B dollars to spend.
What is the maximum number of houses you can buy?
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a single line containing the two integers N and B. The second line contains N integers. The i-th integer is Ai, the cost of the i-th house.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum number of houses you can buy.
Sample
Input | Output |
3 4 100 20 90 40 90 4 50 30 30 10 10 3 300 999 999 999 | Case #1: 2 Case #2: 3 Case #3: 0 |
In Sample Case #2, you have a budget of 50 dollars. You can buy the 1st, 3rd and 4th houses for 30 + 10 + 10 = 50 dollars.
In Sample Case #3, you have a budget of 300 dollars. You cannot buy any houses (so the answer is 0).
Soluzione da me svolta:
Codice:
#include <iostream>
using namespace std;
int* NewArray(int N)
{
int* p;
p= new int[N];
return p;
}
int* Minimo (int *v, int N, int start)
{
int j;
int *p;
p=NewArray(2);
for(j=start;j<N;j++)
{
if(j==start)
{
p[0]=v[j];
p[1]=j;
}
if(v[j]<p[0])
{
p[0]=v[j];
p[1]=j;
}
}
return p;
}
//Ordina un Array
void sortArray(int* v, int N)
{
int t,k,pos;
int *p;
for(k=0;k<N;k++)
{
p=Minimo(v, N, k);
t=v[k];
v[k]=p[0];
pos=p[1];
v[pos]=t;
}
}
int main()
{
int T,N,B,j,k;
int cont;
int*p;
cin>>T;
for(j=0;j<T;j++)
{
cont=0;
cin>>N;
cin>>B;
p=NewArray(N);
for(k=0;k<N;k++)
{
cin>>p[k]; //qui il problema
}
sortArray(p,N);
while(B>=0)
{
B=B-p[cont];
cont++;
}
cout<<endl<<"Case #"<<j+1<<": "<<cont-1<<endl;
}
}
Post unito automaticamente:
Allora, copiando l'esempio intero da questa domanda, funziona!
Il testo del problema arriva da qui: https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc7/00000000001d3f56
Se copio l'esempio dalla pagina del link non funziona, aiutatemi a capire...
Allegati
Ultima modifica: