fork(3) download
  1. #include <vector>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. //ウラムの螺旋をテキストで出力します
  8. //Ulam's Spiral
  9. //Spiral of Ulam
  10. int main()
  11. {
  12. //ここを変えて違う大きさのを生成
  13. //wとhは奇数、さらに同じでなければダメ
  14. int w=41,h=41;
  15. vector<string> AA(h,string(w,' '));
  16. vector<bool> isPrime(w*h+1,true);
  17. isPrime[0]=false;
  18. isPrime[1]=false;
  19. for(int x=0;x<isPrime.size();x++){
  20. if(isPrime[x]){
  21. for(int y=x+x;y<isPrime.size();y+=x){
  22. isPrime[y]=false;
  23. }
  24. }
  25. }
  26. int x=w/2,y=h/2;
  27. int mukiX=1,mukiY=0,step=0,maxStep=1,num=0;
  28. for(int i=1;i<w*h+1;i++){
  29. if(isPrime[i])AA[y][x]='*';
  30. x+=mukiX;
  31. y+=mukiY;
  32. step++;
  33. if(!(step<maxStep)){
  34. step=0;
  35. num++;
  36. if(mukiX==1){mukiX=0;mukiY=-1;}
  37. else if(mukiY==-1){mukiX=-1;mukiY=0;}
  38. else if(mukiX==-1){mukiX=0;mukiY=1;}
  39. else if(mukiY==1){mukiX=1;mukiY=0;}
  40. if(!(num<2)){
  41. num=0;
  42. maxStep++;
  43. }
  44. }
  45. }
  46. for(auto line:AA){
  47. cout << line << endl;
  48. }
  49. return 0;
  50. }
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
*   *             *   *       *   *      
       *     *   * *   *             *   
  *     * *     *   * *                 *
 *     * *                     *     *   
        * *     *   *           *     *  
                   *   * *   *           
* *               *   *       *   * *    
 *     *   *           * *     *         
* *   * *     * *     *     *           *
 * *           *           *   *     * * 
    *     *   *       *     *            
     *   *         *   * *   * * *       
* * *       *     *         * *   *     *
       *     *   * *               *     
                    * *     *   *   * *  
 *   *   *   * *   *       *   * *       
                    * *   *              
   *       *   * *     * * *     * * *   
*   * * * * * * * *   *       *         *
                   * * *           *     
*             *   *  ** * * *   * * *    
       *       * * *                     
                *   *                    
     *   * *   * *   *   * *   *   * *   
  *     *   *   *     *     * *   *      
                 *           *           
* *         * *     *   *   *       * *  
 * *     *   *           *       *     * 
    *     *     *   * *                  
               * *   *     *   *     *   
    * * * *         * *     *     * *   *
       *   *           * *               
  *   * *     *     *   * *           *  
   * *       *         *       *     *   
              * *   * *         *     *  
           *   *     *     *         * * 
*       * *     *                 *      
 *   *       *           *   *     *     
                              *     *   *
 *   * *   *     *           *           
                *     *   * *