商と残りを1つのステップで取得するにはどうすればよいですか? [複製
-
25-10-2019 - |
質問
可能な複製:
分割して同時に残りを取得しますか?
整数師団を2回実行せずに、商と整数部門の残りの両方を単一のステップで取得することは可能ですか?
解決
div
これを行います。見る 参照 と例:
/* div example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
div_t divresult;
divresult = div (38,5);
printf ("38 div 5 => %d, remainder %d.\n", divresult.quot, divresult.rem);
return 0;
}
出力:
38 div 5 => 7, remainder 3.
編集:
C仕様には次のように書かれています。
7.20一般ユーティリティ
The types declared are size_t and wchar_t (both described in 7.17),
div_t
which is a structure type that is the type of the value returned by the div function,
ldiv_t
which is a structure type that is the type of the value returned by the ldiv function, and
lldiv_t
which is a structure type that is the type of the value returned by the lldiv function.
...しかし、それは何の定義を言っているのではありません div_t
は。
他のヒント
はい、呼ばれる標準関数があります div()
(と ldiv
, 、そして多分さえ lldiv
)それはこれを行います。
所属していません StackOverflow