int in BigInteger verwandeln

Status
Nicht offen für weitere Antworten.
L

looki

Gast
Hallo,

ich hab jetzt schon ein kleines Prog um zahlen zu berechnen, wie kann ich nun diese alle in BigInteger ausgeben lassen?
Muss ich alles ändern wo int und long ist, oder geht auch nur das Endergebniss??

Ohne BigInteger funktioniert es nicht mit einem großen Betrag wie z.B. 4000000

Danke




Code:
import java.math.BigInteger; 

public class Geldwechsel { 

   public static int[] betrag = { 2, 3, 5, 7, 11, 13, 17, 23 }; 

   static long Tab[][]; 

   public static long w(int G, int i) { 
      return (G < 0) ? 0 : (i == 0) ? 1 : (Tab[G][i] != 0) ? Tab[G][i] 
            : (Tab[G][i] = w(G, i - 1) + w(G - betrag[i], i)); 
   } 

   public static void main(String[] args) { 


      int G = Integer.parseInt(args[0]); 
       
       Tab = new long[G + 1][8]; 

       System.out.println("der Betrag von  " + G + "kann man auf  " 
             + w(G, 7) 
            + " verschieden Arten ausgeben "); 

   } 

}
 
L

looki

Gast
Code:
import java.math.BigInteger;

public class Geldwechsel {

	
	
	
	
	public static int[] betrag = { 2, 3, 5, 7, 11, 13, 17, 23 };

	static BigInteger Tab[][];

	public static BigInteger w(int G, int i) {

		return (G < 0) ? 0 : (i == 0) ? 1 :
			                                (Tab[G][i] != 0) ? Tab[G][i]
				                                               : (Tab[G][i] = w(G, i - 1).add(w(G - betrag[i], i))  );
	}

	public static void main(String[] args) {


		int G = Integer.parseInt(args[0]);
			
		 Tab = new BigInteger[G + 1][8];

		 System.out.println("der Betrag von  " +G + "kann man auf  " 
		 		+w(G, 7)
				+ " verschieden Arten ausgeben ");

	}

}

ich habs bis hier hin geändert, komm aber nicht mehr weiter..
 
L

looki

Gast
Code:
import java.math.BigInteger;

public class Geldwechsel {

	public static int[] betrag = { 2, 3, 5, 7, 11, 13, 17, 23 };

	static BigInteger Tab[][];

	public static BigInteger w(int G, int i) {

		return (G < 0) ? new BigInteger("0") :

		(i == 0) ? new BigInteger("1")
				: ((Tab[G][i]).compareTo(new BigInteger("0")) !=0 ) ? Tab[G][i]
						: (Tab[G][i] = w(G, i - 1).add(w(G - betrag[i], i)));
	}

	public static void main(String[] args) throws Exception {

		int G = Integer.parseInt(args[0]);

		Tab = new BigInteger[G + 1][8];

		System.out.println("der Betrag von  " + G + "kann man auf  "
				+ (w(G, 7).toString())
				+ " verschieden Arten ausgeben ");

	}

}
 
G

Guest

Gast
Code:
import java.math.BigInteger;

public class Geldwechsel {

	public static int[] betrag = { 2, 3, 5, 7, 11, 13, 17, 23 };

	static BigInteger Tab[][];

	public static BigInteger w(int G, int i) {

		return (G < 0) ? BigInteger.ZERO :

		(i == 0) ? new BigInteger("1")
				: ((Tab[G][i]).compareTo(BigInteger.ZERO) !=0 ) ? Tab[G][i]
						: (Tab[G][i] = w(G, i - 1).add(w(G - betrag[i], i)));
	}

	public static void main(String[] args) throws Exception {

		

		for(int i=0; i<Tab.length;i++){
				for(int j=0; j<8;j++)
						Tab[i][j]= BigInteger.ZERO; 

}
		int G = Integer.parseInt(args[0]);
		Tab = new BigInteger[G + 1][8];

		System.out.println("der Betrag von  " + G + "kann man auf  "
				+ (w(G, 7).toString())
				+ " verschieden Arten ausgeben ");

	}

}
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Oben