10664 - Luggage
Moderator: Board moderators
10664 - Luggage
Someone can help me.... this is my code and i recept wa???? any critical input ???
thankx
[java]
import java.io.*;
import java.util.*;
public class Main {
static String Readln(int maxLg){
byte [] lin = new byte[maxLg];
int lg = 0, car = -1;
try{
while(lg < maxLg){
car = System.in.read();
if((car<0) || (car == '\n')) break;
lin[lg++] +=car;
}
} catch(IOException e){return null;}
if(car<0 && lg==0) return null;
return(new String(lin,0,lg));
}
static void ordenar(int [] v, int size){
int i,j, max, aux;
for(i=0;i<size;i++){
max = i;
for(j=i+1;j<size;j++)
if(v[j] > v[max]) max = j;
aux = v;
v=v[max];
v[max]=aux;
}
}
public static void main(String[] args) throws IOException{
String linha;
while((linha = Readln(1000))!=null){
long sum = 0;
StringTokenizer dados = new StringTokenizer(linha);
int [] valores = new int[20];
int i = 0;
while(dados.hasMoreTokens()){
valores[i++] = Integer.parseInt(dados.nextToken());
sum+= valores[i-1];
}
if(sum%2==1 || (i==1 && sum!=0)){
System.out.println("NO");
}
else if(sum==0){
System.out.println("YES");
}
else{
ordenar(valores,i);
sum/=2;
if(valores[0]>sum){
System.out.println("NO");
}
else if(valores[0]==sum){
System.out.println("YES");
}
else{
int k;
long result = valores[0];
for(k=1;k<i;k++){
result+=valores[k];
if(result==sum){
System.out.println("YES");
break;
}
else if(result>sum){
result-=valores[k];
}
}
if(k==i){
System.out.println("NO");
}
}
}
}
}
}
[/java]
thankx
[java]
import java.io.*;
import java.util.*;
public class Main {
static String Readln(int maxLg){
byte [] lin = new byte[maxLg];
int lg = 0, car = -1;
try{
while(lg < maxLg){
car = System.in.read();
if((car<0) || (car == '\n')) break;
lin[lg++] +=car;
}
} catch(IOException e){return null;}
if(car<0 && lg==0) return null;
return(new String(lin,0,lg));
}
static void ordenar(int [] v, int size){
int i,j, max, aux;
for(i=0;i<size;i++){
max = i;
for(j=i+1;j<size;j++)
if(v[j] > v[max]) max = j;
aux = v;
v=v[max];
v[max]=aux;
}
}
public static void main(String[] args) throws IOException{
String linha;
while((linha = Readln(1000))!=null){
long sum = 0;
StringTokenizer dados = new StringTokenizer(linha);
int [] valores = new int[20];
int i = 0;
while(dados.hasMoreTokens()){
valores[i++] = Integer.parseInt(dados.nextToken());
sum+= valores[i-1];
}
if(sum%2==1 || (i==1 && sum!=0)){
System.out.println("NO");
}
else if(sum==0){
System.out.println("YES");
}
else{
ordenar(valores,i);
sum/=2;
if(valores[0]>sum){
System.out.println("NO");
}
else if(valores[0]==sum){
System.out.println("YES");
}
else{
int k;
long result = valores[0];
for(k=1;k<i;k++){
result+=valores[k];
if(result==sum){
System.out.println("YES");
break;
}
else if(result>sum){
result-=valores[k];
}
}
if(k==i){
System.out.println("NO");
}
}
}
}
}
}
[/java]
thanx
Input:
Output:
Code: Select all
16
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1
2
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
1 2 3 5 7 11 13 17 23
1 2 3 5 7 11 13 17 23 29
1 2 3 5 7 11 13 17 23 29 31
1 2 3 5 7 11 13 17 23 29 31 37
10 20 30 40 50 50
1 15 25 35 45 55
1 2 3 4 5 6 7 8 9 10 11 12 13 91
1 1 2 3 4 5 6 7 8 9 10 11 12 13 90
1 2 3 4 5 6 7 8 9 10 11 12 13 42 49
1 2 3 4 5 6 7 8 9 10 11 12 13 93
Code: Select all
YES
NO
YES
NO
YES
YES
YES
NO
YES
NO
YES
NO
YES
YES
YES
NO
And be carefule for this kind of input.(There is such, becouse I got WA from first time and only after fixing my program for this kind of test case I got AC).
Input
Output
Input
Code: Select all
1
<-----Blank line
Code: Select all
NO
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
To Cytmike.
You must read while end of line and remember them on one array.
This is how to read them by Pascal and remember that numbers on array(a[]).
[pascal]
n:=0;
while not eoln do
begin
n:=n+1;
read(a[n]);
end;
readln;
[/pascal]
You must read while end of line and remember them on one array.
This is how to read them by Pascal and remember that numbers on array(a[]).
[pascal]
n:=0;
while not eoln do
begin
n:=n+1;
read(a[n]);
end;
readln;
[/pascal]
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
-
- Learning poster
- Posts: 95
- Joined: Mon Apr 26, 2004 1:23 pm
- Location: Hong Kong and United States
- Contact:
Thank you for your help.Eduard wrote:To Cytmike.
You must read while end of line and remember them on one array.
This is how to read them by Pascal and remember that numbers on array(a[]).
[pascal]
n:=0;
while not eoln do
begin
n:=n+1;
read(a[n]);
end;
readln;
[/pascal]
But how can I do the "not eoln" function in c++?

Code
Code: Select all
[/cpp]
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
#define maxn 20*200
int a[maxn];
int m; // the size of the array a;
string s; // here will be the line saved
void parse() {
istringstream iss(s);
int num;
m = 0;
while(iss>>num) a[m++]=num;
}
int main() {
int n; cin >> n;
for(int i=0;i<n;i++) {
getline(cin,s);
parse();
....
}
}
I hope I helped .
Why NO for empty line. If there is nothing to load, two boots certainly load the same weight, 0.Eduard wrote:InputOutputCode: Select all
1 <-----Blank line
Code: Select all
NO
By the way, what means "1
I stay home. Don't call me out.