Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Dodatni {
- static final int nMin = 0, nMax = 35;
- static final int rMin = 5, rMax = 20;
- // rekurzivno preko definicije
- static int fRec1(int n, int r){
- if (n < r) return n - 5;
- else if (gRec1(n-r, r) > 0)
- return fRec1(n-r+1, r) + 3 * fRec1(n-r+2, r) - gRec1(n-r, r);
- else
- return fRec1(n-1, r) + 2 * gRec1(n-2, r) - gRec1(n-r+1, r);
- }
- static int gRec1(int n, int r){
- if (n < r) return (int)Math.pow((4-n), 2);
- else if (n % 2 == 0)
- return 3 * gRec1(n-r+1, r) + fRec1(n-1, r);
- else
- return fRec1(n-r, r) + 2 * gRec1(n-r+2, r);
- }
- //iterativno, efikasno
- static int fItr2(int n, int r) {
- int[] f = new int [r + 1];
- int[] g = new int [r + 1];
- for (int i = 0; i < r; i++){
- f[i] = i - 5;
- g[i] = (int)Math.pow((4-i), 2);
- }
- if (n < r) return f[n];
- else {
- for (int i = r; i <= n; i++){
- if (g[0] > 0)
- f[r] = f[1] + 3 * f[2] - g[0];
- else
- f[r] = f[r-1] + 2 * g[r-2] - g[1];
- if (i % 2 == 0)
- g[r] = 3 * g[1] + f[r-1];
- else
- g[r] = f[0] + 2*g[2];
- for (int j = 0; j < r; j++){
- f[j] = f[j+1];
- g[j] = g[j+1];
- }
- }
- }
- return f[r];
- }
- public static void main (String[] args) {
- int n;
- do
- {
- n = Svetovid.in.readInt("Unesite broj n: ");
- } while (!(n < nMin || n < nMax));
- int r;
- do
- {
- r = Svetovid.in.readInt("Unesite broj r: ");
- } while (!(r < rMin || r < rMax));
- System.out.println("fRec1: " + fRec1(n, r));
- System.out.println("fItr2: " + fItr2(n, r));
- /* for (int i = nMin; i <= nMax ; i++){
- for (int j = rMin; j <= rMax; j++){
- System.out.println("i = " + i + "j= " + j + "Def: " + fRec1(i, j));
- }
- } */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement