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
In C++, I have the following input: 12345
How can I achieve this output: 1 2 3 4 5 ?
Another example could be:
input: 123
output: 1 2 3
You can simply do this:
int num = 123;
std::vector<int> digits;
while( num > 0 ) {
digits.push_back(num % 10);
num /= 10;
}
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 last month.
The community is reviewing whether to reopen this question as of 2 days ago.
Improve this question
I had come across a code solution to check whether any number if prime or not.
int isPrime(int N){
if(N<=1){
return 0;
}
if(N==2 || N==3){
return 1;
}
if(N%2==0 or N%3==0){
return 0;
}
for(int i=5;i*i<=N;i=i+6){
if(N%i==0 || N%(i+2) ==0){
return 0;
}
}
return 1;
}
Can anyone explain, why we are increasing i=i+6 and condition N%(i+2)==0??
We are checking N%i==0 then why we are checking it for i+2?
Starting from 4 every second number is not prime. Starting from 6 every third number is not prime. Now consider what is left
4 5 6 7 8 9 10 11 12 13 14 15 16 17
2+x*2 x x x x x x x
3+y*3 x x x x
others 5 7 11 13 17 19 ...
The pattern continues and has a length of 2*3 = 6.
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 3 years ago.
Improve this question
To print multiplication tables, I'm using a for loop which has fixed values up to 10th times table only. I need multiplication table as run time values. Is there an alternate loop to take the values at run time? Here is the code which i tried:
scanf(&num);
for(i=num;i<=num;i++)
for (j=1;j<=10;j++)
This is not a Python For loop
In python,you can use the Range function to have a nice multiplication table.
def table_choice(my_choice=None):
for a in range(10):
if my_choice != None:
print('{0} x {1} = {2}'.format(my_choice, a, my_choice * a))
else:
for b in range(10):
print('{0} x {1} = {2}'.format(a, b, a * b))
table_choice(my_choice = 7)
OUTPUT:
7 x 0 = 0
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
In case you execute table_choice() you will get the full table
See the Range documentation in : https://docs.python.org/3/library/functions.html#func-range
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
For example, given the tree:
10
5 15
0 6 12 20 // full
-5 2 8 14 16 22
4 18 24
26
The value returned by the function highestFull(BinaryNodeX<Comparable> *t) would be 3 as the height of the deepest full level is three.
If a node has no left or right node, you know that the deepest full level is 1 - the node itself.
If it has left and right nodes, recurse and choose the smaller.
highestFull(BinaryNodeX<Comparable> *t)
{
if ( ! t->left || ! t->right ) return 1;
return 1 + std::min( highestFull(t->left), highestFull(t->right) );
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I would like to know all possible combinations from 0-40 in sets of 2. So for example:
1 / 1 -
1 / 2 -
1 / 3 -
1 / 4 -
1 / 5 -
1 / 6 -
1 / 7 -
1 / 8 -
1 / 9 -
1 / 10
All the way to 40 and all possible combinations.
Cheers.
In haskell:
[(x, y) | x <- [1..40], y <- [1..40]]
In other languages you should probably look at for-loops: (this is C#)
Tuple<int, int>[,] things = new Tuple<int, int>[40,40];
for (int i = 0; i < 40; i++)
{
for (int j = 0; j < 40; j++)
{
things[i,j] = Tuple.Create(i+1,j+1);
}
}
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
So assume I have a file as shown below:
1 2 6 2 3 7
3 7 1 2 3 7
In C++, how can I store the values in two arrays like the ones below?
[1, 2, 6, 2, 3, 7]
[3, 7, 1, 2, 3, 7]
Use two std::vector<int>s and a std::stringstream:
std::vector<int> a, b;
std::string str1, str2;
if (std::getline(file, str1) && std::getline(file, str2))
{
std::stringstream iss(str1);
for (int n; iss >> n; )
a.push_back(n);
iss.clear();
iss.str(str2);
for (int n; iss >> n; )
b.push_back(n);
}
Take a look boost::tokenizer and, like a comment said, use std::vector.