Doubles - POJ 1552 水题

本文介绍了一个关于寻找列表中成二倍关系数对的编程竞赛题目,并提供了详细的解题思路及AC代码实现。

Doubles
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 18799 Accepted: 10840

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 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

题意:找到有多少组数是一个是另一个的二倍。

思路:暴力枚举即可。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
bool cmp(int a,int b)
{ return a>b;
}
int num[1010];
char s[10000010];
char c,d;
int main()
{ int t,i,j,k,pos,ans,ret;
  c='a';
  pos=0;
  ans=0;
  while(~scanf("%d%c",&num[++pos],&c) && num[1]!=-1)
  { while(c!='\n')
    { scanf("%d",&num[++pos]);
      scanf("%c",&c);
    }
    sort(num+1,num+1+pos,cmp);
    for(i=1;i<pos;i++)
    { for(j=i+1;j<=pos;j++)
       if(num[j]*2==num[i])
        ans++;
    }
    printf("%d\n",ans);
    pos=0;
    ans=0;
    c='a';
  }
}




### Java泛型练习及解析 #### 1. 泛型类的基本使用 考虑以下泛型类的定义: ```java class Holder<T> { T value; public Holder(T value) { this.value = value; } public T getValue() { return value; } } ``` 该类定义了一个泛型类 `Holder`,其中 `T` 是一个类型参数。可以使用该类来存储任何类型的对象,并通过 `getValue()` 方法获取该对象。例如,以下代码演示了如何创建一个 `Holder` 实例来存储一个字符串: ```java Holder<String> stringHolder = new Holder<>("Hello"); System.out.println(stringHolder.getValue()); // 输出 "Hello" ``` 如果尝试将错误类型的对象传递给 `Holder`,编译器会报错。例如,以下代码会导致编译错误: ```java Holder<Integer> intHolder = new Holder<>("Hello"); // 编译错误 ``` 这是因为 `Holder<Integer>` 要求传入的值必须是 `Integer` 类型,而 `"Hello"` 是 `String` 类型。 #### 2. 原始类型与泛型的兼容性 Java 允许将泛型类的实例赋值给原始类型的变量,但这种做法不推荐,因为它会失去类型安全性。例如: ```java Holder rawHolder = new Holder<String>("Hello"); String value = rawHolder.getValue(); // 不推荐,但可以编译通过 ``` 尽管这段代码可以编译通过,但它失去了泛型带来的类型检查。如果尝试从 `rawHolder` 获取一个非 `String` 类型的对象,可能会在运行时抛出 `ClassCastException`。 #### 3. 类型推断的增强 在 Java 23 中,类型推断得到了增强,允许在某些情况下省略显式的类型参数。例如: ```java List<String> list = Collections.emptyList(); // Java 23 支持空目标类型推断 ``` 在旧版本的 Java 中,必须显式指定类型参数: ```java List<String> list = Collections.<String>emptyList(); ``` 这种改进使得代码更加简洁,并减少了冗余的类型声明。 #### 4. 可变参数与泛型的结合 Java 允许将可变参数与泛型结合使用。例如,可以定义一个泛型方法来接受可变数量的参数: ```java public static <T> void printValues(T... values) { for (T value : values) { System.out.println(value); } } ``` 调用该方法时,可以传入任意数量的参数: ```java printValues("Apple", "Banana", "Cherry"); // 输出三个字符串 printValues(1, 2, 3); // 输出三个整数 ``` 这种方法在处理不确定数量的输入时非常有用,并且保持了类型安全性。 #### 5. 泛型方法的使用 泛型不仅可以用于类,还可以用于方法。例如,定义一个泛型方法来交换两个元素的位置: ```java public static <T> void swap(T[] array, int i, int j) { T temp = array[i]; array[i] = array[j]; array[j] = temp; } ``` 调用该方法时,可以传入任意类型的数组和索引: ```java String[] names = {"Alice", "Bob"}; swap(names, 0, 1); System.out.println(Arrays.toString(names)); // 输出 "[Bob, Alice]" ``` 这种方法可以应用于任何类型的数组,并且保证了类型安全性。 #### 6. 泛型接口的实现 Java 允许定义泛型接口,并由具体的类实现这些接口。例如,定义一个泛型接口 `Container<T>`: ```java interface Container<T> { void add(T item); T get(int index); } ``` 然后,可以实现该接口的具体类: ```java class StringContainer implements Container<String> { private List<String> items = new ArrayList<>(); @Override public void add(String item) { items.add(item); } @Override public String get(int index) { return items.get(index); } } ``` 这样,`StringContainer` 类只能用于处理 `String` 类型的对象,确保了类型安全性。 #### 7. 泛型的边界限制 Java 泛型支持通过边界限制来约束类型参数的范围。例如,定义一个泛型方法,要求类型参数必须是 `Number` 的子类: ```java public static <T extends Number> double sum(T[] numbers) { double total = 0; for (T number : numbers) { total += number.doubleValue(); } return total; } ``` 调用该方法时,只能传入 `Number` 的子类数组: ```java Integer[] integers = {1, 2, 3}; Double[] doubles = {1.5, 2.5, 3.5}; System.out.println(sum(integers)); // 输出 6.0 System.out.println(sum(doubles)); // 输出 7.5 ``` 这种方法确保了类型的安全性和操作的正确性。 #### 8. 多类型参数的使用 Java 泛型支持多个类型参数的定义。例如,定义一个泛型类 `Pair<K, V>`,用于存储键值对: ```java class Pair<K, V> { K key; V value; public Pair(K key, V value) { this.key = key; this.value = value; } public K getKey() { return key; } public V getValue() { return value; } } ``` 调用该类时,可以传入任意类型的键和值: ```java Pair<String, Integer> pair = new Pair<>("Age", 25); System.out.println(pair.getKey()); // 输出 "Age" System.out.println(pair.getValue()); // 输出 25 ``` 这种方法可以灵活地处理不同类型的数据组合。 #### 9. 泛型的通配符使用 Java 泛型支持使用通配符 `?` 来表示未知类型。例如,定义一个方法来打印任意类型的列表: ```java public static void printList(List<?> list) { for (Object obj : list) { System.out.println(obj); } } ``` 调用该方法时,可以传入任意类型的 `List`: ```java List<String> stringList = Arrays.asList("A", "B", "C"); List<Integer> integerList = Arrays.asList(1, 2, 3); printList(stringList); // 输出三个字符串 printList(integerList); // 输出三个整数 ``` 这种方法在处理未知类型的数据时非常有用,并且保持了类型安全性。 #### 10. 泛型的递归类型限制 Java 泛型支持递归类型限制,允许类型参数继承自身。例如,定义一个泛型接口 `SelfBound<T>`: ```java interface SelfBound<T extends SelfBound<T>> { T self(); } ``` 这种设计模式常用于链式调用和构建器模式中,确保返回的类型与当前类一致。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值