ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1104. Не спрашивай даму о возрасте

#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;
}
Because you read the whole string to a variable, and then loop through that string.
You must use C. It's more quick . No C++. Just 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;
}
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
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;
}
*/