HDOJ 1303 Doubles(简单题)

简介: HDOJ 1303 Doubles(简单题)

Problem Description

As part of an arithmetic competency program, your students will be given randomly generated lists of from 2 to 15 unique positive integers and asked to determine how many items in each list are twice some other item in the same list. You will need a program to help you with the grading. This program should be able to scan the lists and output the correct answer for each one. For example, given the list

1 4 3 2 9 7 18 22


your program should answer 3, as 2 is twice 1, 4 is twice 2, and 18 is twice 9.


Input

The input file will consist of one or more lists of numbers. There will be one list of numbers per line. Each list will contain from 2 to 15 unique positive integers. No integer will be larger than 99. Each line will be terminated with the integer 0, which is not considered part of the list. A line with the single number -1 will mark the end of the file. The example input below shows 3 separate lists. Some lists may not contain any doubles.


Output

The output will consist of one line per input list, containing a count of the items that are double some other item.


Sample Input

1 4 3 2 9 7 18 22 0

2 4 8 10 0

7 5 11 13 1 3 0

-1


Sample Output

3

2

0


题意:就是判断输入的一行数中,有多少对数字相差2倍。

输入的数据不会有重复的。

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String strNum = sc.nextLine();
            String[] strsNum = strNum.split(" ");
            int[] num = new int[strsNum.length];
            for(int i=0;i<strsNum.length;i++){
                num[i] = Integer.parseInt(strsNum[i]);
            }
            if(num[0]==-1){
                return ;
            }
            int t=0;
            for(int i=0;i<num.length;i++){
                for(int j=0;j<num.length;j++){
                    if(i!=j){
                        //注意除数不能为0!
                        if(num[j]!=0){
                            double two = (num[i]*1.0)/(1.0*num[j]);
                            if(two==2){
                                t++;
                                break;
                            }
                        }
                    }
                }
            }
            System.out.println(t);
        }
    }
}
目录
相关文章
|
7月前
|
Java C++
hdoj 1715 大菲波数
先java代码
31 1
HDOJ 2033 人见人爱A+B
HDOJ 2033 人见人爱A+B
130 0
HDOJ 2056 Rectangles
HDOJ 2056 Rectangles
110 0
HDOJ 2041 超级楼梯
HDOJ 2041 超级楼梯
84 0
HDOJ 1412 {A} + {B}
Problem Description 给你两个集合,要求{A} + {B}. 注:同一个集合中不会有两个相同的元素. Input 每组输入数据分为三行,第一行有两个数字n,m(0 < n,m marr[mid]) { System.
751 0
|
人工智能 算法
HDOJ 3466 Proud Merchants
Problem Description Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world.
906 0
|
机器学习/深度学习
HDOJ 2074 叠筐
Problem Description 需要的时候,就把一个个大小差一圈的筐叠上去,使得从上往下看时,边筐花色交错。这个工作现在要让计算机来完成,得看你的了。 Input 输入是一个个的三元组,分别是,外筐尺寸n(n为满足0< n< 80的奇整数),中心花色字符,外筐花色字符,后二者都为ASCII可见字符; Output 输出叠在一起的筐图案,中心花色与外筐花色字符从内层起交错相叠,多筐相叠时,最外筐的角总是被打磨掉。
818 0