超级简单
#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>num)
{
tmp1=num;
num=num;
num=tmp1;
tmp2=name;
name=name;
name=tmp2;
}
}
}
for(i=0,a=0,b=0;i<31;a++,b++,i++)
{
printf("%3c%3d",name,num);
}
return 0;
}
//我这个是要给数字和对应的城市排序,现在有两个问题,一个是char类型数组,装不了这么多字符串,怎么解决?还有i一个问题是,tmp2怎么表示,tmp2是暂时储存字符串的东西,就是两个字符串的位置交换的时候要用到。。。。 #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} ;
int i ,j , k , m , t ;
charname[]={"北京","天津","河北","山西","内蒙古","辽宁","吉林","黑龙江","上海","江苏","浙江","安徽","福建","广西","山东","河南","湖北","湖南","广东","广西","海南","重庆","四川","贵州","云南","西藏","陕西","甘肃","青海","宁夏","新疆"} ;
char s ;
for(m = sizeof(num) / sizeof(int) , i = 0 ; i < m - 1 ; i ++) {
for(j = i + 1 ; j < m ; j ++) {
if(num > num) {
t = num ;
num = num ;
num = t ;
for(k = 0 ; name ; k ++ , s = '\0') s = name ;
for(k = 0 ; name ; k ++ , name = '\0') name = name ;
for(k = 0 ; s ; k ++ , name = '\0') name = s ;
}
}
}
for(k = 0 ; k < m ; k ++) printf("%6s - %d\n" , name , num) ;
}
编译、运行实况
D:\00.Excise\C>g++ -o x x.
D:\00.Excise\C>x
西藏 - 20828
青海 - 51945
宁夏 - 61207
海南 - 93525
天津 - 162233
新疆 - 201515
北京 - 250084
内蒙古 - 275828
甘肃 - 338325
上海 - 345592
吉林 - 349177
山西 - 414649
福建 - 420377
贵州 - 446686
黑龙江 - 468616
陕西 - 487378
重庆 - 488115
广西 - 504043
云南 - 514563
广西 - 616828
辽宁 - 672227
浙江 - 759091
湖北 - 779566
安徽 - 853195
河北 - 928642
湖南 - 939818
广东 - 957360
河南 - 1196924
江苏 - 1247962
山东 - 1400109
四川 - 1412294
D:\00.Excise\C> 数字和字符串交换写成函数的版本
#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 ++) ;
for(m2 = 0 ; b ; m2 ++) ;
m = (m2 > m1) ? m2 : m1 ;
c = (char *) malloc(sizeof(char) * (m + 1)) ;
for(k = 0 ; a ; c = a , c = '\0');
for(k = 0 ; b ; a = b , a = '\0');
for(k = 0 ; c ; b = c , b = '\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 ;
charname[]={"北京","天津","河北","山西","内蒙古","辽宁","吉林","黑龙江","上海","江苏","浙江","安徽","福建","广西","山东","河南","湖北","湖南","广东","广西","海南","重庆","四川","贵州","云南","西藏","陕西","甘肃","青海","宁夏","新疆"} ;
char s ;
for(m = sizeof(num) / sizeof(int) , i = 0 ; i < m - 1 ; i ++) {
for(j = i + 1 ; j < m ; j ++) {
if(num > num) {
swapi(& num , & num) ;
swaps(name , name) ;
}
}
}
for(k = 0 ; k < m ; k ++) printf("%6s - %d\n" , name , num) ;
} 虽然我自己已经打出来了,但是你能回答我的问题,来帮我,我还是非常感动地,一个最佳回答属于你。。。
页:
[1]