通過個人不懈努力,研究出了微信紅包算法!ios
數值是純隨機的,按算法來的,能夠保存測試文件噠c++
測試文件長這樣:算法
建議開一個快捷方式調小字體來使用,避免佔滿屏;數組
程序在這裏~(複製打開)微信
https://files.cnblogs.com/files/data-joel/紅包機率模擬.zip (版本1.0)
ide
https://files.cnblogs.com/files/data-joel/%E7%BA%A2%E5%8C%85%E6%A6%82%E7%8E%87%E6%A8%A1%E6%8B%9F1.0.2.zip(版本1.0.2)測試
(上方都已失效)字體
https://files.cnblogs.com/files/data-joel/%E7%BA%A2%E5%8C%85%E6%A6%82%E7%8E%87%E6%A8%A1%E6%8B%9F1.1.3.zip(版本1.1.3.alpha)spa
https://files.cnblogs.com/files/data-joel/%E7%BA%A2%E5%8C%85%E6%A6%82%E7%8E%87%E6%A8%A1%E6%8B%9F1.2.4.zip(版本1.2.4)日誌
代碼本碼:
/* Name: Wechat redpack simulator 1.1.3 Copyright: DA Author: DA Date: 18/10/20 22:48 Description: */ #include<bits/stdc++.h> #include<Windows.h> #include<conio.h> using namespace std; string getTime()//時間獲取,返回string類型 { time_t timep; time (&timep); char tmp[64]; strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S",localtime(&timep) );//對日期和時間進行格式化 return tmp; }; void color(int m) //更改顏色 { HANDLE consolehend;//句柄處理 consolehend = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(consolehend, m);//參數一是句柄,二是顏色代碼。 }; int main() { if(!kbhit())//按鍵檢測 { system("cls");//清屏 color(4); cout<<"紅包分配模擬器"<<endl<<"做者:DDDDDA"<<endl; color(8); cout<<endl<<"按任意鍵開始"; } getch(); double sumlin,sumall; system("cls"); color(4); int n,times;//人數,次數 cout<<"輸入人數~"; cin>>n; n++; cout<<"輸入錢數~"; double sum,man[n],sum2;//錢數,人數累加數組,臨時變量 cin>>sum; cout<<"輸入次數~"; cin>>times; double i2,n2;//初始化臨時浮點變量 n2=n; sumall=n*sum; char percent='%';//百分號,用於輸出。 for(int i=0;i<n;i++) { i2=i;n2=n; man[i]=0;//初始化 system("cls"); printf("初始化中。。。\n進度:%.2lf%c",i2/n2*100,percent); } for( ; ; ) { system("cls"); cout<<"初始化已完成"<<endl; cout<<"按任意鍵開始模擬!!"; if(kbhit()) { break; } } int x; cout<<"第"<<1<<"/"<<times<<"輪正在進行"<<endl; srand((unsigned int)(time(NULL))); for(int i=1;i<=times;i++) { sum2=sum; for(int j=0;j<n-1;j++) { x=ceil(sum2/(n))*2; if(x==0) x=1; color(4); man[j]+=((rand()+0.01)/double(RAND_MAX))+rand()%x;//算法 sum2-=man[j]; } man[n]+=sum2; system("cls"); color(0xb); printf("第%d/%d輪正在進行\n",i,times); for(int k=0;k<n-1;k++) { color(0xe); printf("%-4d",k+1); color(4); printf("-已取得 %.2lf 軟妹幣\n",man[k]); } } double big=-1000;//儲存最大項 double smal=1000000;//儲存最小項 int bi=0,sm=0;//最大最小項的序號 for(int i=0;i<n;i++) sumlin+=man[i]; for(int i=0;i<n;i++) man[i]+=(sumall-sumlin)/n; for(int i=0;i<n-1;i++) { i2=i; system("cls"); printf("模擬已結束 \n計算中。。。\n%.2lf%c",i2/n2*100,percent); if(man[i]>big) { big=man[i]; bi=i; } if(man[i]<smal) { smal=man[i]; sm=i; } } system("cls"); cout<<"計算已完成"<<endl; Sleep(1000); system("cls"); for(int i=0;i<n-1;i++) { //輸出 if(i==bi) { color(0xe);cout<<"最多"<<setw(4)<<i+1;color(4);cout<<"-共拿到約";color(0xe);cout<<fixed<<setprecision(2)<<man[bi];color(4);cout<<"軟妹幣"<<endl; } if(i==sm) { color(0xa);cout<<"最少"<<setw(4)<<i+1;color(4);cout<<"-共拿到約";color(0xa);cout<<fixed<<setprecision(2)<<man[sm];color(4);cout<<"軟妹幣"<<endl; } if(i!=sm&&i!=bi) { color(4); cout<<" "<<setw(4)<<i+1<<"-共拿到約"<<fixed<<setprecision(2)<<man[i]<<"軟妹幣"<<endl; } } color(0xC); cout<<endl<<"*按F保存到文件*"<<endl; if(kbhit()) { if(getch()=='F'||getch()=='f') { system("pause"); char fname[20];//文件名稱 cout<<"輸入文件名稱(帶擴展名)>>"; scanf("%s", fname); ofstream outfile; outfile.open(fname,std::ios::out); outfile<<"*模擬信息*"<<endl; double iii,nnn;nnn=n; outfile<<"模擬時間:"<<getTime()<<endl<<"總人數:"<<n-1<<endl<<"輪數:"<<times<<endl<<"每輪分配的錢數:"<<sum<<endl<<"----------------------------------------"<<endl; outfile<<"*模擬結果*"<<endl; for(int i=0;i<n-1;i++) { iii=i; printf("保存中。。。\n%2.lf%c",iii/nnn*100,percent); //輸出 system("cls"); if(i==bi) { color(0xe);outfile<<"最多"<<setw(4)<<i+1;color(4);outfile<<"-共拿到約";color(0xe);outfile<<fixed<<setprecision(2)<<man[bi];color(4);outfile<<"軟妹幣"<<endl; } if(i==sm) { color(0xa);outfile<<"最少"<<setw(4)<<i+1;color(4);outfile<<"-共拿到約";color(0xa);outfile<<fixed<<setprecision(2)<<man[sm];color(4);outfile<<"軟妹幣"<<endl; } if(i!=sm&&i!=bi) { color(4); outfile<<" "<<setw(4)<<i+1<<"-共拿到約"<<fixed<<setprecision(2)<<man[i]<<"軟妹幣"<<endl; } } cout<<"文件已保存。"<<endl; return 0; } } system("pause"); }
/* Name: Wechat redpack simulator 1.2.4beta Copyright: DA Author: DA Date: 23/10/20 23:37 Description: */ #include<bits/stdc++.h> #include<Windows.h> #include<conio.h> using namespace std; int limit=0;//數據錯誤累加器 bool reto;//是否校準 short slash;//閃動 string getTime()//時間獲取,返回string類型 { time_t timep; time (&timep); char tmp[64]; strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S",localtime(&timep) );//對日期和時間進行格式化 return tmp; } void color(int m) //更改顏色 { HANDLE consolehend;//句柄處理 consolehend = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(consolehend, m);//參數一是句柄,二是顏色代碼。 } void load(double c,int ll)//進度條 參數一是進度,參數二是顏色 { int i; color(ll); printf("\n進度:["); for(i=1;i<=c/4;i++) printf("\\"); color(8); for(i=i;i<=25;i++) printf("\\"); color(ll); printf("]"); printf(" %.0lf%c\n",c,'%'); } int main() { for( ; ; ) { system("cls");//清屏 color(4); cout<<"紅包分配模擬器"<<endl<<"做者:DDDDDA"<<endl; if(slash==0) color(8); else color(7); if(slash==2) color(0xf); if(slash==3) color(0); cout<<endl<<"按任意鍵開始"; slash++;slash%=4; Sleep(40); if(kbhit()) break; } getch(); double sumlin,sumall; //已分配的錢數和應分配的錢數,用於校準 system("cls"); color(4); int n,times;//人數,次數 cout<<"輸入人數~"; cin>>n; n++; cout<<"是否校準(Y/N)"; yn: switch(getch()) { case 'Y':reto=true;color(2);cout<<endl<<"Y"<<endl;color(4);break; case 'N':reto=false;color(0xc);cout<<endl<<"N"<<endl;color(4);break; case 'y':reto=true;color(2);cout<<endl<<"Y"<<endl;color(4);break; case 'n':reto=false;color(0xc);cout<<endl<<"N"<<endl;color(4);break; default:goto yn; } cout<<"輸入錢數~"; double sum,man[n],sum2;//錢數,人數累加數組,臨時變量 cin>>sum; cout<<"輸入次數~"; cin>>times; cout<<"輸入每行輸出的個數(不等於零)"; int line; cin>>line; line=abs(line); if(line==0) line=1; double i2,n2;//初始化臨時浮點變量 n2=n; double t2=times; sumall=times*sum; char percent='%';//百分號,用於輸出。 for(int i=0;i<n;i++) { i2=i;n2=n; color(0xE); man[i]=0;//初始化 system("cls"); printf("初始化中。。。\n"); load(i2/n2*100,0xE); } for( ; ; ) { color(4); system("cls"); cout<<"初始化已完成"<<endl; cout<<"按任意鍵開始模擬!!"; if(kbhit()) { break; } } int x; cout<<"第"<<1<<"/"<<times<<"輪正在進行"<<endl; srand((unsigned int)(time(NULL))); for(int i=1;i<=times;i++) { sum2=sum; for(int j=0;j<n-1;j++) { x=ceil(sum2/(n))*2; if(x==0) x=1; color(4); man[j]+=((rand()+0.01)/double(RAND_MAX))+rand()%x;//算法 sum2-=man[j]; } man[n]+=sum2; system("cls"); color(0xb); printf("第%d/%d輪正在進行\n",i,times); load(i/t2*100,9); for(int k=0;k<n-1;k++) { color(0xe); printf("%-4d",k+1); color(4); printf("-已取得 %-5.2lf RMB",man[k]); if((k+1)%line==0) printf("\n"); else printf(" "); } } double big=-1000;//儲存最大項 double smal=1000000;//儲存最小項 int bi=0,sm=0;//最大最小項的序號 double n3=n; //校準用變量 if(reto==true) { for(int i=0;i<n;i++) { i2=i; system("cls"); color(0xB); printf("模擬已結束\n校準中。。。"); load(i2/n2*50,0xB); sumlin+=man[i]; } } if(reto==true) { int ali=i2/n2*50; for(int i=0;i<n;i++) { i2=i; system("cls"); color(0xb); printf("模擬已結束\n校準中。。。"); load(i2/n2*50+ali,0xB); man[i]*=sumall/sumlin; } for(int i=0;i<n-1;i++) { i2=i; system("cls"); color(3); printf("模擬已結束\n計算中。。。"); load(i2/n2*100,3); if(man[i]>big) { big=man[i]; bi=i; } if(man[i]<smal) { smal=man[i]; sm=i; } } } system("cls"); cout<<"計算已完成"<<endl; Sleep(1000); system("cls"); for(int i=0;i<n-1;i++) { //輸出 if(i==bi) { color(0xe);cout<<"最多"<<setw(4)<<i+1;color(4);cout<<"-共拿到約"; color(0xe); if(reto==false) { cout<<fixed<<setprecision(2)<<man[bi];color(4);cout<<"軟妹幣"<<endl; } else { cout<<man[bi];color(4);cout<<"軟妹幣"<<endl; } } if(i==sm) { color(0xa);cout<<"最少"<<setw(4)<<i+1;color(4);cout<<"-共拿到約"; color(0xa); if(reto==false) { cout<<fixed<<setprecision(2)<<man[sm];color(4);cout<<"軟妹幣"<<endl; } else { cout<<man[sm];color(4);cout<<"軟妹幣"<<endl; } } if(i!=sm&&i!=bi) { color(4); if(reto==false) { cout<<" "<<setw(4)<<i+1<<"-共拿到約"<<fixed<<setprecision(2)<<man[i]<<"軟妹幣"<<endl; } else { cout<<" "<<setw(4)<<i+1<<"-共拿到約"<<man[i]<<"軟妹幣"<<endl; } } } system("pause"); color(0xC); cout<<endl<<"*按F保存到文件,Q鍵退出*"<<endl; char a; a=getch(); if(a=='F'||a=='f') { system("pause"); char fname[20];//文件名稱 cout<<"輸入文件名稱(帶擴展名)>>"; scanf("%s", fname); ofstream outfile; outfile.open(fname,std::ios::out); outfile<<"*模擬信息*"<<endl; double iii,nnn;nnn=n; outfile<<"模擬時間:"<<getTime()<<endl<<"總人數:"<<n-1<<endl<<"輪數:"<<times<<endl<<"每輪分配的錢數:"<<sum<<endl<<"----------------------------------------"<<endl; outfile<<"*模擬結果*"<<endl; for(int i=0;i<n-1;i++) { iii=i; printf("保存中。。。"); load(iii/nnn*100,0xc); //輸出 system("cls"); if(i==bi) { outfile<<"最多"<<setw(4)<<i+1;color(4);outfile<<"-共拿到約";color(0xe);outfile<<fixed<<setprecision(2)<<man[bi];color(4);outfile<<"軟妹幣"<<endl; } if(i==sm) { outfile<<"最少"<<setw(4)<<i+1;color(4);outfile<<"-共拿到約";color(0xa);outfile<<fixed<<setprecision(2)<<man[sm];color(4);outfile<<"軟妹幣"<<endl; } if(i!=sm&&i!=bi) { outfile<<" "<<setw(4)<<i+1<<"-共拿到約"<<fixed<<setprecision(2)<<man[i]<<"軟妹幣"<<endl; } } cout<<"文件已保存。"<<endl; return 0; } if(a=='Q'||a=='q') return 0; }
代碼需經過DEVC++編譯哦哦哦(懼怕VS會出問題)
另外請先裝好環境再下載來用!!
後續算法會繼續更新!!
默默問一句:複製了,能不能關注再走呀?(點個贊也行呀)
/*版本號1.0.2:*/ "·修復了測試文件內容錯誤的bug" "·更改了對齊方式" "·增長了註釋 /*版本號1.1.3 alpha:*/ "·增長算法數值準確度,添加了兩個變量"" /*版本號1.1.3 beta:*/ "·增長了校準\對齊功能" "·增長了數值準確性" /*版本號1.2.4:*/ "·加載樣式已改成進度條" "·增長了細節性bug" "·增長了行對齊方式,節省屏幕空間" "·已經可選是否校準" "·捨棄了本來的測試文件風格" "·加入了十數個變量"