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 having like an assesment exercise.
So given a Number, for example 12345, I must find out the sum sequence of the digits of the given number (1 + 2 +3 + 4 +5) and then add to it the result (15), and repeat this till the sum sequence of the last number is a digit (in this case is 6).
Example : 12345 + 15 + 6 = 12366;
666 + 24 + 6 = 696;
I've been thinkig to store the digits in an array, but then I realized the array's size is static. Now I'm thinking to make a linked list, but I'm not really sure. Does it involve linked lists?
Just guide me to the right path. What should I use?
There's no magic needed here. Just do the obvious computation on integers:
int reduce(int n)
{
int result = 0;
while (n != 0) { result += n % 10; n /= 10; }
return result;
}
int your_problem(int n)
{
int result = n;
while (n >= 10) { n = reduce(n); result += n; }
return result;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
result = getResult( 2, 5)
int getResult(int m, int n)
{
int ans;
if (m < n)
if (n <= 10)
ans = m + n;
else
ans = m * n;
else
ans = n / m;
return (ans);
}
I am stuck between 10 and 2,
does the second else apply because the second if is true? or do i still go with the first else?
For m = 2, n = 5, the first two if conditions are valid: m < n and n <= 10. Thus, ans = m + n = 7 and it's not modified later on, so we expect 7 as the answer.
This can be much easier deduced if you properly format your code (I did it for you in this case). Also, if you use {} in if/else, that's way easier and less error-prone.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
This is the link of the problem.
https://projecteuler.net/problem=8
below is my code.
#include <stdio.h>
int main() {
long i,sum;
long temp = 0;
long arr[1000] = {
// Increasingly large number is ommitted//
// I just add ',' between each numbers//};
for(i=0; i<988; i++){
sum = arr[i]*arr[i+1]*arr[i+2]*arr[i+3]*arr[i+4]*arr[i+5]*arr[i+6]
*arr[i+7]*arr[i+8]*arr[i+9]*arr[i+10]*arr[i+11]*arr[i+12];
if(temp<sum){
temp = sum;
}
}
printf("%ld",temp);
return 0;
}
so I got 2091059712 which seems kind of reasonable answer.
The real problem here is, that you did not account for the size of the product. An integer is 10 digits max (2,147,483,647). So this or something alike might happen:
sum = 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9;
This gives: 2,541,865,828,329 which overflows your integer leading to undefined behaviour.
Use a larger integer type or take a different approach.
That's a brute force solution that will work fine for this size of problem.
Potential improvements:
Split the array on "0", and only test the substrings that are longer than the desired length.
Print out the numbers that ended up being the best substring. That way you can test that it actually is present in the original and the multiplication is done correctly.
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 7 years ago.
Improve this question
For f(n) = f(n – 1) + f(n – 2^10), when 0 <= n < 2^10, f(n)=1 ,
write a function to compute f(n).(not using recursive method)
int compute_f(int n)
{
int result = 0;
...
return result
}
wondering is there any efficient way to do?
You can follow the same idea of fibonacci and do Dynamic Programming.
Pseudo code:
if n < 0:
//throw some exception
arr = new int[max(1024,n+1)]
for i = 0 to 1024:
arr[i] = 1
for i = 1024 to n+1:
arr[i] = arr[i-1] + arr[i-1024]
return arr[n]
Converting it to actual code is left for you.
Bonus: You can do it with O(1) extra space by holding an array of size 1024 and manipulating it and remembering your current place (use modolus operator) without changing the time complexity.
Here is my version, similar to the recursive version of Fibonacci.
int compute_f(int n)
{
if( n < 0)
return -1; //Error
if(n <= 1024)
return 1;
return (compute_f(n-1) + compute_f(n-1024));
}
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 8 years ago.
Improve this question
I am trying to figure out the following problem for an upcoming test. I have searched everywhere, and I understand the basics of recursion. What I don't understand for this particular question is the value of int n and int k respectively. I have the answer to this question as it is a practice question, but I have no idea how the answer was found.
// Precondition: n and k are non-negative integers
int f(int n, int k) {
if (k * n == 0)
return 1
else
return f(n - 1, k - 1) + f(n - 1, k)
}
What value is returned by the call f(4, 2)?
Just look at how it's called.
f(4,2) goes into 2nd block, calls f(3,1)+f(3,2)
f(3,1) calls f(2,0)+f(2,1) = 1+f(1,0)+f(1,1)=1+1+f(0,0)+f(0,1)=1+1+1+1=4
f(3,2) calls f(2,1)+f(2,2)= f(1,0)+f(1,1)+f(1,1)+f(1,2) and so on.
You should be able to work it out from here.
I am not sure what the problem is since
f(4,2)=f(3,1) + f(3,2)
=(f(2,0)+f(2,1) )+ (f(2,1) +f(2,2))
=(1 +(f(1,0)+f(1,1))+((f(1,0)+f(1,1))+(f(1,1)+f(1,2))
=(1 + 1 +(1+1)) +( 1 +(1+1) + (1+1) +1 + 1 ))
=11
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
There is any function to sum relative numbers?
Example
I Have 5 and -10 So the result should be: 15
or
-5 (+) 15 -> 20
-1 (+) 1 -> 2
There is any function in C++ to sum numbers like that?
Do you intend absolute values? You can use the abs function.
abs(-5) + abs(15) gives 20 as a result.
I don't know about such function, however you can simply create one: just add absolute values:
#include <iostream>
int sumAbs( int a, int b) {
return std::abs( a) + std::abs( b);
}
int main() {
int a = -5;
int b = 10;
std::cout << sumAbs( a, b); // 15
return 0;
}
Do you mean the difference? abs(15 - (-5)) also gives 20.