0
write a program which decompose this number into terms.
Input: 4 Output: 1+1+1+1 1+1+2 1+3 4
2 Answers
+ 1
Here is a code that works for addition, in C++ :
#include<iostream>
#include<sstream>
#include<string>
#include<stack>
int main()
{
int v; std::cin>>v; std::stringstream ss; std::stack<std::string> stk;
for(int i=0;i<v;i++)
{
for(int j=0;j<i;j++) ss<<"1+";
ss<<v-i; stk.push(ss.str()); ss.str("");
}
while(!stk.empty()) {std::cout<<stk.top()<<std::endl; stk.pop(); }
}
0
// your problem solved in C++ code
#include <iostream>
using namespace std;
int main()
{
cout << "Input: ";
short n;
do cin >> n;
while (!cin || n < 0);
cout << "Output:\n";
for (short line = 1; line <= n; ++line)
{
for (short i = 0; i != n - line; ++i)
cout << "1 + ";
cout << line << '\n';
}
}