• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Finding biggest number in a string

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to get the biggest number in a string that is contained in numbers and operators randomly.
For example, -123456789 will be (98765432-1), 1234567890 will be (9876543210), 123*456+7 will be (765*432 + 1), and so on.
However, I have no idea how to split the operators and the numbers and find the operation that makes the biggest numbers.



This is a method to find and calculate the number.
And, what I'm trying to do is to make separated between operators and numbers using ArrayList above.
But, is it available to make the operations in this approach? I guess that I need to find the operations in a string without a split.
I know how to organize the numbers from the highest to the lowest in a string like below.



The quetsion is kind of messy, but could you provide some instructions how to approach?


Input
12356789
-123456789
123*456+7

Output
987654321
987665432-1 = ?
(765*432)+1 = ?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Divide and rule. That question looks like something which shou‍ld be broken down into parts. Start by describing how you would work out the largest number formed from the digits 1234567890. Also look at this Unicode link to remind yourself how the ten digits 123456890 are represented in ASCII: do the ASCII numbers an the values of the digits always run in the same order?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would an input of 123*456+7 give an output of 765*432+1? Following normal math operator order of precedence (MDAS), the output expression evaluates to 330481.  However, 7*65432+1, which uses the same digits and operations, will evaluate to 458025, which is greater.

What exactly are the rules you need to follow to determine "largest number formed from the digits"? It seems to me that it's not only just the digits that you have to use but also the operations that would affect how large the number is that you can get.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jiyoung Kim wrote:I have no idea how to split the operators and the numbers and find the operation that makes the biggest numbers.


I would suggest you stop worrying about Java entirely until you have this worked out on paper. This is really the crux of the issue. Once you know how to do this, writing the code is the easy part.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems like a sorting task. 1568 - > 8651. You can use different algorithms for that. The easiest is when we use two cycles, one is included in other. Before you need to convert string to int but you need to check token "-".
 
reply
    Bookmark Topic Watch Topic
  • New Topic