Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Search Today's Posts Mark Forums Read
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 01-03-2005, 10:20 PM   #11
NrmMyth
Hero Gamer

 
Join Date: Jan 2005
Location: ,
Posts: 454
Thumbs down

So here is one good test for all you guys who are bored, or like chalenges.
I am posting this exam so my good friend Unknown Hero (who I know of) can take a little break.

It is called "The Number" (be afraid, be very afraid) and shouldn't be hard.
Here it goes. THE NUMBER

When all natural numbers (1,2,3,4,5), started from 1 to some known N number write in a line one after another
without a space, we get a array of numbers that looks like this:

For N=16 we get { 12345678910111213141516 }.

Write an algorithm/code that will calculate the exact number od digits of that array.

INPUT:
In one and only row there is one number N, 1<=N<=100,000,000.
OUTPUT:
In one and only row on screen you have to write the needed number from the text.

TESTS:
input
5 | 15 | 120
output
5 | 21 | 252

Good luck.
__________________
Never mess with me when I have a cougar, Never!
NrmMyth is offline                         Send a private message to NrmMyth
Reply With Quote
Old 02-03-2005, 12:17 AM   #12
NeKromancer
Newbie

 
Join Date: Feb 2005
Location: ,
Posts: 21
Send a message via MSN to NeKromancer
Default

Here's the answer the Q1 in Java.

Code:
import java.io.*;

public class exam {
	
	private static String input = "";
	private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	
	/**
 * *Constructor
	*/	
	public static void main(String[] args) {
 *
 *int x = -999;
 *int y = -999;
 *
 *try {
 *	x = getNumber("x");
 *	y = getNumber("y");
 *	in.close();
 *}
 *catch(IOException ex) {
 *	ex.printStackTrace();
 *}
 *
 *System.out.print("second_root_of(x*x+y*y) is ");
 *System.out.println(secondRootOf(x,y));
 *
	}
	
	
	/**
 * Function secondRootOf(int , int)
 * 
 * @return int
	*/
	private static int secondRootOf(int x, int y)
	{
 *return x*x+y*y;
	}
	
	
	
	/**
 * *Function getNumber(String)
 * *
 * *@exception NumberFormatException
 * *@throws IOException
 * *@return int
	*/
	private static int getNumber(String which) throws IOException
	{
 *boolean stop = false;
 *int num = 0;
 *
 *while(!stop) {
 *	
 *	try {
 * *System.out.print("Enter "+which+": ");
 * *input = in.readLine();
 * *num = Integer.parseInt(input);
 * *
 * *if(num < 0) {
 * *	num = num*-1;
 * *}
 *	
 * *stop = true;
 *	}
 *	catch(NumberFormatException nfe) {
 * *System.out.println("Not a valid integer, try again");
 * *stop = false;
 *	}
 *}
 *
 *return num;
	}

}//end class
__________________
Rdy to play even older game :P
NeKromancer is offline                         Send a private message to NeKromancer
Reply With Quote
Old 02-03-2005, 08:51 PM   #13
NeKromancer
Newbie

 
Join Date: Feb 2005
Location: ,
Posts: 21
Send a message via MSN to NeKromancer
Default

Gives me a headache just reading the problem. Reminds me of math problems in school, and I hate math and hate school
__________________
Rdy to play even older game :P
NeKromancer is offline                         Send a private message to NeKromancer
Reply With Quote
Old 02-03-2005, 08:54 PM   #14
Unknown Hero
Home Sweet Abandonia

 
Join Date: Aug 2004
Location: Split, Croatia
Posts: 1,028
Default

Quote:
Originally posted by NeKromancer@Mar 2 2005, 09:51 PM
Gives me a headache just reading the problem. Reminds me of math problems in school, and I hate math and hate school
NrmMyth solved this problem without using mathemathics! k: k: k:
Unknown Hero is offline                         Send a private message to Unknown Hero
Reply With Quote
Old 02-03-2005, 09:00 PM   #15
Unknown Hero
Home Sweet Abandonia

 
Join Date: Aug 2004
Location: Split, Croatia
Posts: 1,028
Default

Here's my solution for Number:

program broj;
var n,x,i:longint;
begin
x:=0;
readln(n);
for i:= 1 to n do
begin
if (i<10) then
x:=x+1
else if (i<100) then
x:=x+2
else if (i<1000) then
x:=x+3
else if (i<10000) then
x:=x+4
else if (i<100000) then
x:=x+5
else if (i<1000000) then
x:=x+6
else if (i<10000000) then
x:=x+7
else if (i<100000000) then
x:=x+8;
end;
writeln(x);
end.



Here's optimal solution for the Number:

program broj;
var n, rj, i : longint;
begin
readln(n);
rj := n;
i := 9;
while n>i do
begin
n := n-i;
rj := rj + n;
i := i*10;
end;
writeln(rj);
end.
Unknown Hero is offline                         Send a private message to Unknown Hero
Reply With Quote
Old 02-03-2005, 09:11 PM   #16
Kon-Tiki
[BANNED]

 
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
Default

I'm not too fond of solving problems like that either. They do sound like mathproblems from school.

"If train A leaves station A at 13h40 and drives at 80Km/h and train B leaves at station C at 14h00, driving at 70Km/h, when will they cross each other at station B, which is 112Km from station A and 120Km from station B?" Kind of feels the same as that one.
Kon-Tiki is offline                         Send a private message to Kon-Tiki
Reply With Quote
Old 03-03-2005, 03:57 AM   #17
NeKromancer
Newbie

 
Join Date: Feb 2005
Location: ,
Posts: 21
Send a message via MSN to NeKromancer
Default

Answer to UnknownHero's Q2 of his Exam2.

I only had to change 1 method from my previous program for Q1, ahh the goodness of software reuse

Code:
import java.io.*;

public class exam2 {
	
	private static String input = "";
	private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	
	/**
 * *Constructor
	*/	
	public static void main(String[] args) {
 *
 *int num = -1;
 *
 *try {
 *	
 *	num = getNumber("a number");
 *	in.close();
 *}
 *catch(IOException ex) {
 *	ex.printStackTrace();
 *}
 *
 *calcWeirdMethod(num);
	}
	
	
	private static void calcWeirdMethod(int num) {

 *int x = 0;
 *
 *for(int i=1; i <= num; i++) {
 *	x = x+i;
 *}
 *
 *System.out.println("Answer is : "+x);
	}
	
	
	/**
 * *Function getNumber(String)
 * *
 * *@exception NumberFormatException
 * *@throws IOException
 * *@return int
	*/
	private static int getNumber(String which) throws IOException
	{
 *boolean stop = false;
 *int num = 0;
 *
 *while(!stop) {
 *	
 *	try {
 * *System.out.print("Enter "+which+": ");
 * *input = in.readLine();
 * *num = Integer.parseInt(input);
 * *
 * *if(num < 0) {
 * *	num = num*-1;
 * *}
 *	
 * *stop = true;
 *	}
 *	catch(NumberFormatException nfe) {
 * *System.out.println("Not a valid integer, try again");
 * *stop = false;
 *	}
 *}
 *
 *return num;
	}
	
}//end class
__________________
Rdy to play even older game :P
NeKromancer is offline                         Send a private message to NeKromancer
Reply With Quote
Old 04-03-2005, 10:39 PM   #18
NrmMyth
Hero Gamer

 
Join Date: Jan 2005
Location: ,
Posts: 454
Thumbs up

Ok! here it is, the fast solution to "Number".
Bunch of if-s and all works...

long solution(long n)
{ long digits;

if(n<10)
digits=n;
else if(n<100)
digits=(n-9)<<1+9;
else if(n<1000)
digits=(n-99)*3+189;
else if(n<10000)
digits=(n-999)<<2+2889;
else if(n<100000)
digits=(n-9999)*5+38889;
else if(n<1000000)
digits=(n-99999)*6+488889;
else if(n<10000000)
digits=(n-999999)*7+5888889;
else if(n<100000000)
digits=(n-9999999)<<3+68888889;
else digits=788888889;

return digits;
}

It is'n very prety solution but on the other hand it is fast as lighting (:whistle: kung fu fighting).
And why can't I use tabulators! :ranting:
Any questions?
__________________
Never mess with me when I have a cougar, Never!
NrmMyth is offline                         Send a private message to NrmMyth
Reply With Quote
Old 04-03-2005, 10:57 PM   #19
Kon-Tiki
[BANNED]

 
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
Default

Ok, mathless exam:

A bookstore wants to have a utility that helps them catalogue their books. They have diskettes with files containing information per book. Each book contains this as information: Book title, subtitle if it's available, a list of authors (most of the time one, but can be more), number of pages, price - with the option of seeing it without 21% tax, ISDN-number, category it belongs to and whether it's hard-cover or not.

Their utility has to be able to read in the diskette containing this information, along with listing the books alphabetically on main title, on first author (if the name's the same, on second author too. Name there the same, third author, etc), list from cheapest to most expensive, from most expensive to cheapest, per category and just all books in total.

They also should be able to change the information 'bout a book if they see it needed, or add a book to the file.

Create this utility. It's not required to have a fancy interface. Text-based's just fine.
Kon-Tiki is offline                         Send a private message to Kon-Tiki
Reply With Quote
Old 05-03-2005, 04:05 PM   #20
NrmMyth
Hero Gamer

 
Join Date: Jan 2005
Location: ,
Posts: 454
Default

Take a look at this easy exam.
The input is the height of the piramide you have to print it.

Code:
height=4

 * **
 * * *
 ** * *
* * * *
You got the point.
__________________
Never mess with me when I have a cougar, Never!
NrmMyth is offline                         Send a private message to NrmMyth
Reply With Quote
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Discussion Of Exams Kon-Tiki Programming 8 30-03-2007 08:27 PM
How Often Do You Cheat On Exams / Tests nace Blah, blah, blah... 40 16-10-2006 08:57 PM
Solution To Exams Kon-Tiki Programming 0 08-08-2005 06:37 PM
Exams Unknown Hero Programming 0 12-04-2005 09:40 PM

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 04:27 PM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.