Next Challenge!
Moderator: Coders of Rage
Next Challenge!
okay, here is a nice simple one.
remember when you did old addition
as in.
226
+74
----
300
well if you notice there were 2 carries.
so lets say for every carry, you assign a difficulty point.
your job is to take 2 numbers and tell me how many carries occured.
another example would be.
59216
+43115
---------
102331
there were 3 carries this time.
a full licenced version of VS2008 will be up for offer. the entry that solves the problem with the most innovative way wins. if there are 2 alike, i will hold a poll that will decide who wins.
edit : this progam can be done in 10 lines of code easily. probably less.
remember when you did old addition
as in.
226
+74
----
300
well if you notice there were 2 carries.
so lets say for every carry, you assign a difficulty point.
your job is to take 2 numbers and tell me how many carries occured.
another example would be.
59216
+43115
---------
102331
there were 3 carries this time.
a full licenced version of VS2008 will be up for offer. the entry that solves the problem with the most innovative way wins. if there are 2 alike, i will hold a poll that will decide who wins.
edit : this progam can be done in 10 lines of code easily. probably less.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Next Challenge!
here is a hint. RECURSION
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Next Challenge!
jeez, i must be more nerdy than i thought.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- M_D_K
- Chaos Rift Demigod
- Posts: 1087
- Joined: Tue Oct 28, 2008 10:33 am
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C/++
- Location: UK
Re: Next Challenge!
I'm working on it. It's just hard to think right now
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
Re: Next Challenge!
Heh, don't worry about it.avansc wrote:jeez, i must be more nerdy than i thought.
Here's a simple Java class that solves the problem:
Code: Select all
public class Addition {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter first number: ");
int p = input.nextInt();
System.out.print("Enter second number: ");
int q = input.nextInt();
System.out.println("Carries of " + p + " + " + q +
" = " + numberCarries(p, q, 0));
}
public static int numberCarries(int p, int q, int n) {
if ((p + q) < 10) {
return n;
} else {
if ( ((p % 10) + (q % 10)) < 10)
return numberCarries(p / 10, q / 10, n);
else
return numberCarries((11 + p) / 10, q / 10, n + 1);
}
}
}
Re: Next Challenge!
i'll post some hints. some things i used.
i used mod, with base 10. "num%10= remainer" - this is how you isolate the last digit in the number like 1234%10=4
i have 2 if statements.
and i use recursion.
armed with that you should probably come to the same function as me. its only about 6-7 lines in length
i used mod, with base 10. "num%10= remainer" - this is how you isolate the last digit in the number like 1234%10=4
i have 2 if statements.
and i use recursion.
armed with that you should probably come to the same function as me. its only about 6-7 lines in length
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Next Challenge!
Well, yeah that's exactly what I did. If you want to make the function shorter you can always do this:avansc wrote:i'll post some hints. some things i used.
i used mod, with base 10. "num%10= remainer" - this is how you isolate the last digit in the number like 1234%10=4
i have 2 if statements.
and i use recursion.
armed with that you should probably come to the same function as me. its only about 6-7 lines in length
Code: Select all
public class Addition {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter first number: ");
int p = input.nextInt();
System.out.print("Enter second number: ");
int q = input.nextInt();
System.out.println("Carries of " + p + " + " + q +
" = " + numberCarries(p, q, 0));
}
public static int numberCarries(int p, int q, int n) {
if ((p + q) < 10)
return n;
else
return ( ((p % 10) + (q % 10)) < 10) ? numberCarries(p / 10, q / 10, n) : numberCarries((11 + p) / 10, q / 10, n + 1);
}
}
Re: Next Challenge!
here is a common mistake in recursion.Kleithap wrote:Heh, don't worry about it.avansc wrote:jeez, i must be more nerdy than i thought.
Here's a simple Java class that solves the problem:
Code: Select all
public class Addition { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter first number: "); int p = input.nextInt(); System.out.print("Enter second number: "); int q = input.nextInt(); System.out.println("Carries of " + p + " + " + q + " = " + numberCarries(p, q, 0)); } public static int numberCarries(int p, int q, int n) { if ((p + q) < 10) { return n; } else { if ( ((p % 10) + (q % 10)) < 10) return numberCarries(p / 10, q / 10, n); else return numberCarries((11 + p) / 10, q / 10, n + 1); } } }
Code: Select all
public static int numberCarries(int p, int q) {
if ((p + q) < 10) {
return 0;
} else {
if ( ((p % 10) + (q % 10)) < 10)
return 0 + numberCarries(p / 10, q / 10, n);
else
return 1 + numberCarries((11 + p) / 10, q / 10);
}
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Next Challenge!
your code does not work, it produces 2 carries for the numbers 1901 and 99, it should be 3.Kleithap wrote:Well, yeah that's exactly what I did. If you want to make the function shorter you can always do this:avansc wrote:i'll post some hints. some things i used.
i used mod, with base 10. "num%10= remainer" - this is how you isolate the last digit in the number like 1234%10=4
i have 2 if statements.
and i use recursion.
armed with that you should probably come to the same function as me. its only about 6-7 lines in lengthCode: Select all
public class Addition { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter first number: "); int p = input.nextInt(); System.out.print("Enter second number: "); int q = input.nextInt(); System.out.println("Carries of " + p + " + " + q + " = " + numberCarries(p, q, 0)); } public static int numberCarries(int p, int q, int n) { if ((p + q) < 10) return n; else return ( ((p % 10) + (q % 10)) < 10) ? numberCarries(p / 10, q / 10, n) : numberCarries((11 + p) / 10, q / 10, n + 1); } }
this little challange is trickier than you might think. and illustrates why you should allways test your code.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Next Challenge!
Oops, you're right.avansc wrote:your code does not work, it produces 2 carries for the numbers 1901 and 99, it should be 3.
I've quickly made a simple one that uses an actual carry flag :D
Code: Select all
public static int numberCarries(int p, int q, int carry) {
if ((p + q) + carry < 10)
return 0;
else
return ( ((p % 10) + (q % 10) + carry) < 10) ? 0 + numberCarries(p / 10, q / 10, 0) : 1 + numberCarries(p / 10, q / 10, 1);
}
I'm always happy when my sloppiness can be of use to other people.avansc wrote:this little challange is trickier than you might think. and illustrates why you should allways test your code.
Re: Next Challenge!
hahah, thats almost verbatim what i had. nice work. unless someone gets something better you have yourself a copy of VS2008Kleithap wrote:Oops, you're right.avansc wrote:your code does not work, it produces 2 carries for the numbers 1901 and 99, it should be 3.
I've quickly made a simple one that uses an actual carry flag :DIt can probably be made more efficient but I don't have any time right now and I couldn't leave you without a solution . I didn't really have time to test this one, if you find any other bugs be sure to let me know. If I have time I'll look for a better solution tomorrow.Code: Select all
public static int numberCarries(int p, int q, int carry) { if ((p + q) + carry < 10) return 0; else return ( ((p % 10) + (q % 10) + carry) < 10) ? 0 + numberCarries(p / 10, q / 10, 0) : 1 + numberCarries(p / 10, q / 10, 1); }
I'm always happy when my sloppiness can be of use to other people.avansc wrote:this little challange is trickier than you might think. and illustrates why you should allways test your code.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Next Challenge!
Haha, great minds think alike :Pavansc wrote:hahah, thats almost verbatim what i had.
Seriously though, let everybody know when you think of another challenge, this one was pretty cool!
Re: Next Challenge!
or is it "fools seldom differ"Kleithap wrote:Haha, great minds think alike :Pavansc wrote:hahah, thats almost verbatim what i had.
Seriously though, let everybody know when you think of another challenge, this one was pretty cool!
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Next Challenge!
Well, yeah, that's probably more accurate.
Re: Next Challenge!
Are you serious about VS2008? I mean it's really cool, but I don't live in the US so the costs sending me a copy are probably relatively high.avansc wrote:hahah, thats almost verbatim what i had. nice work. unless someone gets something better you have yourself a copy of VS2008