mtsuyugu
起動オプションの解析
(Nested
Flatten)
gflags を使ってみました。 -oq や -d1 のような連続して指定する形式には対応していません。
see: google-gflags
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <iostream>
#include <google/gflags.h>
DEFINE_bool( o, false, "(required)" );
DEFINE_bool( q, false, "quote(optional)" );
DEFINE_int32( d, 0, "debug(required) {0|1|2}" );
using namespace std;
int main ( int argc, char *argv[] ){
google::SetUsageMessage("-o [-q] [-d{0|1|2}] str1 [str2 ...] ");
google::ParseCommandLineFlags(&argc, &argv, true);
if( !FLAGS_o || FLAGS_d < 0 || FLAGS_d > 2 || argc < 2 ){
google::ShowUsageWithFlags(argv[0]);
return 1;
}
cout << "[option]" << endl
<< "o(output): " << (FLAGS_o ? "ON" : "OFF") << endl
<< "q(quote): " << (FLAGS_q ? "ON" : "OFF") << endl
<< "d(debug): " << FLAGS_d << endl
<< endl
<< "[parameter]" << endl
<< "given " << (argc-1) << endl;
for( int i = 1; i < argc; i++ ){
cout << i << ": " << argv[i] << endl;
}
return 0;
}
|
マルバツゲーム
(Nested
Flatten)
MARU: 5765 BATU: 2707 DRAW: 1528 となりました。
RandPlayer.prototype.think の部分を修正することで Player を差し替えることができます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | var Board = function(){ this.init(); };
Board.prototype = {
cell : [],
size : 3,
init : function(){
for( var i = 0; i < this.size; i++ ){
this.cell[i] = [];
}
},
isVacant : function( x, y ){ return this.cell[x][y] == undefined; },
put : function( x, y, id ){ this.cell[x][y] = id; },
judge : function( x, y ){
var i;
var size = this.size;
var cell = this.cell;
for( i = 1; i < size; i++ ){
if( cell[i-1][y] == undefined || cell[i-1][y] != cell[i][y] )
break;
}
if( i == size ) return true;
for( i = 1; i < size; i++ ){
if( cell[x][i-1] == undefined || cell[x][i-1] != cell[x][i] )
break;
}
if( i == size ) return true;
if( x == y ){
for( i = 1; i < size; i++ ){
if( cell[i-1][i-1] == undefined || cell[i-1][i-1] != cell[i][i] )
break;
}
if( i == size ) return true;
}
if( x + y == size - 1 ){
for( i = 1; i < size; i++ ){
if( cell[i-1][size-1] == undefined || cell[i-1][size-i] != cell[i][size-i-1] )
break;
}
if( i == size ) return true;
}
return false;
}
};
var Player = function(){};
Player.prototype = {
put : function( b, x, y ){ b.put( x, y ); },
think: undefined,
win : 0
};
var RandPlayer = function(){};
RandPlayer.prototype = new Player();
RandPlayer.prototype.think = function( b ){
var x,y,tmp;
var size = b.size
var max = size * size;
while(1){
tmp = Math.floor(Math.random() * max );
x = Math.floor( tmp / size );
y = tmp % size;
if( b.isVacant( x, y ) ) break;
}
return {x:x, y:y};
};
var b = new Board();
var max = b.size * b.size;
var target;
var p = [ new RandPlayer(), new RandPlayer() ];
for( var i = 0; i < 10000; i++ ){
b.init();
for( j = 0; j < max; j++ ){
target = p[j%2].think( b );
b.put(target.x, target.y, j%2 );
if( b.judge( target.x, target.y ) )
break;
}
if( j != max ){
p[j%2].win++;
}
}
document.write("MARU: " + p[0].win + "<br/>" );
document.write("BATU: " + p[1].win + "<br/>" );
document.write("DRAW: " + (10000 - p[0].win - p[1].win) + "<br/>" );
|
HTTPでGET その2
(Nested
Flatten)
#3704 を元にしています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <string>
std::string content;
size_t writeMemoryCallback( char *ptr, size_t size, size_t nmemb ){
size_t realsize = size * nmemb;
content.append( static_cast<const char *>( ptr ), realsize );
return realsize;
}
int main( int argc, char *argv[] ){
try {
cURLpp::Cleanup cleaner;
cURLpp::Easy request;
request.setOpt( new cURLpp::Options::Url( "http://ja.doukaku.org/feeds/comments/" ) );
request.setOpt( new cURLpp::Options::WriteFunction(
cURLpp::Types::WriteFunctionFunctor( &writeMemoryCallback )));
request.setOpt( new cURLpp::Options::Proxy( "example.com:80" ) );
request.setOpt( new cURLpp::Options::Timeout( 1 ));
request.perform();
}
catch ( cURLpp::LogicError & e ) {
std::cerr << e.what() << std::endl;
return 1;
}
catch ( cURLpp::RuntimeError & e ) {
std::cerr << e.what() << std::endl;
return 1;
}
std::cout << content << std::endl;
return 0;
}
|
法演算
(Nested
Flatten)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | normalize_law = function( law ){
return function(num){
num %= law;
num += num < 0 ? law : 0;
return num;
}
}
opfunc = (function(){
var func = {
'+' : function(x,y){return x+y },
'-' : function(x,y){return x-y },
'*' : function(x,y){return x*y }
};
return function(opcode){ return func[opcode]; };
})();
calc = function( exp, law ){
if( ! exp.match(/(-?\d+)\s*([-*+])\s*(-?\d+)/) ){
return null;
}
var ope1 = RegExp.$1;
var opcode = RegExp.$2;
var ope2 = RegExp.$3;
var normalize = normalize_law( law );
var opcode = opfunc(opcode);
return opcode
? normalize( opcode( normalize(ope1), normalize(ope2) ))
: null;
}
alert( calc("1+2",10) );
alert( calc("7 + 3",10) );
alert( calc("11 + 12",10) );
alert( calc("3 - 2",10) );
alert( calc("2 - 3",10) );
alert( calc("2 * 3",10) );
alert( calc("11 * 12",10) );
alert( calc("18 * 39",10) );
alert( calc("6 / 2",10) );
|
指定コマンドを別プロセスで起動
(Nested
Flatten)
FreeBSD 6 で作成しました。fork, pipe, dup2 を使って実現しています。実行するコマンドは date です。
see: http://www.ncad.co.jp/~komata/c-kouza3.htm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define R (0)
#define W (1)
int main( void ){
char buf[1024];
int status = 0;
int pipe_c2p[2];
ssize_t read_size;
pid_t pid;
char command[] = "date";
if( pipe(pipe_c2p) == -1 ){
perror("pipe");
exit(1);
}
pid = fork();
if( pid < 0 ){ /* fail to fork */
close( pipe_c2p[W] );
close( pipe_c2p[R] );
perror("fork");
exit(1);
}
else if( pid == 0 ){ /* child process */
close( pipe_c2p[R] );
dup2( pipe_c2p[W], 1 );
close( pipe_c2p[W] );
execlp("/bin/sh", "sh", "-c", command, NULL);
}
else{ /* parent process */
close( pipe_c2p[W] );
while( (read_size = read( pipe_c2p[R], buf, sizeof(buf)-1 ) ) > 0 ){
buf[read_size] = '\0';
printf("%s",buf);
}
if( read_size == -1 ){
perror("read");
}
close( pipe_c2p[R] );
if( waitpid( pid, &status, 0 ) == -1 ){
perror("waitpid");
exit(1);
}
if( WIFEXITED(status) ){
printf("child process exit with status %d\n", WEXITSTATUS(status) );
}
}
return 0;
}
|
自分自身のファイル名を知る方法
(Nested
Flatten)
1 2 3 4 5 6 7 | #include <libgen.h>
#include <stdio.h>
int main( int argc, char *argv[] ){
printf("%s\n", basename(argv[0]) );
return 0;
}
|
情報オリンピック2006年度国内予選問題4
(Nested
Flatten)
配列を2つ用意し、シャッフルは開いている方に代入していきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <stdlib.h>
#include <stdio.h>
int c,i,k,n,m,*card[2],h[2];
int main( void ){
scanf("%d%d",&n,&m);
for(;i<2;){
card[i] = (int*)malloc(sizeof(int)*2*n);
if(!card[i++]){
free(card[0]);
free(card[1]);
return EXIT_FAILURE;
}
}
for(i=0;i<2*n;card[0][i]=++i);
for(;m-->0&&~scanf("%d",&k);){
// h[0] is head, h[1] is midst
h[k==0] = (h[0]+(k?k:n))%(2*n);
if(!k){
for(i=0;i<2*n;i++){
card[c^1][i] = card[c][h[i%2]++];
h[i%2] %= 2*n;
}
c ^= 1;
h[0] = 0;
}
}
for(i=0;i<2*n;printf("%d\n",card[c][i++]));
free(card[0]);
free(card[1]);
return EXIT_SUCCESS;
}
|
情報オリンピック2006年度国内本選問題3
(Nested
Flatten)
標準入出力を使っています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <stdlib.h>
#include <stdio.h>
int cmp( const void *a, const void *b ){
int *pa = (int *)a;
int *pb = (int *)b;
int r = (*pa<*pb)? -1
: (*pa>*pb)? 1
: (*(pa+1)<*(pb+1)) ? -1
: (*(pa+1)>*(pb+1)) ? 1
: 0;
return r;
}
int i,j,n,s,S,*p,dx,dy,q[4];
int main( void ){
scanf("%d",&n);
p = (int *)malloc(sizeof(int)*n*2);
if(!p) return EXIT_FAILURE;
for(i=0;~scanf("%d%d",p+i,p+i+1);i+=2);
qsort(p,n,sizeof(int)*2,cmp);
for(i=0;i<2*n;i+=2){
for(j=i+2;j<2*n;j+=2){
dx = p[j] - p[i];
dy = p[j+1] - p[i+1];
q[0] = p[i] - dy; // rotate 90
q[1] = p[i+1] + dx;
q[2] = q[0] + dx; // opposite vertex
q[3] = q[1] + dy;
if( bsearch(q,p,n,sizeof(int)*2,cmp)
&& bsearch(q+2,p,n,sizeof(int)*2,cmp) ){
s = dx*dx+dy*dy;
S = s>S?s:S;
}
}
}
free(p);
return printf("%d\n",S);
}
|
情報オリンピック2006年度国内本選問題2
(Nested
Flatten)
input に i があれば a[i] = 1 として、その後で連続する a[k] の数を数えていきます。a[0]=1 時は過去の1つ分だけ覚えておきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <stdlib.h>
#include <stdio.h>
int i,n,k,*a,c,d,C;
FILE *fp;
int main( int argc, char *argv[] ){
fp = fopen("input.txt","r");
if( !fp ) return EXIT_FAILURE;
fscanf(fp,"%d%d",&n,&k);
i=sizeof(int)*(n+1);
a = (int*)malloc(i);
if( !a ) return EXIT_FAILURE;
for(i=0;i<=n;a[i++]=0); // clear
for(;~fscanf(fp,"%d",&k);a[k]=1); // input
fclose(fp);
for(i=1;i<=n;i++){
if(a[i]) c++;
k=c+d+*a;
C = k>C?k:C;
if(!a[i]){
if(*a) d = c;
c = 0;
}
}
fp = fopen("output.txt","w");
if( !fp ) return EXIT_FAILURE;
fprintf(fp,"%d",C);
return fclose(fp);
}
|
文字変換表に基く文字列の変換
(Nested
Flatten)
1 で解答します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <string.h>
#include <stdlib.h>
#include <stdio.h>
char c,subst[128];
int i;
int main( int argc, char *argv[] ){
if( argc < 4 ) return EXIT_FAILURE;
for(;i<strlen(argv[1]);i++){
subst[argv[1][i]] = argv[2][i];
}
for(i=0;i<strlen(argv[3]);i++){
c = subst[argv[3][i]];
putchar(c?c:argv[3][i]);
}
return EXIT_SUCCESS;
}
|
情報オリンピック2006年度国内本選問題1
(Nested
Flatten)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <string.h>
#include <stdlib.h>
#include <stdio.h>
int k,*a,b,i,S,s;
FILE *fp;
int main( void ){
fp = fopen("input.txt","r");
if(!fp) return EXIT_FAILURE;
fscanf(fp,"%d%d",&k,&k);
a = (int *)malloc(sizeof(int)*k);
if(!a) return EXIT_FAILURE;
memset(a,0,sizeof(int)*k);
for(;~fscanf(fp,"%d",&b);i++){
s += b - a[i%k];
a[i%k] = b;
S=(i>k&&s>S)?s:S;
}
free(a);
fclose(fp);
fp = fopen("output.txt","w");
if(!fp) return EXIT_FAILURE;
fprintf(fp,"%d",S);
return fclose(fp);
}
|
情報オリンピック2006年度国内予選問題5
(Nested
Flatten)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include <stdlib.h>
#include <string.h>
#include <stdio.h>
int a,b,c,N,*n,*r,i,j,k;
int cmp(const void *a, const void *b){
int A = *(int *)a;
int B = *(int *)b;
if( A < B ) return 1;
if( A > B ) return -1;
return 0;
}
int main( void ){
scanf("%d%d%d\n%d",&a,&b,&c,&N);
r = (int *)malloc(sizeof(int)*(a+b+c+1));
n = (int *)malloc(sizeof(int)*N*4);
if( !r || !n ){
free(r); free(n); return EXIT_FAILURE;
}
for(;~scanf("%d%d%d%d",n+i+1,n+i+2,n+i+3,n+i);i+=4);
for(i=1;i<=a+b+c;r[i++]=2);
// start with all ok records
qsort(n,N,sizeof(int)*4,cmp);
for(i=0;i<4*N;i+=4){
if(n[i]) // all ok
for(j=1;j<4;r[n[i+j++]]=1);
else
for(k=1;k<4;k++)
// if two of three are ok, the rest is NG
if( r[n[i+k]]== 1 && r[n[i+k%3+1]] == 1 ) r[n[i+(k+1)%3+1]] = 0;
}
for(i=1;i<=a+b+c;printf("%d\n",r[i++]));
free(r);
free(n);
return EXIT_SUCCESS;
}
|
情報オリンピック2006年度国内予選問題2
(Nested
Flatten)
1 2 3 4 5 6 7 | #include <stdio.h>
n[31];
main(c){
for(;~scanf("%d",n);n[*n]=1);
for(;c<31;c++)
if(!n[c]) printf("%d\n",c);
}
|
情報オリンピック2006年度国内予選問題1
(Nested
Flatten)
1 2 3 4 5 6 7 8 9 | #include <stdio.h>
int a,b,i,n;
int main( void ){
while( scanf("%d", &n) != -1 ){
if( i++ < 4 ) a += n;
else b += n;
}
return printf("%d\n", a>b?a:b );
}
|
情報オリンピック2007年度国内予選問題3
(Nested
Flatten)
goto 使ってしまいました(汗
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <stdlib.h>
#include <stdio.h>
#define NONE 0
#define TARO 1
#define HANAKO 2
#define SWITCH 3
int main ( void ){
int n[3], i, *card, turn = TARO;
scanf("%d\n", &n[0]);
n[TARO] = n[HANAKO] = n[0];
card = (int *)malloc( sizeof(int) * 2 * n[0] + 1 );
if( !card ) return EXIT_FAILURE;
while( scanf("%d\n", &i ) != EOF )
card[i] = TARO;
for( i = 1; i <= 2*n[0]; i++ )
if( card[i] != TARO )
card[i] = HANAKO;
while(1){
for( i = 1; i <= 2*n[0]; i++ ){
if( card[i] != turn ) continue;
card[i] = NONE;
if( !--n[turn] ) goto output;
turn ^= SWITCH;
}
turn ^= SWITCH;
}
output:
printf("%d\n%d\n",n[HANAKO],n[TARO]);
free( card );
return EXIT_SUCCESS;
}
|
情報オリンピック2007年度国内予選問題4
(Nested
Flatten)
解説にある計算量が O(mn log n) の実装です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <stdlib.h>
#include <stdio.h>
typedef struct {
int x;
int y;
} point;
int cmp_point( const void *a, const void *b ){
point *pa = (point *)a;
point *pb = (point *)b;
if( pa->x < pb->x ) return -1;
if( pa->x > pb->x ) return 1;
if( pa->y < pb->y ) return -1;
if( pa->y > pb->y ) return 1;
return 0;
}
int main( void ){
point *p[2], tmp;
int n[2], i, j, dx, dy;
for( i = 0; i < 2; i++ ){
scanf("%d", &n[i] );
p[i] = (point *)malloc( sizeof(point) * n[i] );
if( !p[i] ){
free(p[0]);
free(p[1]);
return EXIT_FAILURE;
}
for( j = 0; j < n[i]; j++ )
scanf("%d %d", &p[i][j].x, &p[i][j].y );
}
qsort( p[1], n[1], sizeof(point), cmp_point );
for( j = 0; j < n[1]; j++ ){
dx = p[1][j].x - p[0][0].x;
dy = p[1][j].y - p[0][0].y;
for( i = 1; i < n[0]; i++ ){
tmp.x = p[0][i].x + dx;
tmp.y = p[0][i].y + dy;
if( !bsearch( &tmp, p[1], n[1], sizeof(point), cmp_point) )
break;
}
if( i != n[0] ) continue;
printf("%d %d\n", dx, dy );
break;
}
free(p[0]);
free(p[1]);
return EXIT_SUCCESS;
}
|
情報オリンピック2007年度国内予選問題2
(Nested
Flatten)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <stdlib.h>
#include <stdio.h>
int main( int argc, char *argv[] ){
char *p;
int n = 0, j = 0, i = 0;
if( argc < 2 ) return EXIT_FAILURE;
for( p = argv[1]; *p; p++ ){
if( *p == 'J' || *p == 'I' ){
n = *p;
}
else if( n && *p == 'O' && *(p+1) == 'I' ){
if( n == 'J' ) j++;
if( n == 'I' ) i++;
n = 'I';
p++;
}
else{ n = 0; }
}
printf("%d\n%d\n",j,i);
return EXIT_SUCCESS;
}
|
情報オリンピック2007年度国内予選問題1
(Nested
Flatten)
next >>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] ){
int coins[] = {500,100,50,10,5,1};
int c, i = 0, n = 0;
if( argc < 2 ){
return EXIT_FAILURE;
}
c = 1000 - atoi( argv[1] );
while( c > 0 ){
n += c/coins[i];
c %= coins[i++];
}
printf("%d\n",n);
return EXIT_SUCCESS;
}
|

mtsuyugu
#8675()
[
C
]
Rating0/0=0.00
Rating0/0=0.00-0+
[ reply ]