It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
we did this problem today in math class to kill time after a quiz, and i was wondering if i could make a program along these lines
so the idea is this:
take the number 123,456,789
the first digit from the left is divisible by 1, good, continue
the first 2 digits from the left are divisible by 2, good, continue
the first 3 digits from the left are divisible by 3, good, continue
the first 4 digits from the left are NOT divisible by 4, you get a remainder, bad, restart with different numbers, or go back until one of the numbers can be replaced (the even spaces are pretty interchangeable)
the tricky part here is that you cant use the same integer twice
the only place we can be sure about is the fifth place, so we can tell that the number will look something like: _ _ _, _ 5 _, _ _ _
i want the program to print the number(s) which is(are) perfectly divisible, all the way to the ones place. im pretty sure that only one number fits this criteria, but id like to know if this is true, which is why im trying to make this program.
what i need help with is how i should go about checking if each number divided by its place has no remainder, and how to go back if it doesnt fit.
pretty new to coding..anything helps
Programming is just describing a series of steps to a computer
Edit - sorry, rereading the question it would be easier to store the value as a long integer and then increase it in a loop.
In which case you will have to understand how to get the first 3 digits by dividing by 10^6 (or whatever) and then taking only the integer part.
You migth also want the modulus operator %
You could setup a loop, that uses the modulus operator (%) to get the remainder of a number. I would setup an array with a legnth of 10, and modulus the number by 10 and put the result into the array (because you will read the numbers in reverse you need to place them into the array in reverse), until you have each digit.
I would then setup an int (named maybe value), and put the first value in the array, into this int. I would also designate another int (named maybe place) and set it equal to 1. Define a bool (named maybe flag).
I would then enter a loop so that you have the equation if(value % place == 0) flag = 0.
Then have the loop break if flag == 1, and goto a label before the loop, where you have an algorithm that generates a new number (maybe somthing as simple as adding one, or what ever you want). Also inside the loop if place == 10 and your last value in the number is 0, you need to print the number, and then jump to your label from earlier, before the loop, to pick a new number. Also don't forget that if flag == 0, and place != 10 (the number hasn't failed yet, but isn't at the end yet) , that you need to multiply the value by 10 , then add the next number in the array to be able to test the next place. And you also need to increase place by 1 each time.
You said you're new, so I'll include a few definations:
'=' sets a variable equal to somthing, '==' is used to test equality
'!=' means "not equal to". It's also used to test variables against other variables or numbers.
'%' or modulus divides two numbers but rather returns the remainder (ex. 10 % 3 = 1, because 10 / 3 = 9 remainder 1)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to figure this one out on my own and I can't.
The professor is asking for this.
User will define the length of the sequence.
User will then enter numbers from any range being both neg and pos.
The program:
Find the two largest numbers and display in descending order.
Find the two smallest numbers and display in ascending order.
Can't use sort or arrays.
I have an outline but, my head just wants to use arrays..... I don't see this simple without an array. Confused. I'm a beginner. I just want help. Advice? Tutor?
Start with a solution to an easier problem, and grow it into a solution to the actual problem that you are solving:
Write a program that finds and prints the largest number entered by the user. This is easy to do with a single variable keeping track of "high watermark"
Modify your program to keep track of the smallest number as well. You can do it by adding another variable, and keeping track of the "low watermark".
The challenge in both tasks above is the initial value of the high/low watermark. This is a common source of errors; there are multiple Q&As on SO explaining the fix.
Now for the fun part:
Modify your program to keep track of the second-largest number by "demoting" the number that was previously considered largest to second-largest each time that you find a number that is larger than the largest one, and by replacing the second-largest when you find a value above it that does not exceed the largest value.
This would require you to write a couple of if statements.
Finally, modify your program to keep track of the second-smallest number by applying the "mirror image" of the algorithm above.
Algorithm ("Find the two largest numbers in a sequence without storing the sequence")
(I decided to undelete this answer. It might be helpful to OP to get away from the array frame of mind)
Input: A sequence of values S.
Output: Values Max and SecondMax.
The consumed, current value of S is E
1 : Set Max to E
2 : Set SecondMax to E (Second value in S)
3 : If Max < SecondMax -> Swap Max and SecondMax.
4 : while ( e:= E exists)
4.1 : If ( e > Max ) SecondMax := Max, Max := e
4.1.1 : Else If ( e > SecondMax ) SecondMax := e
This is trivially extended to the minimums.
Have to make program, where you input number, and program outputs most repeated digit in it, can't figure out have to do it. Tried some things, for me it works with static array, but I need dynamic, so I dont now what to do.
Can someone help me?
make an array in size of 10 (the number of digits)
run over your original array and extract from each number all its digit, increase the value of your digit array for each digit you find.
-find the maximal digit using the sight array.
You can upload a code if you want more help
I assume you are served in real time digits [0-9] and you need a function that at any given time returns you the most frequent digit seen so far. A simplest solution would be just to have a hash map of [0-9] keys that maintains the number of times every digit is seen. When you need the most frequent digit you iterate over the 10 keys and return the one with the biggest count.
Recently I was looking through some interview questions, and found some interesting one:
You are given a list of word. Find if two words can be joined to-gather to form a palindrome. eg Consider a list {bat, tab, cat} Then bat and tab can be joined to gather to form a palindrome.
Expecting a O(nk) solution where n = number of works and k is length
There can be multiple pairs, just return true if found one.
Also, in the comments one of the approaches was this:
1) Add the first word to the trie ( A B)
2) Take the second word (D E E D B A) and reverse it (A B D E E D)
3) See how many letters in the reversed word you can match in the trie (the first 2)
4) Take the rest of the string (D E E D) see if it is a palindrome if it is you are done return true
5) add the second word to the trie (D E E D B A)
6) go back to step 2 with the next word
7) when out of words return false
But in my opinion this is not an O(nk) solution.
Can anyone suggest a solution?? Or explain why the algorithm described above is O(nk)??
The algorithms is correct, or at least it gets quite close. There are minor technical issues. In step 4. one should save the proposition of a solution if it's better than the current one, and in step 7. return it, or say it was impossible to make a palindrome.
The main idea is to process words into cores and prefixes. If a core is a palindrome, then we need to match the prefix with other word. Trie serves as a "database" for processed strings, so with each new word, one can check all possible extensions. If words were kept separately one would need to compare prefixes of each word separately.
(Edit: I think there still is a small loophole, in case there are two words in a trie which starts the same, and the incoming one would make a palindrome with the shorter one, but not the longer, but I won't go into details. Handling it would complicate the algo but wouldn't affect complexity.)
It also is O(n*k). Adding and checking a prefix vs a trie takes number of steps proportional to the number of characters. So in this case this is bound by k. Just like tree operations are O(h) where h is the height of the tree. So in conclusion:
k steps.
takes k steps.
also takes at most k steps.
also takes less than k steps but we can bound it by k.
also takes k steps.
Steps 2 to 5 are done n-1 times.
Of course each step has a different dominant operation, so it is hard to specify the exact constant, but all of them are bound by k so the complexity is O(c*(n-1)*k) which essentially is O(n*k).
There's a really interesting discussion of this in an article from Dr. Dobbs, way back in 2004. The full explanation is a little long, but the general idea is:
Suppose you start with Lion, where the pivot is left of the actual word. I can calculate the center of the string, which is position two. The pivot is at zero, so the string is too heavy on the right, but at the moment, Lion qualifies as a partial palindrome. The "dot" at the pivot point matches the dot at the pivot point, so there is at least one correct character, albeit the same character. You now wish to prepend words that end with noil, attempting to convert the string to noil.Lion. I use to mean any string of characters. If you're successful, then you need to locate words starting with so that they can be appended to the string.
Note that he defines a partial palindrome as:
A string is a partial palindrome if, working from the pivot point outwards, either the left or right end of the string is encountered before a mismatch occurs.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the number of anagrams which are palindromes in a string?
Example : string = "aaabbbb";
Possible anagram's which are palindromes "abbabba" , "bbaaabb" and "bababab".
The problem here is the time, i have string of size 10^9.
here's my final code can anybody tell me what's the wrong with it ?
Every letter in your input string has to appear in an even amount, execpt one letter can appear in an odd amount. This letter has a fixed position in the palindron. It has to be exactly in the middle. Lets say the amounts of the letter a,b,c,... are #a, #b, #c, ...
You only care about half of those letters, because in an palindron, the second half depands of the first half. So we only use half of the letters:
I used the floor function, so I calculate the letter, which appears in an odd amount, correct.
So how many permutations are in the first half? This is a case of distinct permutation, so we get
possibilities.
For your example:
string = "aaabbbb";
We get: #a=3, #b=4. Therefore
We get 3 palindroms, these are "abbabba" , "bbaaabb" and "bababab", like you posted.
So, if you have a very large string:
Count the amounts of each letter
Check, if there is only 1 letter that appears in an odd amount. It there are more, you can't create palindroms.
Use the formular to calculate the number of different palindroms.
Since each side of the anagram must be a mirror image of the other, the number of anagrams we care about is basically just the number of anagrams we can form on one side, so:
group the characters in the string so identical characters are together (e.g., by sorting).
Check for an odd number of more than one character (of so, # anagrams = 0).
Take half the characters of each group of identical (truncating in the case of odd number).
Compute the number of unique permutations of those characters.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to change some equations from input file to a "readable" form.
Currently I need to read all numerical values (as substrings, will convert them to int later) within a string P. All the values I'm interested with are between "+" and "n" characters (in this order for one loop, and the inverted order for other loop).
I need a loop which finds all of them and save them to array (of unknown size, since I don't know how long the string P will be).
Input examples (strings P in quotes):
"+n2+-n"
First loop (reads from + to n) so substrings C are: "", "-"
The second loop (reads from n to +) so substrings E are: "2", ""
"+2n3+3n2+n"
First loop: "2", "3", ""
Second loop: "3", "2", ""
"+-n14+-11n+1"
First loop: "-", "-11"
Second loop: "14", ""
I could add "+" to the end of the P string if solution requires.
ps. If someone's have an idea how to extract a constant from the end of string seen in example 3 (+1, or any other) I would really appreciate. The hard thing is I cannot tell how long it'll be (can by +1 can be -300000 or so).
Please consider to take a look at regular expressions (in general) and the new std::regex class from c++0x (in particular).
C++0x: Regular Expressions
Regular expressions are always a elegant solution if you want to parse any more complex patterns.
I didn't really understood your question, but what I personally use whenever i have unknown-length / extremely big inputs is a linked list , that you could implement in C++ with dynamic memory allocation , which is also possible in C with malloc(), if you ever find that you need C syntax.
As with the parsing of the input, you can use a variable initialized with 1, and you just multiply it with -1 every time you get to the end of a substring, and use simple if statement that covers each case. Or you could use a char that takes either "n" or "+", and so you have to make the if statement cover only a few lines of code, after you're done with the actual parsing, and whenever your cursor gets to the above-mentioned char variable, it acts like it's done it's job for another substring.