LoginSignup
0
0

正整数の切り上げの式 【備忘録】

Last updated at Posted at 2023-07-12

Atcoderの解説に出てくる式に度々詰まるのでメモ

詳しく教えてくれてるサイト

自分なりにめも

除数を3とし、被除数1〜5まで考えてみる

1/3= 1/3
2/3= 2/3
3/3= 1
4/3= 1+1/3
5/3= 1+2/3

小数点以下のうち一番小さいのは1/3 つまり1/除数

1-(1/除数)=(除数-1)/除数を足して、繰り上げてから切り捨てることで、整数の切り上げができる。

c++
int a; //被除数
int b; //除数
int ans //計算結果

ans = a/b +(b-1)/b 

式を整理して

c++
int a; //被除数
int b; //除数
int ans //計算結果

ans = (a + b - 1 ) / b; 

値の大きさにより型にも気をつける

int : 2^31-1
long long : 2^63-1

関数

  • 四捨五入:round()
  • 切り上げ:ceil()
  • 切り捨て:floor()
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0