粘贴两个特别简单的冒泡排序
2014: Scramble Sort
Description
In this problem you will be given a series of lists containing both words and numbers. The goal is to sort these lists in such a way that all words are in alphabetical order and all numbers are in numerical order. Furthermore, if the nth element in the list is a number it must remain a number, and if it is a word it must remain a word.
Input
The input will contain multiple lists, one per line. Each element of the list will be separated by a comma followed a space, and the list will be terminated by a period. The input will be terminated by a line containing only a single
period.
Output
For each list in the input, output the scramble sorted list, separating each element of the list with a comma followed by a space, and ending the list with a period.
Sample Input
0.
banana, strawberry, OrAnGe.
Banana, StRaWbErRy, orange.
10, 8, 6, 4, 2, 0.
x, 30, -20, z, 1000, 1, Y.
50, 7, kitten, puppy, 2, orangutan, 52, -100, bird, worm, 7, beetle.
.
Sample Output
0.
banana, OrAnGe, strawberry.
Banana, orange, StRaWbErRy.
0, 2, 4, 6, 8, 10.
x, -20, 1, Y, 30, 1000, z.
-100, 2, beetle, bird, 7, kitten, 7, 50, orangutan, puppy, 52, worm.
分数字和字母,手写个cmp就好了
#include <cstring> #include <cstdio> #include <ctype.h> char s[100][30]; char t[30]; int cmp(int i,int j){ if(s[i][0]=='-'&&s[j][0]=='-'){ if(strlen(s[i])<strlen(s[j])) return 1; else if(strlen(s[i])==strlen(s[j])){ if(strcmp(s[i],s[j])<0) return 1; } } else if(isdigit(s[i][0])&&isdigit(s[j][0])){ if(strlen(s[i])>strlen(s[j])) return 1; else if(strlen(s[i])==strlen(s[j])){ if(strcmp(s[i],s[j])>0) return 1; } } else if(s[j][0]=='-'&&isdigit(s[i][0])) return 1; else if(isalpha(s[i][0])&&isalpha(s[j][0])&&stricmp(s[i],s[j])>0) return 1; return 0; } int main(){ while(~scanf("%s",t)){ memset(s,0,sizeof(s)); if(strcmp(t,".")==0) break; int i; for(i=0;;i++){ int l=strlen(t); for(int j=0;j<l-1;j++) s[i][j]=t[j]; if(t[l-1]=='.') break; else scanf("%s",t); } int n=++i; for(int i=0;i<n-1;i++) for(int j=i+1;j<n;j++){ if(cmp(i,j)){ strcpy(t,s[j]); strcpy(s[j],s[i]); strcpy(s[i],t); } } for(int i=0;i<n;i++){ if(i)printf(", "); printf("%s",s[i]); } printf(". "); } return 0; }
2034: 面积排序
Total Submit: 1163 Accepted:433
Description
给定三角形、矩形、圆等二维几何图形,请根据面积从大到小进行排序。
Input
输入数据包括有多行,每行为一个几何图形(不超过100个几何图形)。各种几何图形的输入格式如下:
三角形(x1,y1,x2,y2,x3,y3分别为三角形顶点坐标):
triangle
x1 y1 x2 y2 x3 y3
矩形(x1,y1,x2,y2为矩形某对角线的端点,矩形的边与坐标轴平行)
rectangle x1 y1 x2 y2
圆(x1,y1为圆心坐标,r为半径)
circle x1 y1 r
Output
对各个图形分别进行编号命名,三角形命名为triangle1、triangle2...,矩形命名为rectangle1、rectangle2...,圆命名为circle1、circle2...
然后按照面积从大到小进行排序,如果面积相同,则根据名字按照字典序排序。每行输出一个形状的名字以及面积,用空格分隔,面积保留3位小数。
Sample Input
rectangle 0.0 0.0 1.0 2.0
circle 0.0 0.0 1.0
triangle 0.0 0.0 1.0 1.0 1.0 0.0
rectangle 0.0 0.0 1.0 1.0
circle 0.0 0.0 2.0
Sample Output
circle2 12.566
circle1 3.142
rectangle1 2.000
rectangle2 1.000
triangle1 0.500
Hint
#include <stdio.h> #include <math.h> #include <string.h> double rectangle() { double x1,y1,x2,y2,s; scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); s=fabs((x1-x2)*(y1-y2)); return s; } double circle() { double x1,y1,r,s; scanf("%lf%lf%lf",&x1,&y1,&r); s=3.1415926*r*r; return s; } double triangle() { double x1,y1,x2,y2,x3,y3,s; scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3); s=fabs((x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2)/2); return s; } int main() { int i,j,k,tmp3; double a[101],tmp1; int p,q,r,c[101]; char s[101][10],st[10]="triangle",str[10]="circle",tmp2[10]; k=0; p=1; q=1; r=1; while(scanf("%s",s[k])!=EOF) { if(strcmp(s[k],st)==0) { a[k]=triangle(); c[k]=p++; } else if(strcmp(s[k],str)==0) { a[k]=circle(); c[k]=q++; } else { a[k]=rectangle(); c[k]=r++; } k++; getchar(); } for(j=0; j<k-1; j++) for(i=0; i<k-j-1; i++) { if(a[i]<a[i+1]) { tmp1=a[i]; a[i]=a[i+1]; a[i+1]=tmp1; tmp3=c[i]; c[i]=c[i+1]; c[i+1]=tmp3; strcpy(tmp2,s[i]); strcpy(s[i],s[i+1]); strcpy(s[i+1],tmp2); } else if(a[i]==a[i+1]) { if(strcmp(s[i],s[i+1])>0) { strcpy(tmp2,s[i]); strcpy(s[i],s[i+1]); strcpy(s[i+1],tmp2); tmp3=c[i]; c[i]=c[i+1]; c[i+1]=tmp3; } else if(strcmp(s[i],s[i+1])==0) { if(c[i]>c[i+1]) { tmp3=c[i]; c[i]=c[i+1]; c[i+1]=tmp3; } } } } for(i=0; i<k; i++) printf("%s%d %.3f ",s[i],c[i],a[i]); return 0; }