![]() |
#11 | ||
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2005
Location: ,
Posts: 454
|
![]() 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! |
||
![]() ![]() |
|
![]() |
#12 | ||
![]() ![]() ![]() ![]() ![]() |
![]() 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 |
||
![]() ![]() |
|
![]() |
#13 | ||
![]() ![]() ![]() ![]() ![]() |
![]() 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 |
||
![]() ![]() |
|
![]() |
#14 | ||
![]() ![]() ![]() Join Date: Aug 2004
Location: Split, Croatia
Posts: 1,028
|
![]() Quote:
|
||
![]() ![]() |
|
![]() |
#15 | ||
![]() ![]() ![]() Join Date: Aug 2004
Location: Split, Croatia
Posts: 1,028
|
![]() 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. |
||
![]() ![]() |
|
![]() |
#16 | ||
![]() ![]() ![]() Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
|
![]() 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. |
||
![]() ![]() |
|
![]() |
#17 | ||
![]() ![]() ![]() ![]() ![]() |
![]() 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 |
||
![]() ![]() |
|
![]() |
#18 | ||
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2005
Location: ,
Posts: 454
|
![]() 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! |
||
![]() ![]() |
|
![]() |
#19 | ||
![]() ![]() ![]() Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
|
![]() 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. |
||
![]() ![]() |
|
![]() |
#20 | ||
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2005
Location: ,
Posts: 454
|
![]() Take a look at this easy exam.
The input is the height of the piramide you have to print it. Code:
height=4 * ** * * * ** * * * * * *
__________________
Never mess with me when I have a cougar, Never! |
||
![]() ![]() |
|
![]() |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Discussion Of Exams | Kon-Tiki | Programming | 8 | 30-03-2007 07:27 PM |
How Often Do You Cheat On Exams / Tests | nace | Blah, blah, blah... | 40 | 16-10-2006 07:57 PM |
Solution To Exams | Kon-Tiki | Programming | 0 | 08-08-2005 05:37 PM |
Exams | Unknown Hero | Programming | 0 | 12-04-2005 08:40 PM |
Thread Tools | |
Display Modes | |
|
|
||
  |