33,321
社区成员




#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char words[101][52]={0};
int i,cnt=0,j,k;
char word[5100];
char buff[52];
i=0;j=0,k=0;
gets(word);
char*token=strtok(word," ");//分割
while(token!=NULL)
{
strcpy(words[i],token);
token=strtok(NULL," ");
cnt++;i++;
}
for(i=0;i<cnt-1;i++)//选择排序
{
j=i;
for(k=i+1;k<cnt;k++)
{
if(strcmp(words[j],words[k])==1)
{
j=k;
}
}
strcpy(buff,words[i]);
strcpy(words[i],words[j]);
strcpy(words[j],buff);
}
for(i=0;i<cnt;i++)//输出
{
if(strcmp(words[i],words[i+1])==0)
{
continue;
}
if(i!=cnt-1)
puts(words[i]);
else
printf("%s",words[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> words;
int cnt = 0;
char word[5100];
gets(word);
char* pch = strtok(word, " \t"); //分割
while (pch != NULL) {
words.push_back(string(pch));
pch = strtok(NULL, " \t\n");
cnt++;
}
// 排序和删除重复
std::sort(words.begin(), words.end());
auto it = unique(words.begin(), words.end());
words.erase(it, words.end());
for (auto it = words.begin(); it != words.end(); it++) { //输出
printf("%s\n", it->c_str());
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char words[101][52]={0};
int i,cnt=0,j,k;
char word[5100];
char buff[52];
i=0;j=0,k=0;
gets(word);
char*token=strtok(word," ");//分割
while(token!=NULL)
{
strcpy(words[i],token);
token=strtok(NULL," ");
cnt++;
i++;
}
/*
printf("cnt = %d\n", cnt);
for (i = 0; i < cnt; i++)
printf("words[%d] = %s\n", i, words[i]);
*/
for(i = 0; i < cnt-1; i++)
{
j = i;
for(k = i + 1; k < cnt; k++)
if(strcmp(words[j], words[k]) > 0)
j = k;
if (j != i) { /*交换条件*/
strcpy(buff, words[i]);
strcpy(words[i], words[j]);
strcpy(words[j], buff);
}
}
/*
for (i = 0; i < cnt; i++)
printf("words[%d] = %s\n", i, words[i]);
*/
for(i = 0; i < cnt; i++)//输出
{
if(strcmp(words[i], words[i+1])==0)
{
continue;
}
if(i!=cnt-1)
puts(words[i]);
else
printf("%s",words[i]);
}
return 0;
}
参考一下吧
主要是选择排序的问题,选择排序算法写的有点问题,详见上面的代码;