#include
#include
#include
#define
NULL
0
int
compare(char
sum1,char
sum2)
{
if(sum1>=sum2)
return
1
else
return
0
}
int
breakal(int
sum)
{
if(sum>21)
return
0
else
return
1
}
char
change(int
card)
{
char
ch,n,m,x
switch(card)
{
case
1:
ch='A'
break
case
10:
{
n=1+int(rand()/(RAND_MAX+1.0))
if(n==1)
ch='K'
else
{
m=1+int(rand()/(RAND_MAX+1.0))
if(m==1)
ch='Q'
else
{
x=1+int(rand()/(RAND_MAX+1.0))
if(x==1)
ch='J'
}
}
break
}
}
//以上switch语句用于将数字转换成字符
return
ch
}
void
game()
{
int
i,k,n
char
host_ch,player_ch,host_card[15],player_card[15]
int
host_sum=0,player_sum=0
srand(time(NULL))
for(i=0i<2i++)
{
int
(10.0*rand()/(RAND_MAX+1.0))
player_card[i]=1+int
(10.0*rand()/(RAND_MAX+1.0))
player_ch=change(player_card[i])
if(player_card[i]>1
&&
player_card[i]<10)
//输出玩家抽到的牌的点数
printf("您抽到的第%d张牌是%d\n",i+1,player_card[i])
else
if(player_card[i]==10)
printf("您要到的第%d张牌是%c\n",i+1,player_ch)
else
printf("您要到的第%d张牌是A\n",i+1)
if(player_ch=='A')
//询问判断玩家的A的点值为1或11
{
printf("
您有一张A,您想要A作1还是11?\n")
printf("
1.
1\n"
)
printf("
2.
11\n")
if(scanf("%d",&n)
&&
n==1)
player_card[i]=1
else
player_card[i]=11
}
host_card[i]=1+int
(10.0*rand()/(RAND_MAX+1.0))
host_ch=change(host_card[i])
if(host_ch=='A')
//电脑自动判断庄家的A的点值为1或11
{
k=1+int(rand()/(RAND_MAX+1.0))
if(k==1)
host_card[i]=11
else
host_card[i]=1
}
player_sum+=player_card[i]
//分别对庄家和玩家的点数求和
host_sum+=host_card[i]
}
printf("您现在的总面值为%d\n",player_sum)//输出玩家的总面值
for(i=2i<5i++)
{
if(host_sum<18
&&
host_sum>0)//电脑自动判断庄家是否要牌
{
host_card[i]=1+int
(10.0*rand()/(RAND_MAX+1.0))
host_ch=change(host_card[i])
if(host_ch=='A')
//电脑自动判断庄家的A的点值为1或11
{
k=1+int(rand()/(RAND_MAX+1.0))
if(k==1)
host_card[i]=11
else
host_card[i]=1
}
host_sum+=host_card[i]
}
printf("您还要继续要牌吗?\n")
printf("
1.要\n")
printf("
2.不要\n")
scanf("%d",&k)
if(k==1)
{
{
player_card[i]=1+int
(10.0*rand()/(RAND_MAX+1.0))
player_ch=change(player_card[i])
if(player_ch=='A')
//询问判断玩家的A的点值为1或11
{
printf("您有一张A,您想要A作1还是11?\n")
printf("
1.
1\n"
)
printf("
2.
11\n")
if(scanf("%d",&n)
&&
n==1)
player_card[i]=1
else
player_card[i]=11
printf("您要到的第%d张牌是A\n",i+1)
}
else
printf("您要到的第%d张牌是%d\n",i+1,player_card[i])
player_sum+=player_card[i]
if(i==4)
printf("您手中已有5张牌,一决胜负的时刻到啦!\n")
if(breakal(host_sum)==0)
{
if(breakal(player_sum)==0)
{
printf("庄家爆牌了,您也爆牌了!庄家的牌面值是%d,您的牌面值是%d\n",host_sum,player_sum)
printf("\nYou
lose..\n")
break
}
}
else
if(breakal(player_sum)==0)
{
printf("您爆牌了!您的牌面值是%d\n
\nYou
lose..\n",player_sum)
break
}
}
if(breakal(host_sum)==0)
{
printf("庄家爆牌了!庄家的牌面值是%d\n",host_sum)
printf("\nCongratulations!
YOu
win!\n")
break
}
}
else
{
printf("庄家的牌面值是%d,您的牌面值是%d\n",host_sum,player_sum)
if(compare(host_sum,player_sum)==1)
printf("\nYou
lose..\n")
else
printf("\nCongratulations!
You
win!\n")
break
}
}
}
int
main()
{
kaishi:
int
n
printf("
欢迎进入21点游戏!\n")
printf("
************************************\n")
printf("
1.开始游戏\n")
printf("
2.退出游戏\n")
printf("
************************************\n")
if(scanf("%d",&n)&&n==1)
game()
else
if(n==2)
{
printf("
******************************\n")
printf("
游戏结束!祝您愉快!\n")
printf("\n\n")
printf("
再见!\n")
printf("
******************************\n")
return
0
}
goto
kaishi
}
//C++ 轻重链剖分代码 有指针#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std
const int debug_mode = false
//int Max(int a, int b) {return a>b?a:b}
//int Min(int a, int b) {return a>b?b:a}
//
const int N = 300012
int n, m
int a[N + N], delta[N + N], b[N + N]
void update(int x) {
a[x] = a[x << 1] + a[x * 2 + 1]
b[x] = max(b[x << 1] , b[x * 2 + 1])
}
void build(int x, int s, int t) {
if (s == t) { a[x] = 0 b[x] = 0 return }
int mid = (s + t) >> 1
build(x << 1, s, mid)
build(x * 2 + 1, mid + 1, t)
update(x)
}
void change(int x, int s, int t, int pos, int key) {
if (s == t && s == pos) { a[x] = key b[x] = key return }
int mid = (s + t) >> 1
if (pos <= mid) change(x << 1, s, mid, pos, key) else change(x * 2 + 1, mid + 1, t, pos, key)
update(x)
}
int query(int x, int s, int t, int l, int r) {
if (l <= s && t <= r) return b[x]
int mid = s + t >> 1 int ans = -0x7fffffff
if (l <= mid) ans =max(ans, query(x << 1, s, mid, l, r))
if (r > mid) ans =max(ans, query(x * 2 + 1, mid + 1, t, l, r))
return ans
}
int querysum(int x, int s, int t, int l, int r)
{
if(l <= s && t <= r) return a[x]
int mid = s + t >> 1 int ans = 0
if(l <= mid) ans += querysum(x<<1, s, mid, l, r)
if(r > mid) ans += querysum(x*2+1, mid+1, t, l, r)
return ans
}
//
struct edge{int u,v, edge *next bool inuse}e[N], *P = e, *point[N]
inline void add_edge(int x, int y) {
edge *Q = ++P ++P
P->u = x P->v = y P->next = point[x] point[x] = P Q->u = y Q->v = x Q->next = point[y]point[y] = Q P->inuse=Q->inuse = true
}
int root,size[N],dep[N],top[N],fa[N],son[N],w[N],va[N]
void dfs1(int father,int x, int depth)
{
fa[x] = father
dep[x] = depth
size[x] = 1
int sum=0, max=-1, maxi= son[x] = 0
for(edge *j = point[x] j j = j->next)
{
if(j->v == fa[x]) continue
dfs1(x,j->v,depth+1)
sum += size[j->v]
if(size[j->v] > max) maxi = j->v, max = size[j->v]
}
size[x] += sum
if(maxi) son[x] = maxi
}
int totw = 0
void dfs2(int v, int Top)
{
w[v] = ++totw top[v] = Top
if(son[v]) dfs2(son[v],top[v])
for(edge *j = point[v] j j = j->next)
if(j->v !=fa[v] && j->v != son[v])
dfs2(j->v,j->v)
}
int Qmax(int u, int v)
{
int ret = -0x7fffffff
while(1)
{
int f1 = top[u], f2 = top[v]
if(f1==f2)
{
if(u==v) return max(ret,query(1,1,n,w[u],w[u]))
if(dep[u] > dep[v]) swap(u,v)
ret = max(ret, query(1,1,n,w[u],w[v]))
return ret
}else
{
if(dep[f1]<dep[f2]) swap(f1,f2), swap(u,v)
ret = max(ret, query(1,1,n,min(w[u],w[f1]),max(w[u],w[f1])))
u = fa[f1]
continue
}
}
}
int Qsum(int u, int v)
{
int ret = 0
while(1)
{
int f1 = top[u], f2 = top[v]
if(f1==f2)
{
if(u==v) return ret+va[u]
ret += querysum(1,1,n,min(w[u],w[v]),max(w[u],w[v]))
return ret
}else
{
if(dep[f1]<dep[f2]) swap(f1,f2), swap(u,v)
// u f1
ret += querysum(1,1,n,w[f1],w[u])
u = fa[f1] f1 = top[u]
continue
}
}
}
void debug(int i)
{
if(!debug_mode) return
cout<<i<<"-----------"<<endl
cout<<"fa[x] : "<<fa[i]<<endl
cout<<"size[x] : "<<size[i]<<endl
cout<<"dep[x] : "<<dep[i]<<endl
cout<<"son[x] : "<<son[i]<<endl
cout<<"top[x] : "<<top[i]<<endl
cout<<"w[x] : "<<w[i]<<endl
}
int main()
{
//freopen("bz1036.in","r",stdin)freopen("bz1036.out","w",stdout)
scanf("%d", &n)
for(int i = 1 i < n i++)
{
int x,y
scanf("%d%d",&x, &y)
add_edge(x,y)
if(i==1) root = x
}
for(int i = 1 i <= n i++) scanf("%d", &va[i])
dfs1(0,root,1)
dfs2(root,root)
for(int i = 1 i <= n i++) change(1,1,n,w[i],va[i])
//for(int i = 1 i <= n i++) debug(i)
int t
scanf("%d", &t)
while(t--)
{
char com[7] scanf("%s", com)
int a,b scanf("%d%d", &a, &b)
switch(com[1])
{
case 'M':
printf("%d\n",Qmax(a,b))
break
case 'S':
printf("%d\n",Qsum(a,b))
break
case 'H':
va[a] = b
change(1,1,n,w[a],b)
break
}
}
} //一个资源管理器模拟的代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <vector>
using namespace std
int idtop=0
struct file
{
int id
string name
bool is_folder
int father
}files[2001],*root=files
vector<int>ct[2001]//content
int history[10005],htop=0,hcur=0
int current
string rem[2001]
string prefix(int depth)
{
if(rem[depth]!="") return rem[depth]
string ret
for(int d = 1 d <= depth d++) ret+=".."
return rem[depth]=ret
}
void print(int id, int depth)
{
//cout<<depth<<endl
cout<<prefix(depth)<<"\\"<<files[id].name<<endl// ../folder
for(int i = 0 i < ct[id].size() i++)
{
if(files[ct[id][i]].is_folder) print(ct[id][i],depth+1)
else cout<<prefix(depth+1)<<files[ct[id][i]].name<<endl
}
}
int newFile(bool isf, string name)
{
for(int i = 0 i < ct[current].size() i++)
if(files[ct[current][i]].name == name && files[ct[current][i]].is_folder == isf) return -1
files[++idtop].id = idtop
files[idtop].name = name
files[idtop].father = current
files[idtop].is_folder = isf
ct[current].push_back(idtop)
if(isf)
{
current = idtop
history[htop = ++hcur] = idtop
}
return 1
}
int enter(string path)
{
int posi = 0, cur_bk = current
if(path[0]=='\\'||path[path.length()-1]!='\\')
{
return -1
}
else
{
//分离path enter
while(posi < path.length())
{
string split="" int curlen = 0
while(posi<path.length())
{
char s = path[posi]
if(path[posi]!='\\') split+=path[posi],posi++,curlen++
else
{
posi++
if(curlen==0) {current = cur_bk return -1}
bool found = false
for(int i = 0 i < ct[current].size() i++)
if(files[ct[current][i]].name == split)
{
if(files[ct[current][i]].is_folder == false) continue
found = true
current = ct[current][i]
break
}
if(!found) {current = cur_bk return -1}
break
}
}
history[htop = ++hcur] = current
}
return 1
}
}
int back()
{
if(hcur)
{
current = history[--hcur]
return 1
}else
{
return -1
}
}
int fore()
{
if(hcur>=htop)
{
return -1
}else
{
current = history[++hcur]
return 1
}
}
int up()
{
if(files[current].father == -1) return -1
else
{
current = files[current].father
history[htop = ++hcur] = current
}
}
void init()
{
for(int i = 0 i < 35 i++) rem[i]=""
root->id = 0 root->name = "exp" root->father = -1 root->is_folder = true
history[0] = 0
current = 0
}
#define Wrong printf("%s\n","WRONG COMMAND.")
int main()
{
//freopen("explorer.in","r",stdin)
//freopen("explorer.out","w",stdout)
init()
int n
scanf("%d", &n)
while(n--)
{
string com
cin>>com
if(com=="back")
{
if(back()==-1) Wrong
continue
}
if(com=="fore")
{
if(fore()==-1) Wrong
continue
}
if(com=="up")
{
if(up()==-1) Wrong
continue
}
if(com=="print")
{
print(0,0)
continue
}
if(com=="new")
{
getchar()
char comd[51] bool isf = false string fname = ""
gets(comd)
for(int i = 0 i < strlen(comd) i++)
{
if(comd[i]==' ')
{
isf = true
continue
}
if(isf)
{
fname += comd[i]
}
}
if(isf)
{
switch(comd[0])
{
case '0':
if(comd[1]!=' ')
{
Wrong
continue
}
isf=false
break
case '1':
if(comd[1]!=' ')
{
Wrong
continue
}
break
default:
Wrong
continue
}
}else
{
for(int i = 0 i < strlen(comd) i++)
fname += comd[i]
}
if(newFile(isf,fname)==-1) Wrong
continue
}
if(com=="enter")
{
string path
cin>>path
if(enter(path)==-1) Wrong
continue
}
}
}
#include <string.h>#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <Windows.h>
#define SIZE 6
#define N3
#define WM 3
void bsxm()
void csxy()
void Information()
int main()
void menu()
void printf_back()
void saveathlete()
void saveprogram()
void saveschool()
void savescore()
void Search()
void ttzf()
int i
FILE *fp1,*fp2,*fp3,*fp4
struct athlete//定义运动员信息
{
char univ[5]
char name[8]
char prog[15]
int mingci[15]
int score
}at[SIZE]
struct xueyuan//定义学院信息
{
char univ[5]
char prog[15]
int num_athlete
}xy[N]
struct program//定义项目信息
{
int quming
char prog[5]
char ath1[5]
char ath2[5]
char univ1[5]
char univ2[5]
}pro[WM]
struct zongfen //定义学院得分信息
{
char univ[5]
char prog1[5]
char prog2[5]
char prog3[5]
int score1
int score2
int score3
int total_score
}zf[N]
void Information()
{
int n,w
do {
puts("\n\n\n")
puts("\t\t-------------- 信息录入--------------\n")
puts("\t\t*************************MENU*************************\n")
puts("\t\t--------- 1.运动员 ----------\n")
puts("\t\t-------- 2.参赛学院 ----------\n")
puts("\t\t-------- 3.比赛项目 ----------\n")
puts("\t\t-------- 4.退出 ----------\n")
puts("\t\t******************************************************\n")
printf("请输入序号(1-4)进行 *** 作:[ ]\b\b")
scanf("%d",&n)
if (n<1||n>4)
{
w=1
getchar()
}
else w=0
}
while (w==1)
switch(n)
{
case 1 : saveathlete()break// 保存运动员信息子函数
case 2 : saveschool()break //保存学院信息子函数
case 3 : saveprogram()break //保存项目比赛信息子函数
case 4 : exit(0)
}
}
void saveathlete()//保存运动员信息子函数
{
printf("请输入各项目获奖运动员的信息:\n")
for(i=0i<SIZEi++)
{
printf("请输入第%d个运动员的信息:\n",i+1)
printf("姓名:")
scanf("%s",at[i].name)
printf("学院:")
scanf("%s",at[i].univ)
printf("参赛项目:")
scanf("%s",at[i].prog)
printf("名次:")
scanf("%d",&at[i].mingci)
printf("得分:")
scanf("%d",&at[i].score)
}
fp1=fopen("ath","w")
for(i=0i<SIZEi++)
{
if(fwrite(&at[i],sizeof(struct athlete),1,fp1)!=1)
printf("File write error!\n")
}
fclose(fp1)
fp1=fopen("ath","r")
if(fp1 == NULL)
{
printf("[saveathlete]文件打开失败!错误码:%d\n", GetLastError())
exit(0)
}
printf("\n现在各运动员的信息如下:\n\n")
printf("姓名 性别 学院 项目名次 得分\n")
for(i=0i<SIZEi++)
{
fread(&at[i],sizeof(struct athlete),1,fp1)
printf("%-10s%-10s%-10s%-10s%-10s%d\n",at[i].name,at[i].univ,at[i].prog,at[i].mingci,at[i].score)
}
fclose(fp1)
printf_back()
}
void saveschool()//保存学院信息子函数
{
printf("\n请输入各参赛学院的信息:\n")
for(i=0i<Ni++)
{
printf("\n请输入第%d个学校的信息:\n",i+1)
printf("学院:")
scanf("%s",xy[i].univ)
printf("参赛的项目:")
scanf("%s",xy[i].prog)
printf("参赛人数:")
scanf("%d",&xy[i].num_athlete)
}
fp2=fopen("xyuan","w")
if(fp2 == NULL)
{
printf("[saveschool]文件打开失败!错误码:%d\n", GetLastError())
exit(0)
}
for(i=0i<Ni++)
{
if(fwrite(&xy[i],sizeof(struct xueyuan),1,fp2)!=1)
printf("File write error!\n")
}
fclose(fp2)
printf_back()
}
void saveprogram()//保存项目比赛信息子函数
{
printf("\n请输入关于比赛项目的信息:")
for(i=0i<WMi++)
{
printf("\n请输入第%d项比赛的信息:\n",i+1)
printf("项目:")
scanf("%s",pro[i].prog)
printf("取分情况:")
scanf("%d",&pro[i].quming)
printf("参赛者1:")
scanf("%s",pro[i].ath1)
printf("所属学院:")
scanf("%s",pro[i].univ1)
printf("参赛者2:")
scanf("%s",pro[i].ath2)
printf("所属学院:")
scanf("%s",pro[i].univ2)
}
fp3=fopen("pr","w")
if(fp3 == NULL)
{
printf("[saveprogram]文件打开失败!错误码:%d\n", GetLastError())
exit(0)
}
for(i=0i<Ni++)
{
if(fwrite(&pro[i],sizeof(struct program),1,fp3)!=1)
printf("File write error!\n")
}
fclose(fp3)
printf_back()
}
void savescore()//保存学院得分情况子函数
{
printf("N-参赛院系总数,M-男子竞赛项目数,W-女子竞赛项目数。\n")
printf("W.100-女子一百米比赛,M.100-男子一百米。\n")
printf("W.200-女子两百米比赛,M.200-男子两百米。\n")
printf("取前三名:第一名7分 第二名5分 第三名3分 第四名2分 第五名1分 \n")
printf("请输入各学院的得分情况:\n")
for(i=0i<Ni++)
{
printf("\n请输入第%d个学院的得分情况:\n",i+1)
printf("学院:")
scanf("%s",zf[i].univ)
printf("参赛项目1:")
scanf("%s",zf[i].prog1)
printf("得分:")
scanf("%d",&zf[i].score1)
printf("参赛项目2:")
scanf("%s",zf[i].prog2)
printf("得分:")
scanf("%d",&zf[i].score2)
printf("参赛项目3:")
scanf("%s",zf[i].prog3)
printf("得分:")
scanf("%d",&zf[i].score3)
zf[i].total_score=zf[i].score1+zf[i].score2+zf[i].score3
}
fp4=fopen("zfen","w")
if(fp4 == NULL)
{
printf("[savescore]文件打开失败!\n")
exit(0)
}
for(i=0i<Ni++)
{
if(fwrite(&zf[i],sizeof(struct zongfen),1,fp4)!=1)
printf("File write error!\n")
}
fclose(fp4)
printf_back()
}
void Search()
{
int n,w2
do
{
puts("\t\t------------ 查看信息 ------------\n")
puts("\t\t*************************MENU*************************\n")
puts("\t\t------------ 1.团体总分 ------------\n")//查看团体总分
puts("\t\t------------ 2.参赛学院 ------------\n")//查看参赛学院信息!
puts("\t\t------------ 3.比赛项目 ------------\n")//查看比赛项目信息!
puts("\t\t------------ 4.退出 -------------n")//退出
puts("\t\t***************************************\n")
printf("请输入序号(1-4)进行 *** 作:[ ]\b\b")
scanf("%d",&n)
if (n<1||n>4)
{
w2=1
getchar()
}
else w2=0
}while (w2==1)
switch(n)
{ case 1:ttzf()break
case 2: csxy()break
case 3:bsxm()break
case 4: exit(0)break
default: printf("未进行任何 *** 作!")
}
}
void ttzf()//查看学院团体总分子函数
{
fp4=fopen("zfen","r")
if(fp4 == NULL)
{
printf("[ttzf]文件打开失败!\n")
exit(0)
}
printf("\n各学院的团体总分情况如下:\n\n")
printf("学院参赛项目 总分\n")
for(i=0i<Ni++)
{
fread(&zf[i],sizeof(struct zongfen),1,fp4)
printf("%-8s%s,%s,%-12s%-12d\n",zf[i].univ,zf[i].prog1,zf[i].prog2,zf[i].prog3,zf[i].total_score)
}
fclose(fp4)
printf_back()
}
void csxy()//查看参赛学院信息子函数
{
fp2=fopen("xyuan","r")
if(fp2 == NULL)
{
printf("[csxy]文件打开失败!错误码:%d\n", GetLastError())
exit(0)
}
printf("\n各参赛学院的情况如下:\n\n")
printf("学院参赛项目 参赛者数\n")
for(i=0i<Ni++)
{
fread(&xy[i],sizeof(struct xueyuan),1,fp2)
printf("%-8s%-20s%-12d\n",xy[i].univ,xy[i].prog,xy[i].num_athlete)
}
fclose(fp2)
printf_back()
}
void bsxm()//查看比赛项目子函数
{
fp3=fopen("pr","r")
if(fp3 == NULL)
{
printf("[bsxm]文件打开失败!失败错误码:%d\n", GetLastError())
exit(0)
}
printf("\n各比赛项目的信息如下:\n\n")
printf("项目取分情况参赛者1 所属学院 参赛者2 所属学院\n")
for(i=0i<Ni++)
{
fread(&pro[i],sizeof(struct program),1,fp3)
printf("%-12s%-10d%-10s%-12s%-10s%-12s\n",pro[i].prog,pro[i].quming,pro[i].ath1,pro[i].univ1,pro[i].ath2,pro[i].univ2)
}
fclose(fp3)
printf_back()
}
int main()
{
menu()
return 0
}
void menu()
{
int n,w1
do
{
puts("\t\t-------- 欢迎进入海经运动会管理系统 --------\n")
puts("\t\t*************************菜单*************************\n")
puts("\t\t----------1.信息录入 --------------\n")
puts("\t\t----------2.比赛结果录入 --------------\n")
puts("\t\t----------3.查询信息--------------\n")
puts("\t\t----------4.退出 --------------\n")
puts("\t\t******************************************************\n")
printf("请输入序号(1-4)进行 *** 作:[ ]\b\b")
scanf("%d",&n)
if (n<1||n>4)
{
w1=1
getchar()
}
else w1=0
}
while (w1==1)
switch(n)
{
case 1:Information()break //输入信息
case 2:savescore()break //比赛结果录入
case 3:Search()break//查询模块
case 4:exit(0)
}
}
//公共函数
void printf_back()
{
int w
printf("\n\n\t *** 作成功!\n\n")
printf("接下来您需要做什么?\n\n\t1).结束[ ] \t2).菜单:[ ]\b\b")
scanf("%d",&w)
if(w==1)
exit(0)
else menu()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)