#include
#include
using namespace std;
struct jewelry {
int price;
int volume;
bool operator < (const jewelry &A) const {
return price > A.price;
}
}buf[101];
int main() {
int v, n, i, ans, ids;
while(~scanf("%d", &v) && v) {
scanf("%d", &n);
for(i=0; i
scanf("%d%d", &buf[i].price, &buf[i].volume);
sort(buf, buf+n);
ids = 0;
ans = 0;
while(v>0 && ids
if(v>=buf[ids].volume) {
v -= buf[ids].volume;
ans += buf[ids].price * buf[ids].volume;
} else {
ans += v * buf[ids].price;
v = 0;
}
ids ++;
}
printf("%d\n", ans);
}
return 0;
求这段c语言代码换成java代码⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
5条回答 默认 最新
- threenewbee 2016-06-01 02:13关注
class jewelry { public int price; public int volume; boolean compareto (jewelry A) { return this.price > A.price; } static jewelry[] buf = new jewelry[101]; void main() { int v, n, i, ans, ids; Scanner in = new Scanner(System.in); while(in.hasNextInt()) { v = in.nextInt(); n = in.nextInt(); for(i=0; i<n; i++) { buf[i] = new jewelry(); buf[i].price= in.nextInt(); buf[i].volume = in.nextInt(); } sort(buf, buf+n); ids = 0; ans = 0; while(v>0 && ids<n) { if(v>=buf[ids].volume) { v -= buf[ids].volume; ans += buf[ids].price * buf[ids].volume; } else { ans += v * buf[ids].price; v = 0; } ids++; } System.out.println(ans); } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
- ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
- ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
- ¥15 python天天向上类似问题,但没有清零
- ¥30 3天&7天&&15天&销量如何统计同一行
- ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
- ¥15 C#调用python代码(python带有库)
- ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
- ¥15 vs2019中数据导出问题
- ¥20 云服务Linux系统TCP-MSS值修改?