Pagini recente » Cod sursa (job #2707879) | Cod sursa (job #2158681) | Cod sursa (job #2171839) | Cod sursa (job #2784645) | Cod sursa (job #361467)
Cod sursa(job #361467)
#include<iostream>
#include<fstream>
using namespace std;
struct nod{
int t;
int semn;
nod *urm;
};
void adaug(nod *&first, nod *&last, int x)
{
nod *p;
p = new nod;
p->t=x;
p->urm=NULL;
p->semn =0;
if(first == NULL)
first = p;
else
last->urm = p;
last = p;
}
int main()
{
nod *first=NULL,*last,*q;
int i,n,x,s;
ifstream f("semne.in");
f>>n>>s;
for(i=1;i<=n;i++)
{
cin>>x;
adaug(first,last,x);
}
last->urm = first;
q=first;
int suma=0;
while(suma!=s)
{
suma-=q->t*q->semn;
if(suma>s)
q->semn=-1;
if(suma<s)
q->semn=1;
suma+=q->t*q->semn;
q=q->urm;
}
ofstream g("semne.out");
do{
if(first->semn == 1)
g<<"+";
else
g<<"-";
first=first->urm;
}while(first!=last);
g.close();
f.close();
}