<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=0147_-_expresie1</id>
	<title>0147 - expresie1 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=0147_-_expresie1"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0147_-_expresie1&amp;action=history"/>
	<updated>2026-05-01T04:05:37Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://wiki.universitas.ro/index.php?title=0147_-_expresie1&amp;diff=9041&amp;oldid=prev</id>
		<title>Miawinator: Pagină nouă: La ora de matematică Georgică a învăţat să evalueze expresii aritmetice. Pentru a verifica acest lucru profesorul de informatică îi propune lui Georgică să evalueze expresii aritmetice de forma:  &lt;code&gt;A  o1  B  o2  C&lt;/code&gt;  Unde &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;B&lt;/code&gt;, &lt;code&gt;C&lt;/code&gt; sunt operanzi, iar &lt;code&gt;o1&lt;/code&gt; şi &lt;code&gt;o2&lt;/code&gt; sunt operatori.  Valorile permise operanzilor pot fi numai termeni din şirul &lt;code&gt;x1&lt;/code&gt;, &lt;code&gt;x2&lt;/code&gt;, …, &lt;code&gt;xn&lt;/code&gt;, iar...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0147_-_expresie1&amp;diff=9041&amp;oldid=prev"/>
		<updated>2024-01-04T23:05:53Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: La ora de matematică Georgică a învăţat să evalueze expresii aritmetice. Pentru a verifica acest lucru profesorul de informatică îi propune lui Georgică să evalueze expresii aritmetice de forma:  &amp;lt;code&amp;gt;A  o1  B  o2  C&amp;lt;/code&amp;gt;  Unde &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;C&amp;lt;/code&amp;gt; sunt operanzi, iar &amp;lt;code&amp;gt;o1&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;o2&amp;lt;/code&amp;gt; sunt operatori.  Valorile permise operanzilor pot fi numai termeni din şirul &amp;lt;code&amp;gt;x1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;x2&amp;lt;/code&amp;gt;, …, &amp;lt;code&amp;gt;xn&amp;lt;/code&amp;gt;, iar...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;La ora de matematică Georgică a învăţat să evalueze expresii aritmetice. Pentru a verifica acest lucru profesorul de informatică îi propune lui Georgică să evalueze expresii aritmetice de forma:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;A  o1  B  o2  C&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unde &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;C&amp;lt;/code&amp;gt; sunt operanzi, iar &amp;lt;code&amp;gt;o1&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;o2&amp;lt;/code&amp;gt; sunt operatori.&lt;br /&gt;
&lt;br /&gt;
Valorile permise operanzilor pot fi numai termeni din şirul &amp;lt;code&amp;gt;x1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;x2&amp;lt;/code&amp;gt;, …, &amp;lt;code&amp;gt;xn&amp;lt;/code&amp;gt;, iar operatorii pot fi numai caracterele &amp;lt;code&amp;gt;+&amp;lt;/code&amp;gt; (adunare) şi &amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt; (înmulţire).&lt;br /&gt;
&lt;br /&gt;
Profesorul îi pune la dispoziție lui Georgică o valoare numerică &amp;lt;code&amp;gt;V&amp;lt;/code&amp;gt; şi îi cere să determine valorile operanzilor &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;C&amp;lt;/code&amp;gt; pentru care expresia are valoarea &amp;lt;code&amp;gt;V&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Cerinţă =&lt;br /&gt;
Scrieţi un program, care pentru &amp;lt;code&amp;gt;V&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;x1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;x2&amp;lt;/code&amp;gt;, …, &amp;lt;code&amp;gt;xn&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;o1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;o2&amp;lt;/code&amp;gt; date, determină indicii corespunzători termenilor din şirul &amp;lt;code&amp;gt;x1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;x2&amp;lt;/code&amp;gt;, …, &amp;lt;code&amp;gt;xn&amp;lt;/code&amp;gt; ce corespund valorilor operanzilor &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;C&amp;lt;/code&amp;gt; pentru ca expresia dată să aibă valoarea &amp;lt;code&amp;gt;V&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Fişierul de intrare &amp;lt;code&amp;gt;input.txt&amp;lt;/code&amp;gt; conţine pe prima linie numerele naturale &amp;lt;code&amp;gt;V&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;x1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;x2&amp;lt;/code&amp;gt;, …, &amp;lt;code&amp;gt;xn&amp;lt;/code&amp;gt; separate între ele printr-un spaţiu şi pe linia a doua exact două caractere asociate operatorilor &amp;lt;code&amp;gt;o1&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;o2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Date de ieşire =&lt;br /&gt;
Fişierul de ieşire &amp;lt;code&amp;gt;output.txt&amp;lt;/code&amp;gt; va conţine, pe prima linie separate între ele exact trei indici ai şirului &amp;lt;code&amp;gt;x1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;x2&amp;lt;/code&amp;gt;, …, &amp;lt;code&amp;gt;xn&amp;lt;/code&amp;gt; corespunzători termenilor folosiţi pentru &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;C&amp;lt;/code&amp;gt;. Dacă nu există soluţie se va scrie în fişier cifra &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; de trei ori separată prin câte un spaţiu: &amp;lt;code&amp;gt;0 0 0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Restricţii şi precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;0 &amp;lt; n &amp;lt; 10001&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Exemplul 1 ==&lt;br /&gt;
input.txt:&lt;br /&gt;
&lt;br /&gt;
100 5 40 13 2 11 20&lt;br /&gt;
&lt;br /&gt;
+*&lt;br /&gt;
&lt;br /&gt;
output.txt:&lt;br /&gt;
&lt;br /&gt;
5 3 1&lt;br /&gt;
&lt;br /&gt;
Explicație:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;A = x5&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;B = x3&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;C = x1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expresia devine &amp;lt;code&amp;gt;20 + 2 * 40 = 100&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Exemplul 2 ==&lt;br /&gt;
input.txt:&lt;br /&gt;
&lt;br /&gt;
100 9999999999999 40 13 2 11 20&lt;br /&gt;
&lt;br /&gt;
+*&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
Input-ul nu convine conditiilor&lt;br /&gt;
&lt;br /&gt;
== Rezolvare ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python3&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
def verificare(n):&lt;br /&gt;
    if not(1&amp;lt;=n&amp;lt;=10000):&lt;br /&gt;
        print(&amp;quot;Input-ul nu convine conditiilor&amp;quot;)&lt;br /&gt;
        exit()&lt;br /&gt;
&lt;br /&gt;
def cit():&lt;br /&gt;
    global V, n, f, o1, o2&lt;br /&gt;
    with open(&amp;quot;input.txt&amp;quot;, &amp;quot;r&amp;quot;) as fin:&lt;br /&gt;
        l=list(map(int, fin.readline().split()))&lt;br /&gt;
        V = l[0]&lt;br /&gt;
        n = l[1]&lt;br /&gt;
        verificare(n)&lt;br /&gt;
        f = [0] * 1005&lt;br /&gt;
        for i in range(1, n + 1):&lt;br /&gt;
            k = l[i+1]&lt;br /&gt;
            f[k] = i&lt;br /&gt;
        l=fin.readline()&lt;br /&gt;
        o1 = l[0]&lt;br /&gt;
        o2 = l[1]&lt;br /&gt;
&lt;br /&gt;
def evalexp(A, B):&lt;br /&gt;
    global V, o1, o2&lt;br /&gt;
    if o1 == &amp;#039;+&amp;#039;:&lt;br /&gt;
        if o2 == &amp;#039;+&amp;#039;:&lt;br /&gt;
            t = V - (A + B)&lt;br /&gt;
        else:&lt;br /&gt;
            t = (V - A) / B&lt;br /&gt;
    else:&lt;br /&gt;
        if o2 == &amp;#039;+&amp;#039;:&lt;br /&gt;
            t = V - A * B&lt;br /&gt;
        else:&lt;br /&gt;
            t = V / (A * B)&lt;br /&gt;
    if t &amp;gt; 0 and t == int(t):&lt;br /&gt;
        return int(t)&lt;br /&gt;
    return -1&lt;br /&gt;
&lt;br /&gt;
def rezolva():&lt;br /&gt;
    global f&lt;br /&gt;
    for iA in range(1001):&lt;br /&gt;
        if f[iA] &amp;gt; 0:&lt;br /&gt;
            for iB in range(1001):&lt;br /&gt;
                if f[iB] &amp;gt; 0:&lt;br /&gt;
                    iC = evalexp(iA, iB)&lt;br /&gt;
                    if 0 &amp;lt;= iC &amp;lt; 1001 and f[iC] &amp;gt; 0:&lt;br /&gt;
                        with open(&amp;quot;output.txt&amp;quot;, &amp;quot;w&amp;quot;) as fout:&lt;br /&gt;
                            fout.write(f&amp;quot;{f[iA]} {f[iB]} {f[iC]}&amp;quot;)&lt;br /&gt;
                        return 1&lt;br /&gt;
    return 0&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    cit()&lt;br /&gt;
    if rezolva() == 0:&lt;br /&gt;
        with open(&amp;quot;output.txt&amp;quot;, &amp;quot;w&amp;quot;) as fout:&lt;br /&gt;
            fout.write(&amp;quot;0 0 0&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Miawinator</name></author>
	</entry>
</feed>