public class ThreeDigits {›ύσεις... · · 2018-01-08... (palindrome(i)){count++;...
Click here to load reader
Transcript of public class ThreeDigits {›ύσεις... · · 2018-01-08... (palindrome(i)){count++;...
1
public class ThreeDigits {
public static boolean palindrome(int i){
if (i >= 0 && i <= 9) return true;
else if (i >= 10 && i <= 99) return (i%10 == i/10);
else return (i % 10 == i / 100);
}
public static void main (String[] args){
int d1 = Integer.parseInt(args[0]);
int d2 = Integer.parseInt(args[1]);
int d3 = Integer.parseInt(args[2]);
int temp = 0;
if (d1 > d2){temp = d1; d1 = d2; d2 = temp;}
if (d1 > d3){temp = d1; d1 = d3; d3 = temp;}
if (d2 > d3){temp = d2; d2 = d3; d3 = temp;}
int max = d3 * 100 + d2 * 10 + d1;
int min = d1 * 100 + d2 * 10 + d3;
System.out.println("Min = " + min + " Max = " + max);
System.out.println("The numbers that are palindromes are:");
int count = 0;
for (int i = min; i <= max; i++)
if (palindrome(i)){count++; System.out.printf("%5d", i);
if (count % 10 == 0) System.out.println();}
System.out.println();
}
}
2
public class FiveFSS {
public static boolean isleap (int y){
return (y % 4 == 0) && (y % 100 != 0 || y % 400 == 0);
}
public static void main (String[] args){
int year = Integer.parseInt(args[0]);
int day = Integer.parseInt(args[1]);
int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String[] months = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
if (isleap(year)) monthDays[1] = 29;
int[] firstD = new int [12];
firstD[0] = day;
for (int i = 1; i < 12; i++){
firstD[i] = (firstD[i-1] + monthDays[i-1])% 7;
}
System.out.println("Months in year " + year +
" with five Fri, Sat and Sun:");
for (int i = 0; i < 12; i++)
if (firstD[i] == 5 && monthDays[i] == 31)
System.out.println(months[i]);
}
}
3
public class Ariadne{
public static boolean ariadne (int n){
int s1 = n % 10;
int s2 = 0;
n = n / 10;
while (n >= 10){
s2 = s2 + n % 10;
n = n / 10;
}
s1 = s1 + n;
return s1 == s2;
}
public static void main (String[] args){
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
if (x > 99 && y > 99 && x < y){
System.out.println("The ariadne nos from " + x + " to " + y + " are:");
int count = 0;
for (int i = x; i <= y; i++)
if (ariadne(i)) {System.out.printf("%6d", i); count++;
if (count % 10 == 0) System.out.println();}
System.out.println();
}
else System.out.println("Wrong Input .. program terminates\n");
}
}
4
public class Candidate {
private String id;
private double score;
private String[] preferences;
private boolean[] activePrefs = {true, true, true, true, true};
private boolean allocationMade = false;
private String studyPlace;
public Candidate(String identity, double s, String[] ps){
id = identity;
score = s;
preferences = ps;
}
public String getId(){ return id; }
public double getScore(){ return score; }
public boolean hasAllocation() { return allocationMade; }
public void makeAllocation (String prog){
allocationMade = true;
studyPlace = prog;
}
public String getPreference (int n) { return preferences[n-1]; }
public String toStringPrefs (){
String s = "\nCandidate " + id + " has the following preferences:\n";
for (int i = 0; i < preferences.length; i++){
s = s + "\t>> Program " + preferences[i] + " is choice " + (i+1) + "\n";
}
return s;
5
}
public String toString(){
String s = "Candidate " + id + " with score " + score;
if (allocationMade) s = s + " has been allocated a place on Program " + studyPlace;
else s = s + " has not been allocated a place";
/* s = s + this.toStringPrefs(); */
return s;
}
public void insertPrefs (Program[] ps){
for (int i = 0; i < ps.length; i++){
String code = ps[i].getCode();
int ppos = -1;
for (int j = 0; j < preferences.length; j++)
if (code.equals(preferences[j])) ppos = j;
if (ppos != -1) ps[i].insertCandidate(this);
}
}
public boolean releasePrefsBelow(String prog, Program[] ps){
boolean releaseMade = false;
int ppos = preferences.length;
for (int i = 0; i < preferences.length; i++)
if (prog.equals(preferences[i])) ppos = i;
for (int i = ppos+1; i < preferences.length; i++){
for (int p = 0; p < ps.length; p++){
if (ps[p].getCode().equals(preferences[i]) && activePrefs[i]){
activePrefs[i] = false;
ps[p].removeCandidate(id);
releaseMade = true;
}
}
}
6
return releaseMade;}
public static void main (String[] args){}
}
public class Program {
private String code;
private int places;
private int count = 0;
private Candidate[] cands;
private int freePlaces;
public Program(String c, int ps, int cs){
code = c;
places = ps;
freePlaces = ps;
cands = new Candidate[cs];
}
public String getCode(){return code;}
public int getPlaces(){return places;}
public int getCount(){return count;}
public Candidate[] getCands(){return cands;}
public int getFreePlaces(){return freePlaces;}
7
public String toStringCands(){
String s = "The ranked list of candidates is";
for (int i = 0; i < count; i++){
s = s + "\n>> " + cands[i];
}
return s;
}
public String toStringAllocations(){
String s = "The following candidates get a place on the programme";
for (int i = 0; i < min(places,count); i++){
s = s + "\n>> " + cands[i].getId() + " " + cands[i].getScore();
}
s = s + "\nThe programme has " + freePlaces + " free places";
return s;
}
public String toString(){
String s = "Program " + code + " has " + places + " places\n" +
this.toStringAllocations();
return s;
}
private static int min(int x, int y){
if (x < y) return x; else return y;
}
public void allocatePlaces(){
for (int i = 0; i < min(places,count); i++){
freePlaces--;
cands[i].makeAllocation(code);
}
}
8
public void insertCandidate(Candidate c){
if (count == 0){
cands[0] = c; count = 1;}
else {int pos = 0;
while (pos < count && c.getScore() < cands[pos].getScore()) pos++;
if (pos == count){cands[count] = c; count++;}
else {for (int i = count; i > pos; i--) cands[i] = cands[i-1];
cands[pos] = c;
count++;
}
}
}
public void removeCandidate(String identity){
int pos = 0;
while (pos < count && !cands[pos].getId().equals(identity)) pos++;
if (pos == count); /* do nothing */
else { for (int i = pos + 1; i < count; i++) cands[i-1] = cands[i];
count--;
}
}
public boolean requestPrefsRelease(Program[] ps){
boolean releaseMade = false;
int x = min(places, count);
for (int i = 0; i < x; i++){
boolean res = cands[i].releasePrefsBelow(code, ps);
releaseMade = releaseMade || res;
}
return releaseMade;
}
9
public static void main(String[] args){}
}
public class EntranceSystem {
public static void displayCandidateAllocations (Candidate[] cands){
System.out.print("\n\nCANDIDATE ALLOCATIONS\n");
for (int i = 0; i < cands.length; i++)
System.out.println(cands[i]);
System.out.print("\n\n");
}
public static void displayProgramAllocations (Program[] progs){
System.out.print("\n\nPROGRAM ALLOCATIONS\n");
for (int i = 0; i < progs.length; i++)
System.out.println(progs[i]);
System.out.print("\n\n");
}
public static void main(String[] args){
int P = StdIn.readInt();
int C = StdIn.readInt();
Program[] progs = new Program[P];
Candidate[] cands = new Candidate[C];
for (int i = 0; i < P; i++){
String code = StdIn.readString();
int places = StdIn.readInt();
progs[i] = new Program(code, places, C);
}
10
for (int i = 0; i < C; i++){
String Id = StdIn.readString();
double score = StdIn.readDouble();
String[] prefs = new String[5];
for (int j = 0; j < 5; j++)
prefs[j] = StdIn.readString();
cands[i] = new Candidate(Id, score, prefs);
}
for (int i = 0; i < C; i++){
cands[i].insertPrefs(progs);
}
/* displayProgramAllocations(progs); */
boolean releaseMade;
do {
releaseMade = false;
for (int i = 0; i < P; i++) {
boolean res = progs[i].requestPrefsRelease(progs);
releaseMade = releaseMade || res;
}
} while(releaseMade);
for (int i = 0; i < P; i++)
progs[i].allocatePlaces();
displayCandidateAllocations(cands);
displayProgramAllocations(progs);
}
}
11
Cands.txt
5 5 5 5 5
1 16.25 2 5 1 0
2 20.0 4 5 2 0
3 18.36 2 5 1 4 0
4 19.56 3 4 5 1 0
5 15.78 5 2 1 3 0
6 12.78 1 2 0
7 15.76 3 1 5 2 0
8 17.50 4 5 3 0
9 15.67 1 3 5 0
10 19.12 1 3 5 0
11 15.45 1 2 3 4 5 0
12 16.34 3 2 4 5 0
13 19.67 5 4 1 3 0
14 18.23 3 2 4 1 0
15 12.80 1 5 3 0
16 10.67 1 4 2 0
17 17.06 3 5 1 4 0
18 18.56 5 2 3 1 0
19 10.50 5 4 0
20 19.05 5 1 0
21 16.45 4 3 2 0
22 15.78 4 3 0
23 12.67 5 4 2 0
24 18.23 2 1 5 0
25 13.45 5 4 0
26 16.12 4 5 0
27 17.23 4 3 5 2 1 0
28 16.78 5 4 3 0
29 13.47 2 4 0
30 19.30 1 3 5 0
12
public class Format {
public static String concatWords (String[] words, int start, int stop){
String line = words[start];
for (int w = start+1; w <= stop; w++) line = line.concat(words[w]);
return line;
}
public static String AllignWords (String[] words, int start, int stop, int len){
if (start == stop) return words[start];
int L = 0;
for (int i = start; i <= stop; i++)
L = L + words[i].length();
int w = start;
String space = " ";
while (L < len){
words[w] = words[w].concat(space); L++;
w++;
if (w == stop) w = start;
}
return concatWords(words, start, stop);
}
public static String spaces (int n){
String line = "";
for (int i = 1; i <= n; i++) line = line.concat(" ");
return line;
}
public static String centralize (String[] words, int start, int stop, int len){
String line = concatWords(words, start, stop);
int L = (len - line.length()) / 2;
13
if (L > 0) return spaces(L).concat(line);
else return line;
}
public static void main (String[] args){
int len = Integer.parseInt(args[1]);
String [] words = new String[1000];
int count = 0;
String space = " ";
while (!StdIn.isEmpty()){
words[count] = StdIn.readString().concat(space);
count++;
}
int start = 0, stop = 1;
do {
int L = words[start].length();
while (stop < count && L < len){
L = L + words[stop].length();
stop++;
}
stop--;
if (L > len) {
stop--;
L = L - words[stop].length(); }
if (args[0].equals("-r")){
if (stop == count-1) System.out.println(concatWords(words, start, stop));
else System.out.println(AllignWords(words, start, stop, len));}
else if (args[0].equals("-l"))
System.out.println(concatWords(words, start, stop));
else if (args[0].equals("-c"))
14
System.out.println(centralize(words, start, stop,
Integer.parseInt(args[2])));
start = stop + 1;
stop = start + 1;
} while (start < count);
}
}
public class Grep{
public static boolean satisfies (boolean[] result, String[] ops){
return andOK(ops, result) && orOK(ops, result) && notOK(ops, result);
}
public static boolean andOK(String[] ops, boolean[] result){
boolean ans = true;
for (int i = 0; i < ops.length; i++){
if (ops[i].equals("-a")) ans = ans && result[i];
}
return ans;
}
public static boolean notOK(String[] ops, boolean[] result){
boolean ans = true;
for (int i = 0; i < ops.length; i++){
if (ops[i].equals("-n")) ans = ans && !result[i];
15
}
return ans;
}
public static boolean orOK(String[] ops, boolean[] result){
boolean ans = false;
boolean flag = false;
for (int i = 0; i < ops.length; i++){
if (ops[i].equals("-o")) {ans = ans || result[i]; flag = true;}
}
if (!flag) return true; else return ans;
}
public static void main(String[] args){
int L = 0;
for (int i = 0; i < args.length; i++){
if (!args[i].equals("-n") && !args[i].equals("-o") && !args[i].equals("-a"))
L++;
}
String[] words = new String[L];
String[] boolOp = new String[L];
boolean[] result = new boolean[L];
String op = ""; int count = 0;
for (int i = 0; i < args.length; i++){
if (args[i].equals("-n") || args[i].equals("-o") || args[i].equals("-a"))
op = args[i];
else {
words[count] = args[i];
boolOp[count] = op;
count++;
16
}
}
while (!StdIn.isEmpty()){
String line = StdIn.readLine();
for (int i = 0; i < result.length; i++)
result[i] = line.contains(words[i]);
if (satisfies(result, boolOp)) System.out.println(line);
}
}
}