由于題目比較長,并且時間已久厂财,我只記得題目大概的意思:給出N個商品的價格和質(zhì)量,判斷“東西越貴越好”這個命題是否正確峡懈。
Input
第1行璃饱,一個數(shù)N,N為商品的數(shù)量肪康。(2 <= N <=100)
第2 - N + 1行:具體N個商品的價格和質(zhì)量荚恶。
Output
判斷是否符合“東西越貴越好”這個命題,輸出yes/no磷支。
Input示例
6
1 2
3 5
4 8
6 9
10 13
15 18
5
1 3
3 6
5 8
6 7
7 10
Output示例
yes
no
解題思想:與求最大斜率那題類似谒撼,將商品按價格排序,對比相鄰兩個商品的質(zhì)量雾狈,判斷是否存在“價格變高廓潜,質(zhì)量卻降低”的情況。
參考代碼(沒有用STL):
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
struct node //存儲商品的信息
{
float x,y;
};
int main()
{
bool flag=true;
int n=0;
while(cin>>n)
{
node nodes[50];
for(int i=0; i<n; i++)
{
cin>>nodes[i].x>>nodes[i].y;
}
for(int i=n; i>0; i--)//商品按價格排序
{
for(int i=0; i+1<n; i++)
{
if(nodes[i].x>nodes[i+1].x)
{
node temp;
temp=nodes[i];
nodes[i]=nodes[i+1];
nodes[i+1]=temp;
}
}
}
for(int i=1; i<n; i++)//遍歷價格相近商品的質(zhì)量
{
if(nodes[i].y-nodes[i-1].y<=0)//尋找是否存在價格高的商品質(zhì)量卻比價格低的差
{
flag=false;
break;
}
}
if(flag==true)//輸出結(jié)果
{
cout<<"yes"<<endl;
}
else cout<<"no"<<endl;
}
return 0;
}