Advertisement
fooker

2018 Autumn MTE

Dec 5th, 2022 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. // CSN103 MTE 2018
  2. // Problem1
  3. import java.util.*;
  4. public class Problem1{
  5.     public static void main(String args[]){
  6.         Scanner sc=new Scanner(System.in);
  7.         int n=sc.nextInt();
  8.         int a[]=new int[n+1];
  9.         for (int i=1; i<=n; i++){
  10.             a[i]=sc.nextInt();
  11.         }
  12.         int x=0;
  13.         for (int i=1; i<n; i++){
  14.             for (int j=i+1; j<=n; j++){
  15.                 if (a[i]>a[j]){
  16.                     x=a[j];
  17.                     a[j]=a[i];
  18.                     a[i]=x;
  19.                 }
  20.             }
  21.         }
  22.         int y=(int)(Math.random()*(n-1));
  23.         if (y==0 || y==2 || y==n-1){
  24.             y++;
  25.         }
  26.         System.out.print(a[y]);
  27.     }
  28. }
  29.  
  30. // Problem2
  31. import java.util.*;
  32. public class Problem2{
  33.     static long fib(long x){
  34.         if (x==1){
  35.             return 1;
  36.         }  
  37.         else if (x==2){
  38.             return 2;
  39.         }
  40.         else if (x>2){
  41.             return fib(x-1)+fib(x-2);
  42.         }
  43.         return 0;
  44.     }
  45.     public static void main(String args[]){
  46.         for (long i=1; i<=50; i++){
  47.             System.out.println(fib(i));
  48.         }
  49.     }
  50. }
  51.  
  52. // Problem3
  53. import java.util.*;
  54. class Problem3{
  55.     static int isprime(int x){
  56.         if (x==2){
  57.             return 1;
  58.         }
  59.         for (int i=2; i*i<=x; i++){
  60.             if (x%i==0){
  61.                 return 0;
  62.             }
  63.         }
  64.         return 1;
  65.     }
  66.     static int palindrome(int x){
  67.         int y=0;
  68.         int n=x;
  69.         while (x>0){
  70.             y=10*y+(x%10);
  71.             x/=10;
  72.         }
  73.         if (y==n){
  74.             return 1;
  75.         }
  76.         else {
  77.             return 0;
  78.         }
  79.     }
  80.     public static void main(String args[]){
  81.         Scanner sc=new Scanner(System.in);
  82.         int x=0, i=2;
  83.         while (x<30){
  84.             if (palindrome(i)==1 && isprime(i)==1){
  85.                 x++;
  86.                 System.out.println(i);
  87.             }
  88.             i++;
  89.         }
  90.     }
  91. }
  92.  
  93. // Problem4
  94. import java.util.*;
  95. public class Problem4{
  96.     class RecursionExample{
  97.         static int counter=0;
  98.         static int Count(int x, int y){
  99.             if (y!=1){
  100.                 if (x!=1){
  101.                     counter++;
  102.                     Count(x/2,y);
  103.                 }
  104.                 else {
  105.                     y=y-1;
  106.                     Count(1024,0);
  107.                 }
  108.             }
  109.         }
  110.     }
  111.     public static void main(String args[]){
  112.         Scanner sc=new Scanner(System.in);
  113.         System.out.println("number of times: " +Count(1024,1024));
  114.     }
  115. }
  116.  
  117. // Problem5
  118. import java.util.*;
  119. public class Problem5{
  120.     public static void main(String args[]){
  121.         Scanner sc=new Scanner(System.in);
  122.         int a[][]=new int[2][2];
  123.         a[0][0]=1;
  124.         a[0][1]=-2;
  125.         a[1][0]=-2;
  126.         a[1][1]=0;
  127.         int b[][]=new int[2][2];
  128.         b[0][0]=-3;
  129.         b[0][1]=-2;
  130.         b[1][0]=-2;
  131.         b[1][1]=-4;
  132.         for (int i=0; i<2; i++){
  133.             for (int j=0; j<2; j++){
  134.                 System.out.print(a[i][j] +" ");
  135.             }
  136.             System.out.println();
  137.         }
  138.         System.out.println();
  139.         for (int i=0; i<2; i++){
  140.             for (int j=0; j<2; j++){
  141.                 System.out.print(b[i][j] +" ");
  142.             }
  143.             System.out.println();
  144.         }
  145.     }
  146. }
  147.  
  148. // Problem6
  149. // Method1
  150. import java.util.*;
  151. public class Problem6{
  152.     static String reverse(String s, int x, int y){
  153.         String u="";
  154.         char c='a';
  155.         for (int i=0; i<y-x+1; i++){
  156.             c=s.charAt(y-i);
  157.             u=u+c;
  158.         }
  159.         return u;
  160.     }
  161.     public static void main(String args[]){
  162.     Scanner sc=new Scanner(System.in);
  163.     String s=sc.nextLine();
  164.     int i=0, j=s.length()-1, k=0;
  165.     String x="";
  166.     for (int m=0; m<=j; m++){
  167.         if (s.charAt(m)==' '){
  168.             k=m-1;
  169.             if (i!=0){
  170.                 x=x+" "+reverse(s,i,k);
  171.             }
  172.             else {
  173.                 x=x+reverse(s,i,k);
  174.             }
  175.             i=m+1;
  176.         }
  177.         else if (m==j){
  178.             k=j;
  179.             x=x+" "+reverse(s,i,k);
  180.             }
  181.         }
  182.     System.out.println(x);
  183.     }
  184. }
  185.  
  186. // Method2 (by kshitij)
  187. import java.util.*;
  188. public class Problem6{
  189.     public static void main(String args[]){
  190.         Scanner sc=new Scanner(System.in);
  191.         String a=sc.nextLine();
  192.         int x=0;
  193.         int n=a.length();
  194.         char b;
  195.         for (int i=0; i<n; i++){
  196.             b=a.charAt(i);
  197.             if (b==' '){
  198.                 for (int j=i-1; j>=x; j--){
  199.                     System.out.print(a.charAt(j));  
  200.                 }
  201.                 x=i;
  202.                 System.out.print(" ");
  203.             }            
  204.         }
  205.     }
  206. }
  207.  
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement