Sprawdzanie podzielności przez 15

0

Mam problem z zadaniem http://pl.spoj.com/problems/WZP09_2F/
Mój kod dostaje przekroczono limit czasu. Jak mogę to inaczej sprawdzić lub przyspieszyć mój kod?

#include<iostream>
#include<string>
#include<cstdlib>

std::string divisible_15(std::string value);

int main() {
	std::string input;
	while(std::cin >> input && input!="0") {
		std::cout << divisible_15(input) << "\n";
	}
	return 0;
}

std::string divisible_15(std::string value) {
	const char *last_digit=value.substr(value.size()-1, 1).c_str();
	if(atoi(last_digit)!=5 && atoi(last_digit)!=0)
		return"NIE";
	int sum_of_digits=0;
	for(int i=0;i<value.size();++i) {
		const char *digit=value.substr(i, 1).c_str();
		sum_of_digits+=atoi(digit);
	}
	if(sum_of_digits%3==0)
		return "TAK";
	else return "NIE";
}
4
const char *last_digit=value.substr(value.size()-1, 1).c_str();
    if(atoi(last_digit)!=5 && atoi(last_digit)!=0)
char last_digit = *value.rbegin();
if (last_digit != '5' && last_digit != '0')
0

@Azarien Dodałem Twoją sugestie oraz

std::ios_base::sync_with_stdio(0);

i zmieściłem się w czasie. Wielkie dzięki.

2

Na szybko sklejone.

#include <iostream>
#include <string>
using namespace std;

bool byThree(const string& str) {
	unsigned acc=0;
	size_t len=str.length();
	for(size_t i=0;i<len;++i) acc+=str[i]-'0';
	return !(acc%3);
}

bool byFive(const string& str) {
	char back=str.back();
	return back=='0'||back=='5';
}

bool byFiveteen(const string& str) {
	return byFive(str)&&byThree(str);
}

int main() {
	char buffer[1024];
	while((scanf("%1023s",buffer))&&buffer[0]!='0')
		printf("%s\n",byFiveteen(buffer)?"TAK":"NIE");
	return 0;
}

Strasznie duzo dziwnych operacji wykonujesz...

2

Czy naprawdę myślicie że gromadzenie znaków w string'u będzie tańsze niż zliczanie sumy online?

0

To i ja dorzucę swoją cegiełkę:

#include <iostream>
#include <numeric>

using namespace std;

bool
podzielne_przez_5(const string &zestaw)
{
  char znak = zestaw[zestaw.length() - 1];
  return znak == '0' || znak == '5';
}


bool
podzielne_przez_3(const string &zestaw)
{
//  int suma = accumulate(zestaw.begin(), zestaw.end(), 0,
//                        [](int init, char first) {return init + (first - 48); });
  int suma = 0;
  for (string::const_iterator it = zestaw.begin(); it != zestaw.end(); it++)
    suma = suma + (*it - 48);

  return suma % 3 == 0;
}


int
main(void)
{
  string zestaw;

  while (true)
  {
    cin >> zestaw;
    if (zestaw == "0")
      return 0;

    if (podzielne_przez_5(zestaw) && podzielne_przez_3(zestaw))
      cout << "TAK\n";
    else
      cout << "NIE\n";
  }

  return 0;
}
1
#include <cctype>
#include <cstdio>
using namespace std;

int main()
  {
   static char *Answer[]={"NIE","TAK"};
   int sum=0,last,ch;
   while(true)
     {
      if(isdigit(ch=getchar())) sum+=(last=ch-'0');
      else if(sum)
        {
         puts(Answer[((last==5)||(!last))&&(!(sum%3))]);
         sum=0;
        }
      else break;
     }
   return 0;
  }
0

0.17 sek.

#include <stdio.h>

int
main(void)
{
  char last, current = '0';
  int c, sum = 0;

  while (1)
  {
    last = current;
    current = getchar();

    sum = sum + (last - '0');

    if (current == '\n')
    {
      if (sum == 0 && last == '0') return 0;

      if ((last == '5' || last == '0') && (sum % 3 == 0))
        printf("TAK\n");
      else
        printf("NIE\n");

      sum = 0;
      current = '0';
      last = 0;
    }
  }

  return 0;
}
0

W sumie, po drobnych modyfikacjach mojego - 0.03. Zbyt zaspany dalej jestem, zeby to ciagnac. :P

#include <iostream>
#include <string>
using namespace std;
 
bool byThree(const string& str) {
    unsigned acc=0;
    size_t len=str.length();
    for(size_t i=0;i<len;++i) acc+=str[i]-'0';
    return !(acc%3);
}
 
bool byFive(const string& str) {
    char back=str[str.length()-1];
    return back=='0'||back=='5';
}
 
bool byFiveteen(const string& str) {
    return byFive(str)&&byThree(str);
}

int main() {
	char buffer[1024],*answers[]={"NIE","TAK"};
	while(gets(buffer)&&buffer[0]!='0')
		puts(answers[byFiveteen(buffer)]);
	return 0;
}

Edit: 0.02 jeszcze.

#include <stdio.h>

int main() {
    char buffer[1024],*answers[]={"NIE","TAK"},*p,back;
    unsigned buf_size,acc;
    while(gets(buffer)&&buffer[0]!='0') {
    	acc=0;
    	for(p=buffer;*p;++p) acc+=*p-'0';
    	back=*(p-1);
    	puts(answers[(back=='0'||back=='5')&&!(acc%3)]);
    }
    return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1