首页 > 代码库 > Colidity--CountDiv

Colidity--CountDiv

很巧妙的思路:先计算A之前的被K除的最大数,然后计算B-A之间能填充多少个K

 1 // you can use includes, for example: 2 // #include <algorithm> 3  4 // you can write to stdout for debugging purposes, e.g. 5 // cout << "this is a debug message" << endl; 6  7 int solution(int A, int B, int K) { 8     // write your code in C++11 9     if(A % K ==0)10     {11         return (B-A)/K+1;//加上A12     }13     else14     {15         return (B-(A-A%K))/K;16     }17 }

 

Colidity--CountDiv