zoj上一简单的题目

来源:百度知道 编辑:UC知道 时间:2024/06/03 13:49:44
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1760

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

scanf("%d",&shu);
while(shu!=-1)
这里有问题,既然用 shu 来判断是否为-1,至少应该把他包括在循环体内,楼主的程序明显不能结束。。。
这题比较简单,楼主自己试着修改下吧,下面是我以前写的,楼主可以参考
#include<stdio.h>
int main()
{
int t[16],i,j,k,n;
while(scanf("%d",&t[0]),t[0]+1)
{
i=k=0;
while(scanf("%d",&t[++i]),t[i]);
for(n=--i;i>=0;--i)
for(j=n;j>=0;--j)
if(t[i]==t[j]+t[j])
k++;
printf("%d\n",k);
}
return 0;
}

以下代码通过zoj:

#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b)
{
return *(const int *)a > *(const int*)b;
}
int main()
{
int a[15 + 1]; // lists of from 2 to 15 unique positive integers
// 因为每行的行尾有0,所以这里必须多分配一个单元。
for (;;)
{
int n = 0;

while (scanf("%d", &a[n]) == 1)
{
if (a[n] == 0) // end of current input l