Advertisement
Guest User

Com máscara

a guest
Oct 24th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class main {
  4.  
  5.     public static Scanner s = new Scanner(System.in);
  6.  
  7.     public static int somaAlgs(int n) {
  8.         int soma = 0;
  9.         for (byte b : Integer.toString(n).getBytes())
  10.             if ((b - 48) == 0)
  11.                 return 0;
  12.             else
  13.                 soma += (b - 48);
  14.         return soma;
  15.     }
  16.  
  17.     public static double combin(int total, int combinados) {
  18.         return fat(total) / (fat(combinados) * fat(total - combinados));
  19.     }
  20.  
  21.     public static double fat(int n) {
  22.         if (n <= 1)
  23.             return 1;
  24.         else
  25.             return (n *= fat(n - 1));
  26.     }
  27.  
  28.     public static int unificar(int n) {
  29.         int res = 1;
  30.         for (int i = 1; i < Integer.toString(n).length(); i++) {
  31.             res *= 10;
  32.             res++;
  33.         }
  34.         return res;
  35.     }
  36.  
  37.     public static String mask(int n, int d) {
  38.         String buffer = "";
  39.         int tamanhoD = Integer.toString(d).length() * 3;
  40.         for (int i = 0; i < tamanhoD; i++) {
  41.             if ((int) (tamanhoD / 2) - 1 == i)
  42.                 buffer += n + "!";
  43.             buffer += " ";
  44.         }
  45.         buffer += "\n";
  46.         for (int i = 0; i < tamanhoD; i++)
  47.             buffer += "-";
  48.         buffer += " + \n";
  49.         int c = 1;
  50.         for (byte b : Integer.toString(d).getBytes()) {
  51.             buffer += (b - 48) + "!";
  52.             if(c!=Integer.toString(d).length())
  53.                 buffer+="*";
  54.             c++;
  55.         }
  56.         buffer += "\n";
  57.         return buffer;
  58.     }
  59.  
  60.     public static void main(String[] args) {
  61.         // TODO Auto-generated method stub
  62.  
  63.         int garotos = 7;
  64.         int garotas = 2;
  65.  
  66.         int c = 0;
  67.         for (int i = unificar((int) Math.pow(10, garotas - 1)); i < Math.pow(
  68.                 10, garotas); i++) {
  69.             if (somaAlgs(i) == garotos) {
  70.                 System.out.println(mask(garotos, i));
  71.                 c++;
  72.             }
  73.         }
  74.  
  75.         System.out.println(c);
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement