程序员12期算法擂台
2007-12-20 22:22
218 查看
看了12期的讲解,赶紧回来把代码写出来:
// huiwen.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
void swap (char * const s, int i, int j)
...{
char temp;
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
int pseudo_bubble (char * const s, int length)
...{
int i,j,k;
int odd;
char alphabet[26];
int count=0;
for (i=0; i<length; i++)
alphabet[s[i]-'a']++;
odd = -1;
for (i=0; i<sizeof(alphabet); i++)
if (alphabet[i] % 2) ...{
if (odd = -1) // the first 'odd'
odd = 'a' + i;
else // 2 odds
return -1;
}
i=0;
while (s[i] != s[length-1-i]) ...{ // until the corresponding element is right
j = length-1-i;
for (k=j-1; k>i && s[k]!=s[i]; k--); // find the corresponding element's location
if (k == i) ...{// find nobody -> this is the last 'odd' - closest to the center
swap(s,k,k+1);
} else ...{
while (k<j) ...{
swap(s,k,k+1); // bubble it out
count++;
k++;
}
i++;
}
}
return count;
}
int _tmain(int argc, _TCHAR* argv[])
...{
int x;
char s[] = ...{'a','b','a','c','a','e','b','a','d','c','d'};
//{'a','b','a','c','a','b','a','d','c','d'};
//{'a','b','a','c','e','b','e','d','c','d'};
x = pseudo_bubble(s,sizeof(s));
if (x==-1)
printf("Impossible");
else
printf("%d",x);
//getchar();
return 0;
}
开的VS2005写的,显得累赘。。。
找奇数次出现的元素所用的循环可以合并到后来的排序的循环里去的,不过那样,太丑。。。
刚看讲解时以为说的是选择成对字符时可以“左右开弓”?? 但做起来似乎代码很麻烦,所以干脆以左半边为准排序。
// huiwen.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
void swap (char * const s, int i, int j)
...{
char temp;
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
int pseudo_bubble (char * const s, int length)
...{
int i,j,k;
int odd;
char alphabet[26];
int count=0;
for (i=0; i<length; i++)
alphabet[s[i]-'a']++;
odd = -1;
for (i=0; i<sizeof(alphabet); i++)
if (alphabet[i] % 2) ...{
if (odd = -1) // the first 'odd'
odd = 'a' + i;
else // 2 odds
return -1;
}
i=0;
while (s[i] != s[length-1-i]) ...{ // until the corresponding element is right
j = length-1-i;
for (k=j-1; k>i && s[k]!=s[i]; k--); // find the corresponding element's location
if (k == i) ...{// find nobody -> this is the last 'odd' - closest to the center
swap(s,k,k+1);
} else ...{
while (k<j) ...{
swap(s,k,k+1); // bubble it out
count++;
k++;
}
i++;
}
}
return count;
}
int _tmain(int argc, _TCHAR* argv[])
...{
int x;
char s[] = ...{'a','b','a','c','a','e','b','a','d','c','d'};
//{'a','b','a','c','a','b','a','d','c','d'};
//{'a','b','a','c','e','b','e','d','c','d'};
x = pseudo_bubble(s,sizeof(s));
if (x==-1)
printf("Impossible");
else
printf("%d",x);
//getchar();
return 0;
}
开的VS2005写的,显得累赘。。。
找奇数次出现的元素所用的循环可以合并到后来的排序的循环里去的,不过那样,太丑。。。
刚看讲解时以为说的是选择成对字符时可以“左右开弓”?? 但做起来似乎代码很麻烦,所以干脆以左半边为准排序。
相关文章推荐
- 《程序员》2008年第1期"算法擂台" - Cantor表解答
- 《程序员》2008年第2期"算法擂台" - 微积分习题解答
- 12期算法擂台解答
- 《程序员》2008年第1期"算法擂台" - Cantor表解答
- 《程序员》2007年第九期算法擂台——骑士聚会
- 《程序员》2008年第1期"算法擂台" - Cantor表解答
- 《程序员》算法擂台之骑士聚会问题(c语言版) 一
- 《程序员》2007年第九期算法擂台——骑士聚会
- 《程序员》算法擂台之骑士聚会问题(c语言版) 二
- 程序员杂志07年12月算法擂台时间表达的解答
- 程序员必须掌握哪些算法?
- 程序员必须知道的10大基础实用算法及其讲解
- 程序员必须知道的10大基础实用算法及其讲解(转)
- 程序员面试题精选100题(13)-第一个只出现一次的字符[算法]
- 程序员必须知道的10大基础实用算法及其讲解
- PHP程序员玩转算法公开课(第一季)02_单链表在内存中存在形式剖析
- 程序员必须知道的10大基础实用算法及其讲解
- 程序员如何快速准备面试中的算法
- 算法的威力:程序员用台式机打破超级计算机保持的世界记录
- 《程序员》第9期智慧擂台题目——高频词汇提取