Vasya has a non-negative integer n. He wants to round it to nearest integer, which ends up with 0. If n already ends up with 0, Vasya considers it already rounded.
For example, if n = 4722 answer is 4720. If n = 5 Vasya can round it to 0 or to 10. Both ways are correct.
For given n find out to which integer will Vasya round it.
The first line contains single integer n (0 ≤ n ≤ 109) — number that Vasya has.
Print result of rounding n. Pay attention that in some cases answer isn't unique. In that case print any correct answer.
5
0
113
110
1000000000
1000000000
5432359
5432360
In the first example n = 5. Nearest integers, that ends up with zero are 0 and 10. Any of these answers is correct, so you can print 0 or 10.
四舍五入,没有看完题就在打,错了啊
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n; cin>>n; cout<<(n/10+(n%10>=5))*10; return 0; }
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.
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.
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.
7
2
3
YES
2 1
100
25
10
YES
0 10
15
4
8
NO
9960594
2551
2557
YES
1951 1949
In first example Vasya can buy two bottles of Ber-Cola and one Bars bar. He will spend exactly 2·2 + 1·3 = 7 burles.
In second example Vasya can spend exactly n burles multiple ways:
- buy two bottles of Ber-Cola and five Bars bars;
- buy four bottles of Ber-Cola and don't buy Bars bars;
- don't buy Ber-Cola and buy 10 Bars bars.
In third example it's impossible to but Ber-Cola and Bars bars in order to spend exactly n burles.
直接for暴力枚举啊
本来在想扩欧的事情,ax+by=gcd(a,b),这样的话可以直接化为扩欧,然后解出a,b,然后扩大相应倍数,然后再给他都加到非负数就可以了
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n,a,b; cin>>n>>a>>b; for(int i=0; i<=n/a; i++) { if((n-(a*i))%b==0) { printf("YES "); printf("%d %d",i,(n-(a*i))/b); return 0; } } printf("NO"); return 0; }
exgcd的实现
#include<bits/stdc++.h> using namespace std; typedef long long ll; void exgcd(ll a,ll b,ll &d,ll &x,ll &y) { if(!b) { d=a; x=1; y=0; return ; } else { exgcd(b,a%b,d,y,x); y-=(a/b)*x; } } int main() { ll n,a,b; scanf("%lld %lld %lld",&n,&a,&b); ll d,x,y; exgcd(a,b,d,x,y); if(n%d!=0) { puts("NO"); return 0; } ll ta=a/d,tb=b/d; x*=(n/d),y*=(n/d); ll p=(x%tb+tb)%tb; ll tl=abs(x-p)/tb; if(x>p)y+=tl*ta; else y-=tl*ta; if(y>=0)printf("YES %lld %lld ",p,y); else puts("NO"); return 0; }