比较逻辑!! [英] comparison logic!!

查看:61
本文介绍了比较逻辑!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个程序,允许您输入3个整数值A,B和C

并输出3个值中的最大值。

//要进行RIGOUROUS TESTING


#include< iostream>

使用命名空间std;


void main(){

int A(0),B(0),C(0);

int max(0),min(0);


cout<<"输入3个整数:" ;;

cin> A> B> C;


if(AB){

if(AC)

max = A;

}否则{

if(BC)

max = B;

否则如果(CA)

max = C;

}

if(A< B){

if(A< C)

min = A;

} else {

if(B< C)

min = C;

else if(C< A)

min = C;

}

cout<<"最大整数:"<< max<< endl;

cout <<" Min integer:"<< min<< endl;


}

Write a program that allows you to input 3 integer values A, B and C
and output the largest of the 3 values.
//TO DO RIGOUROUS TESTING

#include<iostream>
using namespace std;

void main(){
int A(0), B(0), C(0);
int max(0), min(0);

cout<<"Enter 3 integers:";
cin >A >B >C;

if (A B){
if (A C)
max = A;
} else {
if (B C)
max = B;
else if (C A)
max = C;
}
if (A < B){
if (A < C)
min = A;
} else {
if (B < C)
min = C;
else if (C < A)
min = C;
}
cout <<"Max integer: "<<max<<endl;
cout <<"Min integer: "<<min<<endl;

}

推荐答案




curiousEngine写道:
Hi

curiousEngine wrote:

编写一个程序允许您输入3个整数值A,B和C

并输出3个值中的最大值。
Write a program that allows you to input 3 integer values A, B and C
and output the largest of the 3 values.



那么你想让我们做什么?

So what do you want us to do?


//做任务RIGOUROUS
//TO DO RIGOUROUS TESTING



那是个笑话,对吗?

That''s a joke, right?


#include< iostream>

using namespace std;


void main(){

int A(0),B(0),C(0);

int max(0),min(0);


cout<<"输入3个整数:" ;;

cin> A> B> C;


if(AB){

if(AC)

max = A;

}否则{

if(BC)

max = B;

else if(CA)

max = C;

}
#include<iostream>
using namespace std;

void main(){
int A(0), B(0), C(0);
int max(0), min(0);

cout<<"Enter 3 integers:";
cin >A >B >C;

if (A B){
if (A C)
max = A;
} else {
if (B C)
max = B;
else if (C A)
max = C;
}



您遇到了问题。你的其中一个是缺少一个其他分支。所以要么

你的一个条件总是正确的,或者有一条路径没有找到最大的

元素。尝试绘制三个元素和你对它们的了解

在图表中,当A< B然后A位于纸张上低于B的价格

并且有一条连接它们的线。这有助于您找到必要的

比较。 (它与C ++无关)


Markus

You have a problem. One of your if''s is missing an else-branch. So either
one of your conditions is always true or there is a path where no maximum
element is found. Try drawing three elements and your knowledge about them
in a diagram where when A < B then A is located lower than B on your paper
and there''s a line connecting them. This helps you to find the necessary
comparisons. (And it has nothing to do with C++)

Markus


Markus Moll写道:
Markus Moll wrote:




curiousEngine写道:
Hi

curiousEngine wrote:

>写一个程序,允许您输入3个整数值A,B和C
并输出3个值中的最大值。
>Write a program that allows you to input 3 integer values A, B and C
and output the largest of the 3 values.



那么你想让我们做什么?


So what do you want us to do?


> //做严峻的测试
>//TO DO RIGOUROUS TESTING



那是个玩笑,对吗?


That''s a joke, right?


> #include< iostream>
using namespace std;

void main(){
int A(0),B(0),C(0);
int max(0),min(0 );

cout<<"输入3个整数:" ;;
cin> A> B> C;

if(AB) {
如果(AC)
max = A;
}否则{
if(BC)
max = B;
if if(CA)
max = C;
}
>#include<iostream>
using namespace std;

void main(){
int A(0), B(0), C(0);
int max(0), min(0);

cout<<"Enter 3 integers:";
cin >A >B >C;

if (A B){
if (A C)
max = A;
} else {
if (B C)
max = B;
else if (C A)
max = C;
}



你有问题。你的其中一个是缺少一个其他分支。所以要么

你的一个条件总是正确的,或者有一条路径没有找到最大的

元素。尝试绘制三个元素和你对它们的了解

在图表中,当A< B然后A位于纸张上低于B的价格

并且有一条连接它们的线。这有助于您找到必要的

比较。 (它与C ++无关)


Markus


You have a problem. One of your if''s is missing an else-branch. So either
one of your conditions is always true or there is a path where no maximum
element is found. Try drawing three elements and your knowledge about them
in a diagram where when A < B then A is located lower than B on your paper
and there''s a line connecting them. This helps you to find the necessary
comparisons. (And it has nothing to do with C++)

Markus



实际上,这并不重要'' if(AC)''仅在''if(A

Actually, that doesn''t matter as ''if (A C)'' is only called when ''if (A


B)时调用,返回true,如果不是,则无论如何跳过该行。
B) returns true, and if it doesn''t, the line is skipped either way.



我现在不在我的家里,所以我不能自己处理一些代码

,但是我在我的

头部玩了我能想到的所有组合,这段代码似乎总能找到正确的结果。


我不喜欢什么但是,获得:

你可以通过每个

结果使用两个ifs来使代码更具可读性。不是更高效,但更容易玩你的头脑




例如


如果(AB){

if(AC)

max = A; }

否则如果(B A){

if(B C)

max = B; }

否则if(C A){

if(C B)

max = C; }


没那么慢。没错,你的效率会更高,但实际上只要一个人试着跟着它就会很头疼。


我也注意到了,你的int min(0)目前尚未使用。


使用其中任何一个解决方案,两者都只处理三个整数。

为什么不使用数组和排序环?我喜欢灵活性〜

I''m not at my home comp right now, so I can''t mess around with some code
myself, but I played all combinations I could think of through in my
head, and that piece of code always seems to set the right result.

What I don''t get, though:
You could either make the code more readable by using two ifs per
result. Not really more efficient, but a lot easier to play through in
your head.

e.g.

if (A B) {
if (A C)
max = A; }
else if (B A) {
if (B C)
max = B; }
else if (C A) {
if (C B)
max = C; }

Not that much slower. True, yours is more efficient, but really gives
one a headache when one tries to follow it in ones head.

What I also noticed, your int min(0) is currently unused.

Thing with either of the solutions is, both handle only three integers.
Why not use an array and a sorting loop? I like flexibility~


curiousEngine写道:
curiousEngine wrote:

写一个程序,允许你输入3个整数值A ,B和C

并输出3个值中最大的一个。


//要进行严格的测试


#include< iostream>

使用命名空间std;


void main(){
Write a program that allows you to input 3 integer values A, B and C
and output the largest of the 3 values.
//TO DO RIGOUROUS TESTING

#include<iostream>
using namespace std;

void main(){



此时,你有一个格式错误的程序,任何事情都可能发生,

包括你的编译器不接受它。


main()返回int。期间。

At this point, you have an ill-formed program and anything can happen,
including your compiler not accepting it.

main() returns int. Period.


int A(0),B(0),C(0);

int max(0),min( 0);


cout<<"输入3个整数:" ;;

cin> A> B> C;


if(AB){

if(AC)

max = A;

} else {\\ n br />
if(BC)

max = B;

else if(CA)

max = C;

}


if(A< B){

if(A< C)

min = A;

}否则{

if(B
min = C;

否则如果(C
min = C;

}


cout<<"最大整数:"<<<<<<<<<<<<<<<<       >
}
int A(0), B(0), C(0);
int max(0), min(0);

cout<<"Enter 3 integers:";
cin >A >B >C;

if (A B){
if (A C)
max = A;
} else {
if (B C)
max = B;
else if (C A)
max = C;
}
if (A < B){
if (A < C)
min = A;
} else {
if (B < C)
min = C;
else if (C < A)
min = C;
}
cout <<"Max integer: "<<max<<endl;
cout <<"Min integer: "<<min<<endl;

}


这篇关于比较逻辑!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆