標題:
[想想看] 三數比大小 (考慮所有情況)
[打印本頁]
作者:
方浩葦
時間:
2024-6-22 06:48
標題:
[想想看] 三數比大小 (考慮所有情況)
試設計一個小程式, 讓使用者任意輸入三個大小不同的數, 接著電腦回應出這三個數的大小關係, 包括任兩數相等, 或三數皆相等的情況.
譬如:
當使用者依序輸入5, 12, 3, 電腦會回應 12>5>3
當使用者依序輸入10, 8, 8, 電腦會回應 10>8=8
當使用者依序輸入 7, 7, 7, 電腦會回應 7=7=7
提示: 無論if...else的邏輯思考為何, 總共有
13種
情況.
本帖隱藏的內容需要回復才可以瀏覽
作者:
高湘庭
時間:
2024-6-22 15:07
#include<iostream>
#include<cstdlib>
using namespace std;
int main (){
int x ,y, z;
re:
cout<<"請依序輸入三數";
cin>>x;
cin>>y;
cin>>z;
if(x>y&&y>z)
cout<<x<<">"<<y<<">"<<z<<endl;
else if(x>z&&z>y)
cout<<x<<">"<<z<<">"<<y<<endl;
else if(y>x&&x>z)
cout<<y<<">"<<x<<">"<<z<<endl;
else if(y>z&&z>x)
cout<<y<<">"<<z<<">"<<x<<endl;
else if(z>x&&x>y)
cout<<z<<">"<<x<<">"<<y<<endl;
else if(z>y&&y>x)
cout<<z<<">"<<y<<">"<<x<<endl;
else if(x==y&&x>z)
cout<<x<<"="<<x<<">"<<z<<endl;
else if(x==y&&y<z)
cout<<z<<">"<<x<<"="<<x<<endl;
else if(x==z&&z<y)
cout<<y<<">"<<z<<"="<<z<<endl;
else if(x==z&&z>y)
cout<<z<<"="<<z<<">"<<y<<endl;
else if(y==z&&z>x)
cout<<z<<"="<<z<<">"<<x<<endl;
else if(y==z&&z<x)
cout<<x<<">"<<z<<"="<<z<<endl;
else if(x==y&&y==z)
cout<<z<<"="<<z<<"="<<z<<endl;
else
cout<<"輸入錯誤"<<endl;
cout<<endl;
goto re;
system("pause");
return 0;
}
複製代碼
作者:
林少謙
時間:
2024-6-22 15:16
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
int x,y,z;
cout<<"請輸入3個數字:";
cin>>x>>y>>z;
if(x>y&&y>z)
cout<<x<<">"<<y<<">"<<z<<endl;
else if(x>z&&z>y)
cout<<x<<">"<<z<<">"<<y<<endl;
else if(y>x&&x>z)
cout<<y<<">"<<x<<">"<<z<<endl;
else if(y>z&&z>x)
cout<<y<<">"<<z<<">"<<x<<endl;
else if(z>x&&x>y)
cout<<z<<">"<<x<<">"<<y<<endl;
else if(z>y&&y>x)
cout<<z<<">"<<y<<">"<<x<<endl;
else if(x>y&&y==z)
cout<<x<<">"<<z<<"="<<y<<endl;
else if(x>z&&z==y)
cout<<x<<">"<<z<<"="<<y<<endl;
else if(y>x&&x==z)
cout<<y<<">"<<x<<"="<<z<<endl;
else if(y>z&&z==x)
cout<<y<<">"<<z<<"="<<x<<endl;
else if(z>x&&x==y)
cout<<z<<">"<<x<<"="<<y<<endl;
else if(z>y&&y==x)
cout<<z<<">"<<y<<"="<<x<<endl;
else
cout<<x<<"="<<y<<"="<<z<<endl;
cout<<endl;
goto re;
system("pause");
return 0;
}
複製代碼
作者:
李偈睿
時間:
2024-6-22 16:04
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int a,b,c;
re:
cout<<"請輸入任意三個數: ";
cin>>a>>b>>c;
if(a>b && b>c)
cout<<a<<">"<<b<<"<"<<c;
else if(a>c && c>b)
cout<<a<<">"<<c<<"<"<<b;
else if(b>a && a>c)
cout<<b<<">"<<a<<"<"<<c;
else if(b>c && c>a)
cout<<b<<">"<<c<<"<"<<a;
else if(c>a && a>b)
cout<<c<<">"<<a<<"<"<<b;
else if(c>b && b>a)
cout<<c<<">"<<b<<"<"<<a;
else if(a>b && b==c)
cout<<a<<">"<<b<<"="<<c;
else if(b>a && a==c)
cout<<b<<">"<<a<<"="<<c;
else if(c>b && b==a)
cout<<c<<">"<<b<<"="<<a;
else if(a==b && b>c)
cout<<a<<"="<<b<<">"<<c;
else if(a==c && c>b)
cout<<a<<"="<<c<<">"<<b;
else if(b==c && c>a)
cout<<b<<"="<<c<<">"<<a;
else if(a==b && b==c)
cout<<a<<"="<<b<<"="<<c;
goto re;
system("pause");
return 0;
}
複製代碼
作者:
劉奕劭
時間:
2024-6-22 16:24
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
int x,y,z;
cout<<"請輸入第一個數";
cin>>x;
cout<<"請輸入第二個數";
cin>>y;
cout<<"請輸入第三個數";
cin>>z;
if(x>y&&y>z){
cout<<x<<">"<<y<<">"<<z<<endl;
goto re;}
else if(x>z&&z>y){
cout<<x<<">"<<z<<">"<<y<<endl;
goto re;}
else if(y>x&&x>z){
cout<<y<<">"<<x<<">"<<z<<endl;
goto re;}
else if(y>z&&z>x){
cout<<y<<">"<<z<<">"<<x<<endl;
goto re;}
else if(z>x&&x>y){
cout<<z<<">"<<x<<">"<<y<<endl;
goto re;}
else if(z>y&&y>x){
cout<<z<<">"<<y<<">"<<x<<endl;
goto re;}
else if(x==y&&x>z){
cout<<x<<"="<<x<<">"<<z<<endl;
goto re;}
else if(x==y&&y<z){
cout<<z<<">"<<x<<"="<<x<<endl;
goto re;}
else if(x==z&&z<y){
cout<<y<<">"<<z<<"="<<z<<endl;
goto re;}
else if(x==z&&z>y){
cout<<z<<"="<<z<<">"<<y<<endl;
goto re;}
else if(y==z&&z>x){
cout<<z<<"="<<z<<">"<<x<<endl;
goto re;}
else if(y==z&&z<x){
cout<<x<<">"<<z<<"="<<z<<endl;
goto re;}
else(x==y&&y==z);{
cout<<z<<"="<<z<<"="<<z<<endl;
goto re;}
system("pause");
return 0;
}
複製代碼
作者:
李唯銘
時間:
2024-6-29 14:13
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int a, b, c;
re:
cout<<"請任意輸入三個數";
cin>>a>>b>>c;
if(a>b && b>c){
cout<<a<<">"<<b<<">"<<c<<endl;
}
else if(a>c && c>b){
cout<<a<<">"<<c<<">"<<b<<endl;
}
else if(b>a && a>c){
cout<<b<<">"<<a<<">"<<c<<endl;
}
else if(b>c && c>a){
cout<<b<<">"<<c<<">"<<a<<endl;
}
else if(c>a && a>b){
cout<<c<<">"<<a<<">"<<b<<endl;
}
else if(c>b && c>a){
cout<<c<<">"<<b<<">"<<a<<endl;
}
else if(a>b && b==c){
cout<<a<<">"<<b<<"="<<c<<endl;
}
else if(a>b && a==c){
cout<<a<<"="<<c<<">"<<b<<endl;
}
else if(a>c && a==b){
cout<<a<<"="<<b<<">"<<c<<endl;
}
else if(b>a && c==a){
cout<<b<<">"<<a<<"="<<c<<endl;
}
else if(b>a && b==c){
cout<<b<<"="<<c<<">"<<a<<endl;
}
else if(b>c && a==b){
cout<<a<<"="<<b<<">"<<c<<endl;
}
else if(c>a && a==b){
cout<<c<<">"<<a<<"="<<b<<endl;
}
else if(c>a && c==b){
cout<<c<<"="<<b<<">"<<a<<endl;
}
else if(c>b && c==a){
cout<<c<<"="<<a<<">"<<b<<endl;
}
else
cout<<c<<"="<<b<<"="<<a<<endl;
goto re;
system("pause");
return 0;
}
複製代碼
作者:
陳妍蓁
時間:
2024-6-29 14:19
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
int x,y,z;
cout<<"請輸入三個整數"<<endl;
cin>>x>>y>>z;
if(x>y&&y>z)
cout<<x<<">"<<y<<">"<<z<<endl;
else if(x>z&&z>y)
cout<<x<<">"<<z<<">"<<y<<endl;
else if(y>x&&x>z)
cout<<y<<">"<<x<<">"<<z<<endl;
else if(y>z&&z>x)
cout<<y<<">"<<z<<">"<<x<<endl;
else if(z>x&&x>y)
cout<<z<<">"<<x<<">"<<y<<endl;
else if(z>y&&y>x)
cout<<z<<">"<<y<<">"<<x<<endl;
else if(z==y&&y==x)
cout<<x<<"="<<y<<"="<<z<<endl;
else if(x==y&&y>x)
cout<<x<<"="<<y<<">"<<z<<endl;
else if(x>y&&y==x)
cout<<x<<">"<<y<<"="<<z<<endl;
else if(y>x&&x==z)
cout<<y<<">"<<x<<"="<<z<<endl;
else if(z==x&&x>y)
cout<<z<<"="<<x<<">"<<y<<endl;
else if(z>x&&x==y)
cout<<z<<">"<<x<<"="<<y<<endl;
else if(z==y&&y>x)
cout<<z<<"="<<y<<">"<<x<<endl;
else
cout<<"輸入錯誤";
system("pause");
return 0;
}
複製代碼
作者:
洪榮辰
時間:
2024-6-29 14:29
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int a, b, c;
re:
{
cout<<"請輸入三個大小不同的數: "<<endl;
cin>>a>>b>>c;
if(a==b && b==c)
cout<<a<<"="<<b<<"="<<c;
else if(a>b && b<c && a>c)
cout<<a<<">"<<c<<">"<<b;
else if(a>b && b<c && a<c)
cout<<c<<">"<<a<<">"<<b;
else if(a>b && b<c && a==c)
cout<<a<<"="<<c<<">"<<b;
else if(a>b && b<c && a==c)
cout<<c<<"="<<a<<">"<<b;
else if(a<b && b>c && a>c)
cout<<b<<">"<<a<<">"<<c;
else if(a<b && b>c && a<c)
cout<<b<<">"<<c<<">"<<a;
else if(a<b && b<c)
cout<<c<<">"<<b<<">"<<a;
else if(a>b && b>c)
cout<<a<<">"<<b<<">"<<c;
else if(a=b && b>c)
cout<<a<<"="<<b<<">"<<c;
else if(a=b && b<c)
cout<<c<<">"<<a<<"="<<b;
else if(a>b && b==c)
cout<<a<<">"<<b<<"="<<c;
else if(a<b && b==c)
cout<<b<<"="<<c<<">"<<a;
cout<<endl;
}
goto re;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2