all possible combinations to divide pack of candies - c++

I have problem to solve and I'm stuck, I don't know how to start.
Suppose I have R childrens and S candies. I want to divide candies between childrens. Each child can get 0, 1, 2, 3 or 4 candies. How to find all the possibilities of such a division?
#include <iostream>
using namespace std;
void solve(int r, int s) {
if (s == 0)
{
cout << "no more candies" << endl;
return;
}
if (r == 0)
{
cout << "last child" << endl;
return;
}
for (int j = 0; j < 4 && j <= s; ++j)
{
cout << "r: " << r << " j: " << j << endl;
solve(r-1, s - j);
}
}
int main () {
int r, s;
cin >> r >> s;
solve(r, s);
return 0;
}
For now I have sth like this, I see in output that I have solutions here, but I don't know how to grab and store all possibilities into for example vector.

Just store counts and save variants at the last recursion level
vector<int> counts;
vector<vector<int>> sol;
void solve(int r, int s) {
if (s == 0)
{
sol.push_back(counts);
return;
}
if (r == 0)
{
return;
}
for (int j = 0; j <= 4 && j <= s; ++j)
{
counts[r - 1] += j;
solve(r - 1, s - j);
counts[r - 1] -= j;
}
}
int main() {
int r, s;
r = 3;
s = 5;
for (int j = 0; j < r; ++j)
counts.push_back(0);
solve(r, s);
for (int i = 0; i < sol.size(); i++) {
for (int j = 0; j < sol[i].size(); j++) {
cout << sol[i][j] << ' ';
}
cout << endl;
}
return 0;
}

Related

How do this program but in reverse, pattern

so i want output like this
1
123
12345
123
1
i already make the program but it only output these, and im confused how to output the bottom triangle
1
123
12345
here's my program
#include <iostream>
using namespace std;
int main() {
int n = 3 ;
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
return 0;
}
#Mojtaba's answer is a perffect extension to your approach.
However, I wanted to provide another method that is generally used in creating such strings that are formatted in a particular manner. It is common to create the entire pattern line by line and then print to the console all at once.
I have appropriately commented the code for your reference and it should be easy to understand:
#include <iostream>
#include <vector>
void pattern(int n) {
std::vector<std::string> lines; // store the first n lines to print later
int length = 2*n - 1; // length of each line
for(int i = 0; i < n; i++) {
std::string str = std::string(length, ' ');
for(int j = 1; j <= 2*i + 1; j++) {
str[n - i + j - 2] = j + '0';
// indexing can be figured by observing the pattern
}
lines.emplace_back(str);
}
for(int i = 0; i < n; i++) {
std::cout << lines[i] << std::endl;
}
for(int i = n-2; i >= 0; i--) {
std::cout << lines[i] << std::endl;
}
return;
}
int main() {
int n;
std::cin >> n;
pattern(n);
}
I added another for loop exactly like yours with different order from n-1. I modified your code to this:
int main() {
int n = 3 ;
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
for (i = n - 1; i >= 1; i--) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
return 0;
}
Now it returns:
1
123
12345
123
1

How do I output all numbers with a given amount of divisors? (C++)

I need to enter 2 integers, n and k, n is the range and k is the amount of divisors for those numbers, and only display the numbers with k divisors.
#include <iostream>
using namespace std;
int main()
{
int n, k,cnt=0;
cin>>n;
cin>>k;
for(int i=1; i<=n; i++)
{
if(n%i==0)
{
cnt++;
}
if(k==cnt)
cout<<i<<" ";
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, k;
cin>>n>>k;
for(int i = 1; i <= n; i++){
int num_divisors_i = 0;
for (int j = 1, len = sqrt(i); j <= len; j++) {
if (i % j == 0) {
if (i / j == j) {
num_divisors_i++;
}
else {
num_divisors_i = num_divisors_i + 2;
}
}
}
if(num_divisors_i == k) cout<<i<<" has "<<k<<" divisors"<<endl;
}
}
This was my solution:
#include<iostream>
int main(int argc, char* argv[]) {
int n = 0, k = 0;
std::cout << "Enter the range: ";
std::cin >> n; if (!std::cin) throw std::runtime_error("range read failed");
std::cout << std::endl << "Enter the number divisors: ";
std::cin >> k; if (!std::cin) throw std::runtime_error("number of divisors read failed");
std::cout << "numbers with " << k << " divisor:: ";
for (int i = 1; i != n+1/*if k is inclusive else n*/; ++i) {
int cnt = 0;
for (int j = 1; j != i+1; ++j) {
if (i % j == 0)
cnt += 1;
}
if(cnt == k)
std::cout << i << ' ';
}
return 0;
}

program which print a matrix in spiral way. ( is not working fine)

I need to write a program which displays the elements of a matrix in a spiral way.My program does not work fine.Here's the code:
#include <iostream>
using namespace std;
void citireMatrice(int a[100][100], int n) // function to read a matrix
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
cout<<"a[" << i << "][" << j << "]=";
cin >> a[i][j];
}
}
}
void spiral(int a[100][100], int n)
{
int i, j, k;
if (n % 2==0)
{
k = n / 2;
}
else
{
k = n / 2 + 1;
}
for (i = 1; i <= k; ++i)
{
for (j = 1; j <= n - i + 1; ++j)
{
cout << a[i][j] << " ";
}
for (j = i + 1; j <= n - i + 1; ++j)
{
cout << a[j][n - i + 1] << " ";
}
for (j = n-i; j >= i; j--)
{
cout << a[n - i + 1][j] << " ";
}
for (j = n-1;j>=i+1;j--)
{
cout << a[j][i];
}
}
}
int main()
{
int a[100][100];
int n;
cout << "n=";
cin >> n;
citireMatrice(a, n);
spiral(a, n);
return 0;
}
If I enter n=2 with the elements 1, 2, 3, 4 it displays 4 -858993460 and other numbers like this.Where's my mistake?
You're properly using zero based indexing of arrays in citireMatrice but in spiral you're using one based indexing.
You need to start your loops at 0, and end at < n. (Consider what element of a will be first to be printed out.)

HackerRank says ~no response on stdout~. C++

I wrote this solution for the absolute permutation problem on HackerRank. It works fine on dev-C++ but doesn't work on Hackerrank. I've found that the code produces output when I remove the abs_perm(). What's the problem here?
#include <iostream>
using namespace std;
int arr[100000];
int check(int n, int k)
{
if ( (2*k == n) || (k == 0) || (n - 4*k == 0) )
return 1;
else if (k < n/2)
return check(n - 4*k, k);
else
return 0;
}
void swap(int &a, int &b)
{
int c = b;
b = a;
a = c;
}
void ini(int n)
{
for (int i = 0; i < n; i++)
{
arr[i] = i+1;
}
}
void abs_perm(int n, int k)
{
for (int i = 0; i < k; i++)
{
swap(arr[i], arr[k+i]);
}
if (2*k == n)
return;
for (int i = n - 1; i > n - k - 1; i--)
{
swap(arr[i], arr[i-k]);
}
if (n - 4*k == 0)
return;
abs_perm(n - 4*k, k);
}
int main()
{
int T;
cin >> T;
int N[T], K[T];
for (int i = 0; i < T; i++)
{
cin >> N[i] >> K[i];
}
for (int i = 0; i < T; i++)
{
cout << N[i] << " " << K[i] << "\n";
}
for (int i = 0; i < T; i++)
{
if ( !check(N[i], K[i]) )
cout << "-1\n";
else
{
ini(N[i]);
abs_perm(N[i], K[i]);
for (int j = 0; j < N[i]; j++)
{
cout << arr[j] << " ";
}
cout << "\n";
}
}
return 0;
}
Array is a structure to use when you know at compile time the dimension of your structure. What you wrote at the begin in abs_perm() is not correct for standard compilers (in fact you don't know the dimension of your array). You can use a std::vector or a std::list which allocate memory dynamically or (bad solution) you can allocate an array with dimension that certainly contains all elements you will put inside.

Explain this line of code

can someone explain when this line of code ends ? :
void constituteSubsequence(int i){
if( Pred[i] + 1) constituteSubsequence(Pred[i]);
cout << a[i] << " ";
}
In this program that calculate the longest increasing subsequence :
#include <iostream>
using namespace std;
int Pred[1000]; //Pred is previous.
int a[1000], v[1000], n, imax;
void read() {
cout << " n = ";
cin >> n;
cout << " Sequence: ";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
}
void constituteSubsequence(int i) {
if (Pred[i] + 1) constituteSubsequence(Pred[i]);
cout << a[i] << " ";
}
void calculate() {
int i, j;
v[0] = 1;
imax = 0;
Pred[0] = -1;
for (int i = 1; i < n; i++) {
v[i] = 1;
Pred[i] = -1;
for (int j = 0; j < i; j++) {
if (a[j] < a[i] && v[j] + 1 > v[i]) {
v[i] = v[j] + 1;
Pred[i] = j;
}
if (v[i] > v[imax]) {
imax = i;
}
}
}
}
void write() {
cout << " Longest Increasing Subsequence : ";
constituteSubsequence(imax);
cout << endl << " Length: " << v[imax];
}
int main() {
read();
calculate();
write();
return 0;
}
If I run this code,it compiles and works as expected,but how does that condition repeat itself after it found a 0 value (false) and it print cout << a[i] ? .And when does it stop ?
In C++ an integer expression can be treated as a Boolean. For example, in the context of if statement Pred[i] + 1 means (Pred[i] + 1) != 0
This provides the answer to your question: the chain of recursive invocations is going to end when Pred[i] is -1. Of course, an easier to read way to express the same condition would be with the != operator:
if( Pred[i] != -1) {
constituteSubsequence(Pred[i]);
}