Advertisement
rayslhcf

OS01.java

Sep 12th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. public class s5535512037h1{
  2.     public static void main(String args[]){
  3.         int i, uInp = 0;
  4.         if(args.length==1){
  5.  
  6.             try{
  7.                 uInp = Integer.parseInt(args[0]);
  8.             }
  9.             catch(NumberFormatException nfe){
  10.                 System.out.println(nfe.getMessage() + "\n The first argument must be an integer.");
  11.                 System.exit(1);
  12.             }
  13.  
  14.             int[] sn = new int[uInp];
  15.             for(i=0; i<uInp; i++)
  16.             {
  17.                 if(i == 0 || i == 1)
  18.                     sn[i]=1;
  19.                 else if(i == 2)
  20.                     sn[i]=2;
  21.                 else if(i == 3)
  22.                     sn[i]=3;
  23.                 else if((i%3) == 0)
  24.                     sn[i]=sn[i-1];
  25.                 else
  26.                     sn[i]=(sn[i-2]*2)-1;
  27.                 System.out.print(sn[i] + "  ");
  28.             }
  29.  
  30.         }else{
  31.             System.out.println("\n This program requires least one argument.");
  32.         }
  33.     }
  34.     public static int findSn(int n, boolean dep){
  35.         if(n<1) return 1;
  36.  
  37.         if(dep)System.out.print(findSn((n-1), true) + "\t");
  38.        
  39.         switch(n){
  40.             case 0: case 1: return 1;
  41.             case 2: return 2;
  42.             case 3: return 3;
  43.             default:
  44.                 if(n%3==0)
  45.                     return (findSn((n-1), false));
  46.                 else
  47.                     return (findSn((n-2), false)*2)-1;
  48.         }
  49.        
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement