最近一段时间代码汇总

一,求解幸运数

 给定一个十进制的数,它的若干位之和等于二进制形式的各个位之和,则它就是幸运数。

比如十进制数123,各个位之和为6;比如十进制数3,各个位之和为3,二进制表示为 011,各个二进制位之和为2 ,这两个和不相等,故十进制数3不是幸运数。

 

 下面程序实现了,给定一个数 num,求 1,2,3,....num 之间的所有的幸运数。

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt())
        {
            int count = sc.nextInt();
            int[] values = new int[count];
            for(int i = 0; i < count; i++)
            {
                values[i] = sc.nextInt();

            }
            for(int i = 0; i < count; i++)
            {
                int res = luckNumbers(values[i]);
                if(res > 0)
                System.out.println(res);
            }
        }
        sc.close();
    }
    
    private static int luckNumbers(int value){
        int count = 0;
        for(int i = 1; i <= value; i++)
        {
            int ten = process(i);
            int binary = process2(i);
            if(ten == binary)
                count++;
        }
        return count;
    }
    
    private static int process(int value)
    {
        int sum = 0;
        while(value > 0)
        {
            sum += value % 10;
            value = value/10;
        }
        return sum;
    }
    
    private static int process2(int num){
        int count = 0;
        for(; num > 0; count++)
        {
            num &= (num - 1);
        }
        return count;
    }
}

 

二,第一行

import java.util.Arrays;
import java.util.Scanner;  

public class Main {  
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        while(sc.hasNextLine())
        {
            String line = sc.nextLine();
            int tables = Integer.valueOf(line.split(" ")[0]);
            int customers = Integer.valueOf(line.split(" ")[1]);
            
            int[] cap = new int[tables];
            line = sc.nextLine();
            String lines[] = line.split(" ");
            for(int i = 0; i < cap.length; i++)
                cap[i] = Integer.valueOf(lines[i]);
            
            Buy[] buyarr = new Buy[customers];
            for(int i = 0; i < customers; i++)
            {
                line = sc.nextLine();
                int numbers = Integer.valueOf(line.split(" ")[0]);
                int money = Integer.valueOf(line.split(" ")[1]);
                buyarr[i] = new Buy(money, numbers);
            }
            Arrays.sort(cap);
            Arrays.sort(buyarr);
            int count = 0;
            for(int i = 0; i < customers; i++)
            {
                int numbers = buyarr[i].numbers;
                int money = buyarr[i].money;
                int j = 0;
                boolean find = false;
                for(; j < cap.length; j++)
                    if(numbers < cap[j])
                    {
                        cap[j] = 0;
                        find = true;
                        break;
                    }
                if(find == true){
                    count += money;
                }
            }
            System.out.println(count);
        }
    }
    
    private static class Buy implements Comparable<Buy>{
        int money;
        int numbers;
        
        public Buy(int money, int numbers) {
            this.money = money;
            this.numbers = numbers;
        }
        
        @Override
        public int compareTo(Buy o) {
            return o.money - this.money;//按money从大到小排序
        }
    }
}

 

三,使用两个线程,交替打印 1-10。线程A打印奇数,线程B打印偶数

public class Test{
    public static void main(String[] args) {
        Print p = new Print();
        Thread t1 = new ThreadA(p);//负责打印奇数
        Thread t2 = new ThreadB(p);//负责打印偶数
        
        t2.start();
        try {
            Thread.sleep(100);//让当前线程睡一会儿,好让t2能够 start
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        t1.start();//t2 start之后,才启动t1
    }
}

class Print{
    private volatile boolean isA = false;
    public synchronized void printOdd(int i){
        try{
            while(i <= 9)
            {
                while(isA == true)
                    wait();
                System.out.print(i);
                notifyAll();//让t2先启动,由于 isA初始为false,t2会wait,这样 t1获得锁打印了1之后,nofityAll发的通知才不会丢失
                i+=2;
                isA = true;
            }
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        
    }
    public synchronized void print(int i){
        try{
            while(i <= 10)
            {
                while(isA == false)
                    wait();
                System.out.print(i);
                i+=2;
                notifyAll();
                isA = false;
            }
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}

class ThreadB extends Thread{
    Print p;
    
    public ThreadB(Print p) {
        this.p = p;
    }
    
    @Override
    public void run() {
        p.print(2);
    }
}


class ThreadA extends Thread{
    Print p;
    
    public ThreadA(Print p) {
        this.p = p;
    }
    
    @Override
    public void run() {
        p.printOdd(1);
    }
}

 

posted @ 2016-10-15 10:58  大熊猫同学  阅读(496)  评论(0编辑  收藏  举报