有趣的C程序(无关算法,只有想法)

作者在 2008-02-22 08:08:05 发布以下内容

#include<stdio.h>
void main(char _)
{
for(--_;putchar(_++["J!Mpwf!Zpv\24\1"]-1););
}

注:里面的 \24 是我自己加上去的,在VC中多打印了两个感叹号,如果其它编译器不能通过的话,把这几个字删了

---------------------------------------------------------------------------------------

int main()
{
int i,a=2,b=6,r=0,n,z,e[4];
for(i=4;i;--i,r=0)
{
   
--a;--b;
   
for(n=0,z=1;4-n;++n,z<<=1)

r+=(n+2)%3*(1&n^((a+b)&z)>>n);
   
*(r+e)=-((a<<1)*5+b);
    a
+=!(3&i);
    a
<<=1;
    b
+=3*!(3&i);
}

do{printf("%c",*(i+e));}
while(4-++i);
}

---------------------------------------------------------------------

main(){
   
int i;
    unsigned
char _[1|(1<<1)]={
        ((
1<<(1<<(1+(1<<1))))>>1)|((1<<((1<<1)+1))<<(1<<1)<<1)
       
|(1<<(1<<(1<<1)))+(1<<(1<<1))|((1+1)),
        (((
1<<(1<<(1+(1<<1))))>>1)
       
|(1<<(1<<(1<<1)))<<1|(1<<(1<<(1<<1)))
       
|(1<<(1<<1)<<1)|(1<<(1<<1))|(1+1))};
   
for(i=0;i<((1<<1)<<1);i++)
        putchar(_[(i
>>1)]+(i&1)*(-(i>>1)|1)*((1<<1)<<1)+((1-((i&(1<<1))>>1))*i&1)*(1<<1));
}

----------------------------------------------------------------------

main(){
    char i=0,d[5]={1},p[]={2,3,5,7};
   
for(;i<4;i++)
        d[
1]=(d[0]=d[0]*p[i]+(i>2)*4)+(i<<1),d[3]=(d[2]=d[2]*p[i]+(i==1||i==2)+1)-i-1;
    puts(d);
}
--------------------------------------------------------------------------

void main(int a)
{
 char* x = "bB_Z#^B_Z#h1AI.BMB5#VB2]2:B]]=6#RB@1.]]L12#NB6N:]BN#JFHL1@D6#";
 while(a = *x / 4) a -= 8, printf("\n%*s" + !!a, a, "_/_/_/" + *x++ % 4 * 2);
 
}

              _/      _/                           _/
             _/      _/                           _/
            _/_/_/_/_/    _/_/      _/_/ _/      _/       _/_/      _/ _/_/
           _/      _/  _/     _/  _/    _/      _/     _/     _/   _/_/   _/
          _/      _/  _/_/_/_/_/ _/     _/     _/     _/_/_/_/_/  _/
         _/      _/   _/         _/    _/     _/      _/         _/
        _/       _/    _/_/_/     _/_/_/_/_/  _/_/_/   _/_/_/   _/

----------------------------------------------------------------------------------------------

#include <stdio.h>
int main(int a, char*b)
{
   
return  !(a-1)?main(2,&"\n47b8kihjl#aae[cxjerf_kxg"[0]):(putchar(b[a+=(a+2)[b]-a[b]]),(a?main(a,b):0));
}
---------------------------------------------------------------------------------------------

#include <stdio.h>
#define ___(c,f) for(;!v&&*_!=c;++_)if('0'<=*_&&*_<='9'&&i==*_-'0'){++f,v=0x##f;break;}
#define ____(c,f) for(;*_!=c;++_)if(f&&'0'<=*_&&*_<='9'||f&&'a'<=*_&&*_<='f'){v+=(*_>'9'?*_-'a'+10:*_-'0');*_=0;break;}
int main()
{
  
char __[]="4<<1=8;0+5+7=c;7&6=-2+8;b+2=d",*_=__;

  
for(int b=0,c=0,d=0,v=0,i=1; i<=8; _=__,b=c=d=v=0,++i)
   {
      ___(
'=',d);___(';',c); (v==0)?(++b,v=176):(v<<=4);
      ____(
'&',b);____('=',c);____(';',d); putchar(v);
   }
  
return 0;
}

-----------------------------------------------------------------------------------

#include <stdio.h>

#define S(x, y, z) (((long)(x) << (y)) | (z))
int main() 

    char _[] = {1 << 0, 1 << 1, 1 << 2, 1 << 3};
    long __ = S(S(S(_[3] + _[1], _[2], _[2] + _[3]), _[3], S(_[2] + _[3], _[2], _[2])), S(_[3], _[1], 0), S(S(S(_[3] + _[1], _[2], _[0]), _[3], S(_[2] + _[3] + _[0], _[2], 0)), S(_[3], _[0], 0), S(S(_[3] + _[1], _[2], _[2] + _[3]), _[3], S(_[2] + _[3], _[2], _[2]))));

    printf("%s", &__);
    getchar();
    return 0;    
}

-------------------------------------------------------------------------------------------------

#include <stdio.h>
main()
{
    int _,_____,bbb,T_T,o,O,I;
    int a,b,c,d,e;
    I=(O=(o=(T_T=(bbb=(_____=(_=1)*2)*2)*2)*2)*2)*2;
    a=-_-bbb;
    b=-_||bbb;
    c=-_____-bbb;
    d=T_T;
    e=~~~I>_<~~~!bbb;
    printf("%d %d %d %d %d\n",a,b,c,d,e);
    printf("%d\n",I<O>_<O>I==O^_^O?_-_:-_-bbb^_^o);
}

注:输出结果没啥,主要是程序中包含了很多文字表情

=====================================================

自打印1
void main(void){char* s="void main(void){char* s=%c%s%c;printf(s,042,s,042);}";
 printf(s,042,s,042);}
------------------------------------------------------------------------------------------

自打印2
char a[2][1024]={{99,104,97,114,32,97,91,50,93,91,49,48,50,52,93,61,123},{125,59,10,105,110,116,32,109,97,105,110,40,118,111,105,100,41,123,105,110,116,32,105,44,106,59,112,114,105,110,116,102,40,34,37,115,34,44,97,91,48,93,41,59,102,111,114,40,105,61,48,59,105,60,50,59,105,43,43,41,123,112,114,105,110,116,102,40,34,123,34,41,59,102,111,114,40,106,61,48,59,97,91,105,93,91,106,93,59,106,43,43,41,112,114,105,110,116,102,40,34,37,100,37,99,34,44,97,91,105,93,91,106,93,44,97,91,105,93,91,106,43,49,93,61,61,39,92,48,39,63,39,125,39,58,39,44,39,41,59,112,114,105,110,116,102,40,34,37,99,34,44,105,61,61,49,63,39,44,39,58,39,92,48,39,41,59,125,112,114,105,110,116,102,40,34,37,115,34,44,97,91,49,93,41,59,114,101,116,117,114,110,32,48,59,125}};
int main(void){int i,j;printf("%s",a[0]);for(i=0;i<2;i++){printf("{");for(j=0;a[i][j];j++)printf("%d%c",a[i][j],a[i][j+1]==0?'}':',');printf("%c",i==0?',':'\0');}printf("%s",a[1]);return 0;}

注:两个自打印的程序,如果在VC中,需要自己添加头文件

============================================================

有下面的一段代码
int main(int argc, char* argv[])
{
       cout<<"白日依山尽, ";
       return 0;
}
请在不修改main()函数的情况下打印出"白日依山尽,黄河入海流,欲穷千里目,更上一层楼"。

-----------------------------------------------------------------------------------

网友一:        这种实现的第一种实现是使用atexit()函数。该函数是C运行时函数,在main()函数退出之后会执行此函数。因此,我们只要在这里设置一个钩子,让程序退出时执行一段我们的代码,就可以达到此目的。注意,钩子函数不能有任何参数,也不能返回任何值。完整的实现如下: void at_exit_p( void )
...{
       cout << "黄河入海流, " << "欲穷千里目, " << "更上一层楼. ";
} // can also use "_onexit"
int r = atexit( at_exit_p ); int main(void)
...{
       cout << "白日依山尽, ";
       return 0;
}
第二种实现是借助自定义类全局变量销毁的机制。对于自定义类,如果我们在程序中使用该类的全局变量,那么在程序退出时,该类的析构函数会被调用。我们只要在该析构函数内添加需要打印的诗句,就可以达到题目的要求。完整的代码如下: class CPrint
...{
public:
       ~CPrint()
       ...{
           cout << "黄河入海流, " << "欲穷千里目, " << "更上一层楼. ";
       }
}; CPrint g_print; int main(void)
...{
       cout << "白日依山尽, ";
       return 0;
}

--------------------------------------------------------------------------------

网友二:

虽然在C++在一般推荐不要使用宏,但在一些特定的场合,宏还是能发挥很大的作用。下面就是一些宏替换法,思路应该是不言自明的了。 1、替换main()函数 #define main main()
...{
       cout<<"白日依山尽, "<<"黄河入海流, "<<"欲穷千里目, "<<"更上一层楼. ";
       return 0;
}
int no_use
int main()
...{
       cout<<"白日依山尽, ";
       return 0;
}
2、替换cout #define cout cout << "白日依山尽, "<<"黄河入海流, "<<"欲穷千里目, "<<"更上一层楼. ";while (0) cout int main(void)
...{
       cout << "白日依山尽, ";
       return 0;
}
3、替换return #define return cout << "黄河入海流, 欲穷千里目, 更上一层楼. "; return int main()
...{
       cout<<"白日依山尽, ";
       return 0;
}
----------------------------------------------------------------------------

李明华 13:36:53
这种方法是通过各种方式来使原有的代码不再起作用。

 1、添加类作用域 int main()
...{
       cout << "白日依山尽, " << "黄河入海流, " << "欲穷千里目, " << "更上一层楼. ";
       return 0;
} class a
...{
       int main()
       ...{
           cout<<"白日依山尽, ";
           return 0;
       }
};
2、使用注释,精彩 int main()
...{
       cout<<"白日依山尽, "<<"黄河入海流, "<<"欲穷千里目, "<<"更上一层楼. ";
       return 0;
} /**//*
int main()
{
cout<<"白日依山尽, ";
return 0;
}
*/
3、使用预定义宏 #if 0 int main(void)
...{
       cout << "白日依山尽, ";
       return 0;
} #endif int main(void)
...{
       cout << "白日依山尽," << endl << "黄河入海流," << endl << "欲穷千里目," << endl << "更上一层楼." << endl;
       return 0;
}
或 #define AAA
#ifdef AAA
int main()
...{
       cout <<"白日依山尽, "<<"黄河入海流, "<<"欲穷千里目, "<<"更上一层楼. ";
       return 0;
}
#else
int main()
...{
       cout<<"白日依山尽, ";
       return 0;
}
#endif
4、使用重定义 int main()
...{
       cout << "白日依山尽, " << "黄河入海流, " << "欲穷千里目, " << "更上一层楼. ";
       return 0;
} #define main no_use int main(void)
...{
       cout << "白日依山尽, ";
       return 0;
}
------------------------------------------------------------------------------------

网友三:通过重载原有<<操作符,修改打印的内容,达到补全诗句的目的。完整的代码如下: ostream& operator << (ostream & out, const char* pc)
...{
       string s1(pc);
       string s2("白日依山尽, ");        if (s1 != s2)
           printf("%s", pc);       
       else
           printf("白日依山尽, 黄河入海流, 欲穷千里目, 更上一层楼. ");              return out;
} int main(void)
...{
       cout << "白日依山尽, ";
       return 0;
}
--------------------------------------------------------------------------------------

网友四:通过调用VirtualProtectEx来修改,通过修改main()函数的入口地址来达到修改打印内容的目的。完整的代码如下: int main(int argc, char* argv[]); int mine()
...{
       cout << "白日依山尽, 黄河入海流, 欲穷千里目, 更上一层楼. ";
       return 0;
} int SetHook()
...{
       DWORD flag = PAGE_EXECUTE_READWRITE;
       DWORD mineAddr = (DWORD)mine;
       DWORD mainAddr = (DWORD)main;
       mineAddr -= (mainAddr + 5);        VirtualProtectEx(GetCurrentProcess(), (BYTE*)mainAddr, 200, flag, &flag);
       ((BYTE*)mainAddr)[0] = 0xe9;
       ((BYTE*)mainAddr)[1] = (BYTE)(mineAddr);
       ((BYTE*)mainAddr)[2] = (BYTE)(mineAddr>>8);
       ((BYTE*)mainAddr)[3] = (BYTE)(mineAddr>>16);
       ((BYTE*)mainAddr)[4] = (BYTE)(mineAddr>>24);
       VirtualProtectEx(GetCurrentProcess(), (BYTE*)mainAddr, 200, flag, &flag);        return 0;
} int s = SetHook(); int main(int argc, char* argv[])
...{
       cout << "白日依山尽, ";
       return 0;
}

---------------------------------------------------------------------------

我的做法(题目类似,不过是输出字母表): #include <stdio.h>
void printf(char *s)
{
    puts("abcdefg\nhijklmn\nopq rst\nuvw xyz\n");
}
int main(void)
{
    printf("abcdefg\n");
    return 0;
}
VC6通过

#define __STDIO_DEF_
#include <stdio.h>
void printf(char *s)
{
    puts("abcdefg\nhijklmn\nopq rst\nuvw xyz\n");
}
int main(void)
{
    printf("abcdefg\n");
    return 0;
}
TC2通过(不过感觉有点莫名,怎么puts函数还能用)

=======================================================

  有段程序如下: #include <stdio.h>
int main(void)
{
    int i = 0;
    for (i = 1; i <= 9; i = _________________)
        printf("%3d", i);
    printf("\n");
    return 0;
}问:在空白处填什么使得程序输出为:
1  4  7 10 13 16 19 22 25
------------------------------------------------------------------------------

 几种参考答案:
a.  i = printf("  4  7 10 13 16 19 22 25")
b.  i = (printf("  4  7 10 13 16 19 22 25"), 10)
       用逗号表达式是因为预防printf执行失败之后返回一个负数
c.  i = i + (printf("\b\b\b%3d", 3 * i - 2)) * 0 + 1
d.  i = 10) ; for (i = 1; i <=25; i+=3
==========================================================

有段代码如下: if (s > 0)
    printf("1\n");
else if (s > 9)
    printf("2\n");
else
    printf("3\n");问:在什么情况下,这段代码输出为:
2

 ----------------------------------------------------------------------------------------------------

a.  #define printf(x)  printf("2\n")
       这种方法很无耻,-_-
b. int foo()
{
    static int i = -10;
    return i += 10;
}
#define  s  foo()c.   int foo()
  {
      printf("2\n");
      exit(0);
  }
  #define  s  foo()

-----------------------------------------------------------------------------------------------

 我的解法:#include <stdio.h>
class c
{
public:
    bool operator>(const int i)
    {
        if(i==0) return false;
        if(i==9) return true;
        return false;
    }
};
int main(void)
{
    c s;
    if (s > 0)
        printf("1\n");
    else if (s > 9)
        printf("2\n");
    else
        printf("3\n");
    return 0;
} VC通过

C/C++ | 阅读 3046 次
文章评论,共0条
游客请输入验证码