此题,无需输入N,只是简单的输入是个数字,
刚开始的时候,
WA的代码:
#include<iostream>
using namespace std;
int main()
{
int tem;
int max=0;
while(1)
{
for (int i=0;i<10;i++)
{
cin>>tem;
if (max<tem) max=tem;
}
cout<<"max="<<max<<endl;
}
return 0;
}
while(1)是超出时间限制的问题
换成while(cin>>max)的就可以了。
AC的代码:
#include<iostream>
using namespace std;
int main()
{
int tem;
int max=0;
while(cin>>max)
{
for (int i=1;i<10;i++)
{
cin>>tem;
if (max<tem) max=tem;
}
cout<<"max="<<max<<endl;
}
return 0;
}
本文探讨了使用C++编程语言解决一个简单问题的方法:不断读取一系列整数并输出其中的最大值。作者首先尝试使用无限循环进行处理,但遇到了时间超限的问题。最终通过检查输入流的状态来优化解决方案,确保了程序效率。
4424

被折叠的 条评论
为什么被折叠?



