当前位置:网站首页>ZCMU--5115: Buying Keys(C语言)
ZCMU--5115: Buying Keys(C语言)
2022-08-09 02:01:00 【小小小Why】
Description
One day Xiao Ming is not happy because he has no idea about how to run out of his pocket money. At that moment, a mysterious man appears with a smile: "My keys are on sale. one key cost 3 yuans and 3 keys cost 10 yuans. How many keys do you wanna buy?" Xiaoming is attracted by mysterious man and wants to spend all his money on buying keys. He doesn't want keep any money at the end. At the same time, because of the heavy weight of keys, Xiaoming Hopes that he can buy as few keys as possible. At the beginning, Xiao Ming had n yuan. Can you tell Xiaoming the minimum number of keys he can bought if he runs out of his pocket money? If Xiaoming can't run out of his money, please output "orz".
Input
The first line contains one integer n(1≤n≤109), the pocket money Xiaoming have.
Output
If Xiaoming can't run out of his money, please output "orz"(without quotes), otherwise output the minimum number of keys he can bought if he runs out of his money.
Sample Input
3
Sample Output
1
HINT
Xiaoming can spend 3 yuan to buy a key
解析:倘若能刚好花完所有钱,那么假设买了 i 个10元套餐,j 个3元套餐,那么肯定满足3*i+10*j=n,我们可以直接暴力枚举看有无满足的 i 和 j。
#include <stdio.h>
int main()
{
int n,s,i,j;
while(~scanf("%d",&n)){
s=0;
for(i=n/10;i>=0;i--){ //最多买n/10个
for(j=0;j<=n/3;j++){ //最多买n/3个
if(i*10+3*j==n){
s=1;//存在满足i和j
break;
}
}
if(s==1) break;
}
if(s==1) printf("%d\n",i*3+j);
else printf("orz\n");
}
return 0;
}
优化版本:我们买了 i 个10元,那么剩下钱就是n-3*i,倘若这个能整除3,那么就是满足条件。
#include <stdio.h>
int main()
{
int n,s,i;
scanf("%d",&n);
s=0;
for(i=n/10;i>=0;i--){
if((n-i*10)%3==0){ //剩下的钱能整除3
s=1;
break;
}
}
if(s==1) printf("%d\n",i*3+(n-i*10)/3);
else printf("orz\n");
return 0;
}
边栏推荐
- 9.1-----24. Swap the nodes in the linked list in pairs
- Line segment tree of knowledge
- 力扣刷题记录6.1-----203. 移除链表元素
- 33. 分别谈谈联合索引生效和失效的条件
- New Swagger3.0 tutorial, OAS3 quick configuration guide, to automate API interface documentation!
- Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules
- typescript89-展示任务列表功能
- 2020.12.4日志
- Proe/Creo智能硬件产品结构设计要点「干货分享」
- Wireshark packet capture tool
猜你喜欢
在树莓派上使用cpolar(番外篇2)
New Swagger3.0 tutorial, OAS3 quick configuration guide, to automate API interface documentation!
js实现数组去重的方式(7种)
Educational Codeforces Round 132 (Rated for Div. 2)
谷歌翻译下载-免费谷歌翻译软件下载
HCIP-R&S By Wakin自用笔记(2)OSPF之OSPF回顾、虚连接
JDBC technology (1) - a simple JDBC test
使用百度EasyDL实现智能垃圾箱
史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!
《LC刷题总结》—— 二叉树
随机推荐
Difference between KQL and Lucene
配置文件的读取-TOML
MT4/MQL4 Getting Started to Mastering EA Tutorial Lesson 1 - MQL Language Common Functions (1) OrderSend() Function
MT4/MQL4入门到精通EA课程第二课-常用的功能函数
Simple example of .reduce()
低代码开发创新企业应用构建模式
【HNUMSC】C语言第二讲
MT4 / MQ4L entry to the master of EA tutorial lesson two (2) - - MQL language commonly used function account information commonly used functions
《LC刷题总结》——贪心
typescript91-添加任务基本实现
Go-8-Gin框架
在树莓派上使用cpolar(番外篇2)
2020.10.13开发日志
10.1-----19. Delete the Nth node from the bottom of the linked list
MT4 / MQL4 entry to the master of EA course lesson two - commonly used functions
电磁辐射安全标准及检测方法
LeetCode每日两题02:轮转数组 (均1200道)
Using ngrok on Raspberry Pi (Extra 2)
Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules
How to install ngrok in Synology system (Synology 6.X version)