Advertisement
Randomsurpriseguy

Mathematikum

Feb 8th, 2018
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Ort{
  4.     private double X;
  5.     private double Y;
  6.     private boolean B;
  7.     private String Name;
  8.     private int Dest;
  9.    
  10.     Ort(double Xin, double Yin, String Nin){
  11.         X=Xin;
  12.         Y=Yin;
  13.         Name=Nin;
  14.         B=false;
  15.         Dest=-1;
  16.     }
  17.    
  18.     //Variablenset
  19.    
  20.     void setD(int In){
  21.         Dest=In;
  22.     }
  23.    
  24.    
  25.     void setB(boolean In){
  26.         B=In;
  27.     }
  28.    
  29.     //Variablen ausgabe
  30.     double getX(){
  31.         return X;
  32.     }
  33.    
  34.    
  35.     double getY(){
  36.         return Y;
  37.     }
  38.    
  39.     String getN(){
  40.         return Name;
  41.     }
  42.    
  43.     boolean getB(){
  44.         return B;
  45.     }
  46.    
  47.     int getD(){
  48.         return Dest;
  49.     }
  50.    
  51.     double distto(Ort O2){
  52.         return (Math.pow((X-O2.getX())*(X-O2.getX())+(Y-O2.getY())*(Y-O2.getY()), 0.5));
  53.     }
  54.    
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. public class Erbe{
  63.    
  64.     private static Ort[] Land=new Ort[]{new Ort(5,0,"S1"),new Ort(0,5,"S2"),new Ort(-5,0,"S3"),new Ort(0,-5,"S4"),new Ort(0,-10,"S5")};
  65.     private static double MinDist=0;
  66.    
  67.     public static Scanner sc=new Scanner(System.in);
  68.    
  69.     public static void Ausgabe(Ort[] In){
  70.         int c=0;
  71.         while(Land[c].getD()!=-1){
  72.             System.out.println("S"+c);
  73.             c=Land[c].getD();
  74.            
  75.         }
  76.     }
  77.    
  78.    
  79.     public static void Ausgabe2(Ort[] In){
  80.         int c=0;
  81.         while(c<In.length){
  82.             System.out.println("S"+c +"nach: S"+In[c].getD());
  83.         }
  84.     }
  85.    
  86.    
  87.    
  88.     public static double Wegstrecke(int Tiefe,int Loc){
  89.         Land[Loc].setB(true);
  90.         int c=0;
  91.         if(Loc==0)c=1;
  92.         double Strecke=0;
  93.         double TDist=0;
  94.         while (c<Land.length){
  95.             if(!Land[c].getB()){
  96.                 TDist=Land[Loc].distto(Land[c])+Wegstrecke(Tiefe+1,c);
  97.             }
  98.             if (TDist<Strecke||Strecke==0){
  99.                 Strecke=TDist;
  100.                 Land[Loc].setD(c);
  101.             }
  102.             if (Tiefe==0&&(Strecke<MinDist||MinDist==0)){
  103.                 MinDist=Strecke;
  104.                 System.out.println("Neue kürzeste Strecke:");
  105.                 Ausgabe(Land);
  106.                 System.out.println(MinDist);
  107.             }
  108.             c++;
  109.         }
  110.         if (Tiefe==Land.length-1)Land[Loc].setD(-1);
  111.         Land[Loc].setB(false);
  112.         return Strecke;
  113.     }
  114.        
  115.     public static void main(String[] Args){
  116.         System.out.println(Land[1].distto(Land[2]));
  117.        
  118.         System.out.println(Wegstrecke(0,0));
  119.        
  120.     }
  121.    
  122.    
  123.    
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement