学校有个算法程序要写:这个程序要求输入一组数组:
第一个数组有三个值:代表电脑数,下面输入数组的行数,还有问题数;
第二个数组是二维的,每行三个值:代表电脑1,电脑2,还有它们的通信时间;
第三个数组是问题, 每行有四个值:代表电脑1在时间a染的病毒,有没有可能传染给电脑2,在时间b;
最后要求如果可能传染,写出传染路径。
下面是我写的程序,但是输出数组的末尾总带有很多0,我的编程基础是在差,请各位高手告诉我为什么,谢谢! 现把加了注释的代码贴出来,并改了点,但还是出0,
而且会提示NumberFormatException.forInputString(s)参考写的需求分析,可以输入以下数组测试:
5 5 2 //第一个数组;
4 5 3 //第二个数组;
1 2 4
2 4 8
3 4 8
1 4 12
2 5 5 12 //第三个数组;
1 2 3 8
import java.io.*;
public class assignment1{
public static int[] readALine() { //这个方法的目的是返回一行数组;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
String[] tmp = input.split( " + ");
int[] a = new int[tmp.length];
for (int i = 0; i < a.length; i++)
a[i] = Integer.parseInt(tmp[i]);
return a;
} catch (IOException e) {
System.out.println( "read a integer has some problem! ");
return null;
}
}
public static int[] dfs(int[] a, int[][] b, int[] c) //这个是主要的算法,
{ //a 对应main中的require, b对应L, c对应firstL;
int i=0;
int[] Layer = new int[b.length]; //初始化
Layer[i]=a[0]; //初始化根结点;
int det=a[0];
for(int x=0; x <c[1]; x++){ //c[1]是输入的行数;
for(int j=0; j <2; j++){ //每行只遍历前两个元素,第三个代表时间;
while((a[0]==b[x][j])&&(b[x][1-j]!=det)&&(b[x][2] >=a[1])&&(b[x][2] <=a[3])){
//四个必要条件,后两个是时间上的限制;
if (b[x][1 - j] != a[2]) {
//如果a[2]不匹配;
Layer[i] = b[x][1 - j]; //将路径结点输入返回数组中;
a[0] = b[x][1 - j];
det = b[x][j];
i++;
}
else {
Layer[i] = b[x][1 - j];
//否则,b[x][1 - j]即为a[2],返回数组;
return Layer;
}
}
}
}
return null;
}
public static void main(String[] args)
{
System.out.println( "please input the data : ");
int[] firstL=readALine(); //输入第一个数组;
System.out.println( "please input the data : ");
int[][] L = new int[firstL[1]][3]; //输入读取第二个数组;
for(int i=0; i <firstL[1]; i++)
{ int[] mid=readALine();
for(int j=0; j <3; j++)
{
L[i][j] = mid[j];
}
}
System.out.println( "please input the data : ");
int[] require = new int[4]; //输入读取第三个数组;
for(int i=1; i <=firstL[2]; i++)
{require=readALine();
int a =require[0];
int[] fin=new int[firstL[1]*3];
fin=dfs(require, L, firstL);
if(fin!=null)
{
System.out.print( "( "+a+ ", "+fin[0]+ ") "); //打印结果;
for(int j=1;j <fin.length;j++)
System.out.print( "( "+fin[j-1]+ ", "+fin[j]+ ") ");
}else{
System.out.println( "Infection not possible! ");
}
}
}
}
第一个数组有三个值:代表电脑数,下面输入数组的行数,还有问题数;
第二个数组是二维的,每行三个值:代表电脑1,电脑2,还有它们的通信时间;
第三个数组是问题, 每行有四个值:代表电脑1在时间a染的病毒,有没有可能传染给电脑2,在时间b;
最后要求如果可能传染,写出传染路径。
下面是我写的程序,但是输出数组的末尾总带有很多0,我的编程基础是在差,请各位高手告诉我为什么,谢谢! 现把加了注释的代码贴出来,并改了点,但还是出0,
而且会提示NumberFormatException.forInputString(s)参考写的需求分析,可以输入以下数组测试:
5 5 2 //第一个数组;
4 5 3 //第二个数组;
1 2 4
2 4 8
3 4 8
1 4 12
2 5 5 12 //第三个数组;
1 2 3 8
import java.io.*;
public class assignment1{
public static int[] readALine() { //这个方法的目的是返回一行数组;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
String[] tmp = input.split( " + ");
int[] a = new int[tmp.length];
for (int i = 0; i < a.length; i++)
a[i] = Integer.parseInt(tmp[i]);
return a;
} catch (IOException e) {
System.out.println( "read a integer has some problem! ");
return null;
}
}
public static int[] dfs(int[] a, int[][] b, int[] c) //这个是主要的算法,
{ //a 对应main中的require, b对应L, c对应firstL;
int i=0;
int[] Layer = new int[b.length]; //初始化
Layer[i]=a[0]; //初始化根结点;
int det=a[0];
for(int x=0; x <c[1]; x++){ //c[1]是输入的行数;
for(int j=0; j <2; j++){ //每行只遍历前两个元素,第三个代表时间;
while((a[0]==b[x][j])&&(b[x][1-j]!=det)&&(b[x][2] >=a[1])&&(b[x][2] <=a[3])){
//四个必要条件,后两个是时间上的限制;
if (b[x][1 - j] != a[2]) {
//如果a[2]不匹配;
Layer[i] = b[x][1 - j]; //将路径结点输入返回数组中;
a[0] = b[x][1 - j];
det = b[x][j];
i++;
}
else {
Layer[i] = b[x][1 - j];
//否则,b[x][1 - j]即为a[2],返回数组;
return Layer;
}
}
}
}
return null;
}
public static void main(String[] args)
{
System.out.println( "please input the data : ");
int[] firstL=readALine(); //输入第一个数组;
System.out.println( "please input the data : ");
int[][] L = new int[firstL[1]][3]; //输入读取第二个数组;
for(int i=0; i <firstL[1]; i++)
{ int[] mid=readALine();
for(int j=0; j <3; j++)
{
L[i][j] = mid[j];
}
}
System.out.println( "please input the data : ");
int[] require = new int[4]; //输入读取第三个数组;
for(int i=1; i <=firstL[2]; i++)
{require=readALine();
int a =require[0];
int[] fin=new int[firstL[1]*3];
fin=dfs(require, L, firstL);
if(fin!=null)
{
System.out.print( "( "+a+ ", "+fin[0]+ ") "); //打印结果;
for(int j=1;j <fin.length;j++)
System.out.print( "( "+fin[j-1]+ ", "+fin[j]+ ") ");
}else{
System.out.println( "Infection not possible! ");
}
}
}
}
这个我上次写的时候好像不是这样的吧,楼主都没理解意思就随便改成这样String[] tmp = input.split( " +"); //这个表示以若干个空格为分隔符,你上面的写法是以若干个(2n+2,n>=1)空格为分隔符
import java.util.*;class classB{
private int[] b;
public classB(int[] b){
this.b = b;
} public int getA(){
return b[0];
} public int getB(){
return b[1];
} public int getTime(){
return b[2];
}
}class Trace extends ArrayList{
private int time;
public String toString(){
Iterator it = super.iterator();
String s=" ";
while(it.hasNext()){
s = s + it.next().toString() + " ";
}
return s;
}
public Trace(int time){
this.time = time;
}
public int getTime(){
return time;
}
}public class assignment1{
public static void dfs(int[] a, int[][] b, int[] c){
ArrayList underThinck = new ArrayList();
Map hasHack = new HashMap();
for(int i=0;i<b.length;i++){
if( c[1]<= b[i][2] && c[3]>=b[i][2]){
if(c[0] == b[i][0]){
Trace t = new Trace(b[i][2]);
if(c[2] == b[i][1]){
System.out.println(c[0] + " " + c[2]);
return;
}
t.add(new Integer(b[i][0]));
hasHack.put(new Integer(b[i][1]),t);
}
else if(c[0] == b[i][1]){
if(c[2] == b[i][0]){
System.out.println(c[2] + " " + c[0]);
return;
}
Trace t = new Trace(b[i][2]);
t.add(new Integer(b[i][1]));
hasHack.put(new Integer(b[i][0]),t);
}
else{
underThinck.add(new classB(b[i]));
}
}
}
boolean bContinue;
do{
bContinue = false;
Iterator it = underThinck.iterator();
while(it.hasNext()){
classB tempB = (classB)it.next();
Object o1 = hasHack.get(new Integer(tempB.getA()));
Object o2 = hasHack.get(new Integer(tempB.getB()));
if(o1 != null && o2 == null){
Trace t = (Trace)o1;
if(t.getTime() <= tempB.getTime()){
Trace t2 = new Trace(tempB.getTime());
t2.add(t);
t2.add(new Integer(tempB.getA()));
hasHack.put(new Integer(tempB.getB()),t2);
bContinue = true;
if(tempB.getB() == c[2]){
System.out.println(t2.toString() + " " + c[2]);
return;
}
underThinck.remove(tempB);
break;
} }
else if(o1 == null && o2 != null){
Trace t2 = (Trace)o2;
if(t2.getTime() <= tempB.getTime()){
Trace t1 = new Trace(tempB.getTime());
t1.add(t2);
t1.add(new Integer(tempB.getB()));
hasHack.put(new Integer(tempB.getA()),t1);
bContinue = true;
if(tempB.getA() == c[2]){
System.out.println(t1.toString() + " " + c[2]);
return;
}
}
underThinck.remove(tempB);
break;
}
}
}while(bContinue);
System.out.println( "Infection not possible! ");
}
public static int[] readALine() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
String[] tmp = input.split(" ");
int[] a = new int[tmp.length];
for (int i = 0; i < a.length; i++)
a[i] = Integer.parseInt(tmp[i]);
return a;
} catch (IOException e) {
System.out.println( "read a integer has some problem! ");
return null;
} }
public static void main(String[] args)
{
System.out.println( "please input the data : ");
int[] firstL=readALine();
System.out.println( "please input the data : ");
int[][] L = new int[firstL[1]][3];
for(int i=0; i <firstL[1]; i++)
{ int[] mid=readALine();
for(int j=0; j <3; j++)
{
L[i][j] = mid[j];
}
}
System.out.println( "please input the data : ");
int[] require = new int[4];
for(int i=1; i <=firstL[2]; i++)
{require=readALine();
int a =require[0];
int[] fin=new int[firstL[1]*3];
dfs(firstL, L,require);
}
}
}
import java.io.*;
import java.util.*;class classB{
private int[] b;
public classB(int[] b){
this.b = b;
} public int getA(){
return b[0];
} public int getB(){
return b[1];
} public int getTime(){
return b[2];
}
}class Trace extends ArrayList{
private int time;
//public String toString(){
//Iterator it = super.iterator();
//String s=" ";
//while(it.hasNext()){
//s = s + it.next().toString() + " ";
//}
//return s;
//}
public Trace(int time){
this.time = time;
}
public int getTime(){
return time;
}
}public class assignment1{
public static void dfs(int[][] b, int[] c){
ArrayList underThinck = new ArrayList();
Map hasHack = new HashMap();
for(int i=0;i<b.length;i++){
if( c[1]<= b[i][2] && c[3]>=b[i][2]){ //time requirement;
if(c[0] == b[i][0]){ //if first node == root node;
Trace t = new Trace(b[i][2]);
if(c[2] == b[i][1]){
System.out.println("(" + c[0] + ", " + c[2] +" ,"+t.getTime()+" )");
return;
}
t.add(new Integer(b[i][0])); //add a new element to the end of t;
hasHack.put(new Integer(b[i][1]),t); //a mapping btw key and value;
}
else if(c[0] == b[i][1]){ //if second node == root node;
if(c[2] == b[i][0]){
System.out.println("(" + c[0] + ", " + c[2] +" ,"+b[i][2]+" )");
return;
}
Trace t = new Trace(b[i][2]);
t.add(new Integer(b[i][1]));
hasHack.put(new Integer(b[i][0]),t);
}
else{
underThinck.add(new classB(b[i])); //
}
}
}
do{
bContinue = false;
Iterator it = underThinck.iterator(); //b[i];
while(it.hasNext()){ //keep seeking;
classB tempB = (classB)it.next(); //b[i];
Object o1 = hasHack.get(new Integer(tempB.getA())); //o1 <-- getA;
Object o2 = hasHack.get(new Integer(tempB.getB())); //o2 <-- getB;
if(o1 != null && o2 == null){ //is there any other node == getA?
Trace t = (Trace)o1;
if(t.getTime() <= tempB.getTime()){ //time requirement;
Trace t2 = new Trace(tempB.getTime());
t2.add(t);
t2.add(new Integer(tempB.getA()));
hasHack.put(new Integer(tempB.getB()),t2);
bContinue = true;
if(tempB.getB() == c[2]){
//System.out.println("("+t2.toString() + ", " + c[2]+", "+t2.getTime()+" )");
System.out.println("("+tempB.getA() + ", "+tempB.getB()+", " +tempB.getTime()+" )");
return;
}else{
System.out.println("("+tempB.getA() + ", "+tempB.getB()+", " +tempB.getTime()+" )");
}
underThinck.remove(tempB);
break;
}
}
else if(o1 == null && o2 != null){
Trace t2 = (Trace)o2;
if(t2.getTime() <= tempB.getTime()){
Trace t1 = new Trace(tempB.getTime());
t1.add(t2);
t1.add(new Integer(tempB.getB()));
hasHack.put(new Integer(tempB.getA()),t1);
bContinue = true;
if(tempB.getA() == c[2]){
//System.out.println("("+t1.toString() + ", " + c[2]+", "+t2.getTime()+" )");
System.out.println("("+tempB.getA() + ", "+tempB.getB()+", " +tempB.getTime()+" )");
return;
}else{
System.out.println("("+tempB.getA() + ", "+tempB.getB()+", " +tempB.getTime()+" )");
}
}
underThinck.remove(tempB);
break;
}
}
}while(bContinue);
System.out.println( "Infection not possible! ");
}
public static int[] readALine() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
String[] tmp = input.split(" ");
int[] a = new int[tmp.length];
for (int i = 0; i < a.length; i++)
a[i] = Integer.parseInt(tmp[i]);
return a;
} catch (IOException e) {
System.out.println( "read a integer has some problem! ");
return null;
} }
public static void main(String[] args)
{
System.out.println( "please input the data : "); //input the first array;
int[] firstL=readALine();
System.out.println( "please input the data : "); //input the second array;
int[][] L = new int[firstL[1]][3];
for(int i=0; i <firstL[1]; i++)
{ int[] mid=readALine();
for(int j=0; j <3; j++)
{
L[i][j] = mid[j];
}
}
for(int i=0; i<firstL[1]; i++)
if(firstL[0]<L[i][0] || firstL[0]<L[i][1]){
System.out.println("Excuse me, but I don't think we've got so many computers~~");
return;
}
System.out.println( "please input the data : "); //input the third array;
int[] require = new int[4];
for(int i=1; i <=firstL[2]; i++)
{
require=readALine();
//int a =require[0];
//int[] fin=new int[firstL[1]*3];
System.out.println("Question" +i+ ": ");
dfs(L, require);
}
}
}
对比帖子里的输入格式,下面是输出格式:
Question No 1
Infection not possible
Question No 2
(1,2,4)(2,4,8)(4,3,8)