zoukankan      html  css  js  c++  java
  • 控制台画圆

      书上看到一个画空心圆的题,于是用C++写了一个实心圆...

    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    class DrawCircle {
    public:
    	void drawCircle(int r) {
    		int x, y;
    		int params_1 = 2;
    		int params_2 = 5;
    
    		for (y = 0; y < r; y++) {
    			x = -(int)sqrt(2 * y * r - y * y) + r;
    			for (int i = 0; i < params_1*x; i++)
    				cout << ' ';
    			cout << '*';
    			for (int i = ((int)sqrt(2 * y * r - y * y) + r - x)*params_1 + params_2; i >= 0; i--)
    				cout << (i % 2 == 0 ? '*' : i % 3 == 0 ? '$' : '%');
    			cout << '*' << endl;
    		}
    
    		for (; y >= 0; y--) {
    			x = -(int)sqrt(2 * y * r - y * y) + r;
    			for (int i = 0; i < params_1*x; i++)
    				cout << ' ';
    			cout << '*';
    			for (int i = ((int)sqrt(2 * y * r - y * y) + r - x)*params_1 + params_2; i >= 0; i--)
    				cout << (i % 2 == 0 ? '*' : i % 3 == 0 ? '$' : '%');
    			cout << '*' << endl;
    		}
    	}
    };
    
    int main()
    {
    	DrawCircle dc;
    	int r = 12;
    
    	dc.drawCircle(r);
    	getchar();
        return 0;
    }
    

      运行结果:

      更简洁一点的代码:

    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    class DrawCircle {
    public:
    	void drawCircle(int r) {
    		int x, y;
    		int i;
    		double params_1 = 2.5;
    
    		for (y = 10; y >= -r; y--) {
    			x = (int)sqrt(r * r - y * y);
    			for (i = 0; i < 30 - params_1 * x; i++)
    				cout << ' ';
    			cout << '*';
    			for (; i < 30 + params_1 * x; i++)
    				cout << (i % 2 == 0 ? '*' : i % 3 == 0 ? '$' : '%');
    			cout << '*' << endl;
    		}
    	}
    };
    
    int main()
    {
    	DrawCircle dc;
    	int r = 10;
    
    	dc.drawCircle(r);
    	getchar();
        return 0;
    }
    

      运行截图:

  • 相关阅读:
    HL7及PIX相关的测试工具
    HDU4570----Multi-bit Trie----简单的DP
    hdu2248
    poj 3693 Maximum repetition substring (后缀数组)
    高性能通道
    volyaire重振Infiniband
    利用iWARP/RDMA解决以太网高延迟
    linux 单网卡来绑定多IP实现多网段访问以及多网卡绑定单IP实现负载均衡
    C细节学习
    每2秒获取系统的赋值及内存使用率
  • 原文地址:https://www.cnblogs.com/darkchii/p/8437002.html
Copyright © 2011-2022 走看看