| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 351 人关注过本帖
标题:请指点一算法问题
取消只看楼主 加入收藏
wentaiyou
Rank: 2
等 级:论坛游民
帖 子:68
专家分:17
注 册:2004-12-3
结帖率:100%
收藏
 问题点数:0 回复次数:1 
请指点一算法问题

题目:从键盘上输入N个字符串.长度<10,判断在这输入的N个字符串中有相同字符串的个数.并输出不相同的字符串和出相同的次数
如:输入 asdf fdsa asdf xxxx asdf xxxx这样6个字符串
应该输出的是
asdf 3次
fdsa 1次
xxxx 2次

这样写了确得不到要的结果不知道错在哪里

#include "stdio.h"
#define NUM 5

struct student
{
char name[10];

int count;
};
struct student pu[NUM];

int dd(char x[], int y)
{
int i;
for(i=0;i<NUM;i++)

if(strcmp(pu[i].name,x)==0)
{
pu[i].count=pu[i].count+y;

}

else
{
strcpy(pu[i].name,x);
pu[i].count=y;
}
}


main()
{
int i,count;
char name[10];

for(i=0;i<NUM;i++)
{ printf("\nthe %d data:\n",i+1);

printf("please inter name:");
scanf("%s",name);

printf("please inter count:");
scanf("%d",count);

dd(name,count);

}
printf("\n");

for(i=0;i<NUM;i++)
printf("the %d :%s-->%d\n",i,pu[i].name,pu[i].count);

}

[此贴子已经被作者于2007-5-17 18:05:39编辑过]

搜索更多相关主题的帖子: 算法 
2007-05-17 17:19
wentaiyou
Rank: 2
等 级:论坛游民
帖 子:68
专家分:17
注 册:2004-12-3
收藏
得分:0 

#include "stdio.h"
#define NUM 5

struct student
{
char name[10];

int count;
};
struct student pu[NUM];

int dd(char x[], int y)
{
int i;
for(i=0;i<NUM;i++)

if(strcmp(pu[i].name,x)==0)
{
pu[i].count=pu[i].count+y;

}

else
{
strcpy(pu[i].name,x);
pu[i].count=y;
}
}


main()
{
int i,count;
char name[10];

for(i=0;i<NUM;i++)
{ printf("\nthe %d data:\n",i+1);

printf("please inter name:");
scanf("%s",name);

printf("please inter count:");
scanf("%d",count);

dd(name,count);

}
printf("\n");

for(i=0;i<NUM;i++)
printf("the %d :%s-->%d\n",i,pu[i].name,pu[i].count);

}


假如回到过去.我能做些什么? 还是和现在这样有时间没事情?
2007-05-17 18:03
快速回复:请指点一算法问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014786 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved