刷题笔记day4
Dikjstra算法
// #include<limits.h> const int INF = INT_MAX; const int maxv = 1000; int n,g[maxv][maxv]; int d[maxv]; bool vis[maxv] = {false}; void dij(int s) //s为起点 { int i; for(i=0;i<maxv;i++) d[i] = INF; d[s] = 0; for(i=0;i<n;i++) { int u = -1; int MIN = INF; for(int j=0;j<n;j++) { if(vis[j] == false && d[j] < MIN) { u = j; MIN = d[j]; } } if(u == -1) return; vis[u] = true; for(int v = 0;v<n;v++) { if(vis[v] == false && g[u][v] != INF && d[u] + g[u][v] < d[u]) { d[v] = d[u] + g[u][v]; } } } }
【PAT A1003 】Emergency (要复习)
使用多标尺的Dikjstra算法,求最短路和最大医疗资源。
增加了双向路、带权路、求最小路径数的情况。
#include<stdio.h> #include<limits.h> int rescue[505]; int r[505] = {0}; int g[505][505]; int d[505]; int vis[505] = {0}; int num[505] = {0}; int main() { int n,m,now,save; scanf("%d%d%d%d",&n,&m,&now,&save); int i; for(i=0;i<n;i++) { scanf("%d",&rescue[i]); } for(i=0;i<n;i++) { for(int k=0;k<n;k++) g[i][k] = INT_MAX; } int c1,c2; for(i=0;i<m;i++) { scanf("%d%d",&c1,&c2); scanf("%d",&g[c1][c2]); g[c2][c1] = g[c1][c2]; } for(i=0;i<n;i++) { d[i] = INT_MAX; } d[now] = 0; r[now] = rescue[now]; num[now] = 1; for(int q=0;q<n;q++) { int u = -1; int min = INT_MAX; for(i=0;i<n;i++) { if(d[i] < min && vis[i] == 0) { u = i; min = d[i]; } } vis[u] = 1; for(i=0;i<n;i++) { if(vis[i] == 0 && g[u][i] != INT_MAX) { if(d[u]+g[u][i] < d[i]) { d[i] = d[u] + g[u][i]; r[i] = r[u] + rescue[i]; num[i] = num[u]; } else if(d[u]+g[u][i] == d[i]) { if(r[u] + rescue[i] > r[i]) { r[i] = r[u] + rescue[i]; } num[i] += num[u]; } } } } printf("%d %d\n",num[save],r[save]); return 0; }
【PAT A1005】 Spell It Right
#include<stdio.h> #include<string.h> char str[105]; char str2[105]; char num[10][10] = {"zero","one","two","three","four","five","six","seven","eight","nine"}; int main() { scanf("%s",str); int len = strlen(str); int ans = 0; for(int i=0;i<len;i++) { ans = ans + str[i] - '0'; } sprintf(str2,"%d",ans); int len2 = strlen(str2); for(int j=0;j<len2;j++) { if(j!=0)printf(" "); printf("%s",num[str2[j]-'0']); } }
【PAT A1035 】 Password
#include<stdio.h> #include<string.h> int isp[1005] = {0}; char name[1005][20]; char pwd[1005][20]; int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%s %s",name[i],pwd[i]); //printf("%s %s",name[i],pwd[i]); } if(n == 1 ||n == 0) { printf("There is 1 account and no account is modified\n"); return 0; } int mod = 0; int ccount = 0; for(int j=0;j<n;j++) { int jlen = strlen(pwd[j]); for(int k=0;k<jlen;k++) { char c = pwd[j][k]; if(c == '1' || c == '0'||c == 'l' ||c == 'O') { mod = 1; isp[j] = 1; } if(c == '1') { pwd[j][k] = '@'; } else if(c == '0') { pwd[j][k] = '%'; } else if(c == 'l') { pwd[j][k] = 'L'; } else if(c == 'O') { pwd[j][k] = 'o'; } } if(isp[j] == 1) ccount++; } if(mod == 0) { printf("There are %d accounts and no account is modified\n",n); } else { printf("%d\n",ccount); int first = 1; for(int q=0;q<n;q++) { if(isp[q] == 1) { if(first == 1) first = 0; else printf("\n"); printf("%s %s",name[q],pwd[q]); } } } }
【PAT B1001】 害死人不偿命的(3n+1)猜想
水一题。。
#include<stdio.h> int main() { int n; scanf("%d",&n); int ccount = 0; while(n!=1) { if(n%2==0) n /= 2; else n = (3*n+1)/2; ccount++; } printf("%d\n",ccount); }
【PAT B1022】 D进制的A+B
要注意当a+b等于0时,因为使用了另外的数组进行存储结果,需要特别讨论。
#include<stdio.h> int main() { int a,b,d; scanf("%d%d%d",&a,&b,&d); int c = a+b; int ans[100]; int ccount = 0; if(c == 0) {printf("0\n");return 0;} while(c) { ans[ccount++] = c % d; c /= d; } for(int i=ccount-1;i>=0;i--) printf("%d",ans[i]); printf("\n"); }