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 max Divide and Conquer Abstract Algorithm DAC(a,i,j) {   

Reverse Words

Reverse Words

Given a list of space separated words, reverse the order of the words. Each line of text contains L letters and W words. A line will only consist of letters and space characters. There will be exactly one space character between each pair of consecutive words.

Input

The first line of input gives the number of cases, N.
N test cases follow. For each test case there will a line of letters and space characters indicating a list of space separated words. Spaces will not appear at the start or end of a line.
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Rishabh
 */
public class Reversestring  {

    public static void main(String[] args)  {
        // TODO code application logic here
   int i;
    Scanner sc=new Scanner(System.in);
    int N=sc.nextInt();
  String s=null;

 InputStreamReader isr = new InputStreamReader(System.in);
 BufferedReader br=new BufferedReader(isr);
  ArrayList<String> Al=new ArrayList<>();
    for( i=0;i<N;i++)
    {
     
         
           s=sc.next();
      s+=sc.nextLine();
 
           Stack<String> stk=new Stack<>();
           StringTokenizer st=new StringTokenizer(s," ");
         
       
      while(st.hasMoreTokens())
           {
             s=st.nextToken();
         
           s=s.trim();
           stk.push(s);
           }String output = "";
           String out = null;
           while(!stk.empty())
           { out=(String) stk.pop();
         
         
           output+=out;
           output=output+" ";
         
           }
           Al.add(output);
         
           output=null;
          }int m=1;for(String a : Al)
        {
         
         a=a.trim();
         
    System.out.println("Case #"+m+":"+" "+a);
   
    m++;
        }
  }
}   

   
   
 

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 max Divide and Conquer Abstract Algorithm DAC(a,i,j) {   

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

Extracting all states data like population,economy from a wikipedia link of India using Java Language

package urldatafetch; /**  *  * @author Rishabh  */ import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Iterator; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class UrlDataFetch { public static void main(String[] args) throws IOException {       org.jsoup.nodes.Document doc = Jsoup.connect("https://en.wikipedia.org/wiki/India").get();       org.jsoup.select.Elements tables =doc.select("table");                    int i=1;                    String state[]=new String[29];        ArrayList<String> ar=new ArrayList<>();      for(org.jsoup.nodes.Element tableo :tables)      {  if(i==1 ||i==2 ||i==4){i++;}      else if(i==3)      {  org.jsoup.select.Elements rows=tableo.select("tr");         for(org.jsoup.nodes.Element row :rows)         {             org.jsoup.select.Elements co