SRM 635 Div 2 250 & 500 Points
After long time not participate in topcoder because of work load, i decide after office today afternoon to participate in simple problem div 2
*IdentifyingWood (250 points) : *
for this case in my mind i need to 1 loop iterate through n ( length of s text ) and check if i can flag every char in t
public class IdentifyingWood {
public static String check(String s, String t){
int ls = s.length();
int lt = t.length();
int a = 0;
String wood = "Yep, it's wood.";
String none = "Nope.";
for (int i = 0; i < ls; i++) {
if (a < lt){
if (s.charAt(i) == t.charAt(a)){
a++;
}
}
else{
break;
}
}
if (a == lt){
return wood;
}
return none;
}
}
*QuadraticLaw (500 points) : *
in this case the solutison is simple by using quadratic equation :
** ( -b +- sqrt(b2 - 4ac)/-2a**
But when i apply this formula to the solution Java Precition point is killing me. When the solution 999999998999999999l is 999999998 not 999999999 arghhhhhhhhhhhhhhh this is my first answer
public class QuadraticLaw {
public static void main(String[] args) {
System.out.println(getTime(999999998999999999l));
}
public static long getTime(long d){
long bac = (1 + ( 4 * d) );
long res1 = (long) (1 - Math.sqrt(bac))/-2;
long res2 = (long) (1 + Math.sqrt(bac))/-2;
return Math.max(res1, res2);
}
}
But once again i found a bruteforce solution is by using a loop from a square of d and add it once by once :
public class QuadraticLaw {
public static void main(String[] args) {
System.out.println(getTime(999999998999999999l));
}
public static long getTime(long c){
long t = (long) Math.sqrt(c);
long res = t * (t+1);
while(res <= c){
t++;
res = t * (t+1);
if (res < c)return --t;
}
return t-1;
}
}