Tuesday, July 15, 2014

Hackerrank Ad Infinitum July 14 - Sherlock and Divisors solution

Hackerrank Ad Infinitum July 14 - Sherlock and Divisors:  https://www.hackerrank.com/contests/infinitum-jul14/challenges/sherlock-and-divisors

Hackerrank Ad Infinitum July 14 - Sherlock and Divisors editorial: https://www.hackerrank.com/contests/infinitum-jul14/challenges/sherlock-and-divisors/editorial

Hackerrank Ad Infinitum July 14 - Sherlock and Divisors solution: http://ideone.com/0irtP6


#include <iostream> #include <cstdio> using namespace std; int main() { int t, n, cnt; scanf("%d", &t); while(t--) { cnt=0; scanf("%d", &n); for(int i=1; i*i<=n; i++) if(n%i==0) { if(i%2==0) cnt++; if(i*i!=n && n/i%2==0) cnt++; } printf("%d\n", cnt); } return 0; }

Solution version 2: http://ideone.com/mNrA8K

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int t, n, cnt;
    scanf("%d", &t);
    while(t--) {
        cnt=0;
        scanf("%d", &n);
        for(int i=1; i*i<=n; i++) if(n%i==0) {
            if(~i & 1) cnt++;
            if(i*i!=n && ~(n/i)&1) cnt++;
        }
        printf("%d\n", cnt);
    }
    return 0;
}




No comments:

Post a Comment