提问人:loststudent 提问时间:10/14/2021 最后编辑:Dharmanloststudent 更新时间:12/13/2021 访问量:90
我需要提取较低和较高的序列数
I need to extract the lower and higher number of sequence
问:
我正在使用 x 个数字序列并搜索它的较低和较高数字并计算差异,但是当我使用变量保存它不保存的最高数字时,我对较低的变量没有这个问题。
for(int i=0;i<mesos;i++){
cin >>m;
if(m<e) a = m;
else if(m<e) e = m;
一切都已声明
答:
0赞
Júlio Cesar
10/14/2021
#1
要保存最大数字,您必须测试该数字是否大于另一个数字。在你的例子中,你正在根据相同的值、同样的东西进行计算。我没有测试我的分辨率,但它会像我在下面写的一样。我在第一次运行循环时使用了 if,因此最高值和最低值等于您的输入。
但是测试也消失了,看看哪个最适合你。
提示:使用好的变量名称
int lower,higher;
for(int i=0;i<mesos;i++){
cin >> m;
if(i==0){
lower = m;
higher = m;
}
if(m>higher) higher = m;
else if(m<lower) lower = m;
}
评论
0赞
Thomas Matthews
10/14/2021
????你是说还是?cin << m
cin >> m
cout << m
0赞
Thomas Matthews
10/14/2021
您可以通过读取循环之前并将值分配给 和 来消除该语句。循环条件为 。if
m
lower
higher
i < (mesos - 1)
0赞
Júlio Cesar
12/13/2021
托马斯·马修斯,你对这两个评论都是对的。但是他在循环中使用赋值,这就是我在循环中使用它的原因。
0赞
abc
10/14/2021
#2
只是为了优雅,因为这是一个 C++ 问题。
uint64_t min, max;
for(decltype(mesos) i = 0; i < mesos; ++i) {
std::cin >> m;
min = std::min(min, m);
max = std::max(max, m);
}
评论
0赞
Thomas Matthews
10/14/2021
????你是说还是?cin << m
cin >> m
cout << m
0赞
abc
10/14/2021
那绝对不会编译、编辑:)
评论
a
a
e
minValue
maxValue