// http://t...content-available-to-author-only...h.net/test/read.cgi/tech/1354393458/861
#include <stdio.h>
#include <sys/time.h>
// 戻り値 = -1 : 入力値が負
// -2 : 結果が桁あふれする
// メモ = ・計算結果が 31bit-1 = 2147483647 までのものをサポート
int f_31bit(int d)
{
int dd;
int k;
int n; // 桁数
int zero; // ゼロ個数
int res; // 結果
// check
if (d < 0) {
return -1;
}
if (d < 10) {
return d;
}
// 桁数数えなど
dd = d / 10;
k = 1;
for (n = 1; dd >= k; n++) {
k *= 10;
}
// 0以外をresにいれながら0を数える
res = zero = 0;
for (; n > 0; n--) {
if (d >= k) {
res = res * 10 + d / k;
} else {
zero++;
}
d %= k;
k /= 10;
}
// 0を付け加える
for (; zero > 0; zero--) {
if (res > 214748364) { // resが次で10倍されたら桁あふれする
return -2;
}
res *= 10;
}
// 終了
return res;
}
int main()
{
int v; // 値
v = 20010307;
// v = 1020304050;
// v = 2147483647; // 良い
// v = 2000000014; // 良い
// v = 2000000020; // ダメ (計算結果の 2200000000 は 2147483647(== 2^31-1)を越える)
int ex = 100000; // 実行回数
int count = 10; // 計測回数
int res;
int i, j;
struct timeval s, e;
double sum; // 結果
printf("f_31bit(%d);を%d回実行する時間の計測を%d回し、平均時間を出す\n", v
, ex
, count
); if (0 <= (res = f_31bit(v))) { // 試しに実行
sum = 0.0;
for (j = 0; j < count; j++) {
gettimeofday(&s, NULL);
for (i = 0; i < ex; i++) {
res = f_31bit(v);
}
gettimeofday(&e, NULL);
sum += (e.tv_sec - s.tv_sec) + (e.tv_usec - s.tv_usec) / 1000000.0;
}
printf("result = %d, time = %fms\n", res
, 1000.0 * sum
/ ((double) count
)); } else if (-1 == res) {
} else if (-2 == res) {
printf("結果が 2147483647 を越える\n"); }
return 0;
}
Ly8gaHR0cDovL3QuLi5jb250ZW50LWF2YWlsYWJsZS10by1hdXRob3Itb25seS4uLmgubmV0L3Rlc3QvcmVhZC5jZ2kvdGVjaC8xMzU0MzkzNDU4Lzg2MQojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN5cy90aW1lLmg+CgovLyDmiLvjgorlgKQgICA9IC0xIDog5YWl5Yqb5YCk44GM6LKgCi8vICAgICAgICAgICAgLTIgOiDntZDmnpzjgYzmoYHjgYLjgbXjgozjgZnjgosKLy8g44Oh44OiICAgICA9IOODu+ioiOeul+e1kOaenOOBjCAzMWJpdC0xID0gMjE0NzQ4MzY0NyDjgb7jgafjga7jgoLjga7jgpLjgrXjg53jg7zjg4gKaW50IGZfMzFiaXQoaW50IGQpCnsKICAgIGludCBkZDsKICAgIGludCBrOwogICAgaW50IG47ICAgICAgICAgICAgICAgICAgICAgIC8vIOahgeaVsAogICAgaW50IHplcm87ICAgICAgICAgICAgICAgICAgIC8vIOOCvOODreWAi+aVsAogICAgaW50IHJlczsgICAgICAgICAgICAgICAgICAgIC8vIOe1kOaenAoKICAgIC8vIGNoZWNrCiAgICBpZiAoZCA8IDApIHsKICAgICAgICByZXR1cm4gLTE7CiAgICB9CiAgICBpZiAoZCA8IDEwKSB7CiAgICAgICAgcmV0dXJuIGQ7CiAgICB9CiAgICAvLyDmoYHmlbDmlbDjgYjjgarjgakKICAgIGRkID0gZCAvIDEwOwogICAgayA9IDE7CiAgICBmb3IgKG4gPSAxOyBkZCA+PSBrOyBuKyspIHsKICAgICAgICBrICo9IDEwOwogICAgfQoKICAgIC8vIDDku6XlpJbjgpJyZXPjgavjgYTjgozjgarjgYzjgokw44KS5pWw44GI44KLCiAgICByZXMgPSB6ZXJvID0gMDsKICAgIGZvciAoOyBuID4gMDsgbi0tKSB7CiAgICAgICAgaWYgKGQgPj0gaykgewogICAgICAgICAgICByZXMgPSByZXMgKiAxMCArIGQgLyBrOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHplcm8rKzsKICAgICAgICB9CiAgICAgICAgZCAlPSBrOwogICAgICAgIGsgLz0gMTA7CiAgICB9CgogICAgLy8gMOOCkuS7mOOBkeWKoOOBiOOCiwogICAgZm9yICg7IHplcm8gPiAwOyB6ZXJvLS0pIHsKICAgICAgICBpZiAocmVzID4gMjE0NzQ4MzY0KSB7ICAvLyByZXPjgYzmrKHjgacxMOWAjeOBleOCjOOBn+OCieahgeOBguOBteOCjOOBmeOCiwogICAgICAgICAgICByZXR1cm4gLTI7CiAgICAgICAgfQogICAgICAgIHJlcyAqPSAxMDsKICAgIH0KCiAgICAvLyDntYLkuoYKICAgIHJldHVybiByZXM7Cn0KCmludCBtYWluKCkKewogICAgaW50IHY7ICAgICAgICAgICAgICAgICAgICAgIC8vIOWApAogICAgdiA9IDIwMDEwMzA3OwovLyAgICB2ID0gMTAyMDMwNDA1MDsKLy8gICAgdiA9IDIxNDc0ODM2NDc7ICAgICAgICAgICAgIC8vIOiJr+OBhAovLyAgICB2ID0gMjAwMDAwMDAxNDsgICAgICAgICAgICAgLy8g6Imv44GECi8vICAgIHYgPSAyMDAwMDAwMDIwOyAgICAgICAgICAgICAvLyDjg4Djg6EgKOioiOeul+e1kOaenOOBriAyMjAwMDAwMDAwIOOBryAyMTQ3NDgzNjQ3KD09IDJeMzEtMSnjgpLotorjgYjjgospCiAgICBpbnQgZXggPSAxMDAwMDA7ICAgICAgICAgICAgLy8g5a6f6KGM5Zue5pWwCiAgICBpbnQgY291bnQgPSAxMDsgICAgICAgICAgICAgLy8g6KiI5ris5Zue5pWwCiAgICBpbnQgcmVzOwogICAgaW50IGksIGo7CiAgICBzdHJ1Y3QgdGltZXZhbCBzLCBlOwogICAgZG91YmxlIHN1bTsgICAgICAgICAgICAgICAgIC8vIOe1kOaenAoKICAgIHByaW50ZigiZl8zMWJpdCglZCk744KSJWTlm57lrp/ooYzjgZnjgovmmYLplpPjga7oqIjmuKzjgpIlZOWbnuOBl+OAgeW5s+Wdh+aZgumWk+OCkuWHuuOBmVxuIiwgdiwgZXgsIGNvdW50KTsKICAgIGlmICgwIDw9IChyZXMgPSBmXzMxYml0KHYpKSkgeyAgLy8g6Kmm44GX44Gr5a6f6KGMCiAgICAgICAgc3VtID0gMC4wOwogICAgICAgIGZvciAoaiA9IDA7IGogPCBjb3VudDsgaisrKSB7CiAgICAgICAgICAgIGdldHRpbWVvZmRheSgmcywgTlVMTCk7CiAgICAgICAgICAgIGZvciAoaSA9IDA7IGkgPCBleDsgaSsrKSB7CiAgICAgICAgICAgICAgICByZXMgPSBmXzMxYml0KHYpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGdldHRpbWVvZmRheSgmZSwgTlVMTCk7CiAgICAgICAgICAgIHN1bSArPSAoZS50dl9zZWMgLSBzLnR2X3NlYykgKyAoZS50dl91c2VjIC0gcy50dl91c2VjKSAvIDEwMDAwMDAuMDsKICAgICAgICB9CiAgICAgICAgcHJpbnRmKCJyZXN1bHQgPSAlZCwgdGltZSA9ICVmbXNcbiIsIHJlcywgMTAwMC4wICogc3VtIC8gKChkb3VibGUpIGNvdW50KSk7CiAgICB9IGVsc2UgaWYgKC0xID09IHJlcykgewogICAgICAgIHByaW50Zigi5YWl5Yqb5YCk44GM6LKgXG4iKTsKICAgIH0gZWxzZSBpZiAoLTIgPT0gcmVzKSB7CiAgICAgICAgcHJpbnRmKCLntZDmnpzjgYwgMjE0NzQ4MzY0NyDjgpLotorjgYjjgotcbiIpOwogICAgfQoKICAgIHJldHVybiAwOwp9Cg==