SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
14.
B問題 アルゴリズム
• どうやってコードにしよう?
– Nが奇数であるかどうかの判定
• A % B で、「AをBで割った余り」を求められる。
– 言語によっては%の代わりにmodだったりするから調べよう!
• if(N%2==1) で奇数判定出来る!
– C言語などはif(N%2)だけでも良い。
– これを使えば、答えの求め方は簡単!
int N ← input
int ans;
if(N%2 == 1) ans = N + 1;
else ans = N – 1;
output → ans
2016/3/12 14