这是代码的一部分.
public class MyPolynomial {
private double coeffs[];
private int degree;
public MyPolynomial(double ... coeffs) {
if (coeffs != null && coeffs.length > 0) {
this.coeffs = new double[coeffs.length];
System.arraycopy(coeffs, 0, this.coeffs, 0, coeffs.length);
}
//this.coeffs = Arrays.copyOf(coeffs, coeffs.length);
}
public MyPolynomial(String filename) {
Scanner in = null;
try {
in = new Scanner(new File(filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
this.degree = in.nextInt();
coeffs = new double[degree+1];
for (int i = 0; i < coeffs.length; i++) {
coeffs[i] …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main() {
FILE *input;
FILE *output;
input = fopen("algsort.in", "r");
output = fopen("algsort.out", "w");
int n = 0;
fscanf(input, "%d", &n); // reads n = 7
int i;
int a[6] = {1, -1, 0, 33, 6, 5};
for(i = 0; i < 8; i++) { // 8 is on purpuse
printf("%d ", a[i]); // the output is [1 -1 0 33 6 5 7 $(Random location in memory)]
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
fscanf在数组末尾附加一个元素.这是一个问题,因为它干扰了排序算法,遗漏了一个大于n的元素.我知道for循环的限制,问题是,"n"元素仍然是数组的一部分.问题:7来自哪里?