Общий форумCan someone help me to debug this C++ code? It got crash at 19th test-case. #include<cstdio> #include<vector> #include<queue> using namespace std; vector<int> p[1005]; vector<int> idxs[10005]; int dist[10005]; int bef[10005]; queue<int> q; void print(int n) { if(n!=-1) { print(bef[n]); printf("%d ",n); } } int main() { int n,m,c,d,awl,akh,tmp; bool sudah = false; scanf("%d%d",&m,&n); for(c=0;c<=n;c++) { dist[c] = 10000000; bef[c] = -1; } for(c=0;c<m;c++) { scanf("%d",&d); while(d--) { scanf("%d",&tmp); p[c].push_back(tmp); idxs[tmp].push_back(c); } } scanf("%d%d",&awl,&akh); dist[awl] = 0; q.push(awl); while(!q.empty()) { tmp = q.front(); q.pop(); n = idxs[tmp].size(); for(c=0;c<n;c++) { m = p[idxs[tmp][c]].size(); for(d=0;d<m;d++) if(p[idxs[tmp][c]][d]!=tmp) if(dist[p[idxs[tmp][c]][d]] > dist[tmp]+1) { dist[p[idxs[tmp][c]][d]] = dist[tmp] + 1; bef[p[idxs[tmp][c]][d]] = tmp; if(p[idxs[tmp][c]][d]==akh) sudah = true; else q.push(p[idxs[tmp][c]][d]); } if(sudah) break; } if(sudah) break; } if(dist[akh]==10000000) { printf("-1\n"); return 0; } printf("%d\n",dist[akh]); print(akh); printf("\n"); return 0; } Thanks in advance. Your arrays are too small You can also solve it using less than 2 MBs and less than 0.1 seconds :) 32 MB MLE should be even increased because Java and C# coders exist in the world. Nevertheless Timus loves small MLEs for some unknown reasons. But why TLE is 3.0 seconds? 1.0 seconds should be enough. I'm worn out already. Lasarus compiled code. FPC version - 2.6.0. Get the right result. Send in the check, get - Wrong answer (test 3). var c: char; i:longint; l:extended; numbers: array of extended; begin l:=-1; SetLength(numbers,0); while not Eof do begin Read(c); if c in ['0'..'9'] then begin if l=-1 then l:=Ord(c)-48 else l:=l*10+Ord(c)-48; end else begin if l<>-1 then begin SetLength(numbers,length(numbers)+1); numbers[length(numbers)-1]:=sqrt(l); end; l:=-1; end; end; for i:=length(numbers)-1 downto 0 do writeln(numbers[i]:0:4); end. I think couldn't use array to save numbers because size of array not enought to contain. I think on your computer get the correct result because file input data only contain some value, not too much #include <iostream> #include <fstream> #include <iomanip> #include <cmath> using namespace std; int main() { ifstream f("input.txt"); ofstream o("output.txt"); int n=0; long double a[100]; long double k; f >> k; while(!f.eof()) { a[n] = sqrt(k); n++; f >> k; } for ( int i=n-1 ; i >= 0 ; i-- ) o << fixed << setprecision(4) << a[i] << endl; return 0; } size of array a = 100 is not enought to solve a[i]. size of array maybe more than 1 million double :D I think so Edited by author 25.06.2012 21:43 size of array maybe more than 1 million double :D I think so size of array maybe not more than 131072 values size of array maybe not more than 131072 values So that couldn't use array to save Memory limit exceeded #7 time = 1.312 Memory used = 20 620 KB. Why memory used too much???? :(( #include <iostream> #include <math.h> #include <iomanip> #include <string> #include <sstream> using namespace std; string d2s(double x) { stringstream ii; ii << fixed << setprecision(4) << x; return ii.str(); } int main( ) { double n; string s = ""; while ((cin >> n)) { s = d2s(sqrt(n)) + "\n" + s; } cout << s;
return 0; } #include<math.h> #include<stdio.h> int main() { double arr[1000]; int i=0,count=0; while(scanf("%lf",&arr[i])) { arr[i] = sqrt(arr[i]); i++; count++; } for(i=count-1;i>=0;i--) printf("\n %0.4lf",arr[i]); return 0; } size of a = 1000 is not enought -> index was out of size size of a maybe more than. Because file size = 256 KB. Unlimit A[i]. So that use array here is difficut to solve this problem! here is my code: #include <stdio.h> int total(int count, int sum) { int result = 0; if( count > 1 ) { int i=0; for (; i <= 9; i++) { result += total(count - 1, sum - i); } } else { return sum < 10 && sum >= 0 ? 1 : 0; } return result; } int main () { unsigned a; int n,i,c,b;
scanf("%ud", &a); b = a / 2 * 9; for(i=0; i <= b; i++ ) { c = total(a/2, i); n += c * c; } printf("%d", n);
return 0; } i tried 2,4,6,8,the result is correct,but when i submit my solution,[wrong answer] is returned... Edited by author 02.09.2011 14:58 i got it,in visual stdio 2010,auto variable must initialized before used Hello Smart :D, Can you please explain how does your algorithm works? I find out that the summation of digits is multiplication of 9 at most; but why (c* c) ???? ( please explain the whole intention behind your algo as well. Thanks Here is the correct code #include <stdio.h> int total(int count, int sum) { int result = 0; if( count > 1 ) { int i=0; for (; i <= 9; i++) { result += total(count - 1, sum - i); } } else { return sum < 10 && sum >= 0 ? 1 : 0; } return result; } int main () { unsigned a; int n = 0; int i,c,b; scanf("%ud", &a); b = a / 2 * 9; for(i=0; i <= b; i++ ) { c = total(a/2, i); n += c * c; } printf("%d", n); return 0; } Here is the correct code In first line trochee, and iamb both has. why no "not a poem" ? Now, I understand, very easy :) Edited by author 15.02.2012 19:38 #include<stdio.h> int main(void){ int n,m; unsigned int k; scanf("%d %d",&n,&m); k=0; while(n>2 && m>2){ k=k+4; n-=2; m-=2; } if(n==2 && m>=2) k=k+2; else if(m==2 && n>2) k=k+3; else if(m==1 && n>1) k++; printf("%d",k); return 0; } Please check my submission 4323013. It gets AC although to the input 4 3 1 2 2 4 4 3 it answers 1 2 4 when the right answer is clearly 2 1 2 3 4 Your test was added, 15 authors lost AC. Edited by author 23.06.2012 12:37 Используйте связные списки вместо массивов и не надо будет никаких извращений с предпроверкой считываемых символов. Более того, пройдет даже с использованием cin cout вместо scanf printf. I solved with stack))) time - 0.078 seconds)))) I used only one string. Nothing else. Does anybody has any idea? Me too and I dont think I'm wrong Send your email. I help you. use DFS with memorization I don't understand the 2nd sample test. Is a player go on the path from where the othe player reach? Or are the players move on different path? Each player starts from the point that the other player selects. Here's an explanation for the 2nd test 1 / \ 2 3 / \ / \ 4 5 6 7 - + + 0 If the first player chooses the 2 node, the second player will choose the 4th and win. If the first player chooses the 3 node, the second player will choose the 7th node (he prefers to draw than to loose) So, the player will choose the 3rd node, as he prefers drawing rather than loosing. Edited by author 22.06.2012 16:52 abcd aaaaabcdaaaa ans : a a a a abcd a a a a When I print answers,I print them from the end))) Sorry for my poor English)))) Edited by author 22.06.2012 15:47 |
|