由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - HELP: C programming question
相关主题
bloomberg assessment的机经,c语言的(20道题)问一道简单DP题
帮看看这段code两个店面题
算法:给N个不重复的字母,输出个数为M的组合问个算法题
谁能猜猜,这是个什么 algorithm?再请教个:C变长参数的传递问题
least common ancestor的疑惑几个C语言的题目
问一个memory allocate/release的问题Online Coding Test能不能边做边查资料?
问道G题(3)bloomberg on-site赌博失败归来
cc150的solution都对吗?为什么好多我都觉得不对?bloomberg测试题,失败阿
相关话题的讨论汇总
话题: file话题: num话题: sc话题: newstr
进入JobHunting版参与讨论
1 (共1页)
s****1
发帖数: 26
1
Please write a function that takes a string and a “FILE *” as parameters,
assuming the string consists of words separated by a single white space, and
prints into the “FILE *” the words in the string in descending order
sorted by the number of times they appear. For example an input of “a b b”
would generate output of:
b : 2
a : 1
t*******i
发帖数: 4960
2
"assuming the string consists of words"
so if the string is "ab abc ab", we would print
ab 2
abc 1
right?
if "assuming the string consists of characters", it would be much easier.
S**I
发帖数: 15689
3
#include
#include
#include
struct StringCount{
char s[100];
int count;
};
int compare(const void * a, const void * b){
return ((*(struct StringCount *)a).count < (*(struct StringCount *)b).count);
}
void printSortedWords(char * s, FILE * file){
int size = strlen(s);
struct StringCount sc[100];
int num = 0;
char newstr[100];
int i = 0;
for(; i < size; i++){
if(s[i] != ' '){
int j = 0;
while(s[i] != ' ' && s[i] != '\0'){
newstr[j] = s[i];
i++;
j++;
}
newstr[j] = '\0';
for(j = 0; j < num; j++){
if(!strcmp(newstr, sc[j].s)){
sc[j].count++;
break;
}
}
if(j == num){
strcpy(sc[num].s, newstr);
sc[num].count = 1;
num++;
}
}
}
qsort(sc, num, sizeof(struct StringCount), compare);
for(i = 0; i < num; i++){
fprintf(file, "%s: %d\n", sc[i].s, sc[i].count);
}
}
int main (int argc, char * argv[])
{
FILE * file;
file = fopen("output.txt", "w");
printSortedWords(argv[1], file);
fclose(file);
return 0;
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
bloomberg测试题,失败阿least common ancestor的疑惑
IT Interview Questions问一个memory allocate/release的问题
一个grep(?)问题的求助问道G题(3)
关于fdopen和fopencc150的solution都对吗?为什么好多我都觉得不对?
bloomberg assessment的机经,c语言的(20道题)问一道简单DP题
帮看看这段code两个店面题
算法:给N个不重复的字母,输出个数为M的组合问个算法题
谁能猜猜,这是个什么 algorithm?再请教个:C变长参数的传递问题
相关话题的讨论汇总
话题: file话题: num话题: sc话题: newstr