实际上这个想法就是在无符号数与-1的比较上做文章BigSnake.NET 写了:7 楼很厉害, 怎么想到的??
-1 按无符号是最大的数,如果i的第一次是0,第二次是-1,目标只要把 n 变成无符号数就可以了。而 &n 是个无符号数,符合这个原则。
代码: 全选
int i, n=99; main() { for(i=0; i<n; i--); { printf("#"); } }
代码: 全选
#!/bin/bash
TEST_VAR='int i,n=99;main(){for(i=0;i<n;i--){printf("#");}}'
#Generated by Python with
#''.join(map(lambda x: '' if chr(x).isupper() else chr(x), range(32, 127)))
#except '$','(',')', '{','}', '[', ']', '@', "\`", "\"", "#", "'", "\\"
possible_chars=' !'"%&*+,-./0123456789:;<=>?^_abcdefghijklmnopqrstuvwxyz|~"
len=${#TEST_VAR}
num_chars=${#possible_chars}
tmpfile=$(mktemp /tmp/test-$$.XXXXXXX)
outfile="/tmp/test_sh.out"
function is_valid_and_ok()
{
echo $1 > $tmpfile
gcc -xc -c -o $outfile.o $tmpfile &>/dev/null
if [[ $? -eq 0 ]]
then
gcc -o $outfile $outfile.o &>/dev/null
if [[ $? -ne 0 ]]; then
return 0
fi
#nohup $outfile > $resultfile 2> /dev/null &
#sleep 1 && killall $outfile 2> /dev/null
n=$(./get_run_result.pl $outfile)
echo $n
if [[ $n -eq 99 ]] || [[ $n -eq 100 ]] || [[ $n -eq 1 ]]
then
return 1
fi
fi
return 0
}
#main
i=0
while(($i<$len - 16));
do
tmp="${TEST_VAR:0:$i}${TEST_VAR:$((i+1))}"
echo "delete: $tmp"
is_valid_and_ok "$tmp"
if [ $? -eq 1 ]
then
echo "result ==> $tmp" 1>&2
fi
i=$(($i+1))
done;
i=4
while (($i < $len - 14));
do
j=0
while(($j<$num_chars));
do
tmp="${TEST_VAR:0:$i}${possible_chars:$j:1}${TEST_VAR:$i}"
echo "add: $tmp"
is_valid_and_ok "$tmp"
if [ $? -eq 1 ]
then
echo "result ==> $tmp" 1>&2
fi
j=$(($j+1))
done
i=$(($i+1))
if (($i == 11)) ; then
i=17
fi
done;
i=0
while(($i<$len - 16));
do
j=0
while(($j<$num_chars));
do
tmp="${TEST_VAR:0:$i}${possible_chars:$j:1}${TEST_VAR:$((i+1))}"
echo "replace: $tmp"
is_valid_and_ok "$tmp"
if [ $? -eq 1 ]
then
echo "result ==> $tmp" 1>&2
fi
j=$(($j+1))
done
i=$(($i+1))
if (($i == 11)) ; then
i=17
fi
done;
代码: 全选
#!/usr/bin/perl -w
use strict;
use warnings;
my $n;
my $chars;
open(my $in_file, "$ARGV[0]|") or exit 1;
$n = read($in_file, $chars, 101);
print "$n\n";
close $in_file;
代码: 全选
import sys, string, os
def TestSet():
orig = 'int i, n=99; main() { for(i=0; i<n; i--) { printf("#"); } }'
for i in string.printable:
for j in range(0, len(orig)):
yield orig[:j]+i+orig[j:]
yield orig[:j]+i+orig[j+1:]
yield orig[:j] + orig[j+1:]
i = 0
for test in TestSet():
src_name = str(i)+'.c'
f = file(src_name,'w')
f.seek(0)
f.write(test+'\n')
f.close()
if os.system('gcc '+src_name+' 2> /dev/null') != 0 :
os.system('rm '+src_name)
else:
print 'now a.out is ok', i
os.system('./a.out | head -c 101 > result & sleep .2 ; killall a.out')
f = file('result','r')
f.seek(0)
result = f.readline()
f.close()
if result == '#'*99:
os.system('mv '+src_name+' ./99')
elif result == '#'*1:
os.system('mv '+src_name+' ./1')
elif result == '#'*100:
os.system('mv '+src_name+' ./100')
else:
os.system('rm '+src_name)
i+=1
球猫很暴力啊.BigSnake.NET 写了:http://paste.ubuntu.org.cn/7877
我也写了一个脚本, 超时定为0.2s所以找不到屏蔽for的那个解
代码: 全选
import sys, string, os def TestSet(): orig = 'int i, n=99; main() { for(i=0; i<n; i--) { printf("#"); } }' for i in string.printable: for j in range(0, len(orig)): yield orig[:j]+i+orig[j:] yield orig[:j]+i+orig[j+1:] yield orig[:j] + orig[j+1:] i = 0 for test in TestSet(): src_name = str(i)+'.c' f = file(src_name,'w') f.seek(0) f.write(test+'\n') f.close() if os.system('gcc '+src_name+' 2> /dev/null') != 0 : os.system('rm '+src_name) else: print 'now a.out is ok', i os.system('./a.out | head -c 101 > result & sleep .2 ; killall a.out') f = file('result','r') f.seek(0) result = f.readline() f.close() if result == '#'*99: os.system('mv '+src_name+' ./99') elif result == '#'*1: os.system('mv '+src_name+' ./1') elif result == '#'*100: os.system('mv '+src_name+' ./100') else: os.system('rm '+src_name) i+=1