Informatika gyűjtemény

Egy szinttel feljebb kb_szaruman.java

2004050607080910

NézetNyomtat

kb_szaruman.java (Vissza)
Az alábbi letöltési lehetőségek közül választhatsz: (segítség)
Karakterkódolás:
Sortörés:
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: utf-8
Méret: 2 KB
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package szaruman;

import java.io.IOException;

/**
 *
 * @author 03cKrivan
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        BattleField bf = new BattleField();
        try {
            while (bf.nextField()) {
                bf.loadField();
                bf.sort();
                bf.solve();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package szaruman;

import java.io.*;

/**
 *
 * @author 03cKrivan
 */
public class BattleField {

    public int range;
    public int count;
    private BufferedReader bufRead;
    public int[] armies;
    
    public BattleField()
    {
        InputStream is = BattleField.class.getResourceAsStream( "army.txt" );
        this.bufRead = new BufferedReader( new InputStreamReader(is) );        
    }
    
    public boolean nextField() throws IOException
    {         
        String line = bufRead.readLine();
        if( !line.equals("-1 -1") ) {
            // nem az utsó feladat
            
            String[] Rn = line.split(" ");
            this.range = Integer.parseInt( Rn[0] );
            this.count = Integer.parseInt( Rn[1] );
            
            return true;
        } else {
            return false;
        }
    }

    public void loadField() throws IOException {
        String line = bufRead.readLine();
        String[] raw_armies = line.split(" ");
        
        armies = new int[raw_armies.length];
        for( int i=0; i<raw_armies.length; i++ )
        {
            armies[i] = Integer.parseInt( raw_armies[i] );
        }
    }

    public void sort() {
        // megvan az armies tömbünk, ezt rendezzük
        int min;
        for( int i=0; i<armies.length; i++ )
        {
            min = i;
            for( int a=i; a<armies.length; a++ )
            {
                if( armies[a] < armies[min] ) min = a;
            }
            // swap
            int save = armies[i];
            armies[i] = armies[min];
            armies[min] = save;
        }
    }
    
    public void solve()
    {
        int res = 0;
        int carrier;
        int i = 0;
        while( i < armies.length )
        {
            carrier = getNearestNonCoveredArmy( i );
            i = getNearestNonCoveredArmy( carrier )+1; // innentől nézem a kövit.
            res++;
        }
        
        System.out.println(res);
    }

    private int getNearestNonCoveredArmy(int i) {
        int a = i+1;
        while( true )
        {
            if( a == armies.length ) return a-1;
            if( armies[a] - armies[i] <= range )
            {
                a++;
            } else break;
        }
        return a-1;
    }
    
}
(Vissza)