| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
求救:怎么老是WA啊???#include <iostream.h>
int i;
void swap1(int *a,int n);
void swap2(int *a,int n);
int main()
{
int time,n,j = 1;
int cases;
cin >> cases;
while(cases--)
{
cin >> time >> n;
int sum,score;
int * a,b[3][2];
a = new int[n];
for(i = 0;i < n;i++)
cin >> a[i];
i = 0,sum = 0,score = 0;
while(sum < time)
{
if(i < n)
{
sum += a[i++];
score += sum;
}
else if(i == n)
break;
}
if(i == n && sum < time)
{
b[0][0] = i;
b[0][1] = score;
}
else if(i == n && sum >time)
{
b[0][0] = --i;
b[0][1] = score - sum;
}
else
{
b[0][0] = i;
b[0][1] = score;
}
swap1(a,n);
i = 0,sum = 0,score = 0;
while(sum < time)
{
if(i < n)
{
sum += a[i++];
score += sum;
}
else if(i == n)
break;
}
if(i == n && sum < time)
{
b[1][0] = i;
b[1][1] = score;
}
else if(i == n && sum > time)
{
b[1][0] = --i;
b[1][1] = score - sum;
}
else
{
b[1][0] = i;
b[1][1] = score;
}
swap2(a,n);
i = 0,sum = 0,score = 0;
while(sum < time)
{
if(i < n)
{
sum += a[i++];
score += sum;
}
else if(i == n)
break;
}
if(i == n && sum < time)
{
b[2][0] = i;
b[2][1] = score;
}
else if(i == n && sum > time)
{
b[2][0] = --i;
b[2][1] = score - sum;
}
else
{
b[2][0] = i;
b[2][1] = score;
}
cout << "Scenario #" << j++ << ":" << endl;
if(b[0][0] > b[1][0] && b[0][0] > b[2][0] && b[0][1] < b[1][1] && b[0][1] < b[2][1])
{
cout << "Bill wins with " << b[0][0];
cout << " solved problems and a score of ";
cout << b[0][1] << "." << endl << endl;
}
else if(b[1][0] > b[0][0] && b[1][0] > b[2][0] && b[1][1] < b[0][1] && b[1][1] < b[2][1])
{
cout << "Steve wins with " << b[1][0];
cout << " solved problems and a score of ";
cout << b[1][1] << "." << endl << endl;
}
else if(b[2][0] > b[0][0] && b[2][0] > b[1][0] && b[2][1] < b[0][1] && b[2][1] < b[1][1])
{
cout << "Linus wins with " << b[2][0];
cout << " solved problems and a score of ";
cout << b[2][1] << "." << endl << endl;
}
else
{
cout << "Steve wins with " << b[1][0];
cout << " solved problems and a score of ";
cout << b[1][1] << "." << endl << endl;
}
delete []a;
}
return 0;
}
void swap1(int *a,int n)
{
int t,k;
for(i = 0;i < n - 1;i++)
for(k = i + 1;k < n;k++)
if(a[i] > a[k])
{
t = a[i];
a[i] = a[k];
a[k] = t;
}
}
void swap2(int *a,int n)
{
int t,k;
for(i = 0;i < n - 1;i++)
for(k = i + 1;k < n;k++)
if(a[i] < a[k])
{
t = a[i];
a[i] = a[k];
a[k] = t;
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator