简单编程练习

 

#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;
}

 

 

posted @ 2020-03-04 21:51  恶魔岛  阅读(177)  评论(0编辑  收藏  举报