Jump to content
Hertz

[Programare]Valori

Recommended Posts

Posted

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 stringului

De exemplu stringul '123x456' are valoarea 26 .

Care e valoarea stringului :


93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx

?

Posted

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;
?>

Posted

Raspuns: 1492

Codul 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.

Posted

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;
}

Posted

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.com


def 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()

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...