413 - Up and Down Sequences
Moderator: Board moderators
413 Up and Down Sequences
Can anyone help me figure out what is wrong with my code? It works for all the standard tests I can think of. ::This is a repost::
[cpp]#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int UP=1;
const int DOWN=2;
const int UK=3;
int Trend,LastTrend,C,LD,LU,NU,ND;
vector<int> X;
double r(double v)
{
return double(int((v)*1000000+0.5))/1000000;
}
void FU()
{
LU+=C;
NU++;
}
void FD()
{
LD+=C;
ND++;
}
void main()
{
char B[1000],*P;
int i,N;
while(gets(B)!=NULL)
{
X.clear();
P=strtok(B," ");
while(P)
{
sscanf(P,"%d",&N);
X.push_back(N);
P=strtok(NULL," ");
}
X.pop_back();
if(X.size()>0)
{
Trend=UK;
LastTrend=UK;
C=0;
LU=0;
LD=0;
NU=0;
ND=0;
for(i=1;i<X.size();i++)
{
LastTrend=Trend;
if(X>X[i-1]) Trend=UP;
if(X<X[i-1]) Trend=DOWN;
if(Trend!=LastTrend)
{
if(LastTrend!=UK)
{
if(LastTrend==UP)
FU();
else
FD();
C=0;
}
}
C++;
}
if(Trend==UP) FU();
if(Trend==DOWN) FD();
if(NU==0)NU++;
if(ND==0)ND++;
printf("Nr values = %d: %.6f %.6fn",X.size(),r(double(LU)/NU),r(double(LD)/ND));
}
else
{
break;
}
}
}[/cpp]
[cpp]#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int UP=1;
const int DOWN=2;
const int UK=3;
int Trend,LastTrend,C,LD,LU,NU,ND;
vector<int> X;
double r(double v)
{
return double(int((v)*1000000+0.5))/1000000;
}
void FU()
{
LU+=C;
NU++;
}
void FD()
{
LD+=C;
ND++;
}
void main()
{
char B[1000],*P;
int i,N;
while(gets(B)!=NULL)
{
X.clear();
P=strtok(B," ");
while(P)
{
sscanf(P,"%d",&N);
X.push_back(N);
P=strtok(NULL," ");
}
X.pop_back();
if(X.size()>0)
{
Trend=UK;
LastTrend=UK;
C=0;
LU=0;
LD=0;
NU=0;
ND=0;
for(i=1;i<X.size();i++)
{
LastTrend=Trend;
if(X>X[i-1]) Trend=UP;
if(X<X[i-1]) Trend=DOWN;
if(Trend!=LastTrend)
{
if(LastTrend!=UK)
{
if(LastTrend==UP)
FU();
else
FD();
C=0;
}
}
C++;
}
if(Trend==UP) FU();
if(Trend==DOWN) FD();
if(NU==0)NU++;
if(ND==0)ND++;
printf("Nr values = %d: %.6f %.6fn",X.size(),r(double(LU)/NU),r(double(LD)/ND));
}
else
{
break;
}
}
}[/cpp]
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
Re: 413 Up and Down Sequences
Can anyone help me figure out with C Language!!! Please!
Thank you
Thank you
please..please...help 413 ...
i frustrated for keep getting wa for p413. please someone kind help me and tell me what's the trouble of my code.
[c]
#include<stdio.h>
void main()
{
long seq[32];
int l,up,down,ctr,i,temp;
int nup[31],ndown[31];
int tup,tdown;
float avup,avdown;
void hup(int *ctr,int nup[],int *up,int *temp,long seq[]);
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[]);
while(1)
{
scanf("%ld",&(seq[1]));
if(seq[1]==0)
break;
for(i=2;i<=30;i++)
{
scanf("%ld",&seq);
if(seq==0)
break;
}
l=i-1;
temp=0;
up=0;
down=0;
for(ctr=1;ctr<=l&&seq[ctr+1]!=0;)
{
if(seq[ctr]<seq[ctr+1]&&seq[ctr+1]!=0)
hup(&ctr,nup,&up,&temp,seq);
if(seq[ctr]==seq[ctr+1])
{
for(i=ctr;seq[ctr]==seq[ctr+1]&&seq[ctr+1]!=0;ctr++)
temp=temp+1;
}
if(seq[ctr]>seq[ctr+1]&&seq[ctr+1]!=0)
hdown(&ctr,ndown,&down,&temp,seq);
}
avup=0;
tup=0;
for(i=1;i<=up;i++)
tup=tup+nup;
if(up>0)
avup=(float)tup/(float)up;
avdown=0;
tdown=0;
for(i=1;i<=down;i++)
tdown=tdown+ndown;
if(down>0)
avdown=(float)tdown/(float)down;
printf("Nr values = %d: ",l);
printf("%f %f\n",avup,avdown);
}
}
void hup(int *ctr,int nup[],int *up,int *temp,long seq[])
{
*up=*up+1;
nup[*up]=*temp;
*temp=0;
for(;seq[*ctr]<=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]<seq[*ctr+1])
*temp=0;
nup[*up]=nup[*up]+1;
}
}
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[])
{
*down=*down+1;
ndown[*down]=*temp;
*temp=0;
for(;seq[*ctr]>=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]>seq[*ctr+1])
*temp=0;
ndown[*down]=ndown[*down]+1;
}
}
[/c]
many thanks before.![:wink:](./images/smilies/icon_wink.gif)
![:cry:](./images/smilies/icon_cry.gif)
[c]
#include<stdio.h>
void main()
{
long seq[32];
int l,up,down,ctr,i,temp;
int nup[31],ndown[31];
int tup,tdown;
float avup,avdown;
void hup(int *ctr,int nup[],int *up,int *temp,long seq[]);
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[]);
while(1)
{
scanf("%ld",&(seq[1]));
if(seq[1]==0)
break;
for(i=2;i<=30;i++)
{
scanf("%ld",&seq);
if(seq==0)
break;
}
l=i-1;
temp=0;
up=0;
down=0;
for(ctr=1;ctr<=l&&seq[ctr+1]!=0;)
{
if(seq[ctr]<seq[ctr+1]&&seq[ctr+1]!=0)
hup(&ctr,nup,&up,&temp,seq);
if(seq[ctr]==seq[ctr+1])
{
for(i=ctr;seq[ctr]==seq[ctr+1]&&seq[ctr+1]!=0;ctr++)
temp=temp+1;
}
if(seq[ctr]>seq[ctr+1]&&seq[ctr+1]!=0)
hdown(&ctr,ndown,&down,&temp,seq);
}
avup=0;
tup=0;
for(i=1;i<=up;i++)
tup=tup+nup;
if(up>0)
avup=(float)tup/(float)up;
avdown=0;
tdown=0;
for(i=1;i<=down;i++)
tdown=tdown+ndown;
if(down>0)
avdown=(float)tdown/(float)down;
printf("Nr values = %d: ",l);
printf("%f %f\n",avup,avdown);
}
}
void hup(int *ctr,int nup[],int *up,int *temp,long seq[])
{
*up=*up+1;
nup[*up]=*temp;
*temp=0;
for(;seq[*ctr]<=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]<seq[*ctr+1])
*temp=0;
nup[*up]=nup[*up]+1;
}
}
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[])
{
*down=*down+1;
ndown[*down]=*temp;
*temp=0;
for(;seq[*ctr]>=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]>seq[*ctr+1])
*temp=0;
ndown[*down]=ndown[*down]+1;
}
}
[/c]
many thanks before.
![:wink:](./images/smilies/icon_wink.gif)
please..please...help 413 ...
i frustrated for keep getting wa for p413. please someone kind help me and tell me what's the trouble of my code.
[c]
#include<stdio.h>
void main()
{
long seq[32];
int l,up,down,ctr,i,temp;
int nup[31],ndown[31];
int tup,tdown;
float avup,avdown;
void hup(int *ctr,int nup[],int *up,int *temp,long seq[]);
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[]);
while(1)
{
scanf("%ld",&(seq[1]));
if(seq[1]==0)
break;
for(i=2;i<=30;i++)
{
scanf("%ld",&seq);
if(seq==0)
break;
}
l=i-1;
temp=0;
up=0;
down=0;
for(ctr=1;ctr<=l&&seq[ctr+1]!=0;)
{
if(seq[ctr]<seq[ctr+1]&&seq[ctr+1]!=0)
hup(&ctr,nup,&up,&temp,seq);
if(seq[ctr]==seq[ctr+1])
{
for(i=ctr;seq[ctr]==seq[ctr+1]&&seq[ctr+1]!=0;ctr++)
temp=temp+1;
}
if(seq[ctr]>seq[ctr+1]&&seq[ctr+1]!=0)
hdown(&ctr,ndown,&down,&temp,seq);
}
avup=0;
tup=0;
for(i=1;i<=up;i++)
tup=tup+nup;
if(up>0)
avup=(float)tup/(float)up;
avdown=0;
tdown=0;
for(i=1;i<=down;i++)
tdown=tdown+ndown;
if(down>0)
avdown=(float)tdown/(float)down;
printf("Nr values = %d: ",l);
printf("%f %f\n",avup,avdown);
}
}
void hup(int *ctr,int nup[],int *up,int *temp,long seq[])
{
*up=*up+1;
nup[*up]=*temp;
*temp=0;
for(;seq[*ctr]<=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]<seq[*ctr+1])
*temp=0;
nup[*up]=nup[*up]+1;
}
}
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[])
{
*down=*down+1;
ndown[*down]=*temp;
*temp=0;
for(;seq[*ctr]>=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]>seq[*ctr+1])
*temp=0;
ndown[*down]=ndown[*down]+1;
}
}
[/c]
many thanks before.![:wink:](./images/smilies/icon_wink.gif)
![:cry:](./images/smilies/icon_cry.gif)
[c]
#include<stdio.h>
void main()
{
long seq[32];
int l,up,down,ctr,i,temp;
int nup[31],ndown[31];
int tup,tdown;
float avup,avdown;
void hup(int *ctr,int nup[],int *up,int *temp,long seq[]);
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[]);
while(1)
{
scanf("%ld",&(seq[1]));
if(seq[1]==0)
break;
for(i=2;i<=30;i++)
{
scanf("%ld",&seq);
if(seq==0)
break;
}
l=i-1;
temp=0;
up=0;
down=0;
for(ctr=1;ctr<=l&&seq[ctr+1]!=0;)
{
if(seq[ctr]<seq[ctr+1]&&seq[ctr+1]!=0)
hup(&ctr,nup,&up,&temp,seq);
if(seq[ctr]==seq[ctr+1])
{
for(i=ctr;seq[ctr]==seq[ctr+1]&&seq[ctr+1]!=0;ctr++)
temp=temp+1;
}
if(seq[ctr]>seq[ctr+1]&&seq[ctr+1]!=0)
hdown(&ctr,ndown,&down,&temp,seq);
}
avup=0;
tup=0;
for(i=1;i<=up;i++)
tup=tup+nup;
if(up>0)
avup=(float)tup/(float)up;
avdown=0;
tdown=0;
for(i=1;i<=down;i++)
tdown=tdown+ndown;
if(down>0)
avdown=(float)tdown/(float)down;
printf("Nr values = %d: ",l);
printf("%f %f\n",avup,avdown);
}
}
void hup(int *ctr,int nup[],int *up,int *temp,long seq[])
{
*up=*up+1;
nup[*up]=*temp;
*temp=0;
for(;seq[*ctr]<=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]<seq[*ctr+1])
*temp=0;
nup[*up]=nup[*up]+1;
}
}
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[])
{
*down=*down+1;
ndown[*down]=*temp;
*temp=0;
for(;seq[*ctr]>=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]>seq[*ctr+1])
*temp=0;
ndown[*down]=ndown[*down]+1;
}
}
[/c]
many thanks before.
![:wink:](./images/smilies/icon_wink.gif)
please..please...help 413 ...
i frustrated for keep getting wa for p413. please someone kind help me and tell me what's the trouble of my code.
[c]
#include<stdio.h>
void main()
{
long seq[32];
int l,up,down,ctr,i,temp;
int nup[31],ndown[31];
int tup,tdown;
float avup,avdown;
void hup(int *ctr,int nup[],int *up,int *temp,long seq[]);
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[]);
while(1)
{
scanf("%ld",&(seq[1]));
if(seq[1]==0)
break;
for(i=2;i<=30;i++)
{
scanf("%ld",&seq);
if(seq==0)
break;
}
l=i-1;
temp=0;
up=0;
down=0;
for(ctr=1;ctr<=l&&seq[ctr+1]!=0;)
{
if(seq[ctr]<seq[ctr+1]&&seq[ctr+1]!=0)
hup(&ctr,nup,&up,&temp,seq);
if(seq[ctr]==seq[ctr+1])
{
for(i=ctr;seq[ctr]==seq[ctr+1]&&seq[ctr+1]!=0;ctr++)
temp=temp+1;
}
if(seq[ctr]>seq[ctr+1]&&seq[ctr+1]!=0)
hdown(&ctr,ndown,&down,&temp,seq);
}
avup=0;
tup=0;
for(i=1;i<=up;i++)
tup=tup+nup;
if(up>0)
avup=(float)tup/(float)up;
avdown=0;
tdown=0;
for(i=1;i<=down;i++)
tdown=tdown+ndown;
if(down>0)
avdown=(float)tdown/(float)down;
printf("Nr values = %d: ",l);
printf("%f %f\n",avup,avdown);
}
}
void hup(int *ctr,int nup[],int *up,int *temp,long seq[])
{
*up=*up+1;
nup[*up]=*temp;
*temp=0;
for(;seq[*ctr]<=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]<seq[*ctr+1])
*temp=0;
nup[*up]=nup[*up]+1;
}
}
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[])
{
*down=*down+1;
ndown[*down]=*temp;
*temp=0;
for(;seq[*ctr]>=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]>seq[*ctr+1])
*temp=0;
ndown[*down]=ndown[*down]+1;
}
}
[/c]
many thanks before.![:wink:](./images/smilies/icon_wink.gif)
![:cry:](./images/smilies/icon_cry.gif)
[c]
#include<stdio.h>
void main()
{
long seq[32];
int l,up,down,ctr,i,temp;
int nup[31],ndown[31];
int tup,tdown;
float avup,avdown;
void hup(int *ctr,int nup[],int *up,int *temp,long seq[]);
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[]);
while(1)
{
scanf("%ld",&(seq[1]));
if(seq[1]==0)
break;
for(i=2;i<=30;i++)
{
scanf("%ld",&seq);
if(seq==0)
break;
}
l=i-1;
temp=0;
up=0;
down=0;
for(ctr=1;ctr<=l&&seq[ctr+1]!=0;)
{
if(seq[ctr]<seq[ctr+1]&&seq[ctr+1]!=0)
hup(&ctr,nup,&up,&temp,seq);
if(seq[ctr]==seq[ctr+1])
{
for(i=ctr;seq[ctr]==seq[ctr+1]&&seq[ctr+1]!=0;ctr++)
temp=temp+1;
}
if(seq[ctr]>seq[ctr+1]&&seq[ctr+1]!=0)
hdown(&ctr,ndown,&down,&temp,seq);
}
avup=0;
tup=0;
for(i=1;i<=up;i++)
tup=tup+nup;
if(up>0)
avup=(float)tup/(float)up;
avdown=0;
tdown=0;
for(i=1;i<=down;i++)
tdown=tdown+ndown;
if(down>0)
avdown=(float)tdown/(float)down;
printf("Nr values = %d: ",l);
printf("%f %f\n",avup,avdown);
}
}
void hup(int *ctr,int nup[],int *up,int *temp,long seq[])
{
*up=*up+1;
nup[*up]=*temp;
*temp=0;
for(;seq[*ctr]<=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]<seq[*ctr+1])
*temp=0;
nup[*up]=nup[*up]+1;
}
}
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[])
{
*down=*down+1;
ndown[*down]=*temp;
*temp=0;
for(;seq[*ctr]>=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]>seq[*ctr+1])
*temp=0;
ndown[*down]=ndown[*down]+1;
}
}
[/c]
many thanks before.
![:wink:](./images/smilies/icon_wink.gif)
please...please...helppp..413
please someone kind to help me for i'm going frustrated to keep getting WA about this. please found the trouble of my code.
[c]
#include<stdio.h>
void main()
{
long seq[32];
int l,up,down,ctr,i,temp;
int nup[31],ndown[31];
int tup,tdown;
float avup,avdown;
void hup(int *ctr,int nup[],int *up,int *temp,long seq[]);
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[]);
while(1)
{
scanf("%ld",&(seq[1]));
if(seq[1]==0)
break;
for(i=2;i<=30;i++)
{
scanf("%ld",&seq);
if(seq==0)
break;
}
l=i-1;
temp=0;
up=0;
down=0;
for(ctr=1;ctr<=l&&seq[ctr+1]!=0;)
{
if(seq[ctr]<seq[ctr+1]&&seq[ctr+1]!=0)
hup(&ctr,nup,&up,&temp,seq);
if(seq[ctr]==seq[ctr+1])
{
for(i=ctr;seq[ctr]==seq[ctr+1]&&seq[ctr+1]!=0;ctr++)
temp=temp+1;
}
if(seq[ctr]>seq[ctr+1]&&seq[ctr+1]!=0)
hdown(&ctr,ndown,&down,&temp,seq);
}
avup=0;
tup=0;
for(i=1;i<=up;i++)
tup=tup+nup;
if(up>0)
avup=(float)tup/(float)up;
avdown=0;
tdown=0;
for(i=1;i<=down;i++)
tdown=tdown+ndown;
if(down>0)
avdown=(float)tdown/(float)down;
printf("Nr values = %d: ",l);
printf("%f %f\n",avup,avdown);
}
}
void hup(int *ctr,int nup[],int *up,int *temp,long seq[])
{
*up=*up+1;
nup[*up]=*temp;
*temp=0;
for(;seq[*ctr]<=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]<seq[*ctr+1])
*temp=0;
nup[*up]=nup[*up]+1;
}
}
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[])
{
*down=*down+1;
ndown[*down]=*temp;
*temp=0;
for(;seq[*ctr]>=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]>seq[*ctr+1])
*temp=0;
ndown[*down]=ndown[*down]+1;
}
}
[/c]
many thanks before..
[c]
#include<stdio.h>
void main()
{
long seq[32];
int l,up,down,ctr,i,temp;
int nup[31],ndown[31];
int tup,tdown;
float avup,avdown;
void hup(int *ctr,int nup[],int *up,int *temp,long seq[]);
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[]);
while(1)
{
scanf("%ld",&(seq[1]));
if(seq[1]==0)
break;
for(i=2;i<=30;i++)
{
scanf("%ld",&seq);
if(seq==0)
break;
}
l=i-1;
temp=0;
up=0;
down=0;
for(ctr=1;ctr<=l&&seq[ctr+1]!=0;)
{
if(seq[ctr]<seq[ctr+1]&&seq[ctr+1]!=0)
hup(&ctr,nup,&up,&temp,seq);
if(seq[ctr]==seq[ctr+1])
{
for(i=ctr;seq[ctr]==seq[ctr+1]&&seq[ctr+1]!=0;ctr++)
temp=temp+1;
}
if(seq[ctr]>seq[ctr+1]&&seq[ctr+1]!=0)
hdown(&ctr,ndown,&down,&temp,seq);
}
avup=0;
tup=0;
for(i=1;i<=up;i++)
tup=tup+nup;
if(up>0)
avup=(float)tup/(float)up;
avdown=0;
tdown=0;
for(i=1;i<=down;i++)
tdown=tdown+ndown;
if(down>0)
avdown=(float)tdown/(float)down;
printf("Nr values = %d: ",l);
printf("%f %f\n",avup,avdown);
}
}
void hup(int *ctr,int nup[],int *up,int *temp,long seq[])
{
*up=*up+1;
nup[*up]=*temp;
*temp=0;
for(;seq[*ctr]<=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]<seq[*ctr+1])
*temp=0;
nup[*up]=nup[*up]+1;
}
}
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[])
{
*down=*down+1;
ndown[*down]=*temp;
*temp=0;
for(;seq[*ctr]>=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]>seq[*ctr+1])
*temp=0;
ndown[*down]=ndown[*down]+1;
}
}
[/c]
many thanks before..
413 - Up and Down Sequences
i almost crazy for keep getting wa on this.
please be kind to tell me the bug of my code...please!
[c]
#include<stdio.h>
void main()
{
long seq[32];
int l,up,down,ctr,i,temp;
int nup[31],ndown[31];
int tup,tdown;
float avup,avdown;
void hup(int *ctr,int nup[],int *up,int *temp,long seq[]);
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[]);
while(1)
{
scanf("%ld",&(seq[1]));
if(seq[1]==0)
break;
for(i=2;i<=30;i++)
{
scanf("%ld",&seq);
if(seq==0)
break;
}
l=i-1;
temp=0;
up=0;
down=0;
for(ctr=1;ctr<=l&&seq[ctr+1]!=0;)
{
if(seq[ctr]<seq[ctr+1]&&seq[ctr+1]!=0)
hup(&ctr,nup,&up,&temp,seq);
if(seq[ctr]==seq[ctr+1])
{
for(i=ctr;seq[ctr]==seq[ctr+1]&&seq[ctr+1]!=0;ctr++)
temp=temp+1;
}
if(seq[ctr]>seq[ctr+1]&&seq[ctr+1]!=0)
hdown(&ctr,ndown,&down,&temp,seq);
}
avup=0;
tup=0;
for(i=1;i<=up;i++)
tup=tup+nup;
if(up>0)
avup=(float)tup/(float)up;
avdown=0;
tdown=0;
for(i=1;i<=down;i++)
tdown=tdown+ndown;
if(down>0)
avdown=(float)tdown/(float)down;
printf("Nr values = %d: ",l);
printf("%f %f\n",avup,avdown);
}
}
void hup(int *ctr,int nup[],int *up,int *temp,long seq[])
{
*up=*up+1;
nup[*up]=*temp;
*temp=0;
for(;seq[*ctr]<=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]<seq[*ctr+1])
*temp=0;
nup[*up]=nup[*up]+1;
}
}
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[])
{
*down=*down+1;
ndown[*down]=*temp;
*temp=0;
for(;seq[*ctr]>=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]>seq[*ctr+1])
*temp=0;
ndown[*down]=ndown[*down]+1;
}
}
[/c]
thanks a million for helping..
![:evil:](./images/smilies/icon_evil.gif)
please be kind to tell me the bug of my code...please!
![:cry:](./images/smilies/icon_cry.gif)
[c]
#include<stdio.h>
void main()
{
long seq[32];
int l,up,down,ctr,i,temp;
int nup[31],ndown[31];
int tup,tdown;
float avup,avdown;
void hup(int *ctr,int nup[],int *up,int *temp,long seq[]);
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[]);
while(1)
{
scanf("%ld",&(seq[1]));
if(seq[1]==0)
break;
for(i=2;i<=30;i++)
{
scanf("%ld",&seq);
if(seq==0)
break;
}
l=i-1;
temp=0;
up=0;
down=0;
for(ctr=1;ctr<=l&&seq[ctr+1]!=0;)
{
if(seq[ctr]<seq[ctr+1]&&seq[ctr+1]!=0)
hup(&ctr,nup,&up,&temp,seq);
if(seq[ctr]==seq[ctr+1])
{
for(i=ctr;seq[ctr]==seq[ctr+1]&&seq[ctr+1]!=0;ctr++)
temp=temp+1;
}
if(seq[ctr]>seq[ctr+1]&&seq[ctr+1]!=0)
hdown(&ctr,ndown,&down,&temp,seq);
}
avup=0;
tup=0;
for(i=1;i<=up;i++)
tup=tup+nup;
if(up>0)
avup=(float)tup/(float)up;
avdown=0;
tdown=0;
for(i=1;i<=down;i++)
tdown=tdown+ndown;
if(down>0)
avdown=(float)tdown/(float)down;
printf("Nr values = %d: ",l);
printf("%f %f\n",avup,avdown);
}
}
void hup(int *ctr,int nup[],int *up,int *temp,long seq[])
{
*up=*up+1;
nup[*up]=*temp;
*temp=0;
for(;seq[*ctr]<=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]<seq[*ctr+1])
*temp=0;
nup[*up]=nup[*up]+1;
}
}
void hdown(int *ctr,int ndown[],int *down,int *temp,long seq[])
{
*down=*down+1;
ndown[*down]=*temp;
*temp=0;
for(;seq[*ctr]>=seq[*ctr+1]&&seq[*ctr+1]!=0;(*ctr)++)
{
if(seq[*ctr]==seq[*ctr+1])
*temp=*temp+1;
if(seq[*ctr]>seq[*ctr+1])
*temp=0;
ndown[*down]=ndown[*down]+1;
}
}
[/c]
thanks a million for helping..
your program didn't pass even sample input, have you noticed it?
this is your incorrect test case:
4 4 4 3 3 3 4 0
it should be
Nr values = 7: 1.000000 5.000000
but you have
Nr values = 7: 3.000000 5.000000
and here's more test cases for those who have difficulty solving it:
INPUT
OUTPUT
Hope it helps
this is your incorrect test case:
4 4 4 3 3 3 4 0
it should be
Nr values = 7: 1.000000 5.000000
but you have
Nr values = 7: 3.000000 5.000000
and here's more test cases for those who have difficulty solving it:
INPUT
Code: Select all
1 2 3 0
3 2 1 0
1 2 3 2 1 0
2 2 2 2 3 0
4 4 4 4 3 0
4 4 4 3 3 3 3 0
4 4 4 3 3 3 4 0
5 5 5 5 0
1 2 3 2 3 4 5 0
1 2 2 2 3 0
5 5 5 5 4 5 5 5 5 6 0
7 7 6 6 6 6 2 3 0
7 6 6 6 6 7 3 2 1 2 0
1 2 3 4 3 2 1 2 3 4 5 6 5 4 3 2 0
2 2 2 3 3 3 2 2 2 3 3 3 4 4 4 1 1 1 0
1 2 3 9 8 7 4 5 3 7 5 6 0
0
Code: Select all
Nr values = 3: 2.000000 0.000000
Nr values = 3: 0.000000 2.000000
Nr values = 5: 2.000000 2.000000
Nr values = 5: 4.000000 0.000000
Nr values = 5: 0.000000 4.000000
Nr values = 7: 0.000000 6.000000
Nr values = 7: 1.000000 5.000000
Nr values = 4: 0.000000 0.000000
Nr values = 7: 2.500000 1.000000
Nr values = 5: 4.000000 0.000000
Nr values = 10: 5.000000 4.000000
Nr values = 8: 1.000000 6.000000
Nr values = 10: 1.000000 7.000000
Nr values = 16: 4.000000 3.500000
Nr values = 18: 5.500000 3.000000
Nr values = 12: 1.500000 1.666667
keep it real!
413 some more input/ output
i need some more sample input and output, can anyone post some? thanks in advance.
regards,
nymo
nymo
413 why wa
i don't understand why wa.plz help me.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 100000
int main()
{
long a[max];
int i,j,l,m=0,check=0,check1=0,check2=0,k1check=0,k2check=0,in=0;
double totalup=0,totaldown=0,neutral=0,k1=1,k=1;
while(1)
{
scanf("%ld",&a[0]);
if(a[0]==0)
break;
for(l=1;;l++)
{
scanf("%ld",&a[l]);
//if(a[0]==0)
//goto M;
if(a[l]==0)
break;
}
totalup=0,totaldown=0,neutral=0,k=0,k1=0,k1check=0,k2check=0,in=0;
for(i=0;i<l-1;i++)
{
if(a<a[i+1])
{ if(in==0)
{
k++;
in=1;
}
check1=1;
totalup++;
k1check=1;
if(check==1)
{
if(k2check==1)
{
totaldown+=neutral;
k2check=0;
}
else
totalup=totalup+neutral;
check=0;
neutral=0;
//k++;
}
if(check2==1)
{
//totalup=totalup+neutral;
check2=0;
k++;
//m1=1;
}
}
else if(a>a[i+1])
{
if(in==0)
{
in=1;
k1++;
}
check2=1;
totaldown++;
k2check=1;
if(check==1)
{
if(k1check==1)
{
totalup=totalup+neutral;
k1check=0;
}
else
totaldown=totaldown+neutral;
check=0;
neutral=0;
k2check=1;
}
if(check1==1)
{
//totalup=totalup+neutral;
check1=0;
k1++;
}
/* if(m1==1)
{
m1=0;
k++; */
}
else if(a==a[i+1])
{
neutral++;
check=1;
}
}
if(check==1&&k1check==1)
{
totalup+=neutral;
k++;
check=0;
k1check=0;
}
if(check==1&&k2check==1)
{
totaldown+=neutral;
k1++;
check=0;k2check=0;
}
if(totaldown<=0)
{
if(k==0)
k=1;
printf("Nr values = %ld: %.6lf 0.000000\n",l,totalup/k);
}
else if(totalup<=0)
{
if(k1==0)
k1=1;
printf("Nr values = %ld: 0.000000 %.6lf\n",l,totaldown/k1);
}
else
{
if(k==0)
k=1;
if(k1==0)
k1=1;
printf("Nr values = %ld: %.6lf %.6lf\n",l,totalup/k,totaldown/k1);
}
m=0;
check=0,check1=0,check2=0;
}
}
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 100000
int main()
{
long a[max];
int i,j,l,m=0,check=0,check1=0,check2=0,k1check=0,k2check=0,in=0;
double totalup=0,totaldown=0,neutral=0,k1=1,k=1;
while(1)
{
scanf("%ld",&a[0]);
if(a[0]==0)
break;
for(l=1;;l++)
{
scanf("%ld",&a[l]);
//if(a[0]==0)
//goto M;
if(a[l]==0)
break;
}
totalup=0,totaldown=0,neutral=0,k=0,k1=0,k1check=0,k2check=0,in=0;
for(i=0;i<l-1;i++)
{
if(a<a[i+1])
{ if(in==0)
{
k++;
in=1;
}
check1=1;
totalup++;
k1check=1;
if(check==1)
{
if(k2check==1)
{
totaldown+=neutral;
k2check=0;
}
else
totalup=totalup+neutral;
check=0;
neutral=0;
//k++;
}
if(check2==1)
{
//totalup=totalup+neutral;
check2=0;
k++;
//m1=1;
}
}
else if(a>a[i+1])
{
if(in==0)
{
in=1;
k1++;
}
check2=1;
totaldown++;
k2check=1;
if(check==1)
{
if(k1check==1)
{
totalup=totalup+neutral;
k1check=0;
}
else
totaldown=totaldown+neutral;
check=0;
neutral=0;
k2check=1;
}
if(check1==1)
{
//totalup=totalup+neutral;
check1=0;
k1++;
}
/* if(m1==1)
{
m1=0;
k++; */
}
else if(a==a[i+1])
{
neutral++;
check=1;
}
}
if(check==1&&k1check==1)
{
totalup+=neutral;
k++;
check=0;
k1check=0;
}
if(check==1&&k2check==1)
{
totaldown+=neutral;
k1++;
check=0;k2check=0;
}
if(totaldown<=0)
{
if(k==0)
k=1;
printf("Nr values = %ld: %.6lf 0.000000\n",l,totalup/k);
}
else if(totalup<=0)
{
if(k1==0)
k1=1;
printf("Nr values = %ld: 0.000000 %.6lf\n",l,totaldown/k1);
}
else
{
if(k==0)
k=1;
if(k1==0)
k1=1;
printf("Nr values = %ld: %.6lf %.6lf\n",l,totalup/k,totaldown/k1);
}
m=0;
check=0,check1=0,check2=0;
}
}
}
-
- Experienced poster
- Posts: 106
- Joined: Thu Jan 29, 2004 12:07 pm
- Location: Bangladesh
- Contact:
Try these cases :
INPUT :
OUTPUT :
INPUT :
Code: Select all
1 2 3 4 5 6 7 8 0
8 7 6 5 4 3 2 1 0
1 1 1 1 1 0
9 9 9 9 0
1 2 1 2 1 2 0
2 1 2 1 2 1 0
1 2 1 0
2 1 2 0
1 2 3 2 1 2 3 2 1 0
1 5 32 32 32 15 15 1 5 1 1 1 1 1 1 0
3 0
100 100 100 99 100 100 99 100 99 0
0
Code: Select all
Nr values = 8: 7.000000 0.000000
Nr values = 8: 0.000000 7.000000
Nr values = 5: 0.000000 0.000000
Nr values = 4: 0.000000 0.000000
Nr values = 6: 1.000000 1.000000
Nr values = 6: 1.000000 1.000000
Nr values = 3: 1.000000 1.000000
Nr values = 3: 1.000000 1.000000
Nr values = 9: 2.000000 2.000000
Nr values = 15: 2.500000 4.500000
Nr values = 1: 0.000000 0.000000
Nr values = 9: 1.500000 1.666667
jaracz wrote:your program didn't pass even sample input, have you noticed it?
this is your incorrect test case:
4 4 4 3 3 3 4 0
it should be
Nr values = 7: 1.000000 5.000000
but you have
Nr values = 7: 3.000000 5.000000
and here's more test cases for those who have difficulty solving it:
INPUTOUTPUTCode: Select all
1 2 3 0 3 2 1 0 1 2 3 2 1 0 2 2 2 2 3 0 4 4 4 4 3 0 4 4 4 3 3 3 3 0 4 4 4 3 3 3 4 0 5 5 5 5 0 1 2 3 2 3 4 5 0 1 2 2 2 3 0 5 5 5 5 4 5 5 5 5 6 0 7 7 6 6 6 6 2 3 0 7 6 6 6 6 7 3 2 1 2 0 1 2 3 4 3 2 1 2 3 4 5 6 5 4 3 2 0 2 2 2 3 3 3 2 2 2 3 3 3 4 4 4 1 1 1 0 1 2 3 9 8 7 4 5 3 7 5 6 0 0
Hope it helpsCode: Select all
Nr values = 3: 2.000000 0.000000 Nr values = 3: 0.000000 2.000000 Nr values = 5: 2.000000 2.000000 Nr values = 5: 4.000000 0.000000 Nr values = 5: 0.000000 4.000000 Nr values = 7: 0.000000 6.000000 Nr values = 7: 1.000000 5.000000 Nr values = 4: 0.000000 0.000000 Nr values = 7: 2.500000 1.000000 Nr values = 5: 4.000000 0.000000 Nr values = 10: 5.000000 4.000000 Nr values = 8: 1.000000 6.000000 Nr values = 10: 1.000000 7.000000 Nr values = 16: 4.000000 3.500000 Nr values = 18: 5.500000 3.000000 Nr values = 12: 1.500000 1.666667
7 6 6 6 6 7 3 2 1 2 0 -> Why is Nr values = 10: 1.000000 7.000000 ?
may be more correctly is 1.000000 3.500000 or not? please explain i have a problem with testing
![:o](./images/smilies/icon_eek.gif)
413 - up and down sequences
Hello everybody!
I tried lots of stuff but I think I didn't understand the problem at all... I think I am doing the wrong thing when the sequences has neutral values (no up nor down)...
It works fine for lots of inputs, but I tried with this
and it fails on the 10th and in the last cases, but i just cant understand why!!!
itś giving me
but according with a post about the same problem here in the forum it should be
My code is as follows:
I tried lots of stuff but I think I didn't understand the problem at all... I think I am doing the wrong thing when the sequences has neutral values (no up nor down)...
It works fine for lots of inputs, but I tried with this
Code: Select all
1 2 3 4 5 6 7 8 0
8 7 6 5 4 3 2 1 0
1 1 1 1 1 0
9 9 9 9 0
1 2 1 2 1 2 0
2 1 2 1 2 1 0
1 2 1 0
2 1 2 0
1 2 3 2 1 2 3 2 1 0
1 5 32 32 32 15 15 1 5 1 1 1 1 1 1 0
3 0
100 100 100 99 100 100 99 100 99 0
0
itś giving me
Code: Select all
Nr values = 8: 7.000000 0.000000
Nr values = 8: 0.000000 7.000000
Nr values = 5: 0.000000 0.000000
Nr values = 4: 0.000000 0.000000
Nr values = 6: 1.000000 1.000000
Nr values = 6: 1.000000 1.000000
Nr values = 3: 1.000000 1.000000
Nr values = 3: 1.000000 1.000000
Nr values = 9: 2.000000 2.000000
Nr values = 15: 5.500000 3.000000
Nr values = 1: 0.000000 0.000000
Nr values = 9: 1.000000 6.000000
Code: Select all
Nr values = 8: 7.000000 0.000000
Nr values = 8: 0.000000 7.000000
Nr values = 5: 0.000000 0.000000
Nr values = 4: 0.000000 0.000000
Nr values = 6: 1.000000 1.000000
Nr values = 6: 1.000000 1.000000
Nr values = 3: 1.000000 1.000000
Nr values = 3: 1.000000 1.000000
Nr values = 9: 2.000000 2.000000
Nr values = 15: 2.500000 4.500000
Nr values = 1: 0.000000 0.000000
Nr values = 9: 1.500000 1.666667
Code: Select all
#include <stdio.h>
#define UP 1
#define DOWN 2
#define NEUTRAL 3
int main()
{
int serie[31];
int NEUTRALVALUE;
int totalUP;
int totalDOWN;
int totalUPSequences, totalDOWNSequences;
int INDEX;
int stateOLD, stateNEW;
int size;
bool flag = true;
int firstDeviation = -1;
while(flag)
{
totalUP = 0;
totalDOWN = 0;
NEUTRALVALUE = 0;
totalUPSequences = 0;
totalDOWNSequences = 0;
stateOLD = -1; //indefinido
//lendo a s