Sort by

recency

|

348 Discussions

|

  • + 0 comments

    using java 7

    import java .util.Scanner; public class trade { public static void main(String argsp[]) { Scanner scan=new Scanner(System.in); int n; n=scan.nextInt(); int price[]=new int[n]; int days[]=new int[n];

       for(int i=0;i<n;i++)
       {
        price[i]=scan.nextInt();
        int j=i;
        days[i]=++j;      
     }
     for(int i=0;i<n;i++)
     {
     System.out.print( price[i] +" ");
    // System.out.print(days[i]);
     }
     System.out.println();
     //minimum value to find out
     int min=price[0];
     int day=0;
     int max=price[0];
     for(int i =1;i<n;i++)
     {
        if(min>price[i])
        {
            min=price[i];
            day=i;
    
        }
        if(max<price[i])
        {
           max=price[i];
        }
     }
     System.out.println(" trade price "+" "+min +" buy on date"+" "+ (day+1));
     System.out.println(" trade price while selling it"+" "
     + " sold on this day"+ (day+1));
      int profit=max-min;
      if(profit>0)
      {
        System.out.println(" seller found their profit"+ " = "+profit);
      }
      else if(profit<=0 )
      {
         System.out.println(" trader is in loss");
      }
    
    
    }
    

    }

  • + 0 comments

    Using Java 8

    import java.util.*;

    class Student{ private int id; private String fname; private double cgpa; public Student(int id, String fname, double cgpa) { super(); this.id = id; this.fname = fname; this.cgpa = cgpa; } public int getId() { return id; } public String getFname() { return fname; } public double getCgpa() { return cgpa; } }

    //Complete the code public class Solution { public static void main(String[] args){ Scanner in = new Scanner(System.in); int testCases = Integer.parseInt(in.nextLine());

        List<Student> studentList = new ArrayList<Student>();
        while(testCases>0){
            int id = in.nextInt();
            String fname = in.next();
            double cgpa = in.nextDouble();
    
            Student st = new Student(id, fname, cgpa);
            studentList.add(st);
    
            testCases--;
        }
       Collections.sort(studentList,
        Comparator.comparing(Student::getCgpa).reversed()
        .thenComparing(Student::getFname)
        .thenComparing(Student::getId));
    
        for(Student st: studentList){
            System.out.println(st.getFname());
        }
    }
    

    }

  • + 0 comments
    import java.util.*;
    
    class Student implements Comparable<Student>{
        private int id;
        private String fname;
        private double cgpa;
        
        public Student(int id, String fname, double cgpa) {
            super();
            this.id = id;
            this.fname = fname;
            this.cgpa = cgpa;
        }
        
        public int getId() {
            return id;
        }
        
        public String getFname() {
            return fname;
        }
        
        public double getCgpa() {
            return cgpa;
        }
    
        @Override
        public int compareTo(Student anotherStudent) {
            if (Double.compare(anotherStudent.getCgpa(), this.getCgpa()) != 0) {
                return Double.compare(anotherStudent.getCgpa(), this.getCgpa());
            } else if (!this.getFname().equals(anotherStudent.getFname())) {
                return this.getFname().compareTo(anotherStudent.getFname());
            } else {
                return this.getId() - anotherStudent.getId();
            }
        }
    }
    
    //Complete the code
    public class Solution {
        public static void main(String[] args){
            Scanner in = new Scanner(System.in);
            int testCases = Integer.parseInt(in.nextLine());
            
            List<Student> studentList = new ArrayList<Student>();
            
            while (testCases>0) {
                int id = in.nextInt();
                String fname = in.next();
                double cgpa = in.nextDouble();
                
                Student st = new Student(id, fname, cgpa);
                studentList.add(st);
                
                testCases--;
            }
          
            in.close();
    
            Collections.sort(studentList);
          
              for (Student st: studentList) {
                System.out.println(st.getFname());
            }
        }
    }
    
  • + 0 comments
        Comparator<Student> cgpaComp = (s1, s2) -> Double.valueOf(s2.getCgpa()).compareTo(Double.valueOf(s1.getCgpa()));
        Comparator<Student> nameComp = (s1, s2) -> s1.getFname().compareTo(s2.getFname());
        Comparator<Student> idComp = (s1, s2) -> Integer.valueOf(s1.getId()).compareTo(Integer.valueOf(s2.getId()));
    
        studentList = studentList.
                stream().
                sorted(cgpaComp.thenComparing(nameComp).thenComparing(idComp)).
                collect(Collectors.toList());
    
  • + 0 comments

    import java.util.*;

    class Student{ private int id; private String fname; private double cgpa; public Student(int id, String fname, double cgpa) { super(); this.id = id; this.fname = fname; this.cgpa = cgpa; } public int getId() { return id; } public String getFname() { return fname; } public double getCgpa() { return cgpa; } }

    //Complete the code public class Solution { public static void main(String[] args){ Scanner in = new Scanner(System.in); int testCases = Integer.parseInt(in.nextLine());

        List<Student> studentList = new ArrayList<Student>();
        while(testCases>0){
            int id = in.nextInt();
            String fname = in.next();
            double cgpa = in.nextDouble();
    
            Student st = new Student(id, fname, cgpa);
            studentList.add(st);
    
            testCases--;
        }
        //String [] a=new String[testCases];
        for(int i=0;i<studentList.size()-1;i++){
            for(int j=0;j<studentList.size()-1;j++){
                if(studentList.get(j).getCgpa()<studentList.get(j+1).getCgpa()){
                    Student sl=studentList.get(j);
                    studentList.set(j, studentList.get(j+1));
                    studentList.set(j+1, sl);
                }
                else if(studentList.get(j).getCgpa()==studentList.get(j+1).getCgpa()){
                    if(studentList.get(j).getFname().compareTo(studentList.get(j+1).getFname())>0){
                        Student sl=studentList.get(j);
                        studentList.set(j, studentList.get(j+1));
                        studentList.set(j+1, sl);
                    }
                }
            }
        }
    
        for(Student st: studentList){
            System.out.println(st.getFname());
        }
    }
    

    }