|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
int main()
{
int num[]={250084,162233,928642,414649,275828,672227,349177,468616,345592,1247962,759091,853195,420377,504043,1400109,1196924,779566,939818,957360,616828,93525,488115,1412294,446686,514563,20828,487378,338325,51945,61207,201515};
char *name[]={"北京","天津","河北","山西","内蒙古","辽宁","吉林","黑龙江","上海","江苏","浙江","安徽","福建","广西","山东","河南","湖北","湖南","广东","广西","海南","重庆","四川","贵州","云南","西藏","陕西","甘肃","青海","宁夏","新疆"};
int i,j,tmp1,tmp2,a,b;
for(i=1;i<=30;i++)
{
for(j=0;j<=30-1-i;j++)
{
if(num[j]>num[j+1])
{
tmp1=num[j];
num[j]=num[j+1];
num[j+1]=tmp1;
tmp2[0]=name[j];
name[j]=name[j+1];
name[j+1]=tmp2;
}
}
}
for(i=0,a=0,b=0;i<31;a++,b++,i++)
{
printf("%3c %3d",name[a],num[b]);
}
return 0;
}
//我这个是要给数字和对应的城市排序,现在有两个问题,一个是char类型数组,装不了这么多字符串,怎么解决?还有i一个问题是,tmp2怎么表示,tmp2是暂时储存字符串的东西,就是两个字符串的位置交换的时候要用到。。。。
数字和字符串交换写成函数的版本 #include <stdio.h>
#include <stdlib.h>
void swapi(int * a , int * b)
{
int t ;
t = * a ;
* a = * b ;
* b = t ;
}
void swaps(char a[] , char b[])
{
int k , m1 , m2 , m ;
char * c ;
for(m1 = 0 ; a[m1] ; m1 ++) ;
for(m2 = 0 ; b[m2] ; m2 ++) ;
m = (m2 > m1) ? m2 : m1 ;
c = (char *) malloc(sizeof(char) * (m + 1)) ;
for(k = 0 ; a[k] ; c[k ++] = a[k] , c[k] = '\0') ;
for(k = 0 ; b[k] ; a[k ++] = b[k] , a[k] = '\0') ;
for(k = 0 ; c[k] ; b[k ++] = c[k] , b[k] = '\0') ;
free(c) ;
}
int main()
{
int num[]={250084,162233,928642,414649,275828,672227,349177,468616,345592,1247962,759091,853195,420377,504043,1400109,1196924,779566,939818,957360,616828,93525,488115,1412294,446686,514563,20828,487378,338325,51945,61207,201515} ;
int i ,j , k , m , t ;
char name[][24]={"北京","天津","河北","山西","内蒙古","辽宁","吉林","黑龙江","上海","江苏","浙江","安徽","福建","广西","山东","河南","湖北","湖南","广东","广西","海南","重庆","四川","贵州","云南","西藏","陕西","甘肃","青海","宁夏","新疆"} ;
char s[24] ;
for(m = sizeof(num) / sizeof(int) , i = 0 ; i < m - 1 ; i ++) {
for(j = i + 1 ; j < m ; j ++) {
if(num[i] > num[j]) {
swapi(& num[i] , & num[j]) ;
swaps(name[i] , name[j]) ;
}
}
}
for(k = 0 ; k < m ; k ++) printf("%6s - %d\n" , name[k] , num[k]) ;
}
|
|