随笔分类 -  编程之美

发帖水王问题及扩展
摘要:见http://hi.baidu.com/piaoshi111/blog/item/204db32596b8c93e8644f972.html 阅读全文
posted @ 2011-11-01 16:53 吃吃户 阅读(157) 评论(0) 推荐(0)
2.7 最大公约数
摘要:1 #include <string.h> 2 #include <stdio.h> 3 4 int Gcd(int m, int n) 5 { 6 if (m < n) { 7 return Gcd(n, m); 8 } 9 10 if (n == 0) {11 return m;12 } else {13 return Gcd(n, m % n);14 }15 }16 17 int Gcd2(int m, int n)18 {19 if (m < n)20 retur... 阅读全文
posted @ 2011-10-22 21:04 吃吃户 阅读(276) 评论(0) 推荐(0)
2.5 k个最大的数
摘要:1 #include <string.h> 2 #include <stdio.h> 3 4 5 void Swap(int * a, int i, int j) 6 { 7 int temp = a[i]; 8 a[i] = a[j]; 9 a[j] = temp;10 }11 12 void Heapify(int * a, int start, int end)13 {14 int index;15 if (2 * start + 2 <= end) {16 index = a[2 * start + 1] < a[2 * s... 阅读全文
posted @ 2011-10-22 18:41 吃吃户 阅读(192) 评论(0) 推荐(0)
2.4 N到1中1的数目
摘要:1 #include <iostream> 2 using namespace std; 3 4 #include <stdlib.h> 5 #include <sys/types.h> 6 7 int CountOf1(u_int32_t n) 8 { 9 u_int32_t lower = 0;10 u_int32_t cur = 0;11 u_int32_t upper = 0;12 int count = 0;13 u_int32_t factor = 1;14 15 while (n / factor != 0) {16 ... 阅读全文
posted @ 2011-10-22 10:31 吃吃户 阅读(166) 评论(0) 推荐(0)
字符串编辑距离
摘要:1 #include <iostream> 2 using namespace std; 3 4 #include <string.h> 5 6 #define N 100 7 int matrix[N][N]; 8 int penalty = 10; 9 10 int MinOf3(int x, int y, int z) {11 x = x < y ? x : y;12 return x < z ? x : z;13 }14 15 int MinOf2(int x, int y)16 {17 return x < y ? x : y;18 }19 阅读全文
posted @ 2011-10-21 17:06 吃吃户 阅读(209) 评论(0) 推荐(0)
2.3发帖水王
摘要:1 #include <iostream> 2 using namespace std; 3 4 #include <stdlib.h> 5 #include <sys/types.h> 6 7 int Count(int * a, int n) 8 { 9 int count = 0;10 int candidate = a[0];11 while (n) {12 if (count == 0) {13 candidate = a[n-1];14 ++count;15 } else ... 阅读全文
posted @ 2011-10-21 15:23 吃吃户 阅读(151) 评论(0) 推荐(0)
2.2阶乘末尾0的个数,最低位1的位置
摘要:0的个数 1 #include <iostream> 2 using namespace std; 3 4 #include <stdlib.h> 5 #include <sys/types.h> 6 7 int Count(int n) 8 { 9 int count = 0;10 while (n) { 11 count += n / 5;12 n /= 5;13 }14 return count;15 }16 17 int main()18 {19 cout << Count(26) << endl;20 ... 阅读全文
posted @ 2011-10-21 14:33 吃吃户 阅读(312) 评论(0) 推荐(0)
2.1二进制数中1的个数
摘要:1 #include <iostream> 2 using namespace std; 3 4 #include <stdlib.h> 5 #include <sys/types.h> 6 7 int Count(u_int32_t n) 8 { 9 int count = 0;10 while (n) { 11 ++count;12 n = n & (n - 1);13 }14 return count;15 }16 17 int main()18 {19 cout << Count(128) << end... 阅读全文
posted @ 2011-10-21 13:23 吃吃户 阅读(210) 评论(0) 推荐(0)