当前位置: 首页 > article >正文

c++ 小案例:判断质数猜数字用符号填补心形图案

文章目录

  • 判断质数
  • 猜数字
  • 用符号填补心形图案

判断质数

#include<iostream>

using namespace std;

bool isprime(int num)
{
    int i = 2;
    while(i < num)
    {
        if (num % i == 0)
        {
            return false;
        }
        ++i;
    }
    return true;
}

int main()
{
    cout << "请输入一个不超过20亿的自然数" << endl;
    int num;
    cin >> num;

    

    if(isprime(num))
    {
        cout << "是质数" << endl;
    }
    else
    {
        cout << "不是质数" << endl;
    }

    cin.get();
    cin.get();
}

猜数字

#include<iostream>
#include<ctime>

using namespace std;

int rand_int()
{
    srand(time(0));
    int random_num = rand() % 100;
    return random_num;
}



bool game_progress(int random_num)
{
    int guess_num;
    int i = 5;
    int low = 0, high = 100;
    while(i > 0)
    {
        cout << "请输入一个" << low <<"~" << high <<"的数字。" << "你还有" << i << "次机会"<< endl;
        cin >> guess_num;
        if(guess_num == random_num)
        {
            return true;
        }
        else if(guess_num > random_num)
        {
            high = guess_num;
        }
        else
        {
            low = guess_num;
        }
        --i;
    }
    return false;
}

void print_result(bool result, int random_num)
{
    if(result)
    {
        cout << "恭喜你答对了" << endl;
    }
    else
    {
            cout << "游戏失败!你的机会已经用完,正确结果是:" << random_num << endl;
    }
}


int main()
{
    int random_num;
    random_num = rand_int();
    cout << "现在已产生一个0~100的数字,请你猜猜看,注意你只有5次机会噢。" << endl;

    bool result;
    result = game_progress(random_num);
    print_result(result, random_num);
}

用符号填补心形图案

#include<iostream>
#include<cmath>


using namespace std;

void draw_heart()
{
    // (x^2 + y^2 - a)^3 - x^2 * y^3 = 0
    double a = 1;
    double bound = 1.3 * sqrt(a);
    double y_step = 0.05;
    double x_step = 0.025;

    for(double y = bound; y >= -bound; y -= y_step)
    {
        for(double x = bound; x >= -bound; x -= x_step)
        {
            double result = pow((pow(x, 2) + pow(y, 2) - a), 3) - pow(x, 2) * pow(y, 3);
            if(result > 0)
            {
                cout << " ";
            } 
            else
            {
                cout << "*";
            }
        }
        cout << endl;
    }
    cin.get();  
    cin.get();
}

int main()
{
    draw_heart();
}

http://www.kler.cn/news/107922.html

相关文章:

  • ​Vue3响应式原理
  • git clone失败
  • 《HelloGitHub》第 91 期
  • Flutter笔记:完全基于Flutter绘图技术绘制一个精美的Dash图标(中)
  • FFmpeg5.1.3编译动态库踩坑之旅(基于Linux虚拟机)
  • Ps:对象选择工具
  • 【torch高级】一种新型的概率学语言pyro(01/2)
  • PHP聊天系统源码 在线聊天系统网站源码 后台自适应PC与移动端
  • 2 第一个Go程序
  • 【Git推送本地项目到远程仓库】
  • TSINGSEE青犀省级高速公路视频上云联网方案:全面实现联网化、共享化、智能化
  • 【爬虫】python打包可执行程序(ui界面制作完成后)
  • 服务器感染了.secret勒索病毒,如何确保数据文件完整恢复?
  • python下拉框选择测试
  • 论文阅读——BERT
  • AI新能量!FortiGate NGFW面向数据中心全面集成FortiGuard AI 安全服务
  • Flutter框架实现登录注册功能,不连接数据库
  • ETCD备份与恢复
  • Tomcat的日志接收文件catalina.out nohup.out说明
  • C++编译与运行:其一、静态类型和动态类型
  • Vue--》简易资金管理系统后台项目实战(前端)
  • mac版本 Adobe总是弹窗提示验证问题如何解决
  • Go学习第十三章——Gin入门与路由
  • shell_52.Linux测试与其他网络主机的连通性脚本
  • x210项目重新回顾之十七升级到linux4.19.114 +buildroot2018再讨论
  • 常用adb 命令
  • 【mediasoup-sfu-cpp】4: SfuDemo:join并发布视频创建RTCTransport流程分析
  • [ubuntu系统下的文本编辑器nano,vim,gedit,文件使用,以及版本更新问题]
  • [100天算法】-尽量减少恶意软件的传播(day 45)
  • 搜索与图论:匈牙利算法