USACO sec1.5 Superprime Rib

没打表。

/*
PROG : sprime
LANG : C++
*/
# include <stdio.h>

/*******************************/
char isp(int x)
{
    int i;
    if (x%2 == 0) return x == 2;
    if (x%3 == 0) return x == 3;
    if (x%5 == 0) return x == 5;
    for (i = 7; i*i < x; i+= 2)
        if (x%i == 0) return 0;
    return 1;
}

void g(int x, int rem)
{
    int i;
    if (rem == 0) printf("%d\n", x);
    else for (i = 1; i <= 9; i += 2)
        if (isp(x*10+i)) g(x*10+i, rem-1);
}
/*******************************/

void solve(void)
{
    int i, n;
    scanf("%d", &n);
    for (i = 2; i <= 7; ++i) if (isp(i))
        g(i, n-1);
}

int main()
{
    freopen("sprime.in", "r", stdin);
    freopen("sprime.out", "w", stdout);
    
    solve();
    
    fclose(stdin);
    fclose(stdout);
        
    return 0;
}

 

posted on 2012-08-21 11:07  getgoing  阅读(264)  评论(0编辑  收藏  举报

导航