![]() |
| | #1 (permalink) | ||
| Problema c++
Mi-am propus sa calculez o suma de puteri, mai exact S5(n)=1/12*[n2*(n+1)2*(2*n2+2*n-1)]=1/12*(2*n6+6*n5+5*n4-n2). Bun, pana aici toate bune si frumoase, cand scriu programul da insa erori la compilare, iar sincer la ora asta chiar nu stiu ce sa-i mai fac. Daca exista cineva care are vreo solutie, solutia e binevenita. [codebox]#include <iostream> #include <fstream> #include <vector> #include <algorithm> const MAX_NO=20; using namespace std; typedef pair<long, long> TFraction; long cmmdc(long a, long b ){ long r; while(b!=0){ r=a%b; a=b; b=r; } return a; } void simplifyFraction(TFraction &f){ long d; d=cmmdc(f.first, f.second); if(!d){ cout<<"Eroare!!! Impartire la 0!"; return; } f.first /=d; f.second /=d; if(f.second<0){ f.second *=-1; f.first *=-1; } } TFraction sumFractions(TFraction& f1, TFraction& f2){ TFraction f; simplifyFraction(f1); simplifyFraction(f2); f.first=f1.first*f2.second+f1.second*f2.first; f.second=f1.second*f2.second; simplifyFraction(f); return f; } long lcmDenomin(TFraction fr[MAX_NO], short n){ long m; short isM; vector<long> v; for(int i=0; i<n; i++){ if(fr[i].first!=0) v.push_back(fr[i].second); } sort(v.begin(), v.end()); if(v.size()<1) return 1; m=v[v.size()-1]; isM=0; while(!isM){ isM=1; for(size_t i=1; i<v.size() && isM; i++) if(m%v[i] !=0){isM=0; break;} m++; } m--; return m; } void genCombinations(long C[MAX_NO+2][MAX_NO+2], short n){ short i,j; C[0][0]=1; for(i=1; i<=n; i++){ C[i][0]=C[i][i]=1; for(j=1; j<i; j++) C[i][j]=C[i-1][j-1]+C[i-1][j]; } } int main(){ long C[MAX_NO+2][MAX_NO+2]; long A[MAX_NO+1][MAX_NO+2]; long M[MAX_NO+1], Q; short k, p, t, r, sign; TFraction fr[MAX_NO+2], W[MAX_NO+1], fAux, fAux2; genCombinations(C, MAX_NO+1); ifstream in("psum.in"); ofstream out("psum.out"); M[0]=1; A[0][1]=1; A[0][0]=0; for(p=1; p<=MAX_NO; p++){ sign=1; for(t=p-1; t>=0; t--){ W[t].first=sign*C[p+1][p+1-t]; W[t].second=M[t]; sign *=-1; simplifyFraction(W[t]); } for(t=p; t>=0; t--){ fAux.first=0; fAux.second=1; for(r=p-1; r>=t-1 && r>=0; r--){ fAux2.first=W[r].first*A[r][t]; fAux2.second=W[r].second; simplifyFraction(fAux2); fAux=sumFractions(fAux, fAux2); } fr[t]=fAux; } fAux2.first=W[0].first*A[0][0]; fAux2.second=W[0].second; simplifyFraction(fAux2); fr[0]=sumFractions(fr[0], fAux2); fr[p+1].first=1; fr[p+1].second=1; Q=lcmDenomin(fr, p+1); M[p]=(p+1)*Q; for(t=p+1; t>=0; t--){ fAux2.first=fr[t].first*Q; fAux2.second=fr[t].second; simplifyFraction(fAux2); A[p][t]=fAux2.first; } } while(in && !in.eof() && in>>k){ out<<M[k]<< " "; for(t=k+1; t>=0; t--){ out<<A[k][t]<< " "; } out<< endl; } return 0; }[/codebox] | |||
|
| | #2 (permalink) | ||
|
Pai, scrii ditai codul si nu specifici si eroarea pe care o primesti? ![]() La o privire fugara, vad ca n-ai declarat tipul constantei. const int MAX_NO=20;
__________________ Nashpa... "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." (Douglas Adams) | |||
|
| | #3 (permalink) | ||
|
Erorile sunt Could not find a match for 'std:: pair<long,long>:: pair()' Cannot find default constructor to initialize array element of type 'std:: pair<long,long>:: pair()' Could not find a match for 'std:: pair<long,long>:: pair()' Practic eroarea pare a fi in zona asta a programului, dar nu vad ce trebuie schimbat: TFraction sumFractions(TFraction& f1, TFraction& f2){ TFraction f; simplifyFraction(f1); simplifyFraction(f2); f.first=f1.first*f2.second+f1.second*f2.first; f.second=f1.second*f2.second; simplifyFraction(f); return f; } | |||
|
| | #4 (permalink) | ||
|
Mie azi-noapte mi l-a compilat in DevCpp dupa ce am facut modificarea de mai sus (const int..). N-am reusit sa-l testez, pentru ca nu stiam ce trebuie sa pun in psum.in si imi era foarte somn, dar de compilat il compila fara probleme.
__________________ Nashpa... "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." (Douglas Adams) | |||
|
![]() |
|
| Thread Tools | |
|
|