+ 3
A challenging programming excercise for beginners
So, I came across a challenging programming excercise. And I think it is a good practice for beginner/intermediate programmers. "Write a program that outputs all possibilities to put + or - or nothing between the numbers 1,2,…,9 (in this order) such that the result is 100. For example 1 + 2 + 3 - 4 + 5 + 6 + 78 + 9 = 100." Succes!
5 Antworten
+ 4
This is extremely easy, there are only 3^8 possibilities, hence brute force will work. Still a nice exercise for beginners.
+ 4
I think this should be correct, but I did not really check it, as I'm quite tired right now. Quite a short and not too inelegant code, although the approach is rather naive:
https://code.sololearn.com/ctbncskQ1Nxi
+ 4
@Krishneel Nair: I think you should read the problem again. All your code does is to print the numbers up to the user input and put alternating signs in between.
+ 1
in C++
int user_input = 0;
cin >> user_input ;
for (int i = 1; i <= user_input; i++)
{
if (i == user_input )
{
cout << i << " = ";
}
else if (i % 2)
{
cout << i << " - ";
}
else
{
cout << i << " + ";
}
}
0
@Tobi You might be right. I change it to beginner