刷题笔记day10
将军的书
#include<stdio.h> #include<math.h> int n; char page[25]; char result[25]={'0'}; int main() { scanf("%d",&n); int sum = pow(2,n); int i; for(i=0;i<21;i++) result[i] = '0'; for(i=0;i<sum-1;i++) { scanf("%s",page); int j; for(j=0;j<21;j++) { if(page[j] == result[j]) { result[j] = '0'; } else result[j] = '1'; } } int ans = 0; //printf("%s\n",result); for(i=21-n-1;i<21;i++) { ans *= 2; ans += (result[i]-'0'); //printf("%d\n",ans); } printf("%d\n",ans); }
士兵排列
#include<stdio.h> #include<stdlib.h> #include<math.h> int x[10005]; int y[10005]; int cmp(const void* a,const void* b) { int *c = (int*)a; int *d = (int *)b; return (*c)-(*d); } int main() { int n; while(scanf("%d",&n)!=EOF) { int i; for(i=0;i<n;i++) { scanf("%d%d",&x[i],&y[i]); } qsort(x,n,sizeof(int),cmp); qsort(y,n,sizeof(int),cmp); int yy = y[n/2]; for(i=0;i<n;i++) { //printf("%d\n",x[i]); x[i] = x[i]-i; } qsort(x,n,sizeof(int),cmp); int xx = x[n/2]; int ccount = 0; for(i=0;i<n;i++) { ccount += (fabs(x[i]-xx)); ccount += (fabs(y[i]-yy)); } printf("%d\n",ccount); } }
川大2019 p1
#include<stdio.h> int a[105]; void swap(int i,int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } int main() { int n; scanf("%d",&n); int i; for(i=0;i<n;i++) { scanf("%d",&a[i]); } i=0; while(i<n) { if(i%2==0) { } else { swap(i-1,i); } i++; } for(i=0;i<n;i++) printf("%d ",a[i]); }
机器人走迷宫
#include<stdio.h> //up right down left int dx[] = {0,1,0,-1}; int dy[] = {-1,0,1,0}; int mi[20][20]; int posx = 1; int posy = 1; int ccount = 0; int d = -1; int w,h; void init() { int i,j; for(i=0;i<20;i++) { for(j=0;j<20;j++) { mi[i][j] = 0; } } } int islegal(int i,int j) { if(i>=1&& i<=h && j>=1 && j<=w && mi[j][i]==1) return 1; return 0; } int main() { while(scanf("%d%d",&w,&h)!=EOF) { init(); ccount = 0; d = -1; int i,j; for(i=1;i<=w;i++) { char gangn; scanf("%c",&gangn); for(j=1;j<=h;j++) { char x; scanf("%c",&x); if(x>='A'&& x<='Z') { posx = j; posy = i; mi[i][j] = 1; switch(x) { case 'U': d = 0;break; case 'R': d = 1;break; case 'D': d = 2;break; case 'L': d = 3;break; } } else { if(x=='.') mi[i][j] = 1; else mi[i][j] = 0; } } } /* for(i=0;i<20;i++) { for(j=0;j<20;j++) { printf("%d",mi[i][j]); } printf("\n"); } */ int nextx; int nexty; while(islegal(posx,posy)) { ccount++; mi[posy][posx] = 0; nextx = posx + dx[d]; nexty = posy + dy[d]; if(!islegal(nextx,nexty)) { //printf("d:%d\n",d); d++; d %= 4; nextx = posx + dx[d]; nexty = posy + dy[d]; } //printf("x:%d y:%d\n",nextx,nexty); //printf("islegal:%d\n",islegal(nextx,nexty)); posx = nextx; posy = nexty; } printf("%d\n",ccount); } }