• 简单编程练习


    #include<bits/stdc++.h>
    using namespace std;
    
    int main()
    {
        srand((unsigned)time(NULL));
        for(int i = 0;i < 10;i++)
        {
            int n = 0 + rand() % 101;
            int m = 0 + rand() % 101;
            int t = 0 + rand() % 2;
            cout << n;
            if(t)
                cout << "+";
            else
                cout << "-";
            cout << m << endl; 
        }
        return 0;
    }

    #include<bits/stdc++.h>
    using namespace std;
    
    int main()
    {
        srand((unsigned)time(NULL));
        for(int i = 0;i < 10;i++)
        {
            int sum,tt,kk;
            int n = 0 + rand() % 101;
            int m = 0 + rand() % 101;
            int p = 0 + rand() % 101;
            int t = 0 + rand() % 2;
            int k = 0 + rand() % 2;
            cout << n;
            if(t)
            {
                cout << "+";
                tt = 1;
            }
            else
            {
                cout << "-";
                tt = 0;
            }
            cout << m;
            if(tt == 1) sum = n + m;
            else sum = n - m;
            if(k)
            {
                cout << "+";
                kk = 1;
            }
            else
            {
                cout << "-";
                kk = 0;
            }
            cout << p << "=";
            if(kk == 1) sum += p;
            else sum -= p;
            cout << sum << endl;
        }
        return 0;
    }

    利用stringstream

       添加头文件 #include<sstream>

       数字转字符串

       #include <string>

      #include <sstream>

      int main(){
        double a = 123.32;
        string res;
        stringstream ss;          定义流ss
        ss << a;                       将数字a转化成流ss
        ss >> res;                    将流ss转化成字符串
        return 0;
      }

       字符串转数字

      #include <string>

      #include <sstream>

      int main(){
        double a ;
        string res= "123.32";
        stringstream ss;  
        ss << res;                  
        ss >> a;
        return 0;
      }

    资源地址:https://www.cnblogs.com/houchen/p/8984164.html

     以下的代码可以实现如若出现重复的随机数,如出现两个55+5-5=50,则可以筛选出来一个,只保留输出一个

    #include<bits/stdc++.h>
    using namespace std;
    string str1[11],str2[11];
    
    void add()
    {
        srand((unsigned)time(NULL));
        for(int i = 0;i < 10;i++)
        {
            
            int sum,tt,kk;
            int n = 0 + rand() % 101;
            int m = 0 + rand() % 101;
            int p = 0 + rand() % 101;
            int t = 0 + rand() % 2;
            int k = 0 + rand() % 2;
            
            string nnn;
            stringstream nnn1;
            nnn1 << n;
            nnn1 >> nnn;
            str1[i] = str1[i].append(nnn);
    //        cout << str1[i] << "PPP" << endl;
    //        cout << n;
            if(t)
            {
    //            cout << "+";
                tt = 1;
                str1[i] = str1[i].append("+");
    //            cout << str1[i] << "PPP" << endl;
            }
            else
            {
    //            cout << "-";
                tt = 0;
                str1[i] = str1[i].append("-");
            }
            string mmm;
            stringstream mmm1;
            mmm1 << m;
            mmm1 >> mmm;
            str1[i] = str1[i].append(mmm);
    //        cout << m;
            if(tt == 1) sum = n + m;
            else sum = n - m;
            if(k)
            {
                str1[i] = str1[i].append("+");
    //            cout << "+";
                kk = 1;
            }
            else
            {
                str1[i] = str1[i].append("-");
    //            cout << "-";
                kk = 0;
            }
            string ppp;
            stringstream ppp1;
            ppp1 << p;
            ppp1 >> ppp;
            str1[i] = str1[i].append(ppp);
            str1[i] = str1[i].append("=");
    //        cout << p << "=";
            if(kk == 1) sum += p;
            else sum -= p;
            string summm;
            stringstream summm1;
            summm1 << sum;
            summm1 >> summm;
            str1[i] = str1[i].append(summm);
    //        cout << sum << endl;
    //        cout << str1[i] << "LLLLLLLLL" << endl;
        }
        
        //前面不输出,在这里输出。 
        //在这里遍历是否有重复出现的。 
        int flag = 0;
        for(int i = 0;i < 10;i++)
        {
            for(int j = i + 1;j < 10;j++)
            {
                if(str1[i] == str1[j])
                {
                    flag = 1;
                    break;
                }
            } 
            if(flag == 1)
            {
                flag = 0;
                continue;
            }
            else
                cout << str1[i] << endl;
        }
    }
    
    int main()
    {
        add();
        return 0;
    }

  • 相关阅读:
    npm 插件发布和使用
    git 配置与删除远程地址
    elment 编辑输出行数据后,过滤下拉,值必须全等于下拉值
    后台 接口 传值 不同类型的详细解说
    Vue vscode调试
    vue 标题和输入框分两行,调成一行
    ES6 学习笔记(持续更新中)
    vue开发WebApp 开发笔记(持续更新)
    移动端自适应方案 对应设计图制作
    css 颜色使用预览 码表
  • 原文地址:https://www.cnblogs.com/biaobiao88/p/12416442.html
Copyright © 2020-2023  润新知