Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // QCM: a / b / a / f / a / b / c / b / a / b / a / f
- //ex2
- // methode 1
- import java.util.*;
- public class Ex5 {
- public static void main(String[] args) {
- int[] numbers = new int[]{8,2,7,4,6,3,1};
- int sum = 0;
- int _sum = (numbers.length + 1) * (numbers.length + 2) / 2; // arithmetic sum (n+1)*n/2, n=numbers.length+1
- for(int i=0; i<numbers.length;i++)
- sum += numbers[i];
- System.out.print(_sum - sum);
- }
- }
- // methode 2
- import java.util.*;
- public class Ex5 {
- public static void main(String[] args) {
- int[] numbers = new int[]{8,2,7,4,6,3,1};
- ArrayList n = new ArrayList();
- for(int i=0; i<numbers.length;i++)
- n.add(numbers[i]);
- for(int i=1; i<(int)Collections.max(n);i++)
- if(!n.contains(i))
- System.out.print(i);
- }
- }
- // ex3
- 2
- -2
- B
- B
- 0
- -2
- A
- B
- // ex4
- class TabPlein extends Exception{
- public String toString{
- return "tableau plein!";
- }
- }
- abstract class TabTrie{
- Object tab[];
- int nbrObj = 0;
- static boolean plusGrand(Object a, Object b) {
- return false;
- }
- abstract void ajouter(Object a) throws TabPlein;
- public abstract String toString();
- }
- class CoupleLexico{
- private int x, y;
- CoupleLexico(int x, int y){
- this.x = x;
- this.y = y;
- }
- int getX() {
- return x;
- }
- int getY() {
- return y;
- }
- public String toString() {
- return new String("("+getX()+","+getY()+")");
- }
- }
- public class TabTriCouple extends TabTrie {
- TabTriCouple(int n){
- tab = new CoupleLexico[n];
- }
- static boolean plusGrand(CoupleLexico a, CoupleLexico b) {
- return ( a.getX() > b.getX() ) || ( a.getX() == b.getX() && a.getY() > b.getY() );
- }
- void ajouter(Object a) throws TabPlein {
- if( nbrObj == tab.length )
- throw new TabPlein();
- int i;
- for( i=nbrObj ; i>0 ; i-- ) {
- if( plusGrand((CoupleLexico) tab[i-1],(CoupleLexico) a) ) {
- tab[i] = tab[i-1];
- }
- else
- break;
- }
- tab[i] = (CoupleLexico) a;
- nbrObj++;
- }
- public String toString() {
- String str = new String();
- CoupleLexico c;
- for( int i=0 ; i<nbrObj ; i++ ) {
- c = (CoupleLexico) tab[i];
- str += c + ", ";
- }
- return str;
- }
- public static void main(String[] args) {
- TabTriCouple c = new TabTriCouple(8);
- try {
- c.ajouter(new CoupleLexico(2, 4));
- c.ajouter(new CoupleLexico(3, 4));
- c.ajouter(new CoupleLexico(5, 2));
- c.ajouter(new CoupleLexico(4, 2));
- c.ajouter(new CoupleLexico(4, 3));
- c.ajouter(new CoupleLexico(2, 1));
- c.ajouter(new CoupleLexico(3, 5));
- System.out.println(c);
- } catch (TabPlein e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment