0% found this document useful (0 votes)
91 views17 pages

Yolo

The document contains C code to print all possible combinations of a given size from an input array. It defines a combinationUtil function that recursively prints all combinations by replacing elements at each index. It also contains a main function that takes input array and size, calls combinationUtil to print all combinations, and provides sample output.

Uploaded by

Shubhangi Ojha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views17 pages

Yolo

The document contains C code to print all possible combinations of a given size from an input array. It defines a combinationUtil function that recursively prints all combinations by replacing elements at each index. It also contains a main function that takes input array and size, calls combinationUtil to print all combinations, and provides sample output.

Uploaded by

Shubhangi Ojha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

int main() {

int output = 0;

int ip1;

scanf("%d", &ip1);

int ip2;

scanf("%d", &ip2);

int ip3_size = 0;

int ip3_i;

scanf("%d\n", &ip3_size);

int ip3[ip3_size];

for(ip3_i = 0; ip3_i < ip3_size; ip3_i++) {

int ip3_item;

scanf("%d", &ip3_item);

ip3[ip3_i] = ip3_item;

output = GetJumpCount(ip1,ip2,ip3_size,ip3);

printf("%d\n", output);

return 0;

int GetJumpCount(int input1,int input2,int input3_size, int* input3)

int i,j,count=0;

for(i=0;i<input3_size;i++)

j = 0;

while(j<=input3[i]){

j+=input1;

count++;

if(j>=input3[i])
break;

j=j-input2;

return count;

/ Program to print all combination of size r in an array of size n


#include <stdio.h>
void combinationUtil(int arr[], int data[], int start, int end,
int index, int r);

// The main function that prints all combinations of size r


// in arr[] of size n. This function mainly uses combinationUtil()
void printCombination(int arr[], int n, int r)
{
// A temporary array to store all combination one by one
int data[r];

// Print all combination using temprary array 'data[]'


combinationUtil(arr, data, 0, n-1, 0, r);
}

/* arr[] ---> Input Array


data[] ---> Temporary array to store current combination
start & end ---> Staring and Ending indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination to be printed */
void combinationUtil(int arr[], int data[], int start, int end,
int index, int r)
{
// Current combination is ready to be printed, print it
if (index == r)
{
for (int j=0; j<r; j++)
printf("%d ", data[j]);
printf("\n");
return;
}
// replace index with all possible elements. The condition
// "end-i+1 >= r-index" makes sure that including one element
// at index will make a combination with remaining elements
// at remaining positions
for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r);
}
}

// Driver program to test above functions


int main()
{
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r);
}

Output:
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5

1. #include<stdio.h>
2. #include<string.h>
3. #define N 10
4.
5. void print(int *num, int n)
6. {
7. int i;
8. for ( i = 0 ; i < n ; i++)
9. printf("%d ", num[i]);
10. printf("\n");
11. }
12. int main()
13. {
14. int num[N];
15. int *ptr;
16. int temp;
17. int i, n, j;
18. printf("\nHow many number you want to enter: ");
19. scanf("%d", &n);
20. printf("\nEnter a list of numbers to see all combinations:\n");
21. for (i = 0 ; i < n; i++)
22. scanf("%d", &num[i]);
23. for (j = 1; j <= n; j++) {
24. for (i = 0; i < n-1; i++) {
25. temp = num[i];
26. num[i] = num[i+1];
27. num[i+1] = temp;
28. print(num, n);
29. }
30. }
31. return 0;
32. }

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <assert.h>

#include <limits.h>

#include <stdbool.h>

int appearanceCount(int input1,int input2,char* input3,char* input4)

int i,j,p;

int sum=0,s=0,count=0;

for (i = 0; i <input1; i++)

sum = sum + input3[i];


}

p=input1;

for(i=0;i<input2;i++)

for(j=i;j<i+p;j++)

s=s+input4[j];

if(sum==s)

count++;

s=0;

return count;

int main() {

int output = 0;

int ip1;

scanf("%d", &ip1);

int ip2;

scanf("%d", &ip2);

char* ip3;

ip3 = (char *)malloc(512000 * sizeof(char));

scanf("\n%[^\n]",ip3);

char* ip4;

ip4 = (char *)malloc(512000 * sizeof(char));


scanf("\n%[^\n]",ip4);

output = appearanceCount(ip1,ip2,ip3,ip4);

printf("%d\n", output);

return 0;

#include <math.h>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <assert.h>

#include <limits.h>

#include <stdbool.h>

int GetJumpCount(int input1,int input2,int input3_size, int* input3);

int GetJumpCount(int input1,int input2,int input3_size, int* input3)

int i,sum=0,j=0,c=0;

for(i=0;i<input3_size;i++)

if(input1>=input3[i])

j++;

else

while(input3[i]!=sum)
{

sum=sum+input1;

if(input3[i]<=sum)

j++;

break;

else

sum=sum-input2;

j++;

c=c+j;

return (c);

int main() {

int output = 0;

int ip1;

scanf("%d", &ip1);

int ip2;

scanf("%d", &ip2);

int ip3_size = 0;

int ip3_i;

scanf("%d\n", &ip3_size);

int ip3[ip3_size];

for(ip3_i = 0; ip3_i < ip3_size; ip3_i++) {


int ip3_item;

scanf("%d", &ip3_item);

ip3[ip3_i] = ip3_item;

output = GetJumpCount(ip1,ip2,ip3_size,ip3);

printf("%d\n", output);

return 0;

int row()

int i=0;

while(i<input1_size_rows)

int j=0;

while(j<input1_size_cols)

int n=0;

while(n<input1_size_rows)

if(input1[j]==input1[n]||input1[j]!=0||input1[n]!=0)

printf("0");

goto BREAK;

else

{
goto PRINT;

n++;

j++;

i++;

BREAK:break;break;break;

PRINT:printf("1");

return 0;

int column()

int j=0;

while(j<input1_size_cols)

int i=0;

while(i<input1_size_rows)

int n=0;

while(n<input1_size_cols)

if(input1[i]==input1[j]||input1[i]=0||input1[n]=0)

printf("0");

goto BREAK;

else

{
goto PRINT;

n++;

i++;

j++;

BREAK:break;break;break;

PRINT:printf("1");

return 0;

#include <math.h>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <assert.h>

#include <limits.h>

#include <stdbool.h>

int SolveMagicSquare(int input1_size_rows, int input1_size_cols, int** input1)

int r,p,q;

r=input1_size_rows;

p=input1_size_cols;

q=input1;

s=com(r,p,q);
}

int main() {

int output = 0;

int ip1_rows = 0;

int ip1_cols = 0;

scanf("%d", &ip1_rows);

scanf("%d", &ip1_cols);

int** ip1 = (int**)malloc(ip1_rows*sizeof(int*));

int ip1_init_i=0;

for(ip1_init_i=0 ; ip1_init_i<ip1_rows ; ++ip1_init_i)

ip1[ip1_init_i] = (int*)malloc(ip1_cols*(sizeof(int)));

int ip1_i, ip1_j;

for(ip1_i = 0; ip1_i < ip1_rows; ip1_i++) {

for(ip1_j = 0; ip1_j < ip1_cols; ip1_j++) {

int ip1_item;

scanf("%d", &ip1_item);

ip1[ip1_i][ip1_j] = ip1_item;

output = SolveMagicSquare(ip1_rows, ip1_cols,ip1);

printf("%d\n", output);

return 0;
#include <stdio.h>
void main() {
int i,n,column,m,show,full_rows,k,b,flag=0,flag_b=0;
do {
printf("please enter frame size and width<frame size > 2*width>:");
scanf("%d",&n);
scanf("%d",&m);
}
while (n<=2*m||m<0); /*gives the conditions for the input of the
program*/
for (i=0,k=0;k<23;i++,k++) {
for (column=0,b=0;b<79;column++,b++){
if (column==n){
column=0; /*restarts the building of the square*/
flag+=1;
}
if (i==n)
if ((n+1)%2)
flag_b+=1;
else
flag_b=0;

show=m>column;
show=show||m+column>=n; /*this are the conditions to create
a squre with the thicknes(m) in the columns that is needed*/
if(show)
printf("*");
else {
full_rows=m>i;
full_rows=full_rows||m+i>=n;
if (full_rows) /*the condition to create
thickness(m) in the rows that is needed*/
printf("*");
else
printf(" "); /*creates a blank in the
spots that do not fullfill the conditions */
}

if(b==78)
printf("\n");
i=i%n;
}
}
}

#include<stdio.h>
#include<stdlib.h>
#define chessBoardSize 12

int chessBoard[chessBoardSize][chessBoardSize] = {0};


typedef struct point{
int x, y;
}POINT;
int count=0;

int nextPosition(int x, int y, POINT* array){


int m=0;
/* finds the next possible points for the current
position in the chess board:
like
_ _ _ _ _ _
_ * _ * _ _
* _ _ _ * _
_ _ P _ _ _
* _ _ _ * _
_ * _ * _ _

as above if 'P' is the current (x,y)


* represents the next possible points and
also checks it exists within the chess board
*/

if( (x+2) < chessBoardSize ){


if( (y+1) < chessBoardSize ){
array[m].x = x+2;
array[m++].y = y+1;
}
if( (y-1) >-1 ){
array[m].x = x+2;
array[m++].y = y-1;
}
}

if( (x-2) > -1){


if( (y+1) < chessBoardSize ){
array[m].x = x-2;
array[m++].y = y+1;
}
if( (y-1) >-1 ){
array[m].x = x-2;
array[m++].y = y-1;
}
}

if( (y+2) < chessBoardSize){


if( (x+1) < chessBoardSize ){
array[m].x = x+1;
array[m++].y = y+2;
}
if( (x-1) >-1 ){
array[m].x = x-1;
array[m++].y = y+2;
}
}

if( (y-2) > -1){


if( (x+1) < chessBoardSize ){
array[m].x = x+1;
array[m++].y = y-2;
}
if( (x-1) >-1 ){
array[m].x = x-1;
array[m++].y = y-2;
}
}
return m;
}

void displayAnswer(){
int i, j, k;
printf("\n");
for(i=0; i<chessBoardSize; i++){
for(j=0; j<chessBoardSize; j++)
printf("%d\t",chessBoard[i][j]);
printf("\n\n");
}
}

// recursive function using backtrack method


void knightTravel(int x, int y){
POINT array[8] = {{0, 0}, {0, 0}};
// remainin initialized to zero automatically
volatile int noOfPossiblePoints = nextPosition(x, y, array);
volatile int i;

chessBoard[x][y] = ++count;

// base condition uses count


if( count == chessBoardSize * chessBoardSize ){
displayAnswer();
exit(0);
}

for(i=0; i< noOfPossiblePoints; i++)


if( chessBoard[array[i].x][array[i].y] == 0 )
knightTravel(array[i].x, array[i].y);

chessBoard[x][y] = 0;
count--;
}

int main()
{
knightTravel(0, 0);
printf("No solution exists\n");
return 0;
}

#include <bits/stdc++.h>

using namespace std;


struct cell

int x, y;

int dis;

cell() {}

cell(int x, int y, int dis) : x(x), y(y), dis(dis) {}

};

bool isInside(int x, int y)

if (x >= 1 && x <= 8 && y >= 1 && y <= 8)

return true;

return false;

int getStepCount(int input1, int input2,int input3,int input4)

int dx[] = {-2, -1, 1, 2, -2, -1, 1, 2};

int dy[] = {-1, -2, -2, -1, 1, 2, 2, 1};

queue<cell> q;

q.push(cell(input1, input2, 0));

cell t;

int x, y;

bool visit[8 + 1][8 + 1];

for (int i = 1; i <= 8; i++)


for (int j = 1; j <= 8; j++)

visit[i][j] = false;

visit[input1][input2] = true;

while (!q.empty())

t = q.front();

q.pop();

visit[t.x][t.y] = true;

if (t.x == input3 && t.y == input4)

return t.dis;

for (int i = 0; i < 8; i++)

x = t.x + dx[i];

y = t.y + dy[i];

if (isInside(x, y) && !visit[x][y])

q.push(cell(x, y, t.dis + 1));

int main()

int output;

int ip1;

cin>>ip1;

cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
int ip2;

cin>>ip2;

cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');

int ip3;

cin>>ip3;

cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');

int ip4;

cin>>ip4;

cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');

output=getStepCount(ip1,ip2,ip3,ip4);

cout<<output<<endl;

return 0;

You might also like