Skip to content

Instantly share code, notes, and snippets.

@expalmer
Created June 3, 2015 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save expalmer/840f8393de7fe8e1e45d to your computer and use it in GitHub Desktop.
Save expalmer/840f8393de7fe8e1e45d to your computer and use it in GitHub Desktop.
Orientacao a Objetos I
public class Main {
public static void main ( String[] args ) {
int million = 1000000;
int number = 0;
int gt = 0;
int x = 0;
while( --million > 0 ) {
x = collatz( million );
if ( x > gt ) {
number = million;
gt = x;
}
}
System.out.println("Number: " + number );
System.out.println("Total: " + gt );
}
public static int collatz( int n ) {
int cont = 1;
while ( n > 1 ) {
if ( n % 2 == 0 ) {
n = n/2;
} else {
n = 3 * n + 1;
}
cont++;
}
return cont;
}
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args ) throws FileNotFoundException {
Scanner file = new Scanner(new FileReader("src/entrada1.txt"));
Scanner scanner = file.useDelimiter("\n");
int people = scanner.nextInt();
int len = scanner.nextInt();
String[] values = scanner.next().split(" ");
int[] breads = new int[len];
for( int i = 0; i < values.length; i++ ){
breads[i] = Integer.parseInt(values[i]);
}
int total = 0;
while( len-- > 0 ) {
total += breads[len];
}
int avg = total/people;
int res = verify(breads, avg);
while( res < people ) {
avg = avg -1;
res = verify(breads, avg);
}
System.out.println(Math.round(avg));
}
public static int verify( int[] breads, int avg ) {
int total = 0 ;
int len = breads.length;
while( len-- > 0 ) {
total += Math.floor( breads[len] / avg );
}
return total;
}
}
public class Main {
public static void main(String[] args) {
int vet[] = {2, -4, 6, 8, -10, 100, -6, 5};
int total = vet.length;
int maior = 0;
int somaTodos = 0;
int indi = 0;
int indj = 0;
for(int i = 0; i<total; i++){
int val = vet[i];
somaTodos += val;
for(int j=i+1; j<total;j++){
val += vet[j];
if(val > maior){
maior = val;
indi = i;
indj = j;
}
}
}
System.out.println("Todos Somam: " + somaTodos );
System.out.println("Mas do indice ["+indi+"] ao ["+ indj+"] somam: "+ maior);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment