当前位置:网站首页>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;
}边栏推荐
猜你喜欢

torchversion.transforms的使用

PostMan import certificate add certificate

How to install ngrok in Synology system (Synology 6.X version)
![class path resource [bean.xml] cannot be opened because it does not 错误解决方案](/img/e2/6430a511998944a3357984b554b00c.png)
class path resource [bean.xml] cannot be opened because it does not 错误解决方案

在树莓派上使用cpolar(番外篇2)

史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!

基于机器学习之模型树短期负荷预测(Matlab代码实现)

《LC刷题总结》—— 二叉树

MT4/MQL4 Getting Started to Mastering EA Tutorial Lesson 1 - MQL Language Common Functions (1) OrderSend() Function

JDBC technology (1) - a simple JDBC test
随机推荐
多语种翻译-免费多语种翻译软件
JDBC技术(三)——使用Druid数据库连接池测试
Significance Test--Study Notes
德语翻译器在线翻译中文
智能视频监控设计摄像头部分
项目经理VS产品经理,二者到底有何不同?
class path resource [bean.xml] cannot be opened because it does not 错误解决方案
力扣刷题记录8.1-----206. 反转链表
The server quit without updating PID file (/usr/local/mysql/data/localhost.pid).
[机缘参悟-65]:《兵者,诡道也》-6-孙子兵法解读-并战计
Z-Game on grid
Go-11 - Process Control
What is the difference between a project manager and a product manager?
德语翻译-德语在线批量翻译软件
Etcd realize large-scale application service management of actual combat
TP测试查询数据库字段为null或空的字段
虹科技术|如何阻止供应链攻击?
《LC刷题总结》——贪心
MT4/MQL4入门到精通外汇EA教程第一课 认识MetaEditor
Go-8-Gin framework