CodeForces679A

CodeForces679A
也是交互题,这个要稍微难一些.
考虑的过程大概是:
(1)肯定没有问的价值,如果问过(2),那么除了(4)之外的偶数都不用问了.
要问(4)的原因是,如果这个数字是(4),那就会被误判为(prime).
然后,质数是要问的,最后判断问出来的数字中约数是否大于等于(2)个,是的话就是合数,否则就是偶数.
但这样会在完全平方数的时候出错,因为它们可能会有一对质数相乘得到,然后就冇了.所以我们就再判断一下完全平方数.
需要注意的是,只询问小于等于(50)的完全平方数和质数就行了,完全平方数也不用全都去问,不全问的原理和问了(2)就不用问大于(4)的偶数一样.
(Code:)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = a ; i <= b ; ++ i)
#define per(i,a,b) for (int i = a ; i >= b ; -- i)
#define pii pair < int , int >
#define X first
#define Y second
#define rint read<int>
#define int long long
#define pb push_back

using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
using std::swap ;
using std::sort ;
using std::unique ;
using std::greater ;

template < class T >
    inline T read () {
        T x = 0 , f = 1 ; char ch = getchar () ;
        while ( ch < '0' || ch > '9' ) {
            if ( ch == '-' ) f = - 1 ;
            ch = getchar () ;
        }
       while ( ch >= '0' && ch <= '9' ) {
            x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
            ch = getchar () ;
       }
   return f * x ;
}

template < class T >
    inline void write (T x) {
       static T stk[100] , top = 0 ;
       if ( x == 0 ) { putchar ('0') ; return ; }
       if ( x < 0 ) { x = - x ; putchar ( '-' ) ; }
       while ( x ) { stk[++top] = x % 10 ; x /= 10 ; }
       while ( top ) { putchar ( stk[top--] + '0' ) ; }
       putchar ('
') ;
    }

int p[55] , cnt , d[] = { 0 , 4 , 9 , 25 , 49 } , tot ;
char ans[5] ;

inline bool check (int x) {
    if ( x == 1 ) return false ;
    for (int i = 2 ; i * i <= x ; ++ i)
        if ( x % i == 0 ) return false ;
    return true ;
}

signed main (int argc , char * argv[]) {
    rep ( i , 1 , 50 ) if ( check ( i ) ) p[++cnt] = i ;
    rep ( i , 1 , cnt ) {
        printf ("%lld
" , p[i] ) ; fflush ( stdout ) ;
        scanf ("%s" , ans ) ; if ( ans[0] == 'y' ) ++ tot ;
    }
    rep ( i , 1 , 4 ) {
        printf ("%lld
" , d[i] ) ; fflush ( stdout ) ;
        scanf ("%s" , ans ) ; if ( ans[0] == 'y' ) ++ tot ;
    }
    puts ( tot < 2 ? "prime" : "composite" ) ;
    system ("pause") ; return 0 ;
}
May you return with a young heart after years of fighting.
原文地址:https://www.cnblogs.com/Equinox-Flower/p/11470908.html