C(gcc)、104バイト
f(b,o,u,n,c,y){for(o=u=0;b;u+=y?0:o+0*--b,++o)for(n=o,y=3;n/10;)c=n%10,n/=10,y&=(c-=n%10)<0?:c?2:y;b=u;}
こちらからオンラインでお試しください。
ゴルフをしていない:
f(n, // function: return type and type of arguments defaults to int;
// abusing extra arguments to declare variables
i, // number currently being checked for bounciness
s, // sum of the bouncy numbers
j, // copy of i to be used for checking bounciness in a loop
p, // used for investigating the last digit of j
b) { // whether i is not bouncy; uses the two least significant bits to indicate increasing/decreasing
for(i = s = 0; // check numbers from zero up; initial sum is zero
n; // continue until we have n bouncy numbers
s += b ? 0 // not bouncy, no change to the sum
: i + 0* --n, // bouncy, add it to the sum and one less bouncy number to go
++i) // either way, move to the next number
for(j = i, b = 3; j/10; ) // make a copy of the current number, and truncate it from the right until there is just one digit left
// bounciness starts as 0b11, meaning both increasing and decreasing; a value of 0 means bouncy
p = j % 10, // get the last digit
j /= 10, // truncate one digit from the right
b &= // adjust bounciness:
(p -= j % 10) // compare current digit to the next
< 0 ? // not an increasing number, clear second to least significant bit
: p ? 2 // not a decreasing number, clear least significant bit
: b; // keep it the same
n = s; // gcc shortcut for return s
}
sort
番号を確認して、元の番号と同じかどうかを確認できますか?それは組み込み(sort
)を使用していますが、増加しているかどうかを確認するための厳密な組み込みではありません。監視できないプログラムの要件を確認し、「回避策」のメタ投稿でYなしのXを実行します。