Cod sursa(job #1398780)

Utilizator andreib98Borza Andrei andreib98 Data 24 martie 2015 13:22:32
Problema Algoritmul lui Euclid extins Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.36 kb
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin("mxl.in");
ofstream fout("mxl.out");
int a[41][41],n,i,j,k,x,y,q,ii;
struct punct
{
    int r,x,y;
    char c[256];
};
punct b[1601];
bool ok,ok1;
int main()
{
fin>>n>>k;
q=k;
for(i=1;i<=k;i++)
{
    fin>>b[i].x>>b[i].y;
    fin>>b[i].c;
    if(b[i].c[0]!='=')
    {
        for(j=0;j<strlen(b[i].c);j++)
        {
            b[i].r=b[i].r*10+(b[i].c[j]-'0');
        }
        q--;
    }
    else
    {
        ok=true;
        x=0;
        ok1=false;
        for(j=1;j<strlen(b[i].c)&&ok;j++)
        {
            if(b[i].c[j]=='+')
            {
                b[i].r=b[i].r+x;
                x=0;
                ok1=true;
            }
            else if(b[i].c[j]==':') ok=false;
            else
            {
                x=x*10+(b[i].c[j]-'0');
            }
        }
        if(x!=0) b[i].r+=x;
        if(ok==false) b[i].r=0;
        if(ok1==false&&ok) b[i].r=x;
        if(ok) q--;
    }
}
i=1;
while(q!=0)
{
    if(b[i].r==0)
    {
        x=0;
        y=0;
        for(j=1;j<strlen(b[i].c);j++)
        {
            if(b[i].c[j]=='+')
            {
                b[i].r=b[i].r+x;
                x=0;
            }
            else if(b[i].c[j]==':')
            {
                j++;
                ok=false;
                while(b[i].c[j]>='0'&&b[i].c[j]<='9')
                {
                    y=y*10+(b[i].c[j]-'0');
                    j++;
                }
                for(ii=1;ii<=k&&ok==false;ii++)
                {
                    if(x==b[ii].x&&y==b[ii].y&&b[ii].r!=0)
                    {
                        b[i].r=b[i].r+b[ii].r;
                        ok=true;
                    }
                }
                if(ok==false)
                {
                    b[i].r=0;
                    j=strlen(b[i].c);
                }
                x=0;
                y=0;
            }
            else
            {
                x=x*10+(b[i].c[j]-'0');
            }
        }
        if(x!=0&&b[i].r!=0) b[i].r=b[i].r+x;
        if(b[i].r!=0) q--;
    }
    i++;
    if(i>k) i=1;
}
for(i=1;i<=k;i++)
{
    a[b[i].x][b[i].y]=b[i].r;
}
for(i=1;i<=n;i++)
{
    for(j=1;j<=n;j++)
        fout<<a[i][j]<<" ";
    fout<<"\n";
}
    return 0;
}