498 - Polly the Polynomial
Moderator: Board moderators
498 WHY WA!!!
Code: Select all
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
using namespace std;
char c[1000001],x[1000001];
char *p;
char *co[1000001],*num[1000001];
double cof[10001],nu[10001];
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<<" ";
}
}
void reading()
{
while(gets(c)&&gets(x))
{
solve();
cout<<endl;
}
}
int main()
{
reading();
return 0;
}
-
- 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[11];
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])!=0){
k=a[0]-'0';
}
for(i=1;i<a.length();k=k*10+a[i]-'0',i++){
}
if(isdigit(a[0])==0){
k=-k;
}
return k;
*/
}
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());'
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());'
-
- New poster
- Posts: 19
- Joined: Mon May 29, 2006 4:12 pm
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:
Something like this, I believe, should work:
Code: Select all
while(1){
if (!getline(cin,b)) break;
if (!getline(cin,a)) break;
...
}
-
- 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??
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<<" ";
}
.....
-
- New poster
- Posts: 19
- Joined: Mon May 29, 2006 4:12 pm
-
- 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
---------------
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[11];
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])!=0){
k=a[0]-'0';
}
for(i=1;i<a.length();k=k*10+a[i]-'0',i++){
}
if(isdigit(a[0])==0){
k=-k;
}
return k;
}
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');
-
- 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[11];
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])!=0){
k=a[0]-'0';
}
for(i=1;i<a.length();k=k*10+a[i]-'0',i++){
}
if(isdigit(a[0])==0){
k=-k;
}
return k;
}