|
|
вернуться в форумWhy I have Time Limit Exceeded???? c++ Послано h1ci 11 ноя 2008 22:26 #include <iostream> #include <string> using namespace std; int main() { string a; long p=0,max=0,i; cin >> a; long n=a.size(); for(i=0; i<n; i++) { if(a[i]>='0' && a[i]<='9'){ p=p+(a[i]-'0'); if(a[i]-'0'>max) max=a[i]-'0'; } else { p=p+(a[i]-'A'+10); if(a[i]-'A'+10>max) max=a[i]-'A'+10;} } if(max==0) { i=0; cout << "2" << endl;} else { for(i=max+1; i<=36; i++) if(p%(i-1)==0){ cout << i << endl; break; }} if(i==37) cout << "No solution." << endl; return 0; } Re: Why I have Time Limit Exceeded???? c++ Because you read the whole string to a variable, and then loop through that string. Re: Why I have Time Limit Exceeded???? c++ You must use C. It's more quick . No C++. Just C. Re: Why I have Time Limit Exceeded???? c++ In the statements, string_length = 10^6, but your string_size can be maximum 256, write like this, and you will get AC : #include <iostream> #include <string> using namespace std; string a; long p,maxx,i,n; int main() { cin >> a; n=a.size(); for(i=0; i<n; i++) { if(a[i]>='0' && a[i]<='9'){ p=p+(a[i]-'0'); if(a[i]-'0'>maxx) maxx=a[i]-'0'; } else { p=p+(a[i]-'A'+10); if(a[i]-'A'+10>maxx) maxx=a[i]-'A'+10;} } if(maxx==0) { i=0; cout << "2" << endl;} else { for(i=maxx+1; i<=36; i++) if(p%(i-1)==0){ cout << i << endl; break; }} if(i==37) cout << "No solution." << endl; return 0; } Re: Why I have Time Limit Exceeded???? c++ No need to use complicated input styles. Just include cstdio as a header file and use scanf instead. You dont need to switch over to a C file. You can simply use C functions from C++. Regards Nitin Re: Why I have Time Limit Exceeded???? c++ 2 change code and AC:-) /* #include <iostream> #include <string> using namespace std; int main() { string a; long p=0,max=0,i; while(cin >> a)//1 { p=0;max=0;//2 long n=a.size(); for(i=0; i<n; i++) { if(a[i]>='0' && a[i]<='9'){ p=p+(a[i]-'0'); if(a[i]-'0'>max) max=a[i]-'0'; } else { p=p+(a[i]-'A'+10); if(a[i]-'A'+10>max) max=a[i]-'A'+10;} } if(max==0) { i=0; cout << "2" << endl;} else { for(i=max+1; i<=36; i++) if(p%(i-1)==0){ cout << i << endl; break; }} if(i==37) cout << "No solution." << endl; } return 0; } */ |
|
|