系统随机生成一个数,让你猜,提示你猜大了猜小了,一共五次机会。刚入门,这样哪里可以优化一下

#include
#include
using namespace std;
int main()
{
srand((unsigned int)time(NULL));
int a=rand()%100+1;
int b=0;
int c=0;
while(1)
{
c=c+1;
if(c>5)
{
cout << "游戏结束,您输了" << endl;
break;
}
cout << "请猜一个数" << endl;
cin>>b;
if(b<a)
{
cout << "您猜小了" << endl;
}
if(b>a)
{
cout << "您猜大了" << endl;
}
if(b==a)
{
cout << "您猜对了" << endl;
break;
}
}
system("pause");
return 0;
}

三个if,后面两个改为else if

while(1)
改为while(a!=b)
这样就不需要写if(b==a)和break了
c=c+1;改为c++;
把c++和c>5的判断拿到while的最后去,不要还没开始猜就判断它
一堆if要改为if,else结构

优化如下:

int main() {
    srand((unsigned int)time(NULL));
    int a=rand()%100+1;
    int b=0;
    int c=0;
    while(c < 5) {
        cout << "请猜一个数" << endl;
        cin >> b;
        if (b<a) {
            cout << "您猜小了" << endl;
        } else if(b>a) {
            cout << "您猜大了" << endl;
        } else {
            cout << "您猜对了" << endl;
            break;
        }
        c++;
    }
    if(c == 5) {
        cout << "游戏结束,您输了" << endl;
    }
    system("pause");
    return 0;
}