编译环境:
本人用的并不是Visual Studio (因为觉得下载太麻烦了,正好电脑里面也有可以编译C++的dev C++,就正好拿来试试),用什么编译环境其实都可以,看个人需求吧。
程序逻辑:
主体逻辑很简单:(应该算是冒泡算法)
三只小猪ABC:
先判断AB,有两种情况:
1.如果A>B,让A和C比较,这时就出现两种情况了,如果A>C,则最终结果就是A最重。如果A<C,则最终结果就是C最重。
2.否则,A<B,让B和C比较,同样,有两种情况,如果B>C,则最终结果就是B最重。如果B<C,则最终结果就是C最重。
程序结构:
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
//三只小猪称体重
//1.creat three pig's weight
int num1=0;
int num2=0;
int num3=0;
//2.user input three pig's weight
cout << "please input pig A's weight" << endl;
cin>>num1;
cout << "please input pig B's weight" << endl;
cin>>num2;
cout << "please input pig C's weight" << endl;
cin>>num3;
cout << "pig A's weight is "<< num1<<endl;
cout << "pig B's weight is "<< num2<<endl;
cout << "pig C's weight is "<< num3<<endl;
//3.check which one is the weightest
//first check A&B
if(num1>num2)
{
if(num1>num3)
{
cout <<"the pig A is weightest."<<endl;
}
else
{
cout<<"the pig C is weightest."<<endl;
}
}
else
{
if(num2>num3)
{
cout <<"the pig B is weightest."<<endl;
}
else
{
cout <<"the pig C is weightest."<<endl;
}
}
system("pause");
return 0;
}
这样一来三只小猪称重就完成了。