我的代码

· · 休闲·娱乐

关于C++,在编译时,可打开工具(tool)的第一个选项,在连接器命令行加入以下指令以减小exe文件的大小(命令间以空格隔开)。

-O3:最高级代码优化(删除冗余指令、循环展开等);
-s:移除符号表和调试信息;
-ffunction-sections -fdata-sections -Wl,--gc-sections:删除未调用的函数和数据段(“死代码消除”)。

猜数


#include <stdio.h>
#include <ctime>
#include <windows.h>
inline long long read(){
long long n=0;
scanf("%lld",&n);
return n;
}
int work(){
printf("1~100\n");
int ans=rand()%100+1;
int l=1,r=100,x=read(),spend=1;
while(x!=ans){
if(x<l||x>r)printf("请在范围内读入\n");
else{
if(x>ans)r=x-1;
else l=x+1;
printf("%d~%d\n",l,r);
spend++;
}
x=read();
}
printf("%d次猜对了!\n",spend);
Sleep(2000);
return 0;
}
int main(){
srand(time(0));
printf("给出的两个数表示猜数范围,包含那两个数。\n");
int ju_shu=0,num=0,ping_jun=(1ll<<31)-1,zui_shao=(1ll<<31)-1;
while(true){
ju_shu++;
printf("局数:%d\n平均尝试数:%d\n最少记录:%d\n\n",ju_shu,ping_jun,zui_shao);
int l=1,r=100;
int ans=rand()%100+1;
int spend=0;
while(true){
printf("%d~%d\n",l,r);
int x=read();
if(x<l||x>r)printf("请在范围内猜数\n");
else{
spend++;
if(x==ans)break;
if(x>ans)r=x-1;
else l=x+1;
}
}
printf("恭喜你,用%d次猜对了!",spend);
num+=spend;
ping_jun=num/ju_shu;
zui_shao=zui_shao<spend?zui_shao:spend;
Sleep(2000);
system("cls");
}
printf("程序结束"); 
return 0;
}//-O3 -s -ffunction-sections -fdata-sections -Wl,--gc-sections
### 代码佛
```cpp line-numbers
#include <cstdio>
int main(){
    printf("/*\n"
    "                            _ooOoo_  \n"
    "                           o8888888o  \n"
    "                           88\" . \"88  \n"
    "                           (| -_- |)  \n"
    "                            O\\ = /O  \n"
    "                        ____/`---'\\____  \n"
    "                      .   ' \\\\| |// `.  \n"
    "                       / \\\\||| : |||// \\  \n"
    "                     / _||||| -:- |||||- \\  \n"
    "                       | | \\\\\\ - /// | |  \n"
    "                     | \\_| ''\\---/'' |_/ |  \n"
    "                      \\ .-\\__ `-` ___/-. /  \n"
    "                   ___`. .' /--.--\\ `. . __  \n"
    "                .\"\" '< `.___\\_<|>_/___.' >'\"\".  \n"
    "               | | : `- \\`.;`\\ _ /`;.`/ - ` : | |  \n"
    "                 \\ \\ `-. \\_ __\\ /__ _/ .-` / /  \n"
    "         ======`-.____`-.___\\_____/___.-`____.-'======  \n"
    "                            `=---='  "
    "  \n"
    "         .............................................  \n"
    "                  佛祖保佑             永无BUG \n"
    "          佛曰:  \n"
    "                  写字楼里写字间,写字间里程序员;  \n"
    "                  程序人员写程序,又拿程序换酒钱。  \n"
    "                  酒醒只在网上坐,酒醉还来网下眠;  \n"
    "                  酒醉酒醒日复日,网上网下年复年。  \n"
    "                  但愿老死电脑间,不愿鞠躬老板前;  \n"
    "                  奔驰宝马贵者趣,公交自行程序员。  \n"
    "                  别人笑我忒疯癫,我笑自己命太贱;  \n"
    "                  不见满街漂亮妹,哪个归得程序员?  \n"
    "*/");
    return 0;
}

军备竞赛

#include <bits/stdc++.h>
#include <chrono>//处理时间
#include <windows.h>
//#include <conio.h>//非标准的C语言头文件,用于控制台输入输出操作
#include <bitset>//处理二进制位集合
//#include <winuser.h>//被windows.h包含 
#include <thread>//多线程 
using namespace std;

//数据类型
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;

//常数
const double PI=3.1415925;
const int N=(1<<21)-1;
const int inf=(1ll<<31)-1;

//函数
inline int lowbit(int x){//最小的1所代表的数
    return x&(0-x);
}
inline bool is_num(char x){//是否是数字
    return (x>='0'&&x<='9');
}
//#define max(x,y) x>y?x:y
//#define min(x,y) x<y?x:y
int gcd(int x,int y){//最大公因数
    if(y==0)return x;
    return gcd(y,x%y);
}
long long read(void){//快读,long long
    long long n=0;
    int f=1;
    char c=getchar();
    while(c>'9'||c<'0'){
        if(c=='-')f*=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        n=n*10+c-'0';
        c=getchar();
    }
    return n*f;
}//<stdio.h>

//优化
#define endl '\n'
#define fastio std::ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
inline int clear_all(void){//清空所有输出 
    system("cls");
    return 0;
}
inline int comeback(void){//使输入、输出转向控制台 
    freopen("CON", "r", stdin);
    freopen("CON", "w", stdout);
    return 0; 
}
int work();
int main(){
    int t=1;
    while(t--){
        work();
        if(t)putchar('\n');
    }
    return 0;
}
int work(){
    //代码
    return 0;
}

心跳水立方

#include<bits/stdc++.h>
#include<windows.h>
//#include<cmd.h>
#include<conio.h>
#include<bitset>
#include<winuser.h>
//#include<thread>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
#define Long_Long_Max 9223372036854775807
#define Int_Max 2147483647
//#define SHAKE 5
using namespace std;
extern const int
BLACK=0,
DARKBLUE=1,DBLUE=1,
GREEN=2,
BLUE=3,
RED=4,
PURPLE=5,
DIRT=6,YELLOWGREY=6,
LIGHTGREY=7,LGREY=7,
GREY=8,DGREY=8,DARKGREY=8,
SKYBLUE=9,SBLUE=9,
LGREEN=10,LIGHTGREEN=10,
CANYON=11,
LRED=12,LIGHTRED=12,BRED=12,BRIGHTRED=12,
LPURPLE=13,LIGHTPURPLE=13,
LYELLOW=14,LIGHTYELLOW=14,
WHITE=15
;
typedef BOOL (WINAPI *PROCSETCONSOLEFONT)(HANDLE, DWORD);
PROCSETCONSOLEFONT SetConsoleFont;
void ResizeFont(int a)
{
    HMODULE hKernel32=GetModuleHandle("kernel32");
    SetConsoleFont=(PROCSETCONSOLEFONT)GetProcAddress(hKernel32,"SetConsoleFont");
    SetConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void Color(int text,int background)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),text+background*16);
}
void BColor(int background)
{
    if(background==15)Color(0,background);
    else Color(15,background);
}
void BColor(string background)
{
    int len=background.length();
    for(int i=0;i<len;i++)
    {
        if(background[i]>96)background[i]-=32;
    }
    if(background=="BLACK")Color(15,0);
    if(background=="DARKBLUE"||background=="DBLUE")Color(15,1);
    if(background=="GREEN")Color(15,2);
    if(background=="BLUE")Color(15,3);
    if(background=="RED")Color(15,4);
    if(background=="PURPLE")Color(15,5);
    if(background=="DIRT"||background=="YELLOWGREY")Color(15,6);
    if(background=="LIGHTGREY"||background=="LGREY")Color(15,7);
    if(background=="GREY"||background=="DGREY"||background=="DARKGREY")Color(15,8);
    if(background=="SKYBLUE"||background=="SBLUE")Color(15,9);
    if(background=="LGREEN"||background=="LIGHTGREEN")Color(15,10);
    if(background=="CANYON")Color(15,11);
    if(background=="LRED"||background=="LIGHTRED"||background=="BRED"||background=="BRIGHTRED")Color(15,12);
    if(background=="LPURPLE"||background=="LIGHTPURPLE")Color(15,13);
    if(background=="LYELLOW"||background=="LIGHTYELLOW")Color(15,14);
    if(background=="WHITE")Color(0,15);
}
void cls()
{
    BColor(BLACK);
    system("cls");
}
struct CTRL
{
    void SetPointerVisual(bool visual)
    {
        //1=show     0=hide
        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_CURSOR_INFO cci;
        GetConsoleCursorInfo(hOut,&cci);
        cci.bVisible=visual;
        SetConsoleCursorInfo(hOut,&cci);
    }
};
extern CTRL Ctrl;
struct Bas
{
    void Hi()
    {
        cout<<"Hello world!";
    }
};
extern Bas Basic;
void PrintByChar(string str,int t)
{
    int len=str.length();
    for(int i=0;i<len;i++)
    {
        Sleep(double(t/len));
        cout<<str[i];
    }
    cout<<endl;
}
struct BigNum
{
    string num;
    int length()
    {
        return num.length();
    }
    void Print()
    {
        bool m=0;
        for(int i=0;i<length();i++)
        {
            if(num[i]!='0')m=1;
            if(m)cout<<num;
        }
    }
    string operator+(const BigNum& B)
    {
        //string a=this->num;
        string b=B.num;
        return "1";
    };
};
struct TO
{
    template < typename T >
    string String( T& n )
    {
        std::ostringstream stm ;
        stm<<fixed<<setprecision(16)<<n;
        return stm.str() ;
    }
};
extern TO To;
int GetWindowPosX()
{
    RECT rect;
    HWND hwnd=GetForegroundWindow();
    GetWindowRect(hwnd,&rect);
    return rect.left;
}
int GetWindowPosY()
{
    RECT rect;
    HWND hwnd=GetForegroundWindow();
    GetWindowRect(hwnd,&rect);
    return rect.top;
}
int GetCursorPosX()
{
    POINT pt={0,0};
    GetCursorPos(&pt);
    return pt.x;
}
int GetCursorPosY()
{
    POINT pt={0,0};
    GetCursorPos(&pt);
    return pt.y;
}
int GetSubX()
{
    return GetCursorPosX()-GetWindowPosX();
}
int GetSubY()
{
    return GetCursorPosY()-GetWindowPosY();
}
void gotoxy(int x,int y)
{
    int xx=0x0b;
    HANDLE hOutput;
    COORD loc;
    loc.X=x;
    loc.Y=y;
    hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOutput,loc);
}
void CursorSetup(){
    CONSOLE_CURSOR_INFO cursor_info={1,0}; 
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
long long quickpow(long long a,long long b,long long p){
    long long ans=1,base=a;
    while(b){
        if(b%2){
            ans*=base;
            ans%=p;
        }
        base=base*base%p;
        b>>=1;
    }
    return ans%p;
}
int getlog(long long x){
    int i;
    for(i=1;quickpow(2,i,9223372036854775807)<x;i++);
    return i;
}
struct Node{
    int val;
    int front,back;
};
#define wall 1
#define air 0
#define bullet 2
#define bug 3
int world[25][20];
int playerY=5,playerX=3,lhole=2,phole=5,score=0,maxscore;
unsigned long long frame=0;
void setup(){
    int playerY=5,lhole=2,phole=5,score=0,frame=0;
    for(int i=1;i<=20;i++){
        for(int j=1;j<=15;j++){
            world[i][j]=air;
        }
    }
}
void trymove(){
    if(GetAsyncKeyState('A') && world[playerX][playerY]>=0)playerY--;
    if(GetAsyncKeyState('D') && world[playerX][playerY]<=14)playerY++;
}
void spawn(){
    if(score==20000)lhole--;
    if(score==40000)lhole--;
    //if(score==30000)lhole--;
    if(frame%6==0){
        if(score<5000){
            if(rand()%3==0){
                phole=phole;
            }else if(rand()%3==1){
                if(phole>1)phole--;
            }else if(rand()%3==2){
                if(phole<15)phole++;
            }
        }else if(score<10000){
            if(rand()%5==0){
                phole=phole;
            }else if(rand()%5==1){
                if(phole>1)phole--;
                }else if(rand()%5==2){
                if(phole<15)phole++;
            }else if(rand()%5==3){
                if(phole>2)phole-=2;
            }else if(rand()%5==4){
                if(phole<14)phole+=2;
            }
        }else if(score<15000){
            if(rand()%7==0){
                phole=phole;
            }else if(rand()%7==1){
                if(phole>1)phole--;
            }else if(rand()%7==2){
                if(phole<15)phole++;
            }else if(rand()%7==3){
                if(phole>2)phole-=2;
            }else if(rand()%7==4){
                if(phole<14)phole+=2;
            }else if(rand()%7==5){
                if(phole>3)phole-=3;
            }else if(rand()%7==6){
                if(phole<13)phole+=3;
            }
        }else{
            if(rand()%9==0){
                phole=phole;
            }else if(rand()%9==1){
                if(phole>1)phole--;
            }else if(rand()%9==2){
                if(phole<15)phole++;
            }else if(rand()%9==3){
                if(phole>2)phole-=2;
            }else if(rand()%9==4){
                if(phole<14)phole+=2;
            }else if(rand()%9==5){
                if(phole>3)phole-=3;
            }else if(rand()%9==6){
                if(phole<13)phole+=3;
            }else if(rand()%9==7){
                if(phole>4)phole-=4;
            }else if(rand()%9==8){
                if(phole<12)phole+=4;
            }
        } 
        for(int j=1;j<=15;j++){
            world[21][j]=wall;
            if(abs(j-phole)<=lhole)world[21][j]=air;
        }
        world[21][phole]=bug;
    }else{
        for(int j=1;j<=15;j++){
            world[21][j]=air;
        }
    }       
}
void update(){
    for(int i=2;i<=21;i++){
        for(int j=1;j<=15;j++){
            world[i-1][j]=world[i][j];
        }
    }
}
void screenupdate(){
    gotoxy(0,0);
    printf("*------------------------------*\n");
    for(int i=1;i<=20;i++){
        BColor("BLACK");
        printf("|");
        for(int j=1;j<=15;j++){
            if(i==playerX && j==playerY){
                BColor("WHITE");
                printf("  ");
            }else if(world[i][j]==wall){
                BColor("DIRT");
                printf("  ");
            }else if(world[i][j]==bullet){
                BColor("LYELLOW");
                printf("  ");
            }else if(world[i][j]==air){
                BColor("SBLUE");
                printf("  ");
            }else if(world[i][j]==bug){
                BColor("BLUE");
                printf("  ");
            }
        }
        BColor("BLACK");
        printf("|\n");
    }
    BColor("BLACK");
    printf("*------------------------------*\n");
    cout << "X: " << playerX << " Y: " << playerY <<" debug.frame:" << frame << " score: " << score << " debug.phole:" << phole << "\n";
}
int main(){
    srand(time(0));
    system("mode con cols=30 lines=15");
    CONSOLE_CURSOR_INFO cursor_info={1,0}; 
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); 
    cout << "   JUMP INFINITY\n press any key to continue...\n\n\n\n\n\n   made by @sky_of_stars";
    while(!kbhit());
    system("mode con cols=90 lines=30");
    while(1){
        score=0;
        setup();
        system("mode con cols=40 lines=25");
        while(1){
            frame++;
            if(frame>10000){
                frame=0;
            }
            spawn(); 
            update();
            screenupdate(); 
            if(frame%2==0)trymove();
            if(world[playerX][playerY]==wall || world[playerX][playerY]==bullet){
                Sleep(1000);
                maxscore=max(maxscore,score);
                cls();
                cout << " you died!\n\n User fell from a high place\n";
                cout << " score:" << score;
                cout << "\n maxscore:" << maxscore;
                BColor("BLACK");

                Sleep(3000);
                cls();
                break;
            }
            if(world[playerX][playerY]==bug){
                score+=10;
            }
            score+=5; 
            Sleep(50);
        }
    }
}

计时器

#include <stdio.h>
#include <windows.h>
int main(){
    char f,m,kf,km;
    while(1){
        system("cls");
        scanf("%d%d",&f,&m);
        kf=f,km=m;
        if(f<0||m<0)
            return 0;
        while(1){
            system("cls");
            printf("计%d分%d秒\n余%d分%d秒",int(kf),int(km),int(f),int(m));
            if(m==0){
                f--;
                m=59;
            }else m--;
            if(f<0||m<0)break;
            Sleep(1000);
        }
        Beep(2000,100);
        Beep(2000,100);
        MessageBox(NULL, "时间结束!", "计时器", MB_OK|MB_ICONINFORMATION|MB_SYSTEMMODAL);
    }
    return 0;
}

二十四点

#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
#include <bitset>
#include <winuser.h>
using namespace std;
#define clear_screen system("cls")
int nuzhan[1000000],nu,op;
char opzhan[1000000];
bool yxj(char x,char y){
    if(x=='('||y=='('||x==')'||x==')')
        return false;
    if((x=='+'||x=='-')&&(y!='+'&&y!='-'))
        return false;
    if((x=='*'||x=='/')&&(y=='^'))
        return false;
    return true;
}
void jisuan(){
    int y=nuzhan[nu];
    nuzhan[nu--]=0;
    int x=nuzhan[nu];
    nuzhan[nu--]=0;
    if(opzhan[op]=='+'){
        nuzhan[++nu]=x+y;
    }
    if(opzhan[op]=='-'){
        nuzhan[++nu]=x-y;
    }
    if(opzhan[op]=='*'){
        nuzhan[++nu]=x*y;
    }
    if(opzhan[op]=='/'){
        nuzhan[++nu]=x/y;
    }
    if(opzhan[op]=='^'){
        nuzhan[++nu]=1;
        while(y>0){
            nuzhan[nu]*=x;
            y--;
        }
    }
    opzhan[op--]='\0';
    return ;
}
int solve(string s){
    nu=0;
    op=0;
    for(int i=0;i<s.size();i++){
        int num=0;
        while(s[i]>='0'&&s[i]<='9'){
            num=num*10+s[i]-'0';
            i++;
        }
        if(i>=1&&s[i-1]>='0'&&s[i-1]<='9')
            nuzhan[++nu]=num;
        while(op>0&&nu>1&&yxj(opzhan[op],s[i])){
            jisuan();
        }
        if(i!=s.size())opzhan[++op]=s[i];
        if(opzhan[op]==')'){
            opzhan[op--]='\0';
            while(opzhan[op]!='('){
                jisuan();
            }
            opzhan[op--]='\0';
        }
    }
    while(nu>1){
        jisuan();
    }
    return nuzhan[1];
}
bool check(int a,int b,int c,int d){
    return nuzhan[1]==24;
}
void putans(int a,int b,int c,int d){
    return ;
}
int main(){
    srand(time(0));
    system("mode con cols=30 lines=15");
    printf("    二十四点\n");
    printf(" press any key to continue...\n\n\n");
    printf("   made by zmj05130295\n");
    printf("   制作者  zmj05130295\n");
    while(!kbhit());
    printf("   让我们开始游戏!");
    Sleep(1000);
    while(1){
        system("mode con cols=100 lines=25");
        printf("利用+(加法)、-(减法)、*(乘法)、/(除法)、()(小括号,英文模式)凑出24\n");
        printf("不要添加空格,直接输出算式,形如(6/3)*(13-1)\n");
        printf("无法凑出给出no\n");
        printf("*--------------------------------*\n");
        int a=rand()%13+1,b=rand()%13+1,c=rand()%13+1,d=rand()%13+1;
        printf("四个数是%d %d %d %d\n请输入你的答案:",a,b,c,d);
        string ans;
        cin>>ans;
        if(ans=="no"){
            if(!check(a,b,c,d)){
                printf("对了!");
            }else{
                printf("可以凑出哦!\n");
            }
        }else{
            if(solve(ans)!=24){
                printf("再想想\n正确答案是:");
            }else{
                printf("对了!");
            } 
        }
        Sleep(1000);
    }
    Sleep(2000);
    return 0;
}

下棋

#include <stdio.h>
#include <windows.h>
#include <conio.h>//kibhit()
#include <bitset>
#include <winuser.h>
#include <ctime>
//#include<cmd.h>
//#include<thread>
#include <stdlib.h>//exit(0)
#include <chrono>
typedef long long ll;
typedef unsigned long long ull;
inline void break_all(){//结束所有程序
    exit(0);
}
inline void clear_all(){//清空屏幕
    system("cls");
}
inline bool is_num(char x){//是否为数字
    return x>='0'&&x<='9';
}
inline ll read(){//快读 
    ll n=0;
    short f=1;
    char c=getchar();
    while(!is_num){
        if(c=='-')f=-1;
        c=getchar();
    }
    while(is_num){
        n=n*10+c-'0';
        c=getchar();
    }
    return n*f;
}
int wuziqi_board[5][5];//3X3的棋盘
void game_jingzi(){//井字棋
    clear_all();

    printf("棋盘:\n");
    printf("-------\n");
    printf("|7|8|9|\n");
    printf("--+-+--\n");
    printf("|4|5|6|\n");
    printf("--+-+--\n");
    printf("|1|2|3|\n");
    printf("-------\n");
    printf("在某处下棋请输出该位置的序号(例:第1行第3个就是9)\n");
    bool color_qi=true;//true表示黑子 
    for(int step=1;step<=9;step++){
        printf("当前棋子:");
        if(color_qi)putchar('O');
        else putchar('X');
        putchar('\n');
        int weizhi;
        scanf("%d",&weizhi);

    }
    Sleep(2000);
    return ;
}
int work(){
    clear_all();
    printf("井字棋");
    game_jingzi();
    return 0;
}
int main(){
    srand(time(0));
    system("mode con cols=30 lines=15");
    printf("    游戏名\n");
    printf(" press any key to continue...\n\n\n");
    printf("   made by zmj05130295\n");
    printf("   制作者  zmj05130295\n");
    while(!kbhit());
    printf("   让我们开始游戏!");
    Sleep(1000);
    while(true){
        work();
    }
    return 0;
}

完结撒花

回到目录