You are on page 1of 2

8 Sumeleproduselornumerelori Sumeleproduselornumerelori

mparedouăcâtedouăîncepând mparedouăcâtedouăîncepând Listingul programului:


cu primul element cu ultimul element

public class lab1 {


public static void main(String args[]) {
int[] tot= new int[100];
for(int i=1;i<=99;i++){ tot[i] = (int)(Math.random()*98);}
for(int i=1;i<=99;i++){System.out.print(tot[i]+" ");}
System.out.println();
Counter count1, count2;
count1 = new Counter(0, 99, 1, tot);
count2 = new Counter(99, 0, -1, tot);
count1.start();
count2.start();
}
}
class Counter extends Thread {
private int[] tot;
private int from, to, step;
public Counter(int from, int to, int step,int[] tot) {
this.from = from;
this.to = to;
this.step = step;
this.tot = tot;
}
public void run() {
int to11=0; int to12=0;
int to21=0; int to22=0;
int n=0;
for(int i = from; i != to; i += step){
if(tot[i]%2!=0){n++;
if (n==1){to11=tot[i];}
if (n==2){ to12=tot[i];}
if (n==3){to21=tot[i];}
if (n==4){
to22=tot[i];
n=0;
System.out.println(this+":("+to11+"*"+to12+"+"+to21+"*"+to22+")="+
(to11*to12+to21*to22));
}
}
}
}
}

Rezultatele programului:

run:
18 38 50 33 51 15 48 16 7 47 62 86 88 1 68 49 12 36 44 70 62 87 76 38 54 9 1 23 22 56 93 11
17 59 17 64 47 6 71 81 78 82 54 29 2 20 40 53 5 64 79 78 11 42 40 33 52 66 56 44 96 68 34 22
57 29 4 16 82 33 53 14 4 68 3 69 35 37 29 82 78 49 1 7 32 41 24 89 15 45 44 42 65 33 44 11 2
33 43
Thread[Thread-0,5,main]:(33*51+15*7)=1788
Thread[Thread-0,5,main]:(47*1+49*87)=4310
Thread[Thread-1,5,main]:(43*33+11*33)=1782
Thread[Thread-1,5,main]:(65*45+15*89)=4260
Thread[Thread-0,5,main]:(9*1+23*93)=2148
Thread[Thread-1,5,main]:(41*7+1*49)=336
Thread[Thread-0,5,main]:(11*17+59*17)=1190
Thread[Thread-1,5,main]:(29*37+35*69)=3488
Thread[Thread-0,5,main]:(47*71+81*29)=5686
Thread[Thread-1,5,main]:(3*53+33*29)=1116
Thread[Thread-0,5,main]:(53*5+79*11)=1134
Thread[Thread-0,5,main]:(33*57+29*33)=2838
Thread[Thread-1,5,main]:(57*33+11*79)=2750
Thread[Thread-0,5,main]:(53*3+69*35)=2574
Thread[Thread-0,5,main]:(37*29+49*1)=1122
Thread[Thread-1,5,main]:(5*53+29*81)=2614
Thread[Thread-0,5,main]:(7*41+89*15)=1622
Thread[Thread-0,5,main]:(45*65+33*11)=3288
Thread[Thread-1,5,main]:(71*47+17*59)=4340
Thread[Thread-1,5,main]:(17*11+93*23)=2326
Thread[Thread-1,5,main]:(1*9+87*49)=4272
Thread[Thread-1,5,main]:(1*47+7*15)=152
СБОРКА УСПЕШНО ЗАВЕРШЕНА (общее время: 0 секунд)

You might also like