-3

For any two strings of digits,A and B, we define Fa,b to be the sequence (A,B ,AB ,BAB ,ABBAB ,...) in which each term is the concatenation of the previous two.

Further, we define Da,b(n) to be the n-th digit in the first term of Fa,b that contains at least

n digits.

Example:

Let A=1415926535 ,B=8979323846 . We wish to find Da,b(35)

, say.

The first few terms of Fa,b are:

1415926535 8979323846 141592653589793233846 897932384614159265358979323846 14159265358979323846897932384614159265358979323846

Then Da,b(35) is the 35-th digit in the fifth term, which is

9.

You are given q triples (A,B,n). For all of them find

Da,b(n).

Input Format

First line of each test file contains a single integer q that is the number of triples. Then q lines follow, each containing two strings of decimal digits a and b and positive integer n.

Output Format

Print exactly q lines with a single decimal digit on each: value of Da,b(n) for the corresponding triple.

public void fibo(String a,String b,int n){
    String n1=Integer.toString(n);
    char n2;
    n2=n1.charAt(1);
    int n3=Character.getNumericValue(n2);
    String c;
    for(int i=2;i<n3;i++){
        c=a+b;
        System.out.printf(c+" ");
        a=b;
        b=c;
    }

    System.out.println(c.charAt(n));
}

Compile Message

Solution.java:26: error: variable c might not have been initialized System.out.println(c.charAt(n)); ^ 1 error

Exit Status

1

1 Answers1

1

Solution.java:23: error: variable c might not have been initialized String d=Integer.toString(c); ^ 1 error

You are not assigning a value to c prior to your loop. The compiler can't know whether that loop will ever be entered.

Conclusion: you can reach that print statement without ever assigning a value to c.

Can be fixed easily:

int c = 0;

And note: you need that because c is a local variable in a method. Those aren't initalized by default (in contrast to fields of a class for example).

Beyond that: look into naming. Your code is super hard to understand, just because your overly excessive use of one-character names. Use names that mean something, that tell you what the variable is intended to be used for.

GhostCat
  • 137,827
  • 25
  • 176
  • 248