1. 程式人生 > >1040 Round-number 數學題

1040 Round-number 數學題

Description

Most of the time when rounding a given number, it is customary to round to some multiple of a power of 10. However, there is no reason why we cannot use another multiple to do our rounding to. For example, you could round to the nearest multiple of 7, or the nearest multiple of 3.
Given an int n and an int b, round n to the nearest value which is a multiple of b. If n is exactly halfway between two multiples of b, return the larger value.

Input

Each line has two numbers n and b,1<=n<=1000000,2<=b<=500
Output

The answer,a number per line.
Sample Input

5 10
4 10

Sample Output

10
0

題意:輸出離n最近的b的倍數
分析:
1. n ≤ b
① n < b/2 → 0
② n ≥ b/2 → b
2. n > b
d = n % b
① d < b/2 → 0+n-d
② d ≥ b/2 → b+n-d

#include<cstdio>
#include<iostream> using namespace std; typedef long long LL; int main() { #ifdef _DEBUG freopen("data.in", "r", stdin); #endif // _DEBUG int n, b; while (scanf("%d%d", &n, &b) == 2) { int d = n % b; int ans = n - d + ((2 * d < b) ? 0 : b); //注意不能寫成int ans = n - d + ((d < b/2) ? 0 : b)整型變數
printf("%d\n", ans); } return 0; }

相關推薦

1040 Round-number 數學題

Description Most of the time when rounding a given number, it is customary to round to some multiple of a power of 10. However, th

CSU 1040: Round-number

Description    Most of the time when rounding a given number, it is customary to round to some multiple of a power of 10. However, there is no reason why w

POJ-3252 Round Number

題意:給你2個數a,b,求[a,b]區間中的數滿足2進位制0>=1的個數. (1≤a<b≤ 2,000,000,000). 思路:用數位DP來解決,dp[i][count0][count1],表示搜尋到第i位的時候,0與1的數量。記憶化搜尋到底的時候如果0的

Python標準庫:內建函式round(number[, ndigits])

本函式是實現對浮點數進行四捨五入的計算。引數number是浮點數;引數ndigits是保留幾位小數,預設是0值。不過要注意的是轉換出來的小數表示可能還是差異,最好使用庫專用的數字來計算四捨五入運算。例子:#round() print('round(0.5)=', round

JD 題目1040:Prime Number (篩法求素數)

rime 簡單 set end std tdi href num mod OJ題目:click here~~ 題目分析:輸出第k個素數 貼這麽簡單的題目,目的不清純 用篩法求素數的基本思想是:把從1開始的、某一範圍內的正整數從小到大順序排列

CodeFroce Round 340 div2 E XOR and Favorite Number【莫隊算法】

logs pairs num ble ber col nod 操作 app 題面: Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query

Codeforces Round #480 (Div. 2) E. The Number Games

hid boa sse next contest sample art oid ssis E. The Number Games time limit per test 3 seconds memory limit per test 256 megabytes in

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫隊算法 + 異或和前綴和的巧妙】

lag ans integer ons lap stand 推出 open closed 任意門:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit p

[csa round#1]Number Elimination——動態規劃+計數 dalaos' blogs Some Links

題目大意: 你有 n n n個方塊排成一排,每個方塊有一個權值

Codeforces Round #491 (Div. 2) E Bus Number —— 排列組合

題目連結:http://codeforces.com/contest/991/problem/E 有重複問題的全排列是AnnAnum[0]num[0]Anum[1]num[1]...Anum[9]num[9]AnnAnum[0]num[0]Anum[1]num

[csa round#1]Number Elimination——動態規劃+計數

題目大意: 你有nnn個方塊排成一排,每個方塊有一個權值aia_iai​,你每次可以選擇一個二元組(x,y)x&lt;y(x,y) x&lt;y(x,y)x<y,並消除x和y中權值

【Codeforces Round 340 (Div 2)E】【莫隊演算法 真實區間思想】XOR and Favorite Number m組區間詢問 問區間中多少連續段異或值為k

E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input standard input output

【C數學題D貪心】Codeforces Round #461(Div.2)

A copy一個原始檔會新產生一個原始檔和一個副本,copy一個副本會新產生兩個副本,現在有一個原始檔,問能否copy出 xx 個副本和 yy 個原始檔? 瞎搞,注意一些特殊情況。 #include<cstdio> using namesp

Codeforces Round #228 (Div. 2), problem: (A) Fox and Number Game

Note In the first example the optimal way is to do the assignment: x2 = x2 - x1. In the second example the optimal sequence of operations is: x3 = x3 -

Codeforces Round #266 (Div. 2) C. Number of Ways

C. Number of Ways time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

hdu1212 Big Number &第六屆山東省賽Single Round Math (同餘定理,大數取模)

題目大意:每次輸入兩個數,第一個是高精度,第二個數小於100000;求 a mod b 根據同餘定理: (a+b)% c = (a%c+ b%c)%c (a*b)%c =  ( a%c* b%c)%

BestCoder Round #91

output focus block contain scrip tin int rip cte A.Lotus and Characters(技巧 + 思維) Problem Description Lotus has n kinds of characters,ea

消失的數字(number)

哪些 code b+ 描述 橡皮擦 什麽 正序 for 消失 消失的數字(number) Time Limit:1000ms Memory Limit:128MB 題目描述 rsy擁有n個數,這n個數分別是a1,a2,…,an。 後來出現了一個熊孩子zhw,用橡皮擦