Results 1 to 10 of 10
Thread: Java int to array[]
Hybrid View
-
26th May 2010 18:55 #1
Java int to array[]
, , . :
:Code:, n! n [1..100]. , 100! long/int. , . 512 {2, 1, 5}. , ( ).
Code:import java.util.Scanner; public class Factorial { public static void method(int n){ int sumLength; int[] sum = new int[1]; sum[0]=1; for (int i=1;i<=n;i++){ multiply(sum,i); sumLength = (multiply(sum,i)).length; sum= new int[sumLength]; for (int k=0;k<sumLength;k++ ){ sum[k] = multiply(sum,i)[k]; } System.out.print("\nFactorial of "+i+" is: "); for (int numbers: sum){ System.out.print(numbers); } } } public static int[] revString (int[] array){ int length = array.length; int[] reversed = new int[length]; for (int index = 0; index < length; index++) { reversed[length - index - 1] = array[index]; } return reversed; } public static int[] multiply(int[] array, int num){ int temp, arrayLength; arrayLength = array.length; int[] multiplyTemp = new int [arrayLength]; int[] reverse = new int [arrayLength]; revString(array); for (int k=0;k<arrayLength;k++ ){ reverse[k] = revString(array)[k]; } for (int i=0;i<arrayLength;i++){ temp = reverse[i]; multiplyTemp[i] = temp*num; } return multiplyTemp; } public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print("Enter factorial of: "); int facNum = input.nextInt(); method(facNum); } }MSI X570-A PRO, AMD 5800X3D, Noctua NH-D15, Corsair Vengeance LPX 2x8GB-3600, ASUS RTX ROG 3080 Strix White OC, Samsung 980 Pro 1TB NVMe, Corsair RM750, FD Define R5, Dell 32" 4k G3223Q 144Hz + 3xSamsung G5 32" 144hz, SimLab P1-X, Simucube 2 Pro, HE Ultimate, SRB GT3 Wheel
-
26th May 2010 21:06 #2Registered User
Join Date: Dec:2007
Location: Sofia
Posts: 366
, - - 2 , .
, / . . .
-
27th May 2010 18:39 #3
:
revArray / revString Array, String/. , -, - . / /. String 123 -> array[3]={1,2,3} .Code:public static int[] add(int[] num1, int[]num2){ int len; int num1Len = num1.length; System.arraycopy(revArray(num1),0,num1,0,num1Len); int num2Len = num2.length; System.arraycopy(revArray(num2),0,num2,0,num2Len); if (num1Len>num2Len){ len=num1Len; } else{ len=num2Len; } int[] result= new int[len]; int temp; int carry=0; for(int i=0;i<len;i++){ if((i+1)>num1Len){ temp = (0+num2[i]); } else if ((i+1)>num2Len){ temp = (num1[i]+0); } else { temp = (num1[i]+num2[i]); } System.out.println("temp "+i+" e : "+temp); if((temp>8)&&(carry==1)){ result[i]=0; carry=1; } else if((temp>9)&&(carry==0)){ temp=temp+carry; result[i]=temp%10; carry = 1; } else{ temp=temp+carry; result[i]=(temp%10); carry = 0; } } return(revArray(result)); }MSI X570-A PRO, AMD 5800X3D, Noctua NH-D15, Corsair Vengeance LPX 2x8GB-3600, ASUS RTX ROG 3080 Strix White OC, Samsung 980 Pro 1TB NVMe, Corsair RM750, FD Define R5, Dell 32" 4k G3223Q 144Hz + 3xSamsung G5 32" 144hz, SimLab P1-X, Simucube 2 Pro, HE Ultimate, SRB GT3 Wheel
-
27th May 2010 21:04 #4Registered User
Join Date: Dec:2007
Location: Sofia
Posts: 366
int[] , , ArrayList<int>.
- , , ( ASCII '0' '9' '0', 0 9).
- , , '0', '0' '9', .
-
27th May 2010 21:27 #5MSI X570-A PRO, AMD 5800X3D, Noctua NH-D15, Corsair Vengeance LPX 2x8GB-3600, ASUS RTX ROG 3080 Strix White OC, Samsung 980 Pro 1TB NVMe, Corsair RM750, FD Define R5, Dell 32" 4k G3223Q 144Hz + 3xSamsung G5 32" 144hz, SimLab P1-X, Simucube 2 Pro, HE Ultimate, SRB GT3 Wheel
-
27th May 2010 21:53 #6
Join Date: Sep:2005
Location: Sofia
Posts: 18,517
-
27th May 2010 22:24 #7
, / , /lists /.
/
/.
@Pheoman / / - /100% /. , , .
Code:public static void add(int[] num1, int[]num2){ int len; int num1Len = num1.length; System.arraycopy(revArray(num1),0,num1,0,num1Len); int num2Len = num2.length; System.arraycopy(revArray(num2),0,num2,0,num2Len); if (num1Len>num2Len){ len=num1Len; } else{ len=num2Len; } ArrayList<Integer> result = new ArrayList<Integer>(); int temp; int carry=0; for(int i=0;i<len;i++){ if((i+1)>num1Len){ temp = (0+num2[i]); } else if ((i+1)>num2Len){ temp = (num1[i]+0); } else { temp = (num1[i]+num2[i]); } if((temp>8)&&(carry==1)&&(i==len-1)){ temp=temp+carry; while (temp!=0){ result.add(temp%10); temp = temp/10; } } else if((temp>8)&&(carry==1)){ result.add(0); carry=1; } else if((temp>9)&&(carry==0)){ temp=temp+carry; result.add(temp%10); carry = 1; } else{ temp=temp+carry; result.add(temp%10); carry = 0; } } System.out.print("Sum of these numbers is: "); int resultLen = result.size(); for (int i=0; i<resultLen; i++) { System.out.print(result.get(resultLen-i-1)); } }MSI X570-A PRO, AMD 5800X3D, Noctua NH-D15, Corsair Vengeance LPX 2x8GB-3600, ASUS RTX ROG 3080 Strix White OC, Samsung 980 Pro 1TB NVMe, Corsair RM750, FD Define R5, Dell 32" 4k G3223Q 144Hz + 3xSamsung G5 32" 144hz, SimLab P1-X, Simucube 2 Pro, HE Ultimate, SRB GT3 Wheel
-
27th May 2010 22:48 #8
Join Date: Sep:2005
Location: Sofia
Posts: 18,517
Code:BigInteger big = new BigInteger("1"); int factoriel = 100; for (Integer i = 1; i<=factoriel; i++) { BigInteger bi = new BigInteger(i.toString()); big = big.multiply(bi); } System.out.println(big);
-
27th May 2010 23:28 #9
vvvlado,
. , / . 100 double . , 30000 / big integer ,
/.
EDIT : Unbounded range.MSI X570-A PRO, AMD 5800X3D, Noctua NH-D15, Corsair Vengeance LPX 2x8GB-3600, ASUS RTX ROG 3080 Strix White OC, Samsung 980 Pro 1TB NVMe, Corsair RM750, FD Define R5, Dell 32" 4k G3223Q 144Hz + 3xSamsung G5 32" 144hz, SimLab P1-X, Simucube 2 Pro, HE Ultimate, SRB GT3 Wheel
-
23rd March 2011 20:10 #10Registered User
Join Date: Mar:2011
Location:
Posts: 3
.
, .
Code:import java.util.Arrays; import java.util.Scanner; public class CalculateFactorial { public static int[] multiplicationArrayToNumber(int number,int...array){ int arrayLength=array.length; int[] resultArray=new int[arrayLength];// for(int i=0;i<arrayLength;i++){ resultArray[arrayLength-1-i]=array[arrayLength-1-i]*number; // - } return resultArray; } public static int[] becomeElementInArrayToSingleInteger(int...array){ // int arrayLength=array.length; // - int ofMind=0; //5+7=12, 2 1 :)) for(int i=0;i<arrayLength;i++){ // array[arrayLength-1-i]=array[arrayLength-1-i]+ofMind; ofMind=array[arrayLength-1-i]/10; array[arrayLength-1-i]=array[arrayLength-1-i]%10; } return array; } public static int countZeroBeforNumber(int...array){ // int counter=0; // for(int i=0;i<array.length-1;i++){ if(array[i]==0){ counter++; continue; }else{ break; } } return counter; } public static String convertArrayToString(int number,int...array){ // int inArrayLength=array.length; //- , int outArrayLength=array.length-number; // ( ) int[] arrayWithoutZero=new int[outArrayLength]; // for(int i=0;i<outArrayLength;i++){ arrayWithoutZero[outArrayLength-1-i]=array[inArrayLength-1-i]; } String realValue=""; for(int i=0;i<outArrayLength;i++){ realValue+=arrayWithoutZero[i]; } return realValue; } public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter number's length, example - 4 maximum value is 9999 ! "); int length=input.nextInt(); int[] targetArray=new int[length]; // targetArray[targetArray.length-1]=1; System.out.print("Enter to which number calculate factorial: "); int n=input.nextInt(); for(int i=1;i<=n;i++){ targetArray=multiplicationArrayToNumber(i,targetArray); int countZero=countZeroBeforNumber(becomeElementInArrayToSingleInteger(targetArray)); String result=convertArrayToString(countZero,targetArray); System.out.printf("Factorial from %d! is %s\n",i,result); } } }






Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in