10055 - Hashmat the Brave Warrior

All about problems in Volume 100. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

rahid
New poster
Posts: 9
Joined: Tue Oct 13, 2009 7:13 am
Location: Asian University of Bangladesh
Contact:

Re: Hashmat Warrior

Post by rahid »

This problem wants the output must be in positive. For the input 100 200 it should be output 100. But what about the input 200 100? Your code should give the output -100. But it should be 100. So make it possible.

Thats it!!

:D :) :D
anik.bit0104
New poster
Posts: 6
Joined: Thu Sep 23, 2010 1:50 pm
Location: Dhaka, Bangladesh

Re: 10055 - Hashmat the Brave Warrior

Post by anik.bit0104 »

You should have used a loop. Your code would be terminated after one computation.
anik.bit0104
New poster
Posts: 6
Joined: Thu Sep 23, 2010 1:50 pm
Location: Dhaka, Bangladesh

Re: 10055 - Hashmat the Brave Warrior

Post by anik.bit0104 »

I'm having runtime error in my java code of this program after submitting. But in my eclipse ide, it works fine.
Can you guys tell me what's the problem?

class Vol0Prob10055Hashmat {

public static void main(String[] args) {
java.util.Scanner sc=new java.util.Scanner(System.in);


while(sc.hasNextLong()){

System.out.println(java.lang.Math.abs(sc.nextLong()-sc.nextLong()));
}
}
}
raine_iut
New poster
Posts: 2
Joined: Fri Nov 05, 2010 12:23 am

Re: cannot the understand the mistake

Post by raine_iut »

asif_khan_ak_07 wrote:which data type should i use?

long long
Last edited by raine_iut on Sat Jan 15, 2011 7:36 am, edited 1 time in total.
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: cannot the understand the mistake

Post by mf »

No, it ain't.
raine_iut
New poster
Posts: 2
Joined: Fri Nov 05, 2010 12:23 am

Re: cannot the understand the mistake

Post by raine_iut »

mf wrote:No, it ain't.

the minimal range of ''unsigned long int'' is 1 less than the highest possible input of this problem, is this the reason?
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: cannot the understand the mistake

Post by mf »

Yes.

On a 32-bit OS (which UVa judge uses), "unsigned long int" is an unsigned 32-bit integer, and so it can't represent 2^32, which is a possible input in this problem.
mashaheer
New poster
Posts: 4
Joined: Sat Nov 20, 2010 2:23 pm

Re: 10055 - Hashmat the Brave Warrior

Post by mashaheer »

hi all.
i got AC from both below codes.now my question is:
in all problem this 2 way of inputing and outputing are correct?or each one has special situation?
in this code:
get each input and print the output: ----------------------
first get all inputs and after eof print outputs:
Last edited by mashaheer on Sun Nov 21, 2010 10:17 pm, edited 1 time in total.
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: 10055 - Hashmat the Brave Warrior

Post by mf »

Yes, you may read all the input before printing the output, if you'd like.
But it's more usual to just read and solve the test cases one-by-one.
mashaheer
New poster
Posts: 4
Joined: Sat Nov 20, 2010 2:23 pm

Re: 10055 - Hashmat the Brave Warrior

Post by mashaheer »

thanks dear mf!
shahidul
New poster
Posts: 1
Joined: Wed Mar 02, 2011 12:41 pm

Re: 10055 - Hashmat the Brave Warrior

Post by shahidul »

#include <iostream>
#include<stdio.h>
using namespace std;

int main()
{
unsigned int a,b;
while(scanf("%u %u",&a,&b)!=EOF){
if(a>b)printf("%u\n",(a-b));
else printf("%u\n",(b-a));
}
return 0;
}
moxlotus
New poster
Posts: 31
Joined: Sat Sep 17, 2011 6:47 am

Please enlighten me!

Post by moxlotus »

This is my first time attempting a problem on UVa. Therefore I have decided to pick something really easy, so that I will be able to figure out how does the online judge works.
The problem is 10055 - Hashmat the Brave Warrior. A really straightforward problem for the novice. As I used VS2008 to compile the codes, there are some compilation errors that i have received when i submitted it online.(no compilation error with my VS2008) Fortunately, I was able to solve the compilation errors by searching it online.

Now the issue is that I have got a Wrong Answer as my verdict which i don't see anything wrong with my solution.

Can someone out there please enlighten me? is my handling of input and output correct?
The question says Input:standard input, Output: standard ouput. Does that means i m only need to use cout/cin?

Code: Select all

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>

using namespace std;

vector<string> INPUTS;
vector<int> OUTPUTS;
void core()
{

	int loop = INPUTS.size();
	for(int i = 0; i < loop; i++)
	{

		int first = 0, second = 0;
		string temp = INPUTS[i];
		int count = temp.size();
		first = atoi(&temp[0]);
		for(int j = 0; j < count; j++)
		{
			if(isspace(temp[j]))
			{
				second = atoi(&temp[j + 1]);
				break;
			}
		}
		OUTPUTS.push_back(second - first);
	}
}
int main(int argc, char *argv[])
{
	ifstream the_file ("Input.txt");
	if (!the_file.is_open())
	{
		cout<<"Could not open file\n";
		//system("pause");
		return 0;
	}
	else {
		string x;
		while ( !the_file.eof())
		{
			getline(the_file, x);
			INPUTS.push_back(x);
		}

		the_file.close();
	}

	core();
	ofstream file("output.txt");
	if(!file.is_open())
	{
		cout<<"Fail to create dump file"<<endl;
		//system("pause");
		return 0;
	}
	else
	{
		//output to the output file
		for(int a = 0; a <OUTPUTS.size(); a++ )
		{
			file<<OUTPUTS[a]<<endl;
		}
	}
	file.close();
	return 0;
}

spalac24
New poster
Posts: 1
Joined: Sat Jul 02, 2011 7:15 am

Re: Please enlighten me!

Post by spalac24 »

The input is made from stdin, you'll need to use cin, scanf() or other functions that help you with that.
sum_sim
New poster
Posts: 4
Joined: Fri May 21, 2010 12:13 pm

10055 Getting TLE

Post by sum_sim »

Can anyone tell me what is wrong with the code? I've tried several times but I'm getting TLE. Please help.

Code: Select all

import java.util.*;
import java.io.*;

public class Main {
	
	public static void main(String args[]){
		long a, b, c;
		Scanner cin = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		while(cin.hasNextLong()){
			a = cin.nextLong();
			b = cin.nextLong();
			
			c = a-b;
			
			if(c<0) c *=-1;
			
			System.out.println(c);
		}
	}
}
sum_sim
New poster
Posts: 4
Joined: Fri May 21, 2010 12:13 pm

10055 Getting TLE

Post by sum_sim »

Can anyone tell me what is wrong with the code? I've tried several time but getting TLE.
  • hasNext();
  • hasNextLong()
none of them are working. Please help.

Code: Select all

import java.util.*;
import java.io.*;

public class Main {
	
	public static void main(String args[]){
		long a, b, c;
		Scanner cin = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		while(cin.hasNextLong()){
			a = cin.nextLong();
			b = cin.nextLong();
			
			c = a-b;
			
			if(c<0) c *=-1;
			
			System.out.println(c);
		}
	}
}
Post Reply

Return to “Volume 100 (10000-10099)”