## 498 - Polly the Polynomial

Moderator: Board moderators

nukeu666
New poster
Posts: 44
Joined: Sun Feb 13, 2005 1:13 am
Location: India
Contact:
there was a lone 0 coming in the last line had to put that line

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Contact:
nukeu666 wrote:there was a lone 0 coming in the last line had to put that line
I don't get you. The input ends with EOF, not a lone 0. Anyway, removing that line should have solved your problem.

nukeu666
New poster
Posts: 44
Joined: Sun Feb 13, 2005 1:13 am
Location: India
Contact:
oo thnx...dunno wht i was thinking

Staryin
New poster
Posts: 12
Joined: Fri Dec 16, 2005 4:22 pm
Location: shanghai/china
Contact:

### 498 WHY WA!!!

Code: Select all

``````#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
using namespace std;
char c,x;
char *p;
char *co,*num;
double cof,nu;
int cnt,coff;
int i,j,k;
int index;
double sum;
void deal()
{
cnt=0;
p=strtok(c," ");
if(p)
co[cnt++]=p;
while(p)
{
p=strtok(NULL," ");
co[cnt++]=p;
}
coff=cnt-1;
for(i=0;i<coff;i++)
cof[i]=atoi(co[i]);

cnt=0;
p=strtok(x," ");
if(p)
num[cnt++]=p;
while(p)
{
p=strtok(NULL," ");
num[cnt++]=p;
}

cnt=cnt-1;

for(i=0;i<cnt;i++)
nu[i]=atoi(num[i]);

}

void solve()
{
sum=0;
deal();

//for(i=0;i<coff;i++)
for(i=0;i<cnt;i++)
{
sum=0;
for(j=0;j<coff;j++)
sum+=cof[j]*pow(nu[i],coff-j-1);

cout<<sum<<" ";

}

}
{
while(gets(c)&&gets(x))
{

solve();
cout<<endl;
}

}
int main()
{
return 0;
}
``````

thomas1016
New poster
Posts: 19
Joined: Mon May 29, 2006 4:12 pm

### 498 TLE

I have found the reason why is TLE. It is beacuse "getline" that can lead to use 1 more "enter" to get the output on VC compiler,but dev C++ is ok. How can I fix it????

Code: Select all

``````
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int t2w(string);
int main(void){
string a,b,d,e;
//char *c,d;

int sum;

while(1){
getline(cin,b,'\n' );
getline(cin,a,'\n' );

istringstream isss(a);
sum=0;
while(isss>>e){
sum=0;
istringstream iss(b);
while(iss>>d){
sum=sum*t2w(e)+t2w(d);
}
cout<<sum<<" ";
}

cout<<endl;

}

return 0;
}
int t2w(string a){
////////////TO MUCH MEMERY ><
char *c;
int i;
c=(char*) malloc(sizeof(a));
for(i=0;i<a.length();c[i]=a[i],i++);
return atoi(c);
/*  int k,i;
k=0;
if(isdigit(a)!=0){
k=a-'0';

}

for(i=1;i<a.length();k=k*10+a[i]-'0',i++){
}
if(isdigit(a)==0){
k=-k;
}
return k;

*/
}

``````

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:
Your while(1) { ... } doesn't have any terminating conditions. How is your program supposed to stop?
getline() will silently return when it reaches EOF, and your program is destined to output non-sense forever (well, for 10 seconds on UVa) since then.

In Windows' console you can similate EOF by pressing Ctrl-Z, Enter.

Also, you could implement t2w() as just 'return atoi(a.c_str());'

thomas1016
New poster
Posts: 19
Joined: Mon May 29, 2006 4:12 pm
but how can I get the input
i can't use "cin" it has " " space in the input?
my program is stuck so it is TLE.
isn't "getline" is used to "get a line"??

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:
getline is okay here, but you have to check when the end-of-file comes. (In case you don't know, testing at this site is done automatically; the input comes from a *file* on the disk, not a keyboard.)

Something like this, I believe, should work:

Code: Select all

``````while(1){
if (!getline(cin,b)) break;
if (!getline(cin,a)) break;

...
}
``````

thomas1016
New poster
Posts: 19
Joined: Mon May 29, 2006 4:12 pm
Thanks a lot.
May I ask u one more question???
in the following code
I can't correctlly get the input like "2 4 2 1 2"
how can i solve it??

Code: Select all

``````
.....

istringstream isss(a);
sum=0;
while(isss>>e){
sum=0;
istringstream iss(b);
while(iss>>d){
sum=sum*t2w(e)+t2w(d);
}
cout<<sum<<" ";
}

.....

``````

thomas1016
New poster
Posts: 19
Joined: Mon May 29, 2006 4:12 pm
the input which has two space.
it means a 0.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:
When two integers are separated by two or more spaces, it doesn't mean there's a zero in between.
Just ignore extra spaces (istringstream does that automatically for you). If there is a zero, it will be always given explicitely as "0".

thomas1016
New poster
Posts: 19
Joined: Mon May 29, 2006 4:12 pm
Here is my code.
This time is WA.
HELP...
--------------
CPU Memory
9.725 476
---------------

Code: Select all

``````
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int t2w(string);
int main(void){
string a,b,d,e;
//char *c,d;

int sum;

while(1){
if (!getline(cin,b)) break;
if (!getline(cin,a)) break;
getline(cin,b,'\n');
getline(cin,a,'\n');

istringstream isss(a);
sum=0;
while(isss>>e){
sum=0;
istringstream iss(b);
while(iss>>d){
sum=sum*t2w(e)+t2w(d);
}
cout<<sum<<" ";
}

cout<<endl;

}

return 0;
}
int t2w(string a){/*
////////////TO MUCH MEMERY ><
char *c;
int i;
c=(char*) malloc(sizeof(a));
for(i=0;i<a.length();c[i]=a[i],i++);
return atoi(a.c_str());*/
int k,i;
k=0;
if(isdigit(a)!=0){
k=a-'0';

}

for(i=1;i<a.length();k=k*10+a[i]-'0',i++){
}
if(isdigit(a)==0){
k=-k;
}
return k;

}

``````

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:
In this code you skip every second test case. Remove last two getline()'s.

Code: Select all

``````if (!getline(cin,b)) break;
if (!getline(cin,a)) break;
getline(cin,b,'\n');
getline(cin,a,'\n');``````

thomas1016
New poster
Posts: 19
Joined: Mon May 29, 2006 4:12 pm

### Excuese me but this time it turns out to be TLE

Excuese me but this time it turns out to be TLE.  Code: Select all

``````
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int t2w(string);
int main(void){
string a,b,d,e;
//char *c,d;

int sum;

while(1){
if (!getline(cin,b,'\n')) break;
if (!getline(cin,a,'\n')) break;

istringstream isss(a);
sum=0;
while(isss>>e){
sum=0;
istringstream iss(b);
while(iss>>d){
sum=sum*t2w(e)+t2w(d);
}
cout<<sum<<" ";
}

cout<<endl;

}

return 0;
}
int t2w(string a){/*
////////////TO MUCH MEMERY ><
char *c;
int i;
c=(char*) malloc(sizeof(a));
for(i=0;i<a.length();c[i]=a[i],i++);
return atoi(a.c_str());*/
int k,i;
k=0;
if(isdigit(a)!=0){
k=a-'0';

}

for(i=1;i<a.length();k=k*10+a[i]-'0',i++){
}
if(isdigit(a)==0){
k=-k;
}
return k;

}

``````

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:
Perhaps, istringstream is too slow. Use something faster.