Skip to main content

What is Divide And Conquer Technique

Divide and Conquer is a programming technique which makes the program more efficient to write. And this technique work on the concept of recursion to solve a problem step by step. Generally this technique work in three parts:- Divide:-  Divide the problem into some subproblem. Conquer:-  Conquer the subproblem by calling recursively until subproblem solved. Combine:-  (Optional Step) Combine the subproblem solution. So, that we will get the final problem Solution.  When the subproblems are large enough to solve recursively, we call the recursive case. Once the subproblem becomes small enough that we no longer recursive, we say that the recursion "bottom out" and that we have gotten down to the base case.                          Application of Divide and Conquer Quick Sort Strassen's algorithm for matrix multiplication Merge Sort Counting inversions Binary Search Finding Min and ...

Store Credit program in C language

You receive a credit C at a local store and would like to buy two items. You first walk through the store and create a list L of all available items. From this list you would like to buy two items that add up to the entire value of the credit. The solution you provide will consist of the two integers indicating the positions of the items in your list.
Input
The first line of input gives the number of cases, NN test cases follow. For each test case there will be:
  • One line containing the value C, the amount of credit you have at the store.
  • One line containing the value I, the number of items in the store.
  • One line containing a space separated list of I integers. Each integer P indicates the price of an item in the store.
  • Each test case will have exactly one solution.
#include<stdio.h>
int main()
{
int n,c,it,p[1000];

  scanf("%d\n",&n);
  for(int i=1;i<=n;i++)
  {
    scanf("%d\n",&c);
    scanf("%d\n",&it);
    for(int j=0;j<it;j++)
    {
scanf("%d\n",&p[j]);
    }
    for(int k=0;k<it-1;k++)
    {
      for(int h=k+1;h<it;h++)
      {
        if(c==(p[k]+p[h]))
        {
          printf("Case #%d: %d %d\n",i,k+1,h+1);
        }
        else
        {}
      }
    }
  }
  return 0;
}
       
        

Comments

Popular posts from this blog

What is Divide And Conquer Technique

Divide and Conquer is a programming technique which makes the program more efficient to write. And this technique work on the concept of recursion to solve a problem step by step. Generally this technique work in three parts:- Divide:-  Divide the problem into some subproblem. Conquer:-  Conquer the subproblem by calling recursively until subproblem solved. Combine:-  (Optional Step) Combine the subproblem solution. So, that we will get the final problem Solution.  When the subproblems are large enough to solve recursively, we call the recursive case. Once the subproblem becomes small enough that we no longer recursive, we say that the recursion "bottom out" and that we have gotten down to the base case.                          Application of Divide and Conquer Quick Sort Strassen's algorithm for matrix multiplication Merge Sort Counting inversions Binary Search Finding Min and ...

POLYGON FORMATION IN C

                       /*  POLYGON FORMATION IN C  */ #include<stdio.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> void main() {     int n,x[15],y[15],i;     printf("ENTER THE NUMBER OF SIDE OF POLYGON:\n");     scanf("%d",&n);     if(n<3)     { printf("POLYGON CAN'T BE FORM\n"); getch(); exit(0);     }     else     { for(i=1;i<=n;i++) { printf("ENTER THE CORDINATES OF THE POLYGON %d SIDE\n",i); scanf("%d %d",&x[i],&y[i]); } int gd=DETECT,gm; initgraph(&gd,&gm,"C:\\tc\\BGI"); for(i=1;i<n;i++) { line(x[i],y[i],x[i+1],y[i+1]); } line(x[1],y[1],x[n],y[n]); } getch(); }

Bresenham circle drawing program in C

  /* This is the program for bresenham circle drawing  in C */ #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void main() { int gdriver = DETECT,gmode,x,y,r,x1,y1; float e; initgraph(&gdriver,&gmode,"C:\\tc\\BGI"); printf("center co-ordinate of x and y position of circle:\n"); scanf("%d %d",&x,&y); printf("input the radius of circle:\n"); scanf("%d",&r); x1=1; y1=r; putpixel(x+0,y+r,RED); putpixel(x+r,y+0,RED); putpixel(x-0,y-r,RED); putpixel(x-r,y-0,RED); e=3-2*r;   while(x1<y1) { if(e<0) { x1=x1+1; e=e+4*x1+6; } else { x1=x1+1; y1=y1-1; e=e+4*(x1-y1)+10; }           putpixel(x+x1,y+y1,1); putpixel(x+y1,y+x1,2); putpixel(x-x1,y+y1,3); putpixel(x-y1,y+x1,4); putpixel(x-x1,y-y1,5); putpixel(x-y1,y-x1,6); putpixel(x+x1,y-y1,7); putpixel(x+y1,y-x1,8); delay(100); } getch(); } /*NOTE:-You have to change the graphics...