ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1104. Don’t Ask Woman about Her Age

Why I have Time Limit Exceeded???? c++
Posted by h1ci 11 Nov 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++
Posted by gvsmirnov 11 Oct 2009 19:51
Because you read the whole string to a variable, and then loop through that string.
Re: Why I have Time Limit Exceeded???? c++
Posted by IgorKoval(from Pskov) 26 Mar 2010 21:44
You must use C. It's more quick . No C++. Just C.
Re: Why I have Time Limit Exceeded???? c++
Posted by dauren.ktl 19 Aug 2010 00:34
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++
Posted by Nitin Gangahar 29 Oct 2010 21:13
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++
Posted by Mehran.R 30 Oct 2010 17:06
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;
}
*/