zoukankan      html  css  js  c++  java
  • [编程题]混合颜料

    你就是一个画家!你现在想绘制一幅画,但是你现在没有足够颜色的颜料。为了让问题简单,我们用正整数表示不同颜色的颜料。你知道这幅画需要的n种颜色的颜料,你现在可以去商店购买一些颜料,但是商店不能保证能供应所有颜色的颜料,所以你需要自己混合一些颜料。混合两种不一样的颜色A和颜色B颜料可以产生(A XOR B)这种颜色的颜料(新产生的颜料也可以用作继续混合产生新的颜色,XOR表示异或操作)。本着勤俭节约的精神,你想购买更少的颜料就满足要求,所以兼职程序员的你需要编程来计算出最少需要购买几种颜色的颜料? 

    输入描述:
    第一行为绘制这幅画需要的颜色种数n (1 ≤ n ≤ 50)
    第二行为n个数x

    i

    (1 ≤ x

    i

     ≤ 1,000,000,000),表示需要的各种颜料.



    输出描述:
    输出最少需要在商店购买的颜料颜色种数,注意可能购买的颜色不一定会使用在画中,只是为了产生新的颜色。

     

    输入例子:
    3
    1 7 3

     1 import java.util.Arrays;
     2 import java.util.Scanner;
     3 
     4 public class Test27 {
     5 
     6     public static void main(String[] args) {
     7             Scanner sc = new Scanner(System.in);
     8             while(sc.hasNext()){
     9                 int n = sc.nextInt();
    10                 int[] colors = new int[n];
    11                 int count = 0;
    12                 for(int i = 0;i<n;i++){
    13                     colors[i] = sc.nextInt();
    14                 }
    15                 Arrays.sort(colors);
    16                 for(int i=n-1;i>=0;i--){
    17                     for(int j = i-1;j>=0;j--){
    18                         if(getHight(colors[i]) == getHight(colors[j]))
    19                             colors[j] = colors[j]^colors[i];
    20                              
    21                     }
    22                     Arrays.sort(colors);
    23                 }
    24                 for(int i=0;i<n;i++){
    25                     if(colors[i] >0)
    26                         count++;
    27                 }
    28                 System.out.println(count);
    29             }
    30             sc.close();
    31         }
    32         public static int getHight(int i){
    33             int count =0 ;
    34             while(i > 0){
    35                 i<<=1;
    36                 count ++;
    37             }
    38             return count;
    39         }
    40 
    41 }
    Jumping from failure to failure with undiminished enthusiasm is the big secret to success.
  • 相关阅读:
    IntelliJ Idea使用代码折叠
    c# 文件属性读取操作及文件之间操作
    c#文件操作
    c++头文件 #include<iostream>
    基本SQL语句
    dd
    c#属性中的get和set属性
    c#
    c#运算表达式
    c#方法
  • 原文地址:https://www.cnblogs.com/chongerlishan/p/5969233.html
Copyright © 2011-2022 走看看