题目详情:
一份银行流水数据,因打印模糊导致部分金额不清楚。
收入、支出、余额满足以下3条规则:
1、收入、支出、余额三列都是数字
2、同一行收入和支出的值不能同时为非零值
3、第N-1行余额(+第N行收入或-第N行支出)=第N行余额
程序语言: java
请按照规则编写算法,修复不清楚的值
输入描述:
输入数据最多25行,每行都包含四个数据,分别是:数据编号,收入、支出、余额,模糊的数据以?表示,它们之间以;隔开。
以文件结尾。第一组数据为初始数据值,收入、支出、余额数据保留2位小数。
输出描述:
以输入的数据顺序输出修复后的数据。
答题说明:
输入样例:
流水记录ID;收入;支出;余额
1;0.00;51.90;1945.45
2;0.00;1000.00;?
输出样例:
流水记录ID;收入;支出;余额
1;0.00;51.90;1945.45
2;0.00;1000.00;945.45
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
FinalItem[] list = new FinalItem[25];
Boolean hasUnknown;
Integer id;
Double a, b, d;
Boolean[] known = new Boolean[25];
int size = 0;
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] record = line.split(";");
hasUnknown = false;
id = Integer.parseInt(record[0]);
if (!record[1].equals("?")) {
a = Double.parseDouble(record[1]);
} else {
a = null;
}
if (!record[2].equals("?")) {
b = Double.parseDouble(record[2]);
} else {
b = null;
}
if (!record[3].equals("?")) {
d = Double.parseDouble(record[3]);
} else {
d = null;
}
if (a != null && a != 0d && b == null) {
b = 0d;
} else if (b != null && b != 0d && a == null) {
a = 0d;
}
hasUnknown = a == null || b == null || d == null;
known[size] = !hasUnknown;
list[size] = new FinalItem(hasUnknown, id, a, b ,d);
++size;
}
Boolean allDone = false;
while (!allDone) {
allDone = true;
if (size > 1 && known[0] && !known[1]) {
if (!list[1].fixFromAbove(list[0])) {
allDone = false;
} else {
known[1] = true;
}
}
if (size > 1 && known[size - 1] && !known[size - 2]) {
if (!list[size - 2].fixFromBellow(list[size - 1])) {
allDone = false;
} else {
known[size - 2] = true;
}
}
for (int i = 1; i < size - 1; ++i) {
if (known[i] && (!known[i - 1] || !known[i + 1])) {
if (known[i] && !known[i + 1]) {
for (int j = i; j < size - 1; ++j) {
if (known[j + 1])
continue;
if (!list[j + 1].fixFromAbove(list[j])) {
allDone = false;
break;
} else {
known[j + 1] = true;
}
}
}
if (known[i] && !known[i - 1]) {
for (int j = i; j > 0; --j) {
if (known[j - 1])
continue;
if (!list[j - 1].fixFromBellow(list[j])) {
allDone = false;
break;
} else {
known[j - 1] = true;
}
}
}
}
}
}
for (int i = 0; i < size; ++i) {
System.out.printf("%d;%.2f;%.2f;%.2f\n", list[i].id, list[i].a, list[i].b, list[i].d);
}
sc.close();
return;
}
}
class FinalItem {
Boolean hasUnknown = false;
Integer id = 0;
Double a = null;
Double b = null;
Double c = null;
Double d = null;
FinalItem(Boolean hasUnknown, Integer id, Double a, Double b, Double d) {
this.hasUnknown = hasUnknown;
this.id = id;
this.a = a;
this.b = b;
this.d = d;
if (!this.hasUnknown)
this.c = this.b + this.d - this.a;
}
Boolean fixFromAbove(FinalItem ft) {
Double c = ft.d;
if (c == null)
return false;
this.c = c;
if (this.hasUnknown) {
if (this.a == null && this.b != null && this.d != null) {
this.a = this.b + this.d - this.c;
this.hasUnknown = false;
} else if (this.b == null && this.a != null && this.d != null) {
this.b = this.c + this.a - this.d;
this.hasUnknown = false;
} else if (this.d == null && this.a != null && this.b != null) {
this.d = this.c + this.a - this.b;
this.hasUnknown = false;
}
if (this.d != null) {
Double diff = this.c - this.d;
if (diff > 0) {
this.b = diff;
this.a = 0d;
} else {
this.a = -diff;
this.b = 0d;
}
this.hasUnknown = false;
}
return !this.hasUnknown;
} else
return !this.hasUnknown;
}
Boolean fixFromBellow(FinalItem ft) {
Double d = ft.c;
if (d == null)
return false;
if (this.hasUnknown) {
this.d = d;
if (this.a != null && this.b != null) {
if (this.c == null) {
this.c = this.b + this.d - this.a;
}
this.hasUnknown = false;
}
if (this.c != null) {
Double diff = this.c - this.d;
if (diff > 0) {
this.b = diff;
this.a = 0d;
} else {
this.a = -diff;
this.b = 0d;
}
this.hasUnknown = false;
}
return !this.hasUnknown;
} else
return !this.hasUnknown;
}
}
本地测试做了很多了,死活运行时错误。WTF