这次排序禁用STL....代码写的纠结死了。不过还是因为代码功底不行
Problem A hdu 1029
此题算是数据水了点,我用100W数组来记录,然后就水过了......
#include <iostream> #include <cstring> using namespace std; int ind[1000005]; int main() { int n,a; while(cin >> n) { int ans; memset(ind,0,sizeof(ind)); for(int i=0; i<n; i++) { cin >> a; ind[a] ++; if(ind[a] *2 >= n + 1) { ans = a; } } cout << ans << endl; } return 0; }
Problem B hdu 1040
这题写了个冒泡......
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int a[1005]; int main() { int t,i,j,n; cin >> t; while(t--) { cin >> n; for(i=0; i<n; i++) { scanf("%d",&a[i]); } for(j=0; j<n-1; j++) for(i=0; i<n-1-j; i++) { int t; if(a[i] > a[i+1]) { t = a[i+1]; a[i+1] = a[i]; a[i] = t; } } for(i=0; i<n-1; i++) { printf("%d ",a[i]); } printf("%d\n",a[n-1]); } return 0; }
字符串先处理一下,然后冒个泡排序
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; char a[1005]; int num[1005]; int main() { while(cin >> a) { int len = strlen(a); int ok = 0,sum = 0,j=0; int n; for(int i=0; i<len; i++) { if(a[i] == '5' && a[i-1] != '5' && i != 0) { num[j] = sum; j++; sum = 0; continue; } if(a[i] != '5') { int t = a[i] - '0'; sum = sum * 10 + t; } if(i == len-1 && a[i] != '5') { num[j] = sum; j++; } } n = j; for(j=0; j<n-1; j++) for(int i=0; i<n-1-j; i++) { if(num[i] > num[i+1]) { int t; t = num[i+1]; num[i+1] = num[i]; num[i] = t; } } for(int i=0; i<n-1; i++) { //if(num[i] != num[i+1]) printf("%d ",num[i]); } printf("%d\n",num[n-1]); } return 0; }
计算时钟角度得注意分钟移动后,时钟也会移动一定角度。
用结构体,然后冒泡模拟结构体排序
还有要注意的就是角度相同,按照时间由早到晚排序
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; struct Time { int h,m; double ang; } tim[6]; int main() { int n; cin >> n; while(n--) { for(int i=0; i<5; i++) { scanf("%d:%d",&tim[i].h,&tim[i].m); double angle1,angle2; double hh = double(tim[i].h % 12); double mm = double(tim[i].m); angle1 = (360.0 * hh) / 12.0 + mm*1.0 / 2.0 ; angle2 = (360.0 * mm) / 60.0; double aps = fabs(angle1 - angle2); aps = min(aps,360.0 - aps); tim[i].ang = aps; } for(int j=0; j<4; j++) for(int i=0; i<4-j; i++) { if(tim[i].ang > tim[i+1].ang) { double an; int hh,mm; an = tim[i+1].ang; tim[i+1].ang = tim[i].ang; tim[i].ang = an; hh = tim[i+1].h; tim[i+1].h = tim[i].h; tim[i].h = hh; mm = tim[i+1].m; tim[i+1].m = tim[i].m; tim[i].m = mm; } if(tim[i].ang == tim[i+1].ang) { if(tim[i].h *60 +tim[i].m > tim[i+1].h*60 +tim[i+1].m) { double an; int hh,mm; an = tim[i+1].ang; tim[i+1].ang = tim[i].ang; tim[i].ang = an; hh = tim[i+1].h; tim[i+1].h = tim[i].h; tim[i].h = hh; mm = tim[i+1].m; tim[i+1].m = tim[i].m; tim[i].m = mm; } } } /*for(int i=0; i<5; i++) { printf("%.3f %d:%d\n",tim[i].ang,tim[i].h,tim[i].m); }*/ if(tim[2].h >= 10) printf("%d:",tim[2].h); else printf("0%d:",tim[2].h); if(tim[2].m >= 10) printf("%d\n",tim[2].m); else printf("0%d\n",tim[2].m); } return 0; }
Problem E hdu 1236
这题结构体排序....不用stl还是很纠结的,学号的升序排列
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; struct node { char name[30]; int sco; } stu[1005]; int que[11]; int main() { int n,i,j,m,g,mm,t; while(cin >> n && n) { int cnt = 0; cin >> m >> g; for(i=1; i<=m; i++) scanf("%d",&que[i]); for(i=0; i<n; i++) { scanf("%s%d",stu[i].name,&mm); stu[i].sco = 0; for(j=0; j<mm; j++) { scanf("%d",&t); stu[i].sco += que[t]; } if(stu[i].sco >= g) cnt++; } for(j=0; j<n-1; j++) for(i=0; i<n-1-j; i++) { if(stu[i].sco < stu[i+1].sco) { t = stu[i+1].sco; stu[i+1].sco = stu[i].sco; stu[i].sco = t; char temp[30]; strcpy(temp,stu[i+1].name); strcpy(stu[i+1].name,stu[i].name); strcpy(stu[i].name,temp); } if(stu[i].sco <= stu[i+1].sco) { if(strcmp(stu[i].name,stu[i+1].name) > 0) { char temp[30]; strcpy(temp,stu[i+1].name); strcpy(stu[i+1].name,stu[i].name); strcpy(stu[i].name,temp); } } } printf("%d\n",cnt); for(i=0; i<cnt; i++) { printf("%s %d\n",stu[i].name,stu[i].sco); } } return 0; }
这个投机取巧了,1W的数组记录,然后就随意了
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int a[3005],vis[10005]; int main() { int n,m,i,j; while(cin >> n >> m) { memset(vis,0,sizeof(vis)); for(i=0; i<n; i++) { scanf("%d",&a[i]); } for(i=0; i<n; i++) for(j=i+1; j<n; j++) { vis[a[i]+a[j]]++; } i = 0; for(j=10000; j>=0; j--) { while(vis[j] >= 1) { if(i == m-1) printf("%d\n",j); else printf("%d ",j); i++; vis[j] --; if(i == m) break; } if(i == m) break; } } return 0; }
这题模拟了一下规律就行了
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; short map[5005][5005] = {{0}}; void init() { map[0][0] = 0; int ok = 0,cnt = 1; while(ok <= 10002) { if(cnt % 4 == 1) { ok += 2; map[ok/2][ok/2] = cnt; cnt++; } if(cnt % 4 == 2) { map[ok/2+1][ok/2-1] = cnt; cnt++; } if(cnt % 4 == 3) { ok += 2; map[ok/2+1][ok/2-1] = cnt; cnt++; } if(cnt % 4 == 0) { map[ok/2][ok/2] = cnt; cnt++; } } } int main() { int n,x,y; cin >> n; init(); while(n--) { cin >> x >> y; if(map[x][y] == 0 && (x!=0 || y!=0) ) printf("No Number\n"); //else if(x > 5000 || y > 5000) //printf("No Number\n"); else printf("%d\n",map[x][y]); } return 0; }
最后一个看数据,冒泡会tle,所以手写了个快排,也没什么说的
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; int a[10005]; void sort(int a[],int x,int y) { int xx=x,yy=y; int k=a[x]; if(x >= y) return ; while(xx != yy) { while(xx < yy && a[yy] >= k) yy--; a[xx]=a[yy]; while(xx < yy && a[xx] <= k) xx++; a[yy]=a[xx]; } a[xx]=k; sort(a,x,xx-1); sort(a,xx+1,y); } int main() { int n; while(cin >> n) { for(int i=0; i<n; i++) scanf("%d",&a[i]); sort(a,0,n-1); printf("%d",a[n-1]); for(int i=0,j=n-2; i<=j; i++,j--) { if(i == j) printf(" %d",a[i]); else printf(" %d %d",a[i],a[j]); } printf("\n"); } return 0; }