92 条题解
-
-1deepinseas LV 10 @ 2021-02-09 17:16:20
#include <fstream>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <bitset>#define N 400
using namespace std;
int n, m, len, leng, idx, ans[N], temp[N];
string str;
bitset <N> num, num2, num10, tmp;inline bitset <N> add(bitset <N> x, bitset <N> y)
{
bitset <N> tmp;
do
{
tmp = x & y;
x = x ^ y;
y = tmp << 1;
} while (y.any());
return x;
}inline void carry(int x[N], int y)
{
x[y + 1]++;
x[y] -= 10;
}signed main()
{
cin >> str;
len = str.length();
num = str[0] - '0';for (int i = 1; i < len; i++)
{
num10 = str[i] - '0';
num2 = num << 1;
num <<= 3;
num = add(num, num2);
num = add(num, num10);
}for (int i = N - 1; i >= 0; i--)
{
if (num.test(i))
{
num.flip(i);
m = i;
num <<= 1;
num[0] = 1;
break;
}
}memset(ans, 0, sizeof(ans));
memset(temp, 0, sizeof(temp));
temp[0] = temp[1] = 1;
idx = 0;
while (true)
{
if (idx > m) break;
if (num.test(idx))
{
leng = ans[0] > temp[0] ? ans[0] : temp[0];
ans[0] = leng;
for (int i = 1; i <= leng; i++)
{
ans[i] += temp[i];
if (ans[i] > 9) carry(ans, i);
}
if (ans[ans[0] + 1] > 0) ans[0]++;
}
for (int i = 1; i <= temp[0]; i++)
temp[i] *= 2;
for (int i = 1; i <= temp[0]; i++)
if (temp[i] > 9) carry(temp, i);
if (temp[temp[0] + 1] > 0) temp[0]++;
idx++;
}for (int i = ans[0]; i > 0; i--)
printf("%d", ans[i]);
return 0;
} -
-12020-04-28 10:21:00@
考虑变成n/2的情形递推即可。
n = int(input()) def solve(x): if x == 1: return 1 if x % 2 == 0: return solve(x//2)*2-1 return solve((x-1)//2)*2+1 print(solve(n))
-
-12018-04-10 20:10:55@
从1开始手推大概十几个就能摸索出规律了,把N变成二进制后,去掉首位,余下部分乘2加1就ok。
#include <stdio.h> #include <string.h> char tpc; int idx; int a[400],bin[400]; bool scancheck(char x); void getBinary(int x[400],int bin[400]); void overturn(int x[400]); void getTen(int bin[400],int a[400]); void incTen(int a[400],int tp[400]); int main(){ while (true){ scanf("%c",&tpc); if (!scancheck(tpc)) break; idx++; a[idx]=tpc-'0'; } a[0]= idx; overturn(a); getBinary(a,bin); if (bin[0]==1){ printf("%d",1); return 0; } bin[bin[0]]=0; bool flag=false; while((bin[bin[0]]==0)&&(!flag)) { bin[0]--; if (bin[0]==0) flag=true; } if (flag) bin[0]=1; bin[0]++; for (int i=bin[0];i>1;i--) bin[i]=bin[i-1]; bin[1]=1; getTen(bin,a); for (int i=a[0];i>0;i--) printf("%d",a[i]); return 0; } bool scancheck(char x){ if (x>'9') return false; if (x<'0') return false; return true; } void getBinary(int a[400],int bin[400]){ //memset(bin,0,sizeof(bin)); bool flag=false; int idx=0; while (!flag){ idx++; bin[idx]=a[1]%2; for (int i=a[0];i>0;i--){ if (a[i]%2==1) a[i-1]+=10; a[i]/=2; } while ((a[a[0]]==0)&&(!flag)) { a[0]--; if (a[0]==0) flag=true; } } bin[0]=idx; return; } void getTen(int bin[400],int a[400]){ memset(a,0,sizeof(a)); int tp[400]; memset(tp,0,sizeof(tp)); tp[0]=1; tp[1]=1; int idx=0; while (true){ idx++; if (bin[idx]==1) incTen(a,tp); if (idx==bin[0]) break; for (int i=1;i<=tp[0];i++) tp[i]*=2; for (int i=1;i<=tp[0];i++){ if (tp[i]>9){ tp[i+1]++; tp[i]-=10; } } if (tp[tp[0]+1]>0) tp[0]++; } return; } void incTen(int a[400],int tp[400]){ int maxLen=a[0]>tp[0]?a[0]:tp[0]; for (int i=1;i<=maxLen;i++) { a[i]+=tp[i]; if (a[i]>9){ a[i+1]++; a[i]-=10; } } a[0]=maxLen; if (a[a[0]+1]>0) a[0]++; return; } void overturn(int x[400]){ int y[400]; for (int i=1;i<=x[0];i++) y[i]=x[x[0]+1-i]; y[0]=x[0]; for (int i=0;i<=y[0];i++) x[i]=y[i]; return; }
-
-12017-11-04 14:46:29@
额啊,虽然说我很蒟蒻,但是我恶心的代码居然过了。。开心
注意公式Ans=2*(n-2^k)+1 其中 2^k<=n<2^(k+1)
恶心的高精度
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
char st[1005];
int k[100005],t[100005],a[100005],n;
bool com()
{
if (a[0]>k[0]) return true;
if (k[0]>a[0]) return false;
if (a[0]==k[0])
for (int i=1; i<=a[0]; i++) if (a[i]<k[i+1]) return false; else if (a[i]>k[i+1]) return true;
}
void cheng()
{
int l=n+1,x=0;
for (int i=0; i<=l; i++) t[i]=k[i];
for (int i=l; i>=l-k[0]+1; i--) {
k[i]=k[i]*2+x;
x=k[i]/10;
k[i]%=10;
}
while (x>0) {
k[0]++; k[l-k[0]+1]=x%10; x/=10;
}
}
void jian()
{
for (int i=n; i>=1; i--) {
k[i+1]+=a[i]-t[i+1];
if (k[i+1]<0) k[i+1]+=10,k[i]--;
k[i]+=k[i+1]/10;
k[i+1]%=10;
}
k[0]=n+1; int i=1;
while (k[i]==0 && k[0]>0) i++,k[0]--;
}
int main()
{
gets(st+1);
n=strlen(st+1);
for (int i=1; i<=n; i++) a[i]=st[i]-48;
a[0]=n;
k[0]=1; k[n+1]=1;
while (com()){
cheng();
}
memset(k,0,sizeof(k));
jian();
cheng();
k[n+1]++;
int i=n+1;
while (k[i]>9) k[i-1]+=k[i]/10,k[i]%=10,i--;
while (k[n-k[0]+1]!=0) k[0]++;
for (int i=n+2-k[0]; i<=n+1; i++) printf("%d",k[i]);
return 0;
} -
-12017-08-26 10:54:30@
function question(n) {
var arr = [],
b = -1,
pro = [], //依次退出编号
result; //结果
for (var i = 1; i <= n; i++)
arr.push(i);function gg(arrs) {
var arrNew = [];
for (var i = 0; i < arrs.length; i++) {
b = b * -1;
if (b > 0)
arrNew.push(arrs[i]);
else
pro.push(arrs[i]);
}
if (arrNew.length > 1)
gg(arrNew);
else
result = arrNew[0]; //最后一个赋值
}
gg(arr);
return result;
} -
-12017-07-26 01:32:03@
犯了个很蠢的错误,调试了半天……
很有意思的题目,不知道谁能把这个题目的规律证明一下- -
感觉写的代码思路比较清晰,很好看
cpp
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
using namespace std;
const long mod=100000000;
const long d=8;
const long list[]={1,10,100,1000,10000,100000,1000000,10000000};
struct gjd{
long num[102],n;
} a,b1,b2,c;
gjd multi_two(gjd x)
{
long g,i;
g=0;
for (i=1;i<=x.n;i++){
x.num[i]=x.num[i]*2+g;
g=x.num[i]/mod;
x.num[i]%=mod;
}
if (g>0) {x.n++; x.num[x.n]=g;}
return x;
}
bool compare(gjd t1,gjd t2) //To judge whether t1 bigger than t2, if it is, return true, else return false.
{
long i;
if (t1.n>t2.n) return true;
else if (t1.n<t2.n) return false;
else {
for (i=t1.n;i>=1;i--){
if (t1.num[i]>t2.num[i]) return true;
else if (t1.num[i]<t2.num[i]) return false;
}
return false;
}
}
gjd _minus(gjd x,gjd y){
long i;
for (i=1;i<=x.n;i++){
x.num[i]-=y.num[i];
if (x.num[i]<0) {x.num[i]+=mod; x.num[i+1]--;}
}
if (x.num[x.n]==0) x.n--;
return x;
}
gjd _add(gjd x,gjd y){
gjd t;
long g,i,len;
if (x.n>y.n) len=x.n;
else len=y.n;
g=0;
for (i=1;i<=len;i++){
t.num[i]=x.num[i]+y.num[i]+g;
g=t.num[i]/mod;
t.num[i]%=mod;
}
t.n=len;
if (g>0) {t.n++; t.num[t.n]=g;}
return t;
}
main()
{
long i,j,len;
string t;
cin>>t;
len=t.size();
for (i=0;i<len;i++){
if (i%d==0) a.n++;
a.num[a.n]=(t[len-i-1]-'0')*list[i%d]+a.num[a.n];
}
memset(b1.num,0,sizeof(b1.num));
b1.n=1;
b2=b1;
b1.num[1]=1;
c=b1;
while (compare(a,b1)){
b2=b1;
c=multi_two(c);
b1=_add(b1,c);
}
b1=_minus(a,b2);
b2=multi_two(b1);
b2.num[1]--;
i=1;
while (b2.num[i]<0){
b2.num[i]+=mod;
i++;
b2.num[i]--;
}
if (b2.num[b2.n]==0) b2.n--;
cout<<b2.num[b2.n];
for (i=b2.n-1;i>=1;i--){
if (b2.num[i]==0) len=7;
else len=7-long (log10(b2.num[i]));
for (j=1;j<=len;j++) cout<<0;
cout<<b2.num[i];
}
}
-
-12017-03-22 18:12:31@
/* ********************************************** Auther: haibin File Name : Main.java Code : vijos-1095 *********************************************** */ import java.math.BigInteger; import java.io.*; import java.util.Scanner; public class Main { public static void main(String args[]){ Scanner n = new Scanner(System.in); String s1 = n.next(); BigInteger b1 = new BigInteger(s1); String s2 = b1.toString(2).substring(1) + "1"; BigInteger b2 = new BigInteger(s2, 2); System.out.println(b2.toString(10)); } }
-
-12017-02-21 11:46:23@
手写了会 发现了个规律 如果有错误欢迎大佬指正
除了n=1时 特判输出1
当 2^i <= n <= 2^(i+1) 时
输出 (n-2^i)*2+1
不过还没代码实现 -
-12016-11-17 20:22:01@
#include <cstdio>
#include <cstring>
#define Q 1000
#define clear(a) memset(a,0,Q*sizeof(int))void input(int a[]){
clear(a);
char c[120];
scanf("%s",c);
int len=1,k=1;
for(int i=strlen(c)-1;i>=0;i--){
if(k==10000)
k=1,len++;
a[len]+=k*(c[i]-'0');
k*=10;
}
a[0]=len;
}void f2(int a[]){
int c[Q],len=a[0]+1;
clear(c);
for(int i=1;i<=len;i++){
c[i]+=a[i]+a[i];
c[i+1]+=c[i]/10000;
c[i]%=10000;
}
while(len>1&&c[len]==0)len--;
c[0]=len;
memcpy(a,c,Q*sizeof(int));
}void minus(int a[],int b[]){
for(int i=1;i<=a[0];i++){
if(a[i]>=b[i])
a[i]-=b[i];
else
a[i]+=10000-b[i],a[i+1]-=1;
}
int len=a[0];
while(len>1&&a[len]==0)len--;
a[0]=len;
}void output(int a[]){
printf("%d",a[a[0]]);
for(int i=a[0]-1;i>=1;i--)
printf("%04d",a[i]);
}int big(int a[],int b[]){
if(a[0]!=b[0])
return a[0]>b[0];
for(int i=a[0];i>=1;i--)
if(a[i]!=b[i])
return a[i]>b[i];
return 0;
}int main(){
int x[Q],x1[Q]={1,1},x2[Q]={1,2},q[Q]={1,1};
input(x);
while(1){
if(big(x2,x)){
minus(x1,q);
minus(x,x1);
f2(x);
minus(x,q);
output(x);
return 0;
}
f2(x1);
f2(x2);
}
return 0;
} -
-12016-08-09 23:36:07@
#include <cstdio> #include <cstring> #include <iostream> using namespace std; string str; int start[600],ans[600],res[600]; int oldBase, newBase; void change(string str) { int i,len = str.size(); start[0] = len; for(i=1;i<= len;i++) { if(str[i-1] >= '0' && str[i-1] <= '9') { start[i] = str[i-1] - '0'; } } } void solve() { memset(res,0,sizeof(res)); int y,i,j; while(start[0] >= 1) { y=0; i=1; ans[0]=start[0]; while(i <= start[0]) { y = y * oldBase + start[i]; ans[i++] = y/newBase; y %= newBase; } res[++res[0]] = y; i = 1; while((i<=ans[0]) && (ans[i]==0)) i++; memset(start,0,sizeof(start)); for(j = i;j <= ans[0];j++) start[++start[0]] = ans[j]; memset(ans,0,sizeof(ans)); } } void output() { int i; for(i = res[0];i >= 1;--i) cout << res[i]; } void convert(){ int i; str=""; for (i=res[0]-1;i>=1;--i) str+=res[i]+'0'; str+=res[res[0]]+'0'; } int main() { ios::sync_with_stdio(false); cin >> str; oldBase=10; newBase=2; change(str); solve(); convert(); oldBase=2; newBase=10; change(str); solve(); output(); return 0; }
测试数据 #0: Accepted, time = 0 ms, mem = 584 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 584 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 580 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 580 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 580 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 580 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 584 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 584 KiB, score = 10
测试数据 #8: Accepted, time = 0 ms, mem = 580 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 580 KiB, score = 10
Accepted, time = 0 ms, mem = 584 KiB, score = 100
去你大爷的高精度!!!改了半个小时才改出个丑陋的程序,但好歹是0ms的!!! -
-12015-07-18 10:32:42@
#include<iostream>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
using namespace std;HANDLE Mutex=CreateMutex(NULL,FALSE,NULL);//互斥对象
int GameOver=0;
int level=0;
int map[23][23];
//坦克种类,Normal为玩家坦克
#define Normal 0
#define Red 1
#define Blue 2
#define Green 3
//方向的宏定义
#define Up 0
#define Down 1
#define Left 2
#define Right 3
//地图标记的宏定义
#define Empty 0
#define Player 1
#define PlayerBullet 2
#define EnemyBullet 3
#define Enemy 4int Kill;
int KillRed;
int KillGreen;
int EnemyExist;void SetPos(int i,int j)//设定光标位置
{
COORD pos={i,j};
HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}void HideCurSor(void)//隐藏光标
{
CONSOLE_CURSOR_INFO info={1,0};
HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(Out,&info);
}int sharp[4][12]=
{
{0,1,1,0,1,1,1,2,2,0,2,2},
{0,0,0,2,1,0,1,1,1,2,2,1},
{0,1,0,2,1,0,1,1,2,1,2,2},
{0,0,0,1,1,1,1,2,2,0,2,1},
};//此数组用来保存坦克各个方向的形状信息DWORD WINAPI Bulletfly(LPVOID lpParameter);//子弹函数申明
void Updata();//更新界面信息函数申明class Tank//坦克类
{
private:
int Direction;//方向
int hotpoint[2];//活动点
int Speed;//速度
int FirePower;//火力
public:
Tank(int dir,int hot1,int hot2,int typ,int spe,int firepow)//构造函数
{
Direction=dir;
hotpoint[0]=hot1;
hotpoint[1]=hot2;
Type=typ;
Speed=spe;
FirePower=firepow;
}
int Type;//坦克的种类(详见宏定义)
int ID;//坦克在MAP中的标记(详见宏定义)
int FireEnable;//是否可以开火
int Life;//生命值
void Running();//运行函数
int Judge(int x,int y,int ID);//判断是否可以绘制坦克
void DrawTank();//重绘坦克
void Redraw();//擦除坦克
int GetSpeed()//获取速度
{
return Speed;
}
int GetFire()//获取火力
{
return FirePower;
}
int GetDirection()//获取方向
{
return Direction;
}
int GetHotX()//获取活动点坐标
{
return hotpoint[0];
}
int GetHotY()
{
return hotpoint[1];
}
void IncreaseFire()//火力+
{
FirePower++;
}
void IncreaseSpeed()//速度+
{
Speed++;
}
void ChangeDirection(int newD)//改变方向
{
Direction=newD;
}
void ChangePos(int x,int y)//改变活动点
{
hotpoint[0]=x;
hotpoint[1]=y;
}
};Tank player(Right,0,0,Normal,1,1);//玩家
Tank enemy(Left,20,0,Red,1,1);//敌人void Tank::DrawTank()//绘制坦克
{
int i;
int nx,ny;
if(Type==Red)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);
else if(Type==Blue)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_BLUE);
else if(Type==Green)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);
else if(Type==Normal)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
for(i=0;i<6;i++)
{
nx=hotpoint[0]+sharp[Direction][i*2];
ny=hotpoint[1]+sharp[Direction][i*2+1];
SetPos((ny+1)*2,nx+1);//利用sharp数组相对于点x,y绘制形状
map[nx][ny]=ID;
cout<<"■";
}
}void Tank::Redraw()//擦除坦克,原理同上
{
int i;
int nx,ny;
for(i=0;i<6;i++)
{
nx=hotpoint[0]+sharp[Direction][i*2];
ny=hotpoint[1]+sharp[Direction][i*2+1];
map[nx][ny]=Empty;
SetPos((ny+1)*2,nx+1);
cout<<" ";
}
}int Tank::Judge(int x,int y,int dir)//判断当前是否可以绘制坦克
{
int i;
int nx,ny;
for(i=0;i<6;i++)
{
nx=x+sharp[dir][i*2];
ny=y+sharp[dir][i*2+1];
if(nx<0||nx>=23||ny<0||ny>=23||map[nx][ny]!=Empty)//不能绘制,返回1
return 1;
}
return 0;
}void Tank::Running()//坦克运行函数
{
int newD;
//坦克的运行
while(1)
{
if(Life==0)
{
EnemyExist=0;//敌人不存在
return;
}
if(GameOver==1)
return;
if(FireEnable==1&&GameOver==0)//如果可以开火
{
WaitForSingleObject(Mutex,INFINITE);//线程拥有互斥对象
FireEnable=0;//设为不可开火
HANDLE bullet=CreateThread(NULL,0,Bulletfly,&ID,0,NULL);//创建子弹线程
CloseHandle(bullet);
ReleaseMutex(Mutex);//释放互斥对象
Sleep(100);
}
WaitForSingleObject(Mutex,INFINITE);//线程拥有互斥对象
srand((int)time(0));
newD=rand()%4;if(newD==Up)//随机出新的方向并重新绘制坦克
{
Redraw();
if(Judge(hotpoint[0]-1,hotpoint[1],newD)==0)
{
hotpoint[0]--;
Direction=newD;
}
else
{
if(Judge(hotpoint[0],hotpoint[1],newD)==0)
Direction=newD;
}
}
else if(newD==Down)
{
Redraw();
if(Judge(hotpoint[0]+1,hotpoint[1],newD)==0)
{
hotpoint[0]++;
Direction=newD;
}
else
{
if(Judge(hotpoint[0],hotpoint[1],newD)==0)
Direction=newD;
}
}
else if(newD==Left)
{
Redraw();
if(Judge(hotpoint[0],hotpoint[1]-1,newD)==0)
{
hotpoint[1]--;
Direction=newD;
}
else
{
if(Judge(hotpoint[0],hotpoint[1],newD)==0)
Direction=newD;
}
}
else if(newD==Right)
{
Redraw();
if(Judge(hotpoint[0],hotpoint[1]+1,newD)==0)
{
hotpoint[1]++;
Direction=newD;
}
else
{
if(Judge(hotpoint[0],hotpoint[1],newD)==0)
Direction=newD;
}
}
if(GameOver==0&&Life!=0)
DrawTank();
ReleaseMutex(Mutex);//释放互斥对象
Sleep(500-80*Speed);
}
}/*********************子弹线程函数*******************/
DWORD WINAPI Bulletfly(LPVOID lpParameter)
{
int *ID=(int *)lpParameter;//ID用来获取发射子弹坦克的ID
int Pos[2];//子弹活动点
int direction;
int Speed;
int type;
int hit=0;//击中标记
int oldx,oldy;//旧活动点
int flag=0;//子弹是否有移动的标记
if(*ID==Player)//如果是玩家坦克
{
type=PlayerBullet;
direction=player.GetDirection();
Speed=player.GetFire();
Pos[0]=player.GetHotX();
Pos[1]=player.GetHotY();
}
else if(*ID==Enemy)//如果是敌人坦克
{
type=EnemyBullet;
direction=enemy.GetDirection();
Speed=enemy.GetFire();
Pos[0]=enemy.GetHotX();
Pos[1]=enemy.GetHotY();
}
if(direction==Up)//根据坦克的位置和方向确定子弹的初始坐标
{
Pos[0]--;
Pos[1]++;
}
else if(direction==Down)
{
Pos[0]+=3;
Pos[1]++;
}
else if(direction==Left)
{
Pos[0]++;
Pos[1]--;
}
else if(direction==Right)
{
Pos[0]++;
Pos[1]+=3;
}
//子弹的运行
while(1)
{
WaitForSingleObject(Mutex,INFINITE);//这个不再注释了。。。。。
if(flag==1&&hit!=1)//擦除原位置
{
map[oldx][oldy]=Empty;
SetPos((oldy+1)*2,oldx+1);
cout<<" ";
}
if(GameOver==1)
return 0;
if(hit==1||Pos[0]<0||Pos[0]>22||Pos[1]<0||Pos[1]>22)//如果击中
{
ReleaseMutex(Mutex);
Sleep(500);
if(type==PlayerBullet)
player.FireEnable=1;
else if(type=EnemyBullet)
enemy.FireEnable=1;
break;
}
switch(map[Pos[0]][Pos[1]])//子弹经过的MAP的标记
{case Empty://如果是空位置就绘制子弹
map[Pos[0]][Pos[1]]=type;
SetPos((Pos[1]+1)*2,Pos[0]+1);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout<<"■";
break;
case Player://如果是玩家位置
if(type!=PlayerBullet)
{
player.Life--;//生命减少
if(player.Life<=0)
GameOver=1;
}
Updata();
hit=1;
break;
case Enemy://如果是敌人位置
if(type!=PlayerBullet)
hit=1;
else
{
hit=1;
Kill++;
if(Kill%20==0&&player.Life<5)//击杀数++
player.Life++;
if(enemy.Type==Red)//如果击杀红坦克
{
KillRed++;
if(KillRed%10==0&&player.GetFire()<5)
player.IncreaseFire();
}
if(enemy.Type==Green)///如果击杀绿坦克
{
KillGreen++;
if(KillGreen%10==0&&player.GetSpeed()<5)
player.IncreaseSpeed();
}
enemy.Redraw();//擦除敌人
enemy.Life=0;//敌人死亡
}
Updata();
break;
}
oldx=Pos[0];
oldy=Pos[1];
if(direction==Up)//子弹移动
Pos[0]--;
else if(direction==Down)
Pos[0]++;
else if(direction==Left)
Pos[1]--;
else if(direction==Right)
Pos[1]++;
ReleaseMutex(Mutex);
flag=1;
Sleep(60-10*Speed);
}
return 0;
}/*************************敌人线程函数***************************/
DWORD WINAPI TankRuning(LPVOID lpParameter)
{
Sleep(400);
int Pos;
int Start[2];//敌人起始地址
int typ;
int fire;
int spe;
while(1)
{
if(GameOver==1)
return 0;
srand((int)time(0));//随机出敌人起始地址
Pos=rand()%4;
if(Pos==0)
{
Start[0]=2;
Start[0]=2;
}
else if(Pos==1)
{
Start[0]=2;
Start[1]=18;
}
else if(Pos==2)
{
Start[0]=18;
Start[1]=2;
}
else if(Pos==3)
{
Start[0]=18;
Start[1]=18;
}
if(player.Judge(Start[0],Start[1],Down)==0)
break;
}
WaitForSingleObject(Mutex,INFINITE);
srand((int)time(0));
typ=rand()%3+1;//随机出敌人的种类
if(typ==Blue)
{
spe=1+level;
fire=1+level;
}
else if(typ==Red)
{
spe=1+level;
fire=3+level;
}
else if(typ==Green)
{
spe=3+level;
fire=1+level;
}
enemy=Tank(Down,Start[0],Start[1],typ,spe,fire);//重新生成敌人坦克
enemy.ID=Enemy;
enemy.Life=1;
enemy.FireEnable=1;
ReleaseMutex(Mutex);
enemy.Running();
return 0;
}void Init()//初始化函数
{
Kill=0;
KillRed=0;
KillGreen=0;
player=Tank(Left,0,0,Normal,1,1);
enemy=Tank(Left,0,0,Red,1,1);
player.Life=2;
player.FireEnable=1;
enemy.Life=0;
enemy.FireEnable=1;
player.ID=Player;
enemy.ID=Enemy;
EnemyExist=0;
}void Updata()//更新界面信息
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
int i;
SetPos(53,0);
cout<<"生命值:";
SetPos(53,1);
for(i=0;i<5;i++)
{
if(i<player.Life)
cout<<"■";
else
cout<<" ";
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);
SetPos(53,3);
cout<<"移动速度:";
SetPos(53,4);
for(i=0;i<5;i++)
{
if(i<player.GetSpeed())
cout<<"■";
else
cout<<" ";
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);
SetPos(53,5);
cout<<"火力:";
SetPos(53,6);
for(i=0;i<5;i++)
{
if(i<player.GetFire())
cout<<"■";
else
cout<<" ";
}
SetPos(53,8);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout<<"杀敌数:"<<Kill;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);
SetPos(53,9);
cout<<"杀死红坦克:"<<KillRed;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);
SetPos(53,10);
cout<<"杀死绿坦克:"<<KillGreen;}
void DrawMap()//画界面
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
system("cls");
int i;
for(i=0;i<25;i++)
{
SetPos(i*2,0);
cout<<"■";
}
for(i=1;i<25;i++)
{
SetPos(0,i);
cout<<"■";
SetPos(24*2,i);
cout<<"■";
}
for(i=0;i<25;i++)
{
SetPos(i*2,24);
cout<<"■";
}Updata();
}
void Welcome()//欢迎界面
{
int x;
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
SetPos(10,5);
cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";
SetPos(10,6);
cout<<"■ 坦克大战控制台版 ■";
SetPos(10,7);
cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";
SetPos(10,8);
cout<<"■ 方向键移动,空格键射击 ■";
SetPos(10,9);
cout<<"■ 敌人分为3种,蓝色为普通敌人 ■";
SetPos(10,10);
cout<<"■ 红色敌人高射速,绿色敌人高机动性 ■";
SetPos(10,11);
cout<<"■ 每杀死10个红坦克,玩家射速提高(最高五级) ■";
SetPos(10,12);
cout<<"■ 每杀死10个绿坦克,玩家移动性提高(最高五级)■";
SetPos(10,13);
cout<<"■ 每杀死20个坦克,玩家生命+1(最高五格) ■";
SetPos(10,14);
cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";
SetPos(10,15);
cout<<"■ 阎奕然作(百度ID:HapHapYear) ■";
SetPos(10,16);
cout<<"■ 按1-3选择难度 ■";
SetPos(10,17);
cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";
while(1)
{
x=getch();
if(x<='3'&&x>='1')
break;
}
level=x-'0'-1;
}int main()
{
Init();
HideCurSor();
Welcome();
DrawMap();
HANDLE temp;
int newD;
player.DrawTank();
while(GameOver==0)
{
if(GetAsyncKeyState(VK_UP))//按键上
{
WaitForSingleObject(Mutex,INFINITE);
newD=Up;
player.Redraw();
if(player.Judge(player.GetHotX()-1,player.GetHotY(),newD)==0)//移动玩家坦克,原理和敌人函数一样
{
player.ChangePos(player.GetHotX()-1,player.GetHotY());
player.ChangeDirection(newD);
}
else
{
if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
player.ChangeDirection(newD);
}
if(GameOver==0)
player.DrawTank();
ReleaseMutex(Mutex);
Sleep(200-player.GetSpeed()*20);//按键延迟,决定玩家坦克的速度
}
else if(GetAsyncKeyState(VK_DOWN))//按键下,同上
{
WaitForSingleObject(Mutex,INFINITE);
newD=Down;
player.Redraw();
if(player.Judge(player.GetHotX()+1,player.GetHotY(),newD)==0)
{
player.ChangePos(player.GetHotX()+1,player.GetHotY());
player.ChangeDirection(newD);
}
else
{
if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
player.ChangeDirection(newD);
}
if(GameOver==0)
player.DrawTank();
ReleaseMutex(Mutex);
Sleep(200-player.GetSpeed()*20);
}
else if(GetAsyncKeyState(VK_RIGHT))//按键右,同上
{
WaitForSingleObject(Mutex,INFINITE);
newD=Right;
player.Redraw();
if(player.Judge(player.GetHotX(),player.GetHotY()+1,newD)==0)
{
player.ChangePos(player.GetHotX(),player.GetHotY()+1);
player.ChangeDirection(newD);
}
else
{
if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
player.ChangeDirection(newD);
}
if(GameOver==0)
player.DrawTank();
ReleaseMutex(Mutex);
Sleep(200-player.GetSpeed()*20);
}
else if(GetAsyncKeyState(VK_LEFT))//按键左,同上
{
WaitForSingleObject(Mutex,INFINITE);
newD=Left;
player.Redraw();
if(player.Judge(player.GetHotX(),player.GetHotY()-1,newD)==0)
{
player.ChangePos(player.GetHotX(),player.GetHotY()-1);
player.ChangeDirection(newD);
}
else
{
if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
player.ChangeDirection(newD);
}
if(GameOver==0)
player.DrawTank();
ReleaseMutex(Mutex);
Sleep(110-player.GetSpeed()*10);
}
else if(GetAsyncKeyState(VK_SPACE))//按键空格,发射子弹
{
WaitForSingleObject(Mutex,INFINITE);
if(player.FireEnable==1)//如果可以发射
{
HANDLE bullet=CreateThread(NULL,0,Bulletfly,&(player.ID),0,NULL);//创建玩家子弹进程
CloseHandle(bullet);
player.FireEnable=0;
}
ReleaseMutex(Mutex);
}
if(EnemyExist==0&&GameOver==0)//如果敌人不存在生成新敌人
{
WaitForSingleObject(Mutex,INFINITE);
EnemyExist=1;
temp=CreateThread(NULL,0,TankRuning,NULL,0,NULL);//创建敌人线程
CloseHandle(temp);
ReleaseMutex(Mutex);
}
}
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_BLUE);
SetPos(20,10);
cout<<"游戏结束"<<endl;
SetPos(20,11);
cout<<"杀敌数:"<<Kill;
SetPos(20,12);
cout<<"杀死红坦克"<<KillRed;
SetPos(20,13);
cout<<"杀死绿坦克"<<KillGreen<<endl;
return 0;
} -
-12013-02-16 10:21:56@