Hertz Posted August 8, 2009 Report Share Posted August 8, 2009 Am o bucata specifica de text care reprezinta o valoare specifica.O calculez astfel : - incep citirea din stanga spre dreapta - daca un caracter e cifra , il adaug la suma mea - daca e un 'x' , il sterg si merg inapoi 2 pozitii - continui pana ajung la sfarsitul stringuluiDe exemplu stringul '123x456' are valoarea 26 .Care e valoarea stringului :93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx ? Quote Link to comment Share on other sites More sharing options...
tromfil Posted August 8, 2009 Report Share Posted August 8, 2009 Se mai pot face optimizari. Adica sa facem o inmultire in functie de cati de x gasim, dar n-are rost ca stringul e mic. E bine?1492<?php$s = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx';$suma = 0;for ($i=0;$i<strlen($s);$i++){ if (is_numeric($s{$i})) $suma += $s{$i}; else { $a = strpos($s,'x'); $b = $a+1; $s = substr($s,0,$a).substr($s,$b,(strlen($s)-$); $i=$i-3; }}echo $suma;?> Quote Link to comment Share on other sites More sharing options...
CyberWolf08 Posted August 8, 2009 Report Share Posted August 8, 2009 Raspuns: 1492Codul in VB.net ca il aveam deschis cand am vazut challenge-ul Dim a As String a = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx" Dim i, s As Integer While i < a.Length() If (a.Chars(i) = "x") Then a = a.Remove(i, 1) i = i - 2 End If s = s + Val(a.Chars(i)) - "0" i = i + 1 End While MsgBox(s)Later edit: Ah, ca il facuse cineva inainte. Quote Link to comment Share on other sites More sharing options...
dranaxum Posted August 8, 2009 Report Share Posted August 8, 2009 Se poate face in O(n) renuntand la strpos, substr etc retinand la fiecare pas ultimile 2 cifre iar in momentul cand se intalneste un x ele se readauga la suma.In felul asta algoritmul e muuult mai rapid.Asta e codul:#include<fstream>#include<ctype.h>using namespace std;ifstream fin("sirhertz.in");ofstream fout("sirhertz.out");int main(){ char c; int s=0,a=0,b=0; while(!fin.eof()) { fin>>c; if(!fin.eof()) if(isdigit(c)) { s+=c-'0'; a=b; b=c-'0'; } else if(c=='x') s+=a+b; } fout<<s; fin.close(); fout.close(); return 0;} Quote Link to comment Share on other sites More sharing options...
dranaxum Posted August 8, 2009 Report Share Posted August 8, 2009 Am tratat cazul general, nu m-am limitat la stringul dat. Quote Link to comment Share on other sites More sharing options...
Hertz Posted August 8, 2009 Author Report Share Posted August 8, 2009 Corect toti 3,imi place abordarea draxanum.Felicitari. Quote Link to comment Share on other sites More sharing options...
dranaxum Posted August 8, 2009 Report Share Posted August 8, 2009 Poti pune asta la concursuri pe hackpedia. Sunt curios cine va avea o abordare in O(n). Quote Link to comment Share on other sites More sharing options...
cmiN Posted August 9, 2009 Report Share Posted August 9, 2009 Am vazut in php, VB.net, c++, am facut si eu in python . Si eu m-am gandit prima data la ideea lui dranaxum, dar am conceput sursa pentru a urma exact pasii dati de Hertz.Python#! /usr/bin/env python# 09.08.2009 <> 09.08.2009 | cmiN# Challenge [Programare] Valori (Hertz) @ rstcenter.comdef main(): string = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx" strlist = list(string) strsum, x = 0, 0 while True: try: item = strlist[x] except: break if item == "x": strlist.remove("x") x -= 2 else: strsum += int(item) x += 1 input("The string value is %s." % str(strsum))if __name__ == "__main__": main() Quote Link to comment Share on other sites More sharing options...