连接:目前uva挂掉了~= =。。。
大意是给你N个字符串,每个串的第一个单词代表国家,后面的单词表示女人,要你找出每个国家出现了几个女女。按字字典书序输出。
一开始re,发现数组开小了。后来wa很郁闷,最后发现没有删除中间测试数据的输出= =。。。
以下是代码
View Code
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct node { char con[205]; char girl[205]; int num; }wokao; int cmp_string(wokao *a,wokao *b) { return strcmp(a->con,b->con); } int cmp ( const void *a , const void *b ) { return strcmp( (*(wokao *)a).con , (*(wokao *)b).con ); } int main() { int i,j,count,n; char str[205]; wokao a[2005]; scanf("%d",&n); count = 0; for(i = 0;i < n;i++) a[i].num = 0; for(i = 0;i < n;i++) { scanf("%s",str); for(j = 0;j < count;j++) if(strcmp(str,a[j].con) == 0) { a[j].num++; break; } if(j == count) { a[count].num++; strcpy(a[count].con,str); count++; } getchar(); gets(str); } qsort(a,count,sizeof(a[0]),cmp); for(i = 0;i < count;i++) printf("%s %d\n",a[i].con,a[i].num); return 0; }
还有个c++很简单的代码就是利用map~表示不会用c++的人伤不起啊,先下楼问的秦老师~map<string,int>r就相当于r是一个结构体数组,string是变量i,int 是里面的一个整形值。代码
View Code
#include<iostream> #include<string> #include<map> #include<cstdio> using namespace std; const int kMaxn(2007); int main() { /* freopen("data.in","r",stdin); freopen("data.out","w",stdout); //*/ int n; cin>>n; map<string,int> r; for(int i=1;i<=n;i++) { string country,t; cin>>country; r[country]++; getchar(); getline(cin,t); } for(map<string,int>::iterator i=r.begin();i!=r.end();i++) cout<<i->first<<" "<<i->second<<endl; return 0; }
另外有一个很重要的用法就是qsort对结构体字符串的排序!!!!
int cmp ( const void *a , const void *b )
{
return strcmp( (*(struct node *)a).str , (*(struct node *)b).con );
}
qsort(a,count,sizeof(a[0]),cmp);