Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CSN103 MTE 2018
- // Problem1
- import java.util.*;
- public class Problem1{
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- int n=sc.nextInt();
- int a[]=new int[n+1];
- for (int i=1; i<=n; i++){
- a[i]=sc.nextInt();
- }
- int x=0;
- for (int i=1; i<n; i++){
- for (int j=i+1; j<=n; j++){
- if (a[i]>a[j]){
- x=a[j];
- a[j]=a[i];
- a[i]=x;
- }
- }
- }
- int y=(int)(Math.random()*(n-1));
- if (y==0 || y==2 || y==n-1){
- y++;
- }
- System.out.print(a[y]);
- }
- }
- // Problem2
- import java.util.*;
- public class Problem2{
- static long fib(long x){
- if (x==1){
- return 1;
- }
- else if (x==2){
- return 2;
- }
- else if (x>2){
- return fib(x-1)+fib(x-2);
- }
- return 0;
- }
- public static void main(String args[]){
- for (long i=1; i<=50; i++){
- System.out.println(fib(i));
- }
- }
- }
- // Problem3
- import java.util.*;
- class Problem3{
- static int isprime(int x){
- if (x==2){
- return 1;
- }
- for (int i=2; i*i<=x; i++){
- if (x%i==0){
- return 0;
- }
- }
- return 1;
- }
- static int palindrome(int x){
- int y=0;
- int n=x;
- while (x>0){
- y=10*y+(x%10);
- x/=10;
- }
- if (y==n){
- return 1;
- }
- else {
- return 0;
- }
- }
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- int x=0, i=2;
- while (x<30){
- if (palindrome(i)==1 && isprime(i)==1){
- x++;
- System.out.println(i);
- }
- i++;
- }
- }
- }
- // Problem4
- import java.util.*;
- public class Problem4{
- class RecursionExample{
- static int counter=0;
- static int Count(int x, int y){
- if (y!=1){
- if (x!=1){
- counter++;
- Count(x/2,y);
- }
- else {
- y=y-1;
- Count(1024,0);
- }
- }
- }
- }
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- System.out.println("number of times: " +Count(1024,1024));
- }
- }
- // Problem5
- import java.util.*;
- public class Problem5{
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- int a[][]=new int[2][2];
- a[0][0]=1;
- a[0][1]=-2;
- a[1][0]=-2;
- a[1][1]=0;
- int b[][]=new int[2][2];
- b[0][0]=-3;
- b[0][1]=-2;
- b[1][0]=-2;
- b[1][1]=-4;
- for (int i=0; i<2; i++){
- for (int j=0; j<2; j++){
- System.out.print(a[i][j] +" ");
- }
- System.out.println();
- }
- System.out.println();
- for (int i=0; i<2; i++){
- for (int j=0; j<2; j++){
- System.out.print(b[i][j] +" ");
- }
- System.out.println();
- }
- }
- }
- // Problem6
- // Method1
- import java.util.*;
- public class Problem6{
- static String reverse(String s, int x, int y){
- String u="";
- char c='a';
- for (int i=0; i<y-x+1; i++){
- c=s.charAt(y-i);
- u=u+c;
- }
- return u;
- }
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- String s=sc.nextLine();
- int i=0, j=s.length()-1, k=0;
- String x="";
- for (int m=0; m<=j; m++){
- if (s.charAt(m)==' '){
- k=m-1;
- if (i!=0){
- x=x+" "+reverse(s,i,k);
- }
- else {
- x=x+reverse(s,i,k);
- }
- i=m+1;
- }
- else if (m==j){
- k=j;
- x=x+" "+reverse(s,i,k);
- }
- }
- System.out.println(x);
- }
- }
- // Method2 (by kshitij)
- import java.util.*;
- public class Problem6{
- public static void main(String args[]){
- Scanner sc=new Scanner(System.in);
- String a=sc.nextLine();
- int x=0;
- int n=a.length();
- char b;
- for (int i=0; i<n; i++){
- b=a.charAt(i);
- if (b==' '){
- for (int j=i-1; j>=x; j--){
- System.out.print(a.charAt(j));
- }
- x=i;
- System.out.print(" ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement