資源限制
時間限制:1.0s 內存限制:256.0MB
問題描述
一年一度的雙十一又來了,某網購網站又開始了半價銷售的活動。
小G打算在今年的雙十一里盡情地購物,以享受購買的極度快感,她已經列好了她想買的物品的列表。
當然小G並不是出身富貴家庭,所以她網銀裏的錢只是一個有限的整數S(單位:元)。
這次搶購她打算遵循這三個原則選擇每一個物品:
1.先買能“賺”最多的;
2.在“賺”一樣多的情況下,先買最便宜的(這樣買的東西就可能更多了);
3.在前兩條裏都判斷不了購買順序的話,先購買在列表裏靠前的。
(由於網站裏還是有一部分商品並沒有打五折,所以2的情況(“賺”的錢數爲0)是完全可能發生的)
現在,在雙十一的這一天,你要幫小G編寫一個程序,來看看她應該去買她列表裏的哪些物品。(總價格不要超過S哦)
要是幫她寫好這個程序的話,或許你能在光棍節這一天裏贏得她的芳心哦~
輸入格式
輸入共N+1行。
第一行包含兩個整數S和N,S表示小G的可用金額,N表示她看上的物品個數。
接下來N行,對應每一個物品,每行有兩個整數a和b,a是物品的原價(單位:元),b爲0或1,若b爲0,則此物品不半價,若b爲1,則此物品半價銷售。
輸出格式
輸出共一行,爲小G要買的物品序號(從1開始),用空格隔開,注意按序號從小到大輸出。
若小G一件都買不了,則輸出0.
樣例輸入
10 3
5 0
4 0
10 1
樣例輸出
2 3
樣例輸入
10 3
11 0
21 1
100 1
樣例輸出
0
數據規模和約定
0<S<=10000,0<N<=1000,每一個a和b滿足0<a<=1000且b=0或1。
解題思路:
該題的相對合理的方法是結構體,通過判別結構體內的屬性然後進行判斷,最後得到最終結果,思路簡單,但是如果換成是二維數組那麼解法相同,但是代碼量複雜,且難以分辨,我第一次用的是二維數組,最終因爲過多判斷而失敗,第二個方法是用結構體,參考了別人的代碼,非常輕鬆實現,二次的代碼如下:
#include<bits/stdc++.h>
using namespace std;
int a[1000][3];
int b[1000];
int main(){
int s, n;
cin >> s >> n;
for (int i = 0; i < n; i ++){
cin >> a[i][0] >> a[i][1];
a[i][2] = i + 1;
}
for(int i = 0; i < n - 1; i ++){
for (int j = 0; j < n - i - 1; j ++){
int temp1 = a[j][0];
int temp2 = a[j][1];
int temp3 = a[j][2];
if(a[j][1] == 1 && a[j + 1][1] == 0){
continue;
}else if(a[j][1] == 0 && a[j + 1][1] == 0){
if(a[j][0] <= a[j + 1][0]){
continue;
}else{
a[j][0] = a[j + 1][0];
a[j][1] = a[j + 1][1];
a[j][2] = a[j + 1][2];
a[j + 1][0] = temp1;
a[j + 1][1] = temp2;
a[j + 1][2] = temp3;
}
}else if(a[j][1] == 0 && a[j + 1][1] == 1){
a[j][0] = a[j + 1][0];
a[j][1] = a[j + 1][1];
a[j][2] = a[j + 1][2];
a[j + 1][0] = temp1;
a[j + 1][1] = temp2;
a[j + 1][2] = temp3;
}else{
if(a[j][0] <= a[j + 1][0]){
continue;
}else{
a[j][0] = a[j + 1][0];
a[j][1] = a[j + 1][1];
a[j][2] = a[j + 1][2];
a[j + 1][0] = temp1;
a[j + 1][1] = temp2;
a[j + 1][2] = temp3;
}
}
}
}
int count = 0;
int index = 0;
for(int i = 0; i < n; i ++){
if(a[i][1] == 0 && count + a[i][0] <= s){
b[index ++] = a[i][2];
count += a[i][0];
}else if(a[i][1] == 1 && count + a[i][0] * 0.5 <= s){
b[index ++] = a[i][2];
count += a[i][0] * 0.5;
}else{
continue;
}
}
if(index != 0){
for(int i = 0; i < index - 1; i ++){
for (int j = 0; j < index - i - 1; j ++){
int temp = b[j];
if(b[j] > b[j + 1]){
b[j] = b[j + 1];
b[j + 1] = temp;
}
}
for(int i = 0; i < index; i ++){
cout << b[i] << " ";
}
}
}else{
cout << "0";
}
return 0;
}
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int b[1005];
int cnt=1;
typedef struct
{
float cost;
int off;
int num;
}things;
things a[1005];
int main()
{
memset(b,0,sizeof(b));
float s,n;
cin>>s>>n;
for(int i=1;i<=n;i++)
{
a[i].num=i;
cin>>a[i].cost>>a[i].off;
}
for(int i=1;i<n;i++)
for(int j=i+1;j<=n;j++)
{
if(a[i].cost*a[i].off/2<a[j].cost*a[j].off/2)//判斷條件1
{
things temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else if(a[i].cost*a[i].off/2==a[j].cost*a[j].off/2)
{
if(a[i].cost>a[j].cost)//判斷條件2
{
things temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else if(a[i].cost==a[j].cost)
{
if(a[i].num>a[j].num)//判斷條件3
{
things temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
for(int i=1;i<=n;i++)//按照排序依次將買得起的物品買入,將買的物品的num存入數組b
{
if(a[i].off==0&&a[i].cost<=s)
{
s-=a[i].cost;
b[cnt++]=a[i].num;
}
else if(a[i].off==1&&a[i].cost/2<=s)
{
s-=a[i].cost/2;
b[cnt++]=a[i].num;
}
}
if(cnt==1)//一樣都買不起輸出0
printf("%d",0);
sort(b,b+cnt);
for(int i=1;i<cnt;i++)
{
printf("%d ",b[i]);
}
}
參考的代碼地址:https://blog.csdn.net/aqdk1/article/details/103984719