Advertisement
Guest User

Untitled

a guest
Mar 14th, 2015
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<algorithm>
  5. #include<vector>
  6. #include<string>
  7. #include<set>
  8. #include<map>
  9. #include<cmath>
  10. #include<memory.h>
  11. #include <unistd.h>
  12. #include <wait.h>
  13. using namespace std;
  14. typedef long long ll;
  15. const int __limitofthreads = 4;
  16. vector<pid_t> __ids;
  17. char __str[256];
  18.  
  19. const int N = 1e6+6;
  20.  
  21.  
  22.  
  23. void solve(int test){
  24.     /*+++start read data+++*/
  25.    
  26.     int r;
  27.     cin>>r;
  28.    
  29.     /*---end read data---*/
  30.     if(__ids.size()>=__limitofthreads) wait(0);
  31.     pid_t __id = fork();
  32.     if(__id>0){
  33.         __ids.push_back(__id);
  34.         return ;
  35.     }else{
  36.         sprintf(__str, "thread%d.out", test);
  37.         freopen(__str,"w",stdout);
  38.     }
  39.     /*+++start solution and write output+++*/
  40.    
  41.     int res = 0;
  42.     for(int x=0;x<=r;++x)
  43.     for(int y=0;y<=r;++y) if(x*x+y*y<=r*r) ++res;
  44.     cout<<"Case #"<<test<<": "<<res<<endl;
  45.    
  46.     /*---end solution---*/
  47.     sprintf(__str, "test #%d ok.\n", test);
  48.     cerr<<__str;
  49.     exit(0);
  50. }
  51.  
  52. int main(){
  53.     freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout);
  54.    
  55.     int t;
  56.     cin>>t;
  57.     for(int i=1;i<=t;++i){
  58.         solve(i);
  59.     }
  60.    
  61.     /*combining outputs*/
  62.     for(pid_t __id : __ids) waitpid(__id,0,0);
  63.     int __bufsize = 1<<16;
  64.     char *__buf = new char[__bufsize];
  65.     for(int i=1;i<=t;++i){
  66.         sprintf(__str, "thread%d.out", i);
  67.         FILE *f = fopen(__str, "r");
  68.         while(fgets(__buf, __bufsize, f)) printf("%s",__buf);
  69.         fclose(f);
  70.         remove(__str);
  71.     }
  72.    
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement