1 Replies - 1964 Views - Last Post: 25 September 2012 - 07:14 AM

#1 captainhampton  Icon User is offline

  • Jawsome++;
  • member icon

Reputation: 14
  • View blog
  • Posts: 548
  • Joined: 17-October 07

Determine Bouncy Numbers

Posted 31 December 2011 - 09:47 AM

Description: A more thorough explanation is provided in the comment section above the snippet.
/*
Author: Vincent Russo

	Working from left-to-right if no digit is exceeded by the digit to its left
	it is called an increasing number; for example, 134468.

	Similarly if no digit is exceeded by the digit to its right it is called a
	decreasing number; for example, 66420.

	We shall call a positive integer that is neither increasing nor decreasing a
	"bouncy" number; for example, 155349.
*/

//Determines if number is "bouncy".
bool isBouncy(string num){

	int upCount = 0, downCount = 0;

	//Keep track of number form.
	for (int i = 0; i < num.length()-1; i++){
		if (num[i] <= num[i+1])
			upCount++;
		if (num[i] >= num[i+1])
			downCount++;
	}//end for

	//If either increasing or decreasing number, it's not bouncy.
	if (upCount == num.length()-1 || downCount == num.length()-1)
		return false;
	return true;
}

int main(){

	//Test cases.
	string num1 = "155349";
	string num2 = "134468";
	
	isBouncy(num1);		//True
	isBouncy(num2);		//False
	
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Determine Bouncy Numbers

#2 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 103
  • View blog
  • Posts: 218
  • Joined: 23-September 12

Re: Determine Bouncy Numbers

Posted 25 September 2012 - 07:14 AM

Project Euler? Me too. Still trying to figure out how to get to googol, though. I'm betting there's a closed form expression for the number of bouncy numbers less than 10^n, but I certainly haven't cracked it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1