• 个人简介

    如何设置《密码》从入门到精通

    有用的小网站

    《 小 学 数 学 题 》

    《 御 坂 妹 妹 》

    《 超 电 磁 炮 》

    A0558

    A0582

    3712

    tycpc7

    P4147

    P3695

    BY01

    int 数组最大值:536698431 还是我一个个试出来的

    ——————————打字慢 必备 :dev c-- 反而更慢了

    #include<iostream>
    #include<windows.h>
    #include<conio.h>
    #include<string>
    #include<fstream>
    using namespace std;
    int h[1005],hh[1000]={4},h2=3;
    int fs;
    ///////////各种各样的初始化 
    string coutt[15]={"0","\n1.定义变量\n","2.输入/输出\n","3.循环\n","4.删除代码\n","5.if else\n","6.自定义\n","7.切换行(也可以按↑ ↓)\n","8.新增头文件\n","9.调用函数\n","s.保存代码\n","o.读取代码\n","z.撤回\n","x.恢复\n","c.编译\n"};
    string s[1005][1000]={"0","#include<iostream>","using namespace std;","int main(){","}"},sr;
    int ss[1005][1000];//存代码缩进 
    
    void ini2(){// 初始化代码
    	s[1][0]="#include<iostream>";
    	s[2][0]="using namespace std;";
    	s[3][0]="int main(){";
    	s[4][0]="}";
    	ss[1][0]=0;
    	ss[2][0]=0;
    	ss[3][0]=-1;
    	ss[4][0]=-100;
    }
    void ini(){
    	hh[fs]=hh[fs-1];
    	for(int i=1;i<=hh[fs];i++){
    		s[i][fs]=s[i][fs-1];// 复制上一层的代码
    		ss[i][fs]=ss[i][fs-1];
    	}
    }
    string tj[10]={"0","iostream","algorithm","cmath","cstring","cstdio","iomanip","string","bits/stdc++.h"};
    string f[10]={"0","sort","memset"};
    ///////////// 
    
    ///////////////自动缩进与输出 
    void k2(int i){
    	if(ss[i][fs]>0){
    		for(int j=1;j<=ss[i][fs];j++){
    			cout<<"    ";
    		}
    	}else if(ss[i][fs]>-100){
    		for(int j=1;j<abs(ss[i][fs]);j++){
    			cout<<"    ";
    		}
    	}else{
    		for(int j=1;j<=abs(ss[i][fs]+100);j++){
    			cout<<"    ";
    		}
    	}
    }
    void sf(int h2){//定义缩进等级 
    	int a=0;
    	for(int i=h2-1;i>=1;i--){
    		
    		if(ss[i][fs]<0&&ss[i][fs]>-100){
    			a--;
    			if(a==-1){
    				ss[h2][fs]=abs(ss[i][fs]);
    				break;	
    			}
    
    		}
    		if(ss[i][fs]<=-100)a++;
    	}
    }
    void sff(int h2){//大括号缩进处理 
    	int a=0;
    	for(int i=h2-1;i>=1;i--){
    		if(ss[i][fs]<=-100)a++;
    		if(ss[i][fs]>-100&&ss[i][fs]<0){
    			if(a==0){
    				ss[h2][fs]=ss[i][fs]-1;
    				ss[h2+1][fs]=ss[h2][fs]-99;
    				break;
    			}else a--;
    		}
    	}
    }
    ///////////// 
    
    void cr(int h2,int x){//插入 
    	hh[fs]+=x;
    	for(int i=hh[fs];i>h2;i--){
    		s[i][fs]=s[i-x][fs];
    		ss[i][fs]=ss[i-x][fs];
    	}
    }
    void sc(int h2,int l){//删除 
    	for(int j=h2;j<hh[fs];j++){
    		s[j][fs]=s[j+l-h2+1][fs];
    		ss[j][fs]=ss[j+l-h2+1][fs];
    	}
    	hh[fs]-=(l-h2+1);h2-=(l-h2+1);
    }
    string j(string sr){//贴心的加上分号 
    	if(sr[sr.size()-1]!=';'&&sr[sr.size()-1]!='{')sr+=";";
    	return sr;
    }
    char ch;
    int p,fc;
    bool z;
    void k(int a){
    	int c=3;
    	while(a!=0){
    		a/=10;
    		c--;
    	}
    	for(int i=1;i<=c;i++)cout<<" ";
    }
    void out(){//输出 
    	for(int i=1;i<=hh[fs];i++){
    		cout<<h[i];
    		k(i);k2(i);
    		HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    		SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN); // 设置文字颜色为绿色
    		if(s[i][fs][0]=='#')cout<<s[i][fs]<<"\n",SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    		else{
    			SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // 重置为默认颜色
    			cout<<s[i][fs]<<"\n";
    		}
    		
    	}
    	cout<<"当前行:"<<h2<<"\n";
    	for(int i=1;i<=14;i++)cout<<coutt[i];
    }
    // 键盘输入处理函数
    void ax(){
    	if(_kbhit()){
    		system("cls");
        	ch=_getch();
            if(ch==72)if(h2>1)h2--;
            if(ch==80)if(h2<hh[fs])h2++;
            if(ch>=48&&ch<=57)p=ch-48,z=true;
            if(ch>='a'&&ch<='z')p=ch,z=true;
       		out();
        }
    }
    
    ////////////////自动保存功能 
    bool bAutoSave=false; // 自动保存开关 
    bool bAutoLoad=false; // 自动读取开关 
    
    void save_to(){      // 保存代码 
    	const char* path="用代码写的代码.cpp";  // 保存文件名字 
    	ofstream fout(path);
    	if(fout.is_open()){
    		for(int i=1;i<=hh[fs];i++){
    			fout<<s[i][fs]<<endl;
    		}
    		fout.close();
    	}else{
    		cerr<<"保存文件失败"<<endl; 
    	}
    	
    	const char* inden="缩进信息.txt";
    	fout.open(inden);
    	if(fout.is_open()){
    		for(int i=1;i<=hh[fs];i++){
    			fout<<ss[i][fs]<<endl;
    		}
    		fout.close();
    	}else{
    		cerr<<"保存文件失败"<<endl; 
    	}
    }
    
    void load_from(){   // 读取代码 
    	const char* path="用代码写的代码.cpp";  // 要读取的文件的名字
    	string line;
    	ifstream fin(path);
    	if(fin.is_open()){
    		int cnt=0;
    		while(getline(fin,line)){
    			cnt++;
    			s[cnt][fs]=line;
    		}
    		fin.close();
    	}
    	
    	const char* inden="缩进信息.txt";
    	int number;
    	fin.open(inden);
    	if(fin.is_open()){
    		hh[fs]=0;
    		while(fin>>number){
    			hh[fs]++;
    			ss[hh[fs]][fs]=number;
    		}
    		h2=hh[fs];
    		fin.close();
    	}
    }
    
    struct CodeLS{
    	CodeLS(){
    		if(bAutoLoad){
    			load_from();
    		}
    	}
    	~CodeLS(){
    		if(bAutoSave){
    			save_to();
    		}
    	}
    }code_ls;
    
    ////////////////
    bool aa;
    
    /////////////编译功能
    void bracket(){//检查括号 
    	int l=0,r=0;
    	for(int i=1;i<=hh[fs];i++){
    		for(int j=0;j<s[i][fs].size();j++){
    			if(s[i][fs][j]=='('||s[i][fs][j]=='['||s[i][fs][j]=='{')l++;
    			if(s[i][fs][j]==')'||s[i][fs][j]==']'||s[i][fs][j]=='}')r++;
    		}
    	}
    	if(l!=r)cout<<"[Error]你的代码似乎缺了括号。。。\n";
    	else cout<<"编译成功!\n";
    	Sleep(1000);
    } 
    /////////////
    int main(){
    	ini2();
    	for(int i=1;i<=105;i++)h[i]=i;
    	fs=0;
    	while(true){
    		fs++;if(fc!=0&&aa==true)fc--;
    		if(aa==false)ini();
    		aa=false;
    		if(h2>hh[fs])h2=hh[fs];
    		if(h2<1)h2=1;	
    		out();
    		while(true){
    			ax();
    			if(z==true){
    				z=false;
    				break;
    			}
    		}
    		string bl;
    		int x;
    		system("cls");
    
    		for(int i=1;i<=hh[fs];i++){
    			cout<<h[i];
    			k(i);k2(i);
    			HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    			SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN); // 设置文字颜色为绿色
    			if(s[i][fs][0]=='#')cout<<s[i][fs]<<"\n",SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    			else{
    				SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // 重置为默认颜色
    				cout<<s[i][fs]<<"\n";
    			}
    		}
    		cout<<"当前行:"<<h2<<"\n";
    		cout<<coutt[p];
    		if(fs==1)cout<<"全部输入0返回\n"; 
    		if(p==7){
    			int g;
    			cout<<"切换到...\n";
    			cin>>g;
    			if(g!=0)h2=g;
    			else {system("cls");continue;}
    		}
    		if(p==1){
    			cout<<"类型 名称\n";
    			cin>>sr>>bl;
    			if(sr=="0"&&bl=="0"){system("cls");continue;}
    			cr(h2,1);sf(h2);
    			s[h2][fs]=sr+" "+bl;
    			s[h2][fs]=j(s[h2][fs]);h2++;
    		}
    		if(p==3){
    			cout<<"1.for\n2.while\n";
    			cin>>x;
    			if(x==0){system("cls");continue;}
    			if(x==1){
    				string b,e,d,m;
    				cout<<"变量名 起始 终止 +/-\n";
    				cin>>m>>b>>e>>d;
    				sr="for(int "+m+"="+b+";"+m;
    				if(d=="+")sr+="<="+e+";"+m+"++";
    				else sr+=">="+e+";"+m+"--";
    			}else{
    				cout<<"条件\n";
    				cin>>bl;
    				sr="while("+bl;
    			}
    			cr(h2,2);sf(h2);sff(h2);
    			s[h2][fs]=sr+"){";
    			s[h2+1][fs]="}";h2++;
    		}
    		string x2;
    		if(p==2){
    			cout<<"1.输入 2.输出\n变量\n";
    			cin>>bl>>x2;
    			
    			if(bl=="0"&&x2=="0"){system("cls");continue;}
    			if(bl=="1")sr="cin>>"+x2+";";
    			else sr="cout<<"+x2+";";
    			cr(h2,1);sf(h2);
    			s[h2][fs]=sr;h2++;
    		}
    		if(p==4){
    			cout<<"1.修改 2.删除\n";
    			cin>>x;
    			if(x==0){system("cls");continue;}
    			if(x==1){
    				getline(cin,bl);
    				getline(cin,x2); 
    				s[h2][fs]=x2;
    			}else{
    				int l;
    				cout<<"结束行";
    				cin>>l;
    				sc(h2,l);h2-=abs(h2-l);
    			}
    		}
    		if(p==5){
    			cout<<"1.if; 2.else if; 3.else;\n";
    			cin>>x;
    			if(x==0){system("cls");continue;}
    			if(x==1){
    				cout<<"条件\n"; 
    				cin>>bl;
    				sr="if("+bl+"){";
    			}
    			if(x==2){
    				cout<<"条件\n"; 
    				cin>>bl;
    				sr="else if("+bl+"){";				
    			}
    			if(x==3)sr="else{";
    			cr(h2,2);sf(h2);sf(h2+1);
    			s[h2][fs]=sr;
    			s[h2+1][fs]="}";h2++;
    		}
    		sr='-';
    		if(p==6){
    			cout<<"代码\n";
    			while(sr.size()<2)getline(cin,sr);//防止读入回车 
    			if(sr=="0"){system("cls");continue;}
    			sr=j(sr);
    			cr(h2,1);sf(h2);
    			s[h2][fs]=sr;h2+=1;
    		}
    		if(p==8){
    			for(int i=1;i<=8;i++)cout<<i<<"."<<tj[i]<<"\n";		
    			cin>>x;
    			if(x==0){system("cls");continue;}
    			cr(1,1);sf(1);
    			sr="#include<"+tj[x]+">";
    			s[1][fs]=sr;h2++;
    		}
    		if(p==9){
    			cout<<"你要调用哪个函数?\n";
    			for(int i=1;i<=2;i++)cout<<i<<"."<<f[i]<<"\n";
    			cin>>x;sr=f[x]+"(";
    			if(x==0){system("cls");continue;}
    			cout<<"数组名:";
    			cin>>bl;
    			if(x==1){
    				string s,e;
    				cout<<"起始 终止:";
    				cin>>s>>e;
    				sr+=bl+"+"+s+","+bl+"+"+e+"+1);";
    			}
    			if(x==2){
    				string a;
    				cout<<"遍历数:";
    				cin>>a;
    				sr+=bl+","+a+",sizeof("+bl+"));";
    			}
    			cr(h2,1);sf(h2);sff(h2);
    			s[h2][fs]=sr;h2++;
    		}
    		if(p=='o')load_from();
    		if(p=='s')save_to();
    		if(p=='z'){
    			if(fs>=2)fs-=2;
    			fc+=2;
    		}
    		if(p=='x'){
    			if(fc!=0){
    				aa=true;
    				fc--;
    			}
    		}
    		if(p=='c'){
    			bracket();
    		}
    		system("cls");
    	}
    }
    
    

    Deepseek写的:

    
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <stack>
    #include <cmath>
    #include <map>
    #include <stdexcept>
    using namespace std;
    
    class Fraction {
    private:
        long long numerator;
        long long denominator;
    
        long long gcd(long long a, long long b) {
            return b == 0 ? a : gcd(b, a % b);
        }
    
        void reduce() {
            if (denominator < 0) {
                numerator = -numerator;
                denominator = -denominator;
            }
            long long common = gcd(abs(numerator), abs(denominator));
            numerator /= common;
            denominator /= common;
        }
    
    public:
        Fraction(long long n = 0, long long d = 1) : numerator(n), denominator(d) {
            reduce();
        }
    
        operator double() const {
            return static_cast<double>(numerator) / denominator;
        }
    
        Fraction operator-() const {
            return Fraction(-numerator, denominator);
        }
    
        friend Fraction operator+(const Fraction& a, const Fraction& b);
        friend Fraction operator-(const Fraction& a, const Fraction& b);
        friend Fraction operator*(const Fraction& a, const Fraction& b);
        friend Fraction operator/(const Fraction& a, const Fraction& b);
        friend Fraction operator^(const Fraction& a, int power);
    
        bool operator==(const Fraction& other) const {
            return numerator == other.numerator && denominator == other.denominator;
        }
    
        bool operator!=(const Fraction& other) const {
            return !(*this == other);
        }
    
        friend ostream& operator<<(ostream& os, const Fraction& f) {
            if (f.denominator == 1) {
                os << f.numerator;
            } else {
                os << "(" << f.numerator << "/" << f.denominator << ")";
            }
            return os;
        }
    };
    
    Fraction operator+(const Fraction& a, const Fraction& b) {
        return Fraction(a.numerator * b.denominator + b.numerator * a.denominator, 
                        a.denominator * b.denominator);
    }
    
    Fraction operator-(const Fraction& a, const Fraction& b) {
        return Fraction(a.numerator * b.denominator - b.numerator * a.denominator,
                        a.denominator * b.denominator);
    }
    
    Fraction operator*(const Fraction& a, const Fraction& b) {
        return Fraction(a.numerator * b.numerator, a.denominator * b.denominator);
    }
    
    Fraction operator/(const Fraction& a, const Fraction& b) {
        return Fraction(a.numerator * b.denominator, a.denominator * b.numerator);
    }
    
    Fraction operator^(const Fraction& a, int power) {
        if (power >= 0) {
            return Fraction(pow(a.numerator, power), pow(a.denominator, power));
        } else {
            return Fraction(pow(a.denominator, -power), pow(a.numerator, -power));
        }
    }
    
    struct LinearExpr {
        Fraction coeff;  // x的系数
        Fraction constant; // 常数项
    
        LinearExpr() : coeff(0), constant(0) {}
        LinearExpr(Fraction c, Fraction k) : coeff(c), constant(k) {}
    };
    
    LinearExpr operator+(const LinearExpr& a, const LinearExpr& b) {
        return {a.coeff + b.coeff, a.constant + b.constant};
    }
    
    LinearExpr operator-(const LinearExpr& a, const LinearExpr& b) {
        return {a.coeff - b.coeff, a.constant - b.constant};
    }
    
    LinearExpr operator*(const LinearExpr& a, const LinearExpr& b) {
        if (a.coeff != Fraction(0) && b.coeff != Fraction(0)) {
            throw runtime_error("检测到二次项,不是一元一次方程");
        }
        return {a.coeff * b.constant + a.constant * b.coeff, a.constant * b.constant};
    }
    
    LinearExpr operator/(const LinearExpr& a, const LinearExpr& b) {
        if (b.coeff != Fraction(0)) {
            throw runtime_error("不能除以含有x的表达式");
        }
        if (b.constant == Fraction(0)) {
            throw runtime_error("除以零");
        }
        return {a.coeff / b.constant, a.constant / b.constant};
    }
    
    LinearExpr operator^(const LinearExpr& a, int power) {
        if (power < 0) throw runtime_error("不支持负指数");
        if (a.coeff != Fraction(0)) {
            throw runtime_error("不能对x进行幂运算");
        }
        return {0, a.constant ^ power};
    }
    
    vector<string> tokenize(const string& expr) {
        vector<string> tokens;
        string num;
        for (size_t i = 0; i < expr.size(); ++i) {
            char c = expr[i];
            if (isdigit(c)) {
                num += c;
            } else {
                if (!num.empty()) {
                    tokens.push_back(num);
                    num.clear();
                }
                if (isspace(c)) continue;
                if (c == 'x') {
                    tokens.push_back("x");
                } else {
                    tokens.push_back(string(1, c));
                }
            }
        }
        if (!num.empty()) {
            tokens.push_back(num);
        }
    
        // 插入隐式乘法
        vector<string> processed;
        for (size_t i = 0; i < tokens.size(); ++i) {
            processed.push_back(tokens[i]);
            if (i < tokens.size() - 1) {
                string current = tokens[i];
                string next = tokens[i+1];
                bool insert = false;
                
                if ((isdigit(current[0]) || current == "x" || current == ")") && 
                    (next == "x" || next == "(" || isdigit(next[0]))) {
                    insert = true;
                }
                if (insert) {
                    processed.push_back("*");
                }
            }
        }
        return processed;
    }
    
    vector<string> toRPN(const vector<string>& tokens) {
        vector<string> output;
        stack<string> ops;
        map<string, int> prec = {{"^", 4}, {"*", 3}, {"/", 3}, {"+", 2}, {"-", 2}, {"(", 1}};
    
        for (auto& token : tokens) {
            if (isdigit(token[0]) || token == "x") {
                output.push_back(token);
            } else if (token == "(") {
                ops.push(token);
            } else if (token == ")") {
                while (!ops.empty() && ops.top() != "(") {
                    output.push_back(ops.top());
                    ops.pop();
                }
                ops.pop();
            } else {
                while (!ops.empty() && prec[ops.top()] >= prec[token]) {
                    output.push_back(ops.top());
                    ops.pop();
                }
                ops.push(token);
            }
        }
    
        while (!ops.empty()) {
            output.push_back(ops.top());
            ops.pop();
        }
        return output;
    }
    
    LinearExpr evaluateRPN(const vector<string>& rpn) {
        stack<LinearExpr> s;
        for (auto& token : rpn) {
            if (isdigit(token[0])) {
                s.emplace(Fraction(0), Fraction(stoll(token), 1));
            } else if (token == "x") {
                s.emplace(Fraction(1), Fraction(0));
            } else {
                if (s.size() < 2) throw runtime_error("无效表达式");
                LinearExpr b = s.top(); s.pop();
                LinearExpr a = s.top(); s.pop();
                
                if (token == "+") s.push(a + b);
                else if (token == "-") s.push(a - b);
                else if (token == "*") s.push(a * b);
                else if (token == "/") s.push(a / b);
                else if (token == "^") s.push(a ^ stoi(token));
                else throw runtime_error("未知运算符: " + token);
            }
        }
        if (s.size() != 1) throw runtime_error("无效表达式");
        return s.top();
    }
    
    void solveEquation(const string& equation) {
        size_t eq_pos = equation.find('=');
        if (eq_pos == string::npos) {
            cout << "无效方程格式" << endl;
            return;
        }
    
        try {
            LinearExpr left = evaluateRPN(toRPN(tokenize(equation.substr(0, eq_pos))));
            LinearExpr right = evaluateRPN(toRPN(tokenize(equation.substr(eq_pos + 1))));
    
            LinearExpr final = left - right;
    
            if (final.coeff == Fraction(0)) {
                if (final.constant == Fraction(0)) {
                    cout << "无穷多解" << endl;
                } else {
                    cout << "无解" << endl;
                }
            } else {
                Fraction solution = (-final.constant) / final.coeff;
                cout << "解:x = " << solution << endl;
            }
        } catch (const exception& e) {
            cout << "错误: " << e.what() << endl;
        }
    }
    
    int main() {
        while (true) {
            cout << "请选择模式:\n1. 表达式计算\n2. 解方程\n其他键退出\n";
            char choice;
            cin >> choice;
            cin.ignore();
    
            if (choice == '1') {
                cout << "请输入表达式(以=结束,例如:2*(3+4)^2/5=):";
                string expr;
                getline(cin, expr, '=');
    
                try {
                    LinearExpr result = evaluateRPN(toRPN(tokenize(expr)));
                    if (result.coeff != Fraction(0)) {
                        cout << "错误:表达式中包含变量x" << endl;
                    } else {
                        cout << "结果:" << endl;
                        cout << result.constant << endl;
                        cout << double(result.constant) << endl;
                    }
                } catch (const exception& e) {
                    cout << "错误: " << e.what() << endl;
                }
                
            } else if (choice == '2') {
                cout << "请输入一元一次方程(例如:2(x+3)=4x-5):";
                string equation;
                getline(cin, equation);
                
                solveEquation(equation);
                
            } else {
                break;
            }
        }
        return 0;
    }
    

    解不等式(组)

    #include <iostream>
    #include <vector>
    #include <string>
    #include <sstream>
    #include <cmath>
    #include <algorithm>
    #include <stdexcept>
    
    using namespace std;
    
    // 表示一个不等式 a*x + b > 0 (或 <, >=, <=, ==)
    struct Inequality {
        double a;   // x的系数
        double b;   // 常数项
        char op;    // 操作符: >, <, >=, <=, ==
        
        Inequality(double a, double b, char op) : a(a), b(b), op(op) {}
    };
    
    // 分数结构体,用于精确表示解
    struct Fraction {
        int numerator;   // 分子
        int denominator; // 分母
        
        Fraction(int num, int den) : numerator(num), denominator(den) {
            simplify();
        }
        
        // 约分
        void simplify() {
            int gcd_val = gcd(abs(numerator), abs(denominator));
            numerator /= gcd_val;
            denominator /= gcd_val;
            
            if (denominator < 0) {
                numerator = -numerator;
                denominator = -denominator;
            }
        }
        
        // 计算最大公约数
        int gcd(int a, int b) {
            return b == 0 ? a : gcd(b, a % b);
        }
        
        // 转换为double
        double toDouble() const {
            return static_cast<double>(numerator) / denominator;
        }
        
        // 输出分数形式
        string toString() const {
            if (denominator == 1) {
                return to_string(numerator);
            }
            return to_string(numerator) + "/" + to_string(denominator);
        }
    };
    
    // 解析不等式字符串
    Inequality parseInequality(const string& input) {
        stringstream ss(input);
        double a = 0, b = 0;
        char op = 0;
        char x;
        
        // 处理左边表达式
        string left;
        getline(ss, left, '<'); // 尝试按 < 分割
        if (left.size() == input.size()) { // 如果没有 <,尝试按 > 分割
            ss.clear();
            ss.str(input);
            getline(ss, left, '>');
            if (left.size() == input.size()) { // 如果没有 >,尝试其他操作符
                ss.clear();
                ss.str(input);
                size_t pos = input.find_first_of("<=>=");
                if (pos == string::npos) {
                    throw invalid_argument("无效的不等式操作符");
                }
                left = input.substr(0, pos);
                op = input[pos];
                if (pos + 1 < input.size() && input[pos+1] == '=') {
                    op = (op == '<') ? '{' : '}'; // 临时表示 <=, >=
                }
            } else {
                op = '>';
            }
        } else {
            op = '<';
        }
        
        // 修正操作符
        if (op == '{') {
            op = 'L'; // 表示 <=
        } else if (op == '}') {
            op = 'G'; // 表示 >=
        }
        
        // 解析左边表达式 ax + b
        stringstream left_ss(left);
        double coeff = 1, constant = 0;
        char sign = '+';
        string term;
        
        while (left_ss >> term) {
            if (term == "+") {
                sign = '+';
            } else if (term == "-") {
                sign = '-';
            } else if (term.find('x') != string::npos) {
                string coeff_str = term.substr(0, term.find('x'));
                if (coeff_str.empty()) {
                    coeff = 1;
                } else if (coeff_str == "-") {
                    coeff = -1;
                } else {
                    coeff = stod(coeff_str);
                }
                if (sign == '-') coeff = -coeff;
                a += coeff;
            } else {
                constant = stod(term);
                if (sign == '-') constant = -constant;
                b += constant;
            }
        }
        
        // 解析右边表达式
        string right;
        ss >> right;
        double right_val = 0;
        if (!right.empty()) {
            right_val = stod(right);
        }
        
        // 移项使右边为0
        b -= right_val;
        
        // 标准化为 ax + b > 0 形式
        return Inequality(a, b, op);
    }
    
    // 解单个不等式
    void solveSingleInequality(const Inequality& ineq) {
        double a = ineq.a;
        double b = ineq.b;
        char op = ineq.op;
        
        if (a == 0) {
            // 0*x + b > 0 => b > 0
            bool satisfied = false;
            switch(op) {
                case '>': satisfied = (b > 0); break;
                case '<': satisfied = (b < 0); break;
                case 'G': case 'L': // >= or <=
                    if (op == 'G') satisfied = (b >= 0);
                    else satisfied = (b <= 0);
                    break;
                case '=': satisfied = (b == 0); break;
            }
            if (satisfied) {
                cout << "解为所有实数" << endl;
            } else {
                cout << "无解" << endl;
            }
            return;
        }
        
        // 解方程 ax + b = 0
        double solution = -b / a;
        
        // 计算精确分数解
        Fraction exact_solution(round(-b * 1000), round(a * 1000));
        
        cout << "解: x ";
        if (a > 0) {
            switch(op) {
                case '>': cout << "> "; break;
                case '<': cout << "< "; break;
                case 'G': cout << ">= "; break;
                case 'L': cout << "<= "; break;
                case '=': cout << "= "; break;
            }
        } else {
            // 当a为负数时,不等号方向需要反转
            switch(op) {
                case '>': cout << "< "; break;
                case '<': cout << "> "; break;
                case 'G': cout << "<= "; break;
                case 'L': cout << ">= "; break;
                case '=': cout << "= "; break;
            }
        }
        
        // 输出解
        if (exact_solution.denominator == 1 || exact_solution.denominator == -1) {
            cout << exact_solution.numerator << endl;
        } else {
            cout << exact_solution.toString() << " (约 " << solution << ")" << endl;
        }
    }
    
    // 解不等式组
    void solveInequalitySystem(const vector<Inequality>& inequalities) {
        vector<pair<double, bool>> criticalPoints; // 临界点和是否包含
        
        for (const auto& ineq : inequalities) {
            double a = ineq.a;
            double b = ineq.b;
            
            if (a == 0) {
                // 处理0*x + b > 0的情况
                bool satisfied = false;
                switch(ineq.op) {
                    case '>': satisfied = (b > 0); break;
                    case '<': satisfied = (b < 0); break;
                    case 'G': satisfied = (b >= 0); break;
                    case 'L': satisfied = (b <= 0); break;
                    case '=': satisfied = (b == 0); break;
                }
                if (!satisfied) {
                    cout << "无解" << endl;
                    return;
                }
                continue;
            }
            
            double point = -b / a;
            bool include = false;
            
            switch(ineq.op) {
                case 'G': case 'L': case '=': include = true; break;
                default: include = false;
            }
            
            criticalPoints.emplace_back(point, include);
            
            // 根据a的符号和操作符确定解的方向
            if ((a > 0 && (ineq.op == '>' || ineq.op == 'G')) || 
                (a < 0 && (ineq.op == '<' || ineq.op == 'L'))) {
                // 解在point右侧
                criticalPoints.emplace_back(numeric_limits<double>::infinity(), false);
            } else {
                // 解在point左侧
                criticalPoints.emplace_back(-numeric_limits<double>::infinity(), false);
            }
        }
        
        // 如果没有临界点,说明所有不等式都是恒成立的
        if (criticalPoints.empty()) {
            cout << "解为所有实数" << endl;
            return;
        }
        
        // 对临界点排序
        sort(criticalPoints.begin(), criticalPoints.end(), 
            [](const pair<double, bool>& a, const pair<double, bool>& b) {
                return a.first < b.first;
            });
        
        // 寻找所有不等式的交集
        double left = -numeric_limits<double>::infinity();
        double right = numeric_limits<double>::infinity();
        bool leftInclusive = true;
        bool rightInclusive = true;
        
        for (const auto& point : criticalPoints) {
            if (point.first == -numeric_limits<double>::infinity()) {
                left = point.first;
                leftInclusive = false;
            } else if (point.first == numeric_limits<double>::infinity()) {
                right = point.first;
                rightInclusive = false;
            } else {
                // 更新左右边界
                if (point.first > left) {
                    left = point.first;
                    leftInclusive = point.second;
                }
                if (point.first < right) {
                    right = point.first;
                    rightInclusive = point.second;
                }
            }
        }
        
        // 输出解
        if (left > right) {
            cout << "无解" << endl;
        } else if (left == -numeric_limits<double>::infinity() && right == numeric_limits<double>::infinity()) {
            cout << "解为所有实数" << endl;
        } else if (left == right) {
            if (leftInclusive && rightInclusive) {
                cout << "解: x = " << left << endl;
            } else {
                cout << "无解" << endl;
            }
        } else {
            cout << "解: ";
            if (left == -numeric_limits<double>::infinity()) {
                cout << "x ";
                if (rightInclusive) cout << "<= ";
                else cout << "< ";
                cout << right;
            } else if (right == numeric_limits<double>::infinity()) {
                cout << "x ";
                if (leftInclusive) cout << ">= ";
                else cout << "> ";
                cout << left;
            } else {
                cout << left;
                if (leftInclusive) cout << " <= ";
                else cout << " < ";
                cout << "x ";
                if (rightInclusive) cout << "<= ";
                else cout << "< ";
                cout << right;
            }
            cout << endl;
        }
    }
    
    int main() {
        cout << "不等式求解器" << endl;
        cout << "输入格式示例: 2x + 3 > 5 或 3x - 2 <= 4x + 1" << endl;
        cout << "输入不等式(组),每行一个不等式,输入空行结束:" << endl;
        
        vector<Inequality> inequalities;
        string line;
        
        while (getline(cin, line)) {
            if (line.empty()) break;
            try {
                Inequality ineq = parseInequality(line);
                inequalities.push_back(ineq);
            } catch (const exception& e) {
                cout << "错误: " << e.what() << endl;
                return 1;
            }
        }
        
        if (inequalities.empty()) {
            cout << "没有输入不等式" << endl;
            return 0;
        }
        
        if (inequalities.size() == 1) {
            solveSingleInequality(inequalities[0]);
        } else {
            solveInequalitySystem(inequalities);
        }
        
        return 0;
    }
    

    #pragma GCC optimize("-Ofast")

    c++火车头:可以将你的代码速度提升几毫秒

    #pragma GCC optimize(3)
    #pragma GCC target("avx")
    #pragma GCC optimize("Ofast")
    #pragma GCC optimize("inline")
    #pragma GCC optimize("-fgcse")
    #pragma GCC optimize("-fgcse-lm")
    #pragma GCC optimize("-fipa-sra")
    #pragma GCC optimize("-ftree-pre")
    #pragma GCC optimize("-ftree-vrp")
    #pragma GCC optimize("-fpeephole2")
    #pragma GCC optimize("-ffast-math")
    #pragma GCC optimize("-fsched-spec")
    #pragma GCC optimize("unroll-loops")
    #pragma GCC optimize("-falign-jumps")
    #pragma GCC optimize("-falign-loops")
    #pragma GCC optimize("-falign-labels")
    #pragma GCC optimize("-fdevirtualize")
    #pragma GCC optimize("-fcaller-saves")
    #pragma GCC optimize("-fcrossjumping")
    #pragma GCC optimize("-fthread-jumps")
    #pragma GCC optimize("-funroll-loops")
    #pragma GCC optimize("-fwhole-program")
    #pragma GCC optimize("-freorder-blocks")
    #pragma GCC optimize("-fschedule-insns")
    #pragma GCC optimize("inline-functions")
    #pragma GCC optimize("-ftree-tail-merge")
    #pragma GCC optimize("-fschedule-insns2")
    #pragma GCC optimize("-fstrict-aliasing")
    #pragma GCC optimize("-fstrict-overflow")
    #pragma GCC optimize("-falign-functions")
    #pragma GCC optimize("-fcse-skip-blocks")
    #pragma GCC optimize("-fcse-follow-jumps")
    #pragma GCC optimize("-fsched-interblock")
    #pragma GCC optimize("-fpartial-inlining")
    #pragma GCC optimize("no-stack-protector")
    #pragma GCC optimize("-freorder-functions")
    #pragma GCC optimize("-findirect-inlining")
    #pragma GCC optimize("-fhoist-adjacent-loads")
    #pragma GCC optimize("-frerun-cse-after-loop")
    #pragma GCC optimize("inline-small-functions")
    #pragma GCC optimize("-finline-small-functions")
    #pragma GCC optimize("-ftree-switch-conversion")
    #pragma GCC optimize("-foptimize-sibling-calls")
    #pragma GCC optimize("-fexpensive-optimizations")
    #pragma GCC optimize("-funsafe-loop-optimizations")
    #pragma GCC optimize("inline-functions-called-once")
    #pragma GCC optimize("-fdelete-null-pointer-checks")
    #pragma GCC optimize(2)
    
    
    #include<iostream>
    using namespace std;
    struct node{
    	int data;
    	node *next;
    };
    node *head=NULL,*p,*q;
    void creat_list(){
    	int n,x;
    	cin>>n;
    	for(int i=1;i<=n;i++){
    		cin>>x;
    		p=new node;//分配空间 
    		p->data=x;
    		//建立关联 
    		p->next=head;
    		head=p;
    	}
    }
    void print_list(){
    	p=head;
    	while(p!=NULL){
    		cout<<p->data<<" ";
    		p=p->next;
    	}
    	cout<<endl; 
    }
    void insert_list(int k,int x){
    	q=new node;
    	q->data=x;
    	if(k==1){
    		q->next=head;
    		head=q;
    	}else{//中间 
    		//找
    		int i=1;
    		p=head;
    		while(i<k-1){
    			p=p->next;
    			i++;
    		}
    		q->next=p->next;
    		p->next=q;
    	}
    }
    void delete_list(int k){
    	if(k==1){
    		p=head;
    		head=p->next;
    		delete p;
    	}else{
    		int i=1;
    		p=head;
    		while(i<k-1){
    			p=p->next;
    			i++;
    		}
    		q=p->next;//删 
    		p->next=q->next;
    		delete q;
    	}
    }
    int main(){
    	creat_list();
    	print_list();
    	insert_list(3,8);
    	print_list();
    	delete_list(4);
    	print_list();
    }
    
    #include<iostream>
    using namespace std;
    struct node{
    	int data;
    	int next;
    }a[15005];
    int n,x,head=-1,tot;
    
    void print_l(){
    	int p=head;
    	while(p!=-1){
    		cout<<a[p].data<<" ";
    		p=a[p].next;
    	}
    	cout<<endl;
    }
    int search(int x){
    	if(head==-1||a[head].data>x)return -1;
    	int p=head;
    	while(a[p].next!=-1&&a[a[p].next].data<x){
    		p=a[p].next;
    	}
    	return p;
    }
    void insert_l(int x){
    	a[++tot].data=x;
    	int p=search(x);//它所在位置的前一个 
    	if(p==-1){
    		a[tot].next=head;
    		head=tot;
    	}else{
    		a[tot].next=a[p].next;
    		a[p].next=tot;
    	}
    }
    int main(){
    	cin>>n;
    	for(int i=1;i<=n;i++){
    		cin>>x;
    		insert_l(x);
    	}
    	print_l();
    }
    
    
    #include<bits/stdc++.h>
    using namespace std;
    long long k,a[3000000];
    string s;
    priority_queue<int,vector<int>,greater<int> > q;
    int main(){
    	for(int n=1;n<=3000000;n++){
    		while(!q.empty())q.pop();
    		memset(a,0,sizeof(a));k=0;s="";
    		q.push(1);
    		while(k!=50000){
    			k++;
    			a[k]=q.top();
    			q.pop();
    			q.push(a[k]*2+1);
    			q.push(a[k]*4+5);
    		}
    		for(int i=1;i<=50000;i++)s+=to_string(a[i]);
    		for(int i=0;i<n;i++)cout<<s[i];
    		cout<<",";
    	}
    
    }
    
    
  • 最近活动

  • Stat

  • Rating