Codeforces Round#451 Div.2 B(思路简单,主要是代码的简洁性问题)

B. Proper Nutrition

Vasya has n burles. One bottle of Ber-Cola costs a burles and one Bars bar costs b burles. He can buy any non-negative integer number of bottles of Ber-Cola and any non-negative integer number of Bars bars.
Find out if it's possible to buy some amount of bottles of Ber-Cola and Bars bars and spend exactly n burles.
In other words, you should find two non-negative integers x and y such that Vasya can buy x bottles of Ber-Cola and y Bars bars and x·a + y·b = n or tell that it's impossible.
Input
First line contains single integer n (1 ≤ n ≤ 10 000 000) — amount of money, that Vasya has.
Second line contains single integer a (1 ≤ a ≤ 10 000 000) — cost of one bottle of Ber-Cola.
Third line contains single integer b (1 ≤ b ≤ 10 000 000) — cost of one Bars bar.
Output
If Vasya can't buy Bars and Ber-Cola in such a way to spend exactly n burles print «NO» (without quotes).
Otherwise in first line print «YES» (without quotes). In second line print two non-negative integers x and y — number of bottles of Ber-Cola and number of Bars bars Vasya should buy in order to spend exactly n burles, i.e. x·a + y·b = n. If there are multiple answers print any of them.
Any of numbers x and y can be equal 0.

数据:
Examples
Input
7
2
3
Output
YES
2 1
Input
100
25
10
Output
YES
0 10
Input
15
4
8
Output
NO
Input
9960594
2551
2557
Output
YES
1951 1949

大体题意:就是直接满足x·a + y·b = n这个式子就可以了,这个就是这个题的关键;

思路:就是选定一个从0开始循环,另一个看能否接着被整除就可以了,然后只要满足整除就跳出,但是这题我在比赛过程中做的时候虽然答案对了,但是时间太长,在与别人的代码比较之后发现,循环的起始位置与循环的长度相同,差别就在于中间进行了过多的算数运算,以至于使得整个程序的时间变得很长,虽然不会出现超时的情况,但是会让整个代码变得很丑;

改进:下一次对于判断一个循环是否完全执行完的时候,如果那个循环到条件是个算术语句的话,尽量专门设置一个标记语句来判断是否中间跳出的;if else的语句可以被三目条件语句代替,有时候直接输出,不进行后面的时候,用三目条件语句可以使代码更简洁;

自己的:

include<stdio.h>

int main()
{

  int n,a,b,x,y,t;
  scanf("%d%d%d",&n,&a,&b);
  for(x=0;x<=n/a;x++)
  {
      t=(n-a*x)%b;
      if(t==0)
      {
          y=(n-a*x)/b;
          break;
      }
  }
  if(x==n/a+1)
      printf("NO\n");
  else
  {
      printf("YES\n");
      printf("%d %d\n",x,y);
  }

  return 0;

}

一个运用标记变量的代码:

include <stdio.h>

int main ()
{

  int n,a,b,x,y,flag=0;
  scanf("%d%d%d",&n,&a,&b);
  for(x=0;x*a<=n;x++)
  {
      if((n-a*x)%b==0) 
      {
          flag=1;
          y=(n-a*x)/b;
          break;
      }
  }
  if(flag) printf("YES\n%d %d\n",x,y);
  else printf("NO\n");
  return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值