早教吧作业答案频道 -->其他-->
一道简单的acm题目中的一个bug求调试DescriptionAspartofanarithmeticcompetencyprogram,yourstudentswillbegivenrandomlygeneratedlistsoffrom2to15uniquepositiveintegersandaskedtodeterminehowmanyitemsineachlistar
题目详情
一道简单的acm题目中的一个bug求调试
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
我写的程序是这样的:
#include
#include
int *a;
int length;
void output()
{
\x05for(int i = 0 ; i < length ; i ++)
\x05\x05printf("%d\n",a[i]);
}
int input()
{
\x05int temp;
\x05scanf("%d",&temp);
\x05for(length = 1 ; temp!=0 ; length++)
\x05{
\x05\x05if(a)
\x05\x05{
\x05\x05\x05a=(int *)realloc(a,sizeof(int)*length);
\x05\x05}
\x05\x05else
\x05\x05{
\x05\x05\x05a=(int *)malloc(sizeof(int));
\x05\x05}
\x05\x05a[length-1]=temp;
\x05\x05scanf("%d",&temp);
\x05}
\x05return a[0];
}
int solve()
{
\x05int count = 0,max = a[0];
\x05//首先查找最大值
\x05for(int i = 1 ; i< length ; i++)
\x05{
\x05\x05if(max < a[i])
\x05\x05\x05max = a[i];
\x05}
\x05for(int i = 0 ; i < length ; i++)
\x05{
\x05\x05for(int j = 0 ; j < length ; j++)
\x05\x05{
\x05\x05\x05if(a[i]
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
我写的程序是这样的:
#include
#include
int *a;
int length;
void output()
{
\x05for(int i = 0 ; i < length ; i ++)
\x05\x05printf("%d\n",a[i]);
}
int input()
{
\x05int temp;
\x05scanf("%d",&temp);
\x05for(length = 1 ; temp!=0 ; length++)
\x05{
\x05\x05if(a)
\x05\x05{
\x05\x05\x05a=(int *)realloc(a,sizeof(int)*length);
\x05\x05}
\x05\x05else
\x05\x05{
\x05\x05\x05a=(int *)malloc(sizeof(int));
\x05\x05}
\x05\x05a[length-1]=temp;
\x05\x05scanf("%d",&temp);
\x05}
\x05return a[0];
}
int solve()
{
\x05int count = 0,max = a[0];
\x05//首先查找最大值
\x05for(int i = 1 ; i< length ; i++)
\x05{
\x05\x05if(max < a[i])
\x05\x05\x05max = a[i];
\x05}
\x05for(int i = 0 ; i < length ; i++)
\x05{
\x05\x05for(int j = 0 ; j < length ; j++)
\x05\x05{
\x05\x05\x05if(a[i]
▼优质解答
答案和解析
{
free(a);
a = NULL; // 释放完你还不赶紧把a设为NULL
length = 1;
}
总共最多15个数,为啥要动态分配内存啊
void initialise(){
free(a);
a = NULL; // 释放完你还不赶紧把a设为NULL
length = 1;
}
看了 一道简单的acm题目中的一个...的网友还看了以下:
求拉格朗日乘数求极值从二元单条件限制推广到多元多条件限制的证明如求F=f(X,Y,Z,T)在g(X 2020-04-25 …
英语翻译公司全称是:T.G时尚资源商贸有限公司.简称”T.G时尚资源”.请帮忙翻译个英文的全称、简 2020-05-13 …
为括号部分发音相同的单词归类st(ay)m(ee)tn(o)d(ow)nr(oo)ml(oo)kt 2020-05-14 …
英语单词辨音找出画()部分读音与其余不同的单词()1.A .kn(ee) B.n(e)ck C.r 2020-05-17 …
下列各项中字音不正确的一项是:()A.涕(tì)泪梯(tī)子府第(dì)B.饿莩(fú)俘(fú 2020-07-12 …
matlab求解二阶导数方程,四个方程四个未知量>>symst>>E=32;G=10.81;b=2 2020-07-19 …
写单词,这些单词打乱顺序了!:1.d,f,e,n,i,f,e,r,t,()2.g,h,o,e,t, 2020-07-26 …
推断题:现有如下反应(反应条件均不给),推断物质⑴A→B+C+D⑵C+E→C+F+D⑶D+G→H⑷ 2020-07-30 …
选出下列词语中注音有误的一项:()A.幕mù后墓mù地倒塌tā下榻tàB.糟蹋tà竹篙gāo枯槁gā 2020-12-26 …
字母a.c.e.g.o.p.q.t.u.v.y.z开头的单词求a.c.e.g.o.p.q.r.t.u 2021-02-07 …