\u7528C\u8bed\u8a00\u8f93\u51653\u4e2a\u6574\u6570\uff0c\u600e\u4e48\u6309\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u8f93\u51fa\uff1f

C\u8bed\u8a00\u5927\u5c0f\u5b57\u6bcd\u8f93\u51fa

\u4ee3\u78011.
// \u8f93\u51653\u4e2a\u6570\uff0c\u8981\u6c42\u6309\u4ece\u5c0f\u5230\u5927\u987a\u5e8f\u8f93\u51fa #include int main() { int a,b,c,t; printf("\u8bf7\u8f93\u5165\u4e09\u4e2a\u6570\uff1a"); scanf("%d%d%d",&a,&b,&c); if(a > b) { t = a; a = b; b = t; } if(a > c) { t = a; a = c; c = t; } if(b > c) { t = b; b = c; c = t; } printf("\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u662f\uff1a%d %d %d\n",a,b,c); return 0; }\u4ee3\u78012.
\u8f93\u51653\u4e2a\u5b57\u7b26\u4e32\uff0c\u6309\u4ece\u5c0f\u5230\u5927\u987a\u5e8f\u8f93\u51fa\u3002 //\u5148\u7528\u7a0b\u5e8f\u5bf9\u4e09\u4e2a\u6570\u8fdb\u884c\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff0c\u7136\u540e\u4fee\u6539\u7a0b\u5e8f#include#includeint main(){void swap(char *pt1,char *pt2); char a[20],b[20],c[20]; char *p1,*p2,*p3; printf("\u8bf7\u8f93\u5165\u4e09\u4e2a\u5b57\u7b26\u4e32:"); gets(a); gets(b); gets(c); //\u6216\u7528scanf("%s,%s,%s",a,b,c); p1=&a[0];p2=&b[0];p3=&c[0];//\u4e09\u4e2a\u6307\u9488\u5206\u522b\u6307\u5411\u4e09\u4e2a\u5b57\u7b26\u6570\u7ec4 if(strcmp(*p1,*p2)>0)swap(p1,p2);//if(strcmp(a,b)>0)swap(a,b); //\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\uff0c\u4e3a\u4ec0\u4e48\u7528\u524d\u4e00\u53e5\u7684\u65f6\u5019\u4f1a\u51fa\u73b0\u8b66\u544a\u5462 if(strcmp(a,c)>0)swap(a,c);//if(strcmp(*p1,*p3)>0)swap(*p1,*p3); if(strcmp(b,c)>0)swap(b,c);// if(strcmp(*p2,*p3)>0)swap(*p2,*p3); printf("\u7531\u5c0f\u5230\u5927\u6392\u5217\uff1a%s\n%s\n%s\n",a,b,c); return 0;}void swap(char *pt1,char *pt2){ char t[20]; strcpy(t,pt1); strcpy(pt1,pt2); strcpy(pt2,t);//t=*pt1;*pt1=*pt2;*pt2=t;}\u4ee3\u78013.
#include #include #define SIZE 3 #define LEN 50 int main(void) { char str[SIZE][LEN]; char (*pst)[LEN]=str; char temp[LEN]; int i,j; printf("Please enter 3 string.\n"); for(i=0;i<SIZE;i++) { fgets(*(pst+i),LEN,stdin); } printf("Befor sort:\n"); for(i=0;i<SIZE;i++) { fputs(*(pst+i),stdout); } for(i=0;i<SIZE-1;i++) for(j=i+1;j<SIZE;j++) { if(strcmp(*(pst+i),*(pst+j)) == 1) { strcpy(temp,*(pst+i)); strcpy(*(pst+i),*(pst+j)); strcpy(*(pst+j),temp); } } printf("After sort:\n"); for(i=0;i<SIZE;i++) { fputs(*(pst+i),stdout); } }

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网