随笔分类 - 杭电ACM
摘要:一开始被数据规模下到了,后来看了别人的思路,恍然大悟,可以先写普通的程序将每个数打印出来,最后再打表输出。View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int fan(int n) 5 { 6 int i,ans=1; 7 for(i=1; i<=n; i++) 8 { 9 ans = ans*i;10 }11 return ans;12 } 13 14 int main()15 {16 int i,j,ncases,t,m,n;17 int sum...
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 以前用BFS写过,这次改用DFS写发现DFS掌握的不好,挑了好久才过,今下午才AC了两道题,还要加紧练习DFS! 5 int row,col,count,vis[21][21]; 6 char map[21][21]; 7 int dx[] = {0,0,-1,1}; 8 int dy[] = {1,-1,0,0}; 9 void DFS(int x,int y)10 {11 int sx,sy,i;12 vis
阅读全文
摘要:思路很简单,一开始没有用优先队列,WA了几次后来改了,想一想走迷宫停顿时应该用优先队列。if(u.btime==1) continue;此句不能省略当时间为1时,这一步无法出去,只有重新来过View Code 1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #include<cstring> 5 #include<queue> 6 7 using namespace std; 8 int N,M,map[10][10]; 9 int dx[] = {0,0,-
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h>//回溯法运用。模拟汉诺塔过程 3 4 int flag; 5 void hanor(int n,int *a,int *b,int *c) 6 { 7 int i; 8 if(!flag) 9 return ;10 if(n == 0)11 {12 flag = 1;13 return ;14 } 15 if(n == b[1])16 {17 f...
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<math.h> 5 #include<ctype.h> 6 7 int len; 8 long n,num; 9 char s[15]; 10 void DFS(int t,long ans)11 {12 int i; 13 if(t == len)14 { 15 if(ans == n) 16 num++; 17 return ; 18 }...
阅读全文
摘要:解题思路:看过题就知道是要找规律的,而从下列式子中不难发现, S(1)=1,//在s(n)从左往右看 S(2)=11, s(1)中有1个1; S(3)=21, s(2)中有2个1; S(4)=1211, s(3)中有1个2和1个1; S(5)=111221,s(4)中有1个1、1个2和2个1; S(6)=312211, s(5)中有3个1、2个2和1个1;……让求s(n)的长度,首先必须找出 s(n)的组成结构,其实仔细一看,会发现s(n)的组成是跟 s(n-1) 密切相关的,过程如上所述。由于 n<=30,数据不大,我就定义了一个二维字符数组来保存s(n)的所有信息,然后就可以求出所需
阅读全文
摘要:View Code 1 不要把它当做一般的水题看,尽管算法比较简单属于 2 入门的DP可是测试数据真的很坑爹!一开始写了个函 3 数将start,end,a[],设为全局变量可还是过不了,最后 4 我把函数全写在了主函数内了,全部改成局部变量,最后过了! 5 #include<stdio.h> 6 #include<stdlib.h> 7 8 int main() 9 {10 int i, ncases, n, j,sum,b,k;11 int a[100001],start,end;12 13 scanf("%d",&ncases);14
阅读全文
摘要: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
阅读全文
摘要:一看到此题想都没想就用简单的回溯,可是写好代码测试样里也通过已提交就是TLE;后来请教了大神他说测试数据可能有多组单纯的输出可能会超时,好吧!我把每种情况都提前算好放在数组里接收一个我就输出一个,这样提前就把N皇后给初始化,最后输出果断AC了。 1 #include<cstdio> 2 #include<iostream> 3 4 using namespace std; 5 6 int N, visit[3][30], tot, C[50]; 7 8 void dfs(int cur,int N) 9 {10 int i, j; 11 if(cur == ...
阅读全文
摘要:此题的大致意思是在一个迷宫内,营救天使,r代表天使的朋友a代表天使,x代表卫兵;见到卫兵就打死他这要多花1各单位时间,问你要多长时间才能到达天使所处的位置。一见到此题第一反应就光搜代码很快就写完了,测试样例通过,直接提交我傻眼了,WA一直WA纠结死了!我不信邪就去搜别人代码,第一眼看到的是优先队列。我恍然大悟,我用的是普通队列只能一步步往外搜不能停顿,这是普通队列的优点也是缺点。结果我使用优先队列后只是在我代码的基础上稍作改动即实现了优先队列的功能,因为优先队列是按顺序从小到大排列时间的,所以用稍作修改果断0ms通过,真是开心啊! 1 #include<cstdio> 2 #inc
阅读全文
摘要:1 #include<stdio.h> 2 #include<string.h> 3 #define maxn 40005//10000阶乘后的位数大约为40000一开始开得太大结果超时了,最后数组开销了之后便AC 4 5 int main() 6 { 7 int i, j, c, s, n, f[maxn]; 8 9 while(scanf("%d",&n)!= EOF)10 { 11 memset(f,0,sizeof(f));12 f[0] = 1; 13 for(i=2; i<=n; i++)14 ...
阅读全文
摘要:1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 int main() 6 { 7 int i, T, length_a, length_b, j, k; 8 char a[1000], b[1000],c[1000]; 9 scanf("%d", &T);10 for (i=0; i<T; i++)11 {12 scanf("%s%s",a,b);13 length_a = strlen(a);14 length_b =
阅读全文
摘要:1 #include<stdio.h> 2 3 int main() 4 { 5 int a, b, n, i, c[49]; 6 7 while(scanf("%d%d%d", &a, &b, &n) != EOF) 8 { 9 if(a==0 && b==0 && n==0) break; 10 c[1] = 1; 11 c[2] = 1; 12 a = a%7;13 b = b%7;14 for (i=3; i<=49; i++)15 ...
阅读全文
摘要:1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 char s[7][7]; //将变量名放在主函数外面为了递归调用时方便。 7 int n,m,t,ok; 8 int visit[7][7]; 9 int dir[4][2] = {-1,0,0,1,1,0,0,-1};//分为查找的四个方向10 int Si,Sj,di,dj;11 12 int dfs(int x,int y,int step)13 {14 if(ok == 1) return 0; //ok==1表示找到出口了
阅读全文
摘要:需要用到一些数学公式:(a+b)%m = (a%m + b%m)%m(a*b)%m = (a%m * b%m)%m这是利用它的余数来优化,从而减少了计算量 1 #include<stdio.h> 2 #include<string.h> 3 4 int main() 5 { 6 int i, ncases, num, m; 7 long long k, sum; 8 char n[101]; 9 10 while(scanf("%d", &ncases) != EOF)11 {12 while(ncases--)13 ...
阅读全文
摘要:1 #include<stdio.h> 2 #include<string.h> 3 #define N 101 4 5 int main() 6 { 7 int ncases, i, sum1, sum2; 8 int len1, len2, j; 9 char a[N], b[N];10 11 while(scanf("%d",&ncases) != EOF)12 {13 for (i=1; i<=ncases; i++)14 {15 scanf("%s%s",a,b);16 ...
阅读全文

浙公网安备 33010602011771号