随笔分类 - ZOJ
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int main() 5 { 6 int i,last=0,num[14],n; 7 int a,b,c,d,e,f; 8 9 while(scanf("%d",&n)&&n)10 {11 if(last) printf("\n"); 12 for(i=0; i<n; i++)13 scanf("%d",&num[i]); 1...
阅读全文
摘要:View Code 1 #include<stdio.h>//求出最小生成树之后,再求出第n-m大的值就行了 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 #define N 501 6 #define Maxint 9999999 7 8 double c[N][N],x[N],y[N],dist[N]; 9 int s[N], closest[N],n;10 11 int cmp(const void *a,const void *b) 12 { 13 retur
阅读全文
摘要:View Code 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<map> 6 7 using namespace std; 8 9 map<string,string>entry;10 map<string,string>::iterator location,pos;11 int main()12 {13 14 char line[30];15 char english[1
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 int main() 6 { 7 int cell, i, n, j, ncases; 8 int a[101], k; 9 10 scanf("%d",&ncases); 11 for(i=1; i<=ncases; i++)12 {13 scanf("%d",&cell);14 memset(a, 0, sizeof(a)); 15 for(j
阅读全文
摘要:PE了几次,还好及时地发现,最后一句话没好好理解,原来空行还可以这么弄View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 #define N 101 6 7 int father[N], citys; 8 double x[N], y[N], dis_sum; 9 typedef struct10 {11 int x,y; 12 double w;13 }edge;14 edge e[N*N+1];15 16 in
阅读全文
摘要:题目意思很简单计算出所修电线的最小值,很明显Kruskal算法比Prim算法要简单。View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 #define N 505 6 7 int father[N],n,row,dis_sum; 8 typedef struct 9 {10 int x,y;11 int w;12 }edge;13 edge e[N*N/2];14 int cmp(const void *a,con
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 5 int cmp(const void *a,const void *b)//这里要用double貌似用int不行 6 { 7 return *(double *)a > *(double *)b ? 1 : -1; 8 } 9 10 int main()11 {12 int i, n, ncases;13 double total; 14 double weight[101];15 16 while( ..
阅读全文
摘要:此算法在POJ上跑超时了,用了3s+;具体的思路和Prim算法一样View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define N 755 5 6 int rank[N],father[N]; 7 int dis_sum, x[N], y[N]; 8 typedef struct 9 {10 int x, y;11 int w; 12 }edge;13 edge e[N*N];14 15 int cmp(const void *a,const void *b
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define N 760 5 #define Maxint 99999999 6 7 double lowcost[N], c[N][N]; 8 double x[N], y[N]; 9 int towns, m, s[N], closest[N],path[N][N];10 11 double distance(int i,int j)//用double为了防止int数据存不下12 {13 return (x
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<math.h> 3 #include<stdlib.h> 4 5 int main() 6 { 7 int ncases, d, t; 8 double n1, n2, num1,num2,k; 9 int sum1, sum2;10 11 scanf("%d",&ncases);12 while( ncases-- )13 {14 scanf("%d%d",&d,&t);15 num1 = 0.0; num2 =
阅读全文
摘要:题目大意是给你几组数据,表示每个人之间的信息传递的时间,要求所用时间最短的人的编号和所用的最短时间。可以用图论中的Floyd算法,经过Floyd算法后在邻接矩阵当中的就有了每个人的信息传递时间及编号;接下来要做的是从邻接矩阵中查找信息。View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 int cost[101][101],n; 6 void Floyd() 7 { 8 int i,j,k; 9 for(k=1; k<=n; k++)10 for(i=1
阅读全文
摘要:题目大意:给你几种货币,并给出货币之间的汇率,问你是否存在有一种货币经过兑换后,可以盈利赚钱,这就是所谓的炒外汇吧!嘿嘿!View Code #include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>int main(){ int i, j, k, ncases, n, ans=1, flag; char name[30][300]; char from[31], to[31]; double rate, cur[31][31]; while(scanf("
阅读全文
摘要:题目意思很简单了,输入一个行数和列数相同的矩阵a[n][n]然后求最大的子矩阵和。它实际上就是最大子段和的二维推广,可以按照一维的思路来做。首先将每一列的和压缩成一个b[j]数组的元素,它代表a[i1][j]+a[i1+1][j]+......+a[i2][j]的和,这是保持列数不变再把它压缩成一个元素,这样进行压缩后实际上就是求b[1].....b[j]..b[n]的最大子段和,这就转化为一维的情况。View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 4 #define N 101 5 int a[N][N]; 6
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define MAX 1000 5 6 int c[MAX][MAX],len1,len2; 7 char x[MAX],y[MAX]; 8 void LCSLength(int m,int n,char *x,char *y) 9 {10 int i,j;11 for(i=0; i<m; i++) 12 c[i][0] = 0; 13 for(i=0; i<n; i++)14 c[0][i] =
阅读全文
摘要:思路很简单,简单的素数环,相邻位必定是一奇一偶,当n为奇数时就不必在判断了,输出肯定木有素数环(剪枝的重要判定条件),因为n为奇数时奇数的数量一定大于偶数的数量,最后一定存在两个奇数相连(雀巢原理)。 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<math.h> 5 6 int A[20],visit[20],ok,n; 7 int isp[40]={0,1,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0
阅读全文
摘要:此题的大致意思是在一个迷宫内,营救天使,r代表天使的朋友a代表天使,x代表卫兵;见到卫兵就打死他这要多花1各单位时间,问你要多长时间才能到达天使所处的位置。一见到此题第一反应就光搜代码很快就写完了,测试样例通过,直接提交我傻眼了,WA一直WA纠结死了!我不信邪就去搜别人代码,第一眼看到的是优先队列。我恍然大悟,我用的是普通队列只能一步步往外搜不能停顿,这是普通队列的优点也是缺点。结果我使用优先队列后只是在我代码的基础上稍作改动即实现了优先队列的功能,因为优先队列是按顺序从小到大排列时间的,所以用稍作修改果断0ms通过,真是开心啊! 1 #include<cstdio> 2 #inc
阅读全文
摘要:1 我的做法是先用BFS求的到达T最短的步数,再用DFS 2 做判断这样的路总共有几条和踩到地雷的路有几条, 3 DFS中用到了剪支,最后求比值即可 4 #include<cstdio> 5 #include<cstdlib> 6 #include<iostream> 7 #include<cstring> 8 #include<queue> 9 10 using namespace std; 11 12 int dx[4] = {1,-1,0, 0}; 13 int dy[4] = {0, 0,1,-1}; 14 char map[
阅读全文
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<iostream> 5 #include<queue> 6 7 using namespace std; 8 9 int dx[4] = {1, 0, -1, 0};//控制四个方向10 int dy[4] = {0, 1, 0, -1};11 int visit[65536];12 char s[4][4];13 14 typedef struct15 {16 int data;17 int s
阅读全文
摘要:1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<queue> 5 #include<cmath> 6 #include<cstring> 7 8 using namespace std; 9 10 int dx[] = {1,-1, 0, 0, 0, 0};//三维坐标,六个方向,每次只是其中一个坐标变 化,而保持另外两个不变。11 int dy[] = {0, 0, 1,-1, 0, 0};12 int dz[] = {0, 0
阅读全文
摘要:1 思路:此题属于隐式图的搜索,从1111出发发散成一个 2 树状结构从每一层中搜索看是否由于目标相等的数,有责打 3 印无责要还原;先对每个数加1,再减1,最后左右互换; 4 #include<cstdio> 5 #include<cstring> 6 #include<iostream> 7 #include<cstdlib> 8 #include<queue> 9 10 using namespace std;11 12 typedef struct13 {14 int data;15 int step;16 }State;//
阅读全文

浙公网安备 33010602011771号