题解 | 支付宝消费打折

支付宝消费打折

https://www.nowcoder.com/practice/f8997c9b82714f058e12433a32614993

#include <stdio.h>
typedef struct 				//结构体存 价格,折扣
{
    int price;
    int discount;
} Item;

int cmp(const void* a, const void* b) 
{
    return ((Item*)a)->price - ((Item*)b)->price;
}

int main() 
{
    int n,k;					//输入
    scanf("%d%d", &n, &k);

    Item it[100001];
    for (int i = 0; i < n; i++)		
        scanf("%d", &it[i].price);

    char s[100001];				//输入是否折扣
    scanf("%s",s);
  	
    for (int i = 0; i < n; i++)			//如果折扣,原价格*19,没有*20,等同于95%,95%100
    {									//可以避免浮点数运算
        it[i].discount = s[i] - '0';
        if(it[i].discount ==1)
            it[i].price= it[i].price*19;
        else
            it[i].price= it[i].price*20;
    }    

    qsort(it, n, sizeof(Item), cmp);		//价格排序
    
    long int count=0;
    int i=0;
    for( i=0;i<n;i++)
    {
       if (count + it[i].price <= k*20) 		//从价格最低累加比较
        {
          count+=it[i].price;  
        }
        else
           break; 
    }

    printf("%d",i);  

    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
03-20 12:46
瘦嘟嘟右卫门:百度文库网盘的暑期也没约面吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务