题目挂在wustoj上,不知道什么原因不能粘贴链接。。
第一题题号为1314。。这题是智力题。。。换成7的阶乘就可以了。。
代码如下。
#include<cstdio> int main() { printf("...............................................................................................\n"); printf("..#.........................#...........#.........................#............................\n"); printf(".#...#####...#...........#...#.........#...#####...#...........#...#..............#####...#....\n"); printf("#........#...#.....#.....#....#.......#........#...#...........#....#.................#...#....\n"); printf("#.......#....#.....#.....#....#.......#.......#....#...........#....#....#####.......#....#....\n"); printf("#......#.....#...#####...#....#...#...#......#.....#...#####...#....#...............#.....#....\n"); printf("#.....#......#.....#.....#....#.......#.....#......#...........#....#....#####.....#......#....\n"); printf("#.....#......#.....#.....#....#.......#.....#......#...........#....#..............#...........\n"); printf(".#....#......#...........#....#........#....#......#...........#....#..............#......#....\n"); printf("..#..........................#..........#..........................#...........................\n"); printf("...............................................................................................\n"); return 0; }
第二题题号是1315,这题需要预处理。。注意运用前面的结果。。这部分代码如下“
for(i=2;i<=len;i++) { if(s[i]==s[i-1]) a[i]++; a[i]=a[i]+a[i-1]; }
完整代码为:
#include<cstdio> #include<cstring> int a[100002]; int main() { char s[100002]; int len,i,pos,j,m,le,ri; while(scanf("%s",s)!=EOF) { memset(a,0,sizeof(a)); a[1]=0; len=strlen(s); for(i=len;i>=1;i--) s[i]=s[i-1]; s[len+1]=‘\0‘; for(i=2;i<=len;i++) { if(s[i]==s[i-1]) a[i]++; a[i]=a[i]+a[i-1]; } scanf("%d",&m); for(i=1;i<=m;i++) { scanf("%d%d",&le,&ri); printf("%d\n",a[ri]-a[le]); } } return 0; }
第四题为常数快速幂:
代码为:
#include<cstdio> #define mode 100000007 int main() { long long a,b,c; long long ans,muli; while(scanf("%lld%lld%lld",&a,&b,&c)!=EOF) { ans=c; muli=a; while(b) { if(b&1)//基数就乘 ans=(ans*muli)%mode; muli=(muli*muli)%mode; b>>=1; } printf("%lld\n",ans%mode); } return 0; }
具体题目如下:
这道题目是一道最短路。。和周练的那道楼梯感觉是一样的。。。
就是如果一个区间的任意一个端点在另外一个区间内,则这个区间可以到另外一个区间
则edge边设为1,否则为无穷.
用dijkstra算法。。
具体代码如下:
#include<cstdio> #include<cstring> #define maxn 300 #define INF 0x3f3f3f3f int cnt; int a[maxn],edge[maxn][maxn],vis[maxn],b[maxn],dis[maxn]; int dijkstra(int start,int End) { int i,j,tmp,now; memset(vis,0,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); dis[start]=0; for(i=1;i<=cnt;i++) { tmp=INF; for(j=1;j<=cnt;j++) { if(!vis[j]&&tmp>dis[j]) { tmp=dis[j]; now=j; } } vis[now]=1; for(j=1;j<=cnt;j++) { if(!vis[j]&&dis[j]>dis[now]+edge[now][j]) dis[j]=dis[now]+edge[now][j]; } } if(dis[End]<INF) return 1; return 0; } int main() { int i,T,j; int flag,x,y; while(~scanf("%d",&T)) { cnt=0; memset(edge,0x3f,sizeof(edge)); for(i=1;i<=T;i++) { scanf("%d%d%d",&flag,&x,&y); if(flag==1) { cnt++; a[cnt]=x; b[cnt]=y; for(j=1;j<cnt;j++) { if(x>a[j]&&x<b[j]||y>a[j]&&y<b[j]) edge[cnt][j]=1; if(a[j]>x&&a[j]<y||b[j]>x&&b[j]<y) edge[j][cnt]=1; } } else { if(dijkstra(x,y)) printf("YES\n"); else printf("NO\n"); } } } return 0; }
第六题是很简单的并查集:
代码如下:
#include<cstdio> int root[10001]; int findroot(int x) { if(root[x]!=x) root[x]=findroot(root[x]); return root[x]; } int main() { int T; int n,m,i,pos; int a,b,fx,fy; scanf("%d",&T); while(T--) { pos=0; scanf("%d%d",&n,&m); for(i=1;i<=n;i++) root[i]=i; for(i=1;i<=m;i++) { scanf("%d%d",&a,&b); fx=findroot(a); fy=findroot(b); if(fx!=fy) root[fx]=fy; } for(i=1;i<=n;i++) { if(root[i]==i) pos++; } printf("%d\n",pos); } }
还有两道题目我会慢慢更新。。
四月月赛解题报告,码迷,mamicode.com
时间: 2024-11-04 05:46:47