Sequence
Problem Description:
Sequence a satisfies an = An-1 + An-2 + An-3, n >= 3
Write programs, given A0, A1, and A2, to calculate A99
Input:
Input contains multiple rows of data
Each row of data contains 3 integers A0, A1, A2 (0 <= A0, A1, A2 <= 32767)
Data ends with EOF
Output:
The value of the output A99 for each line of input
Sample input:
1 1 1
Sample output:
69087442470169316923566147
Code implementation:
* @author:
Import java.util.*;
Import Java.math.BigInteger;
public class Main {
Static string Doadd (string A, string b) {//Two a large number of methods added.
String str = "";
int lenA = A.length ();
int lenB = B.length ();
int maxlen = (LenA > LenB)? lena:lenb;
int minlen = (LenA < LenB)? Lena:lenb;
String strtmp = "";
for (int i = Maxlen-minlen; i > 0; i--) {
Strtmp + = "0";
}
//Adjust the length to the same
if (maxlen = = LenA) {
B = strtmp + b;
} else
A = strtmp + A;
int JW = 0;//carry
for (int i = maxLen-1; I >= 0; i--) {
int tempa = Integer.parseint (string.valueof (A.C Harat (i)));
int TEMPB = Integer.parseint (string.valueof (B.charat (i)));
Int temp;
if (Tempa + TEMPB + JW >= && i! = 0) {
Temp = Tempa + TEMPB + JW-10;
JW = 1;
} else {
temp = Tempa + TEMPB + JW;
JW = 0;
}
str = string.valueof (temp) + str;
}
return str;
}
public static void Main (string[] args) {
Scanner in=new Scanner (system.in);
String A[]=new string [100];
while (In.hasnext ()) {
A[0]=integer.tostring (In.nextint ());
A[1]=integer.tostring (In.nextint ());
A[2]=integer.tostring (In.nextint ());
for (int i=3;i< 100;i++) {
String Temp=doadd (a[i-1],a[i-2]);
A[i]=doadd (temp,a[i-3]);
}
System.out.println (a[99]);
}
}
}
Method Two:
Import java.math.*;
Import java.util.*;
public class Main
{
public static BigInteger Calc (BigInteger a,biginteger b,biginteger c)
{
BigInteger now = c;
BigInteger last = b;
BigInteger llast = A;
BigInteger answer;
for (int i=0;i< 97;i++) {
Answer = Now.add (last);
Answer = Answer.add (llast);
Llast = Last;
last = Now;
now = answer;
}
return now;
}
public static void Main (string[] args)
{
Scanner in = new Scanner (system.in);
while (In.hasnext ()) {
int a0 = In.nextint ();
BigInteger A0 = biginteger.valueof (a0);
int a1 = In.nextint ();
BigInteger A1 = biginteger.valueof (A1);
int a2 = In.nextint ();
BigInteger A2 = biginteger.valueof (A2);
System.out.println (Calc (a0,a1,a2));
}
}
}
POJ 3982 Sequence [problem solving report] Java