03 2012 档案

UVa 674 - coin change
摘要:之前做过一道HDOJ上很类似的一道题,当时用的是枚举(依次从大到小枚举某一面值使用的次数,然后把所有情况相加),今天在UVa动态规划里看到这道题,一下子就没思路了,后来翻到了Rookie_Yang的代码,实在不理解。。硬着头皮求助队友,最后找到了正确的状态转移方程,才理解他的代码是优化过的;dp(i, j)表示 i 分钱用前 j(0,1,2,3,4) 种类型的硬币兑换总的方法;2012.4.2 以下注释中的考虑没必要,因为dp()中已经对j == 0的情况处理了。/* 记忆画搜索时还需要注意:对于 s[0..4][0],要在搜索前初始化(因为最终追溯到的是 s[0..4][0] 这 5 个数) 阅读全文

posted @ 2012-03-31 20:39 getgoing 阅读(621) 评论(2) 推荐(0) 编辑

UVa10405 - Longest Common Subsequence
摘要:最大公共子序列,需要注意的是x[]最开始保存的是相应字符串的长度,在比较时应比较s1[x[1]-1]与s2[x[2]-1];水题,做一道过一道。。。 1 /* 10405 - Longest Common Subsequence */ 2 # include <stdio.h> 3 # include <string.h> 4 5 # define MAXN 1001 6 7 int lena; 8 int x[2]; 9 char s1[MAXN];10 char s2[MAXN];11 int ans[MAXN*MAXN];12 13 int get(int *x) 阅读全文

posted @ 2012-03-31 14:50 getgoing 阅读(287) 评论(0) 推荐(0) 编辑

UVa 103 - Stacking Boxes
摘要:DAG上的动态规划,需要注意最后要打印起点到终点的一条路,因此状态d[i]定义为从 i 出发的最长路的距离。UVa今天这是怎么了,两道题都是将近5min才出结果。 1 # include <stdio.h> 2 # include <string.h> 3 # include <stdlib.h> 4 5 # define MAXD 11 6 # define MAXN 31 7 8 int k, n; 9 int d[MAXN];10 int box[MAXN][MAXD];11 int g[MAXN][MAXN];12 13 int cmp(const 阅读全文

posted @ 2012-03-31 14:13 getgoing 阅读(232) 评论(0) 推荐(0) 编辑

UVa 111 - History Grading
摘要:最大公共子序列;要读懂题目中对于输入的描述;仍然是上次的GDKOI最大公共子串的写法。 1 # include <stdio.h> 2 3 int n; 4 int x[2]; 5 int cor[21]; 6 int cur[21]; 7 int ans[441]; 8 9 int get(int *x);10 11 int main()12 {13 int i, t;14 15 scanf("%d", &n);16 for (i = 1; i <= n; ++i) 17 {18 scanf("%d", &t);19 阅读全文

posted @ 2012-03-31 13:02 getgoing 阅读(473) 评论(0) 推荐(0) 编辑

TLE: poj 1011 Sticks
摘要:Ozy的方法(dfs): 1 # include <stdio.h> 2 # include <string.h> 3 4 int f[51]; 5 int ok; 6 int length; 7 8 void dfs(int cnt, int len, int cur) 9 {10 int j;11 12 if (cnt == 0) {ok = 1; return;}13 14 --f[cur];15 16 len -= cur;17 if (len != 0)18 {19 j = len<cur ?... 阅读全文

posted @ 2012-03-30 12:56 getgoing 阅读(172) 评论(0) 推荐(0) 编辑

poj 3278 catch that cow
摘要:bfs,RE多次之后学习大牛的代码,不要惊讶怎么限定在100000范围内!更好的解法:http://www.cnblogs.com/longzhiri/articles/1555344.html 1 # include <stdio.h> 2 # include <string.h> 3 # include <queue> 4 5 using namespace std; 6 7 int n, k, x; 8 int d[100002]; 9 queue<int>Q;10 11 int main()12 { 13 while (~scanf(&q 阅读全文

posted @ 2012-03-29 20:33 getgoing 阅读(175) 评论(0) 推荐(0) 编辑

GDKOI2003 最大公共子串
摘要:AOJ链接:最大公共子串这道题求多个字符串的最大公共序列(非连续)的长度,题目中说明了所有串的乘积不超过30000;题解将状态记录在一个长度为30000的数组中,使用类似编码的方式(我的理解)进行存取;和算法导论上对LCS的解法不大一样(递归而不是递推,计算量会少一些),仍然是动态规划的思想;0MS,学习了。下面的代码是看懂了书上的后,自己写的;起先觉得第47、48行的恢复多余,后来发现并不是:包含回溯的过程,需要恢复原来的下标。 1 # include <stdio.h> 2 # include <string.h> 3 4 char str[102][102]; 5 阅读全文

posted @ 2012-03-28 22:47 getgoing 阅读(412) 评论(0) 推荐(0) 编辑

poj 1008 Maya Calendar
摘要:POJ的陷阱多多。。first WA:没考虑到一年中最后一天的情况,改了,也通过了一组discuss中的数据;2~3th WA:完全忽略了题目要求第一行输出test的个数;4th WA:输出格式中日的后面没有'.'。。。彻底服了 1 # include <stdio.h> 2 3 const char habbm[20][10] = {" ", "pop", "no", "zip", "zotz", "tzec", "xul", 阅读全文

posted @ 2012-03-28 13:46 getgoing 阅读(261) 评论(0) 推荐(0) 编辑

poj 1006 Biorhythms
摘要:直接枚举(16MS,可以接受),测试数据有点纠结。 1 # include <stdio.h> 2 3 int p, e, i, d; 4 const int add[] = {1, 23, 28, 23*28, 33, 23*33, 28*33}; 5 6 int mod(int x, int m); 7 8 int main() 9 {10 int cnt, day, sta;11 12 cnt = 0;13 while (1)14 {15 scanf("%d%d%d%d", &p, &e, &i, &d);16 ... 阅读全文

posted @ 2012-03-28 11:23 getgoing 阅读(311) 评论(0) 推荐(0) 编辑

poj 2785 4 Values whose Sum is 0
摘要:二分枚举;也可以将两个和数组都排序,这样可以在查找时保持沿一个方向,最坏情况下复杂度为O(n),不如二分查找;计算cpd[]时,c[i] + d[j]错写成c[i]+d[i]查了半天才发现。。。 1 # include <stdio.h> 2 # include <stdlib.h> 3 4 # define MAXN 4001 5 6 int apb[MAXN*MAXN], cpd[MAXN*MAXN]; 7 int a[MAXN], b[MAXN], c[MAXN], d[MAXN]; 8 9 int cmp(const void *a, const void *b 阅读全文

posted @ 2012-03-28 02:13 getgoing 阅读(437) 评论(0) 推荐(0) 编辑

UVa 10815 - Andy's First Dictionary
摘要:这道题貌似有陷阱:用gets过不了。 1 # include <stdio.h> 2 # include <ctype.h> 3 # include <string.h> 4 # include <stdlib.h> 5 6 # define MAX_LEN 205 7 # define MAXN 5005 8 9 char dic[MAXN][MAX_LEN];10 char word[MAX_LEN];11 12 int cmp(const void *a, const void *b);13 14 int main()15 {16 char 阅读全文

posted @ 2012-03-27 00:37 getgoing 阅读(697) 评论(0) 推荐(0) 编辑

UVa 10878 - Decode the tape
摘要:开始以为统计示例那句话中26个字母对应的字符串就行了,后来发现‘A’和‘a’不一样,仔细一看有一个位不一样,突然想到了ascii,一看7位,这不正好吗! 1 # include <stdio.h> 2 # include <string.h> 3 4 char str[15]; 5 char t[8] = {64, 32, 16, 8, 0, 4, 2, 1}; 6 7 int main() 8 { 9 short int i, c;10 11 gets(str);12 while (gets(str) != NULL)13 {14 ... 阅读全文

posted @ 2012-03-27 00:33 getgoing 阅读(219) 评论(0) 推荐(0) 编辑

UVa 409 - Excuses, Excuses!
摘要:写的很繁琐。 1 # include <stdio.h> 2 # include <ctype.h> 3 4 # define MAX_WORD_LEN 25 5 # define MAX_LINE_LEN 75 6 # define MAXN 25 7 8 char keyw[MAXN][MAX_WORD_LEN]; 9 char line[MAXN][MAX_LINE_LEN];10 char copy[MAX_LINE_LEN];11 int cnt[MAXN];12 13 int key_cnt(char *line, int len, char *keywor 阅读全文

posted @ 2012-03-27 00:30 getgoing 阅读(352) 评论(0) 推荐(0) 编辑

TLE:csu 1205 放石子游戏
摘要:表示第一次用dfs。。。 1 # include <stdio.h> 2 # include <string.h> 3 4 char board[1001][1001]; 5 char vis[1001]; 6 int trace[1001]; 7 int N, M, ans; 8 9 void dfs(int x, int y);10 void special_dfs(int x, int y, int trace[]);11 12 int main()13 {14 int i, j, x, y;15 16 while (~scanf("%d%d" 阅读全文

posted @ 2012-03-27 00:26 getgoing 阅读(232) 评论(0) 推荐(0) 编辑

CSU 1109 FIbonacci-prime
摘要:实际上就是求第(<=)600000个素数,但是。。要优化内存,要优化速度。。最重要的要估计第600000个素数有多大,直接打印当然行,但是。。总之学了素数定理:1/ln(n)的分布。。 1 # include <stdio.h> 2 3 # define MAXN 600005 4 # define LN 15 5 # define INDEX(i) ((i) >> 5) 6 # define OFFSET(i) ((i) % 32) 7 # define GET_BIT(i) ((ptable[INDEX(i)]>>OFFSET(i)) & 阅读全文

posted @ 2012-03-26 00:57 getgoing 阅读(228) 评论(0) 推荐(0) 编辑

poj 1163 The Triangle
摘要:简单的动态规划,重点是怎样才算是好的实现。 1 # include <stdio.h> 2 3 # define MAXN 100 4 5 short int a[MAXN][MAXN]; 6 7 int main() 8 { 9 int n, i, j;10 11 scanf("%d", &n);12 13 for (i = 0; i < n; ++i)14 for (j = 0; j <= i; ++j)15 scanf("%d", &a[i][j]);16 17 for (i = n... 阅读全文

posted @ 2012-03-25 15:15 getgoing 阅读(187) 评论(0) 推荐(0) 编辑

poj 3070 Fibonacci
摘要:先计算幂乘表,根据输入n的二进制位判断是否乘该位相应的幂;以前做过类似的,但是当时递推矩阵不一样,这次拜读了Ozy的大作,自己敲了一遍;运行时间: 0ms,神奇吧! 1 /* poj 3070 */ 2 3 # include <stdio.h> 4 5 # define MAXN 30 6 # define MOD 10000 7 8 short int power[MAXN][4] = {{1,1,1,0}}; 9 short int ans[4];10 11 void mul(short int *a, short int *b, short int *c);12 13 in 阅读全文

posted @ 2012-03-25 14:37 getgoing 阅读(329) 评论(0) 推荐(0) 编辑

poj 1061 青蛙的约会
摘要:求线性同余方程的最小非负解;gcd(a,n)=d;若d|b则有d个解,分布在[0,n-1]上,周期为n/d,所以取[0,n/d-1]上的即可(取模)。/* 谁让你试int型,WA活该 */ 1 # include <stdio.h> 2 3 long long int d, xx, yy; 4 5 long long int extended_euclid(long long int a, long long int b) 6 { 7 long long int t; 8 if (!b) d = a, xx = 1, yy = 0; 9 else 10 {1... 阅读全文

posted @ 2012-03-23 18:11 getgoing 阅读(241) 评论(0) 推荐(0) 编辑

csu 1102 Palindrome
摘要:(当然,思路是大牛的)找出反转串与原串的最长公共子列(不连续),然后总长度减去这个LCS的长度即可;dp,空间优化是显然可以的,但是……先AC了再说。。;3WA:题目要求大小写是distinct,没有仔细读题就想当然地把大写转为小写了; 求c[i][j]时,比较的是x[i-1]与y[j-1]; 将c定义为字符型数组,显然通不过,字符型最大才127,就改为int吧。。1MLE:接上,int型的c会严重MLE的,题目要求最大长度为5000,short足够用,再改为short。/* LCS 问题 */# include <stdio.h># define MAX(a,b) ((a)> 阅读全文

posted @ 2012-03-22 23:46 getgoing 阅读(191) 评论(0) 推荐(0) 编辑

美文一篇。。。
摘要:http://roba.rushcj.com/?p=548 阅读全文

posted @ 2012-03-21 22:51 getgoing 阅读(120) 评论(0) 推荐(0) 编辑

TLE:csu 1016 最小公倍数
摘要:求删改方案总共多少种,预料之中的超时,因为所有提交的22次中有12次超时。 1 # include <stdio.h> 2 3 # define MAXN 205 4 5 int gcd(int a, int b); 6 7 int m[MAXN]; 8 int f[MAXN][MAXN]; 9 10 int main()11 {12 int i, j, T, n, lcm, tmp, cnt;13 14 scanf("%d", &T);15 while (T--)16 {17 cnt = 0;18 ... 阅读全文

posted @ 2012-03-21 01:25 getgoing 阅读(501) 评论(2) 推荐(1) 编辑

HDOJ 1108 最小公倍数
摘要:题目来源:POJ。。。# include <stdio.h>int gcd(int x, int y);int main(){ int x, y; while (~scanf("%d%d", &x, &y)) printf("%d\n", x/gcd(x,y)*y); return 0;}int gcd(int x, int y){ while (x != y) if (x > y) x = x - y; else y = y - x;}下面这个是不对的,原因是Runtim... 阅读全文

posted @ 2012-03-21 01:22 getgoing 阅读(276) 评论(0) 推荐(0) 编辑

csu 1010 Water Drinking
摘要:并查集变种;# include <stdio.h># include <memory.h># define MAXN 100005int pre[MAXN];int dis[MAXN];int main(){ int n, x, y, i, mind, ans; while (~scanf("%d", &n)) { memset(dis, 0, sizeof(dis)); for (i = 0; i < MAXN; ++i) pre[i] = i; while (n--) { ... 阅读全文

posted @ 2012-03-20 23:04 getgoing 阅读(294) 评论(0) 推荐(0) 编辑

csu 1004 Xi and Bo
摘要:并查集;/* 下面的问题已经找到原因:dev c++使用c++编译器,变量end的命名可能出现冲突,使用gcc编译不会出现报错*/遇到了一个诡异的问题:全局变量放在main外报错。# include <stdio.h># define MAXN 105int father[MAXN];int main(){ int T, sta, end, n, m, x, y; int i; scanf("%d", &T); while (T--) { scanf("%d%d%d", &sta,&end,&n); f... 阅读全文

posted @ 2012-03-20 17:45 getgoing 阅读(586) 评论(3) 推荐(0) 编辑

csu 1009 抛硬币
摘要:稍微演算一下,就会发现只要把每一列大小次序依次选择元素,然后相乘相加,结果一定是最大的;比如a > b, c > da cb dac+bd > ad+bc (移向做差),然后扩展到多行多列的情况就行了。有一条警告,不知道怎么排除。29 *\csu1009.c [Warning] passing arg 4 of `qsort' from incompatible pointer type 1 # include <stdio.h> 2 # include <stdlib.h> 3 4 # define LMAX 105 5 # define C 阅读全文

posted @ 2012-03-20 17:43 getgoing 阅读(490) 评论(2) 推荐(1) 编辑

[转载]register修饰符
摘要:以下内容转载自百度百科:Register register修饰符暗示编译程序相应的变量将被频繁地使用,如果可能的话,应将其保存在CPU的寄存器中,以加快其存储速度。例如下面的内存块拷贝代码, 但是使用register修饰符有几点限制。 首先,register变量必须是能被CPU所接受的类型。这通常意味着register变量必须是一个单个的值,并且长度应该小于或者等于整型的长度。不过,有些机器的寄存器也能存放浮点数。 其次,因为register变量可能不存放在内存中,所以不能用“&”来获取register变量的地址。 由于寄存器的数量有限,而且某些寄存器只能接受特定类... 阅读全文

posted @ 2012-03-20 00:30 getgoing 阅读(293) 评论(0) 推荐(0) 编辑

csu 1008 Horcrux
摘要:换成栈就解决了超时,时间上还可以优化。 1 # include <stdio.h> 2 # include <memory.h> 3 4 # define MAXN 100005 5 6 unsigned short s[MAXN]; 7 int top; 8 9 int main()10 {11 int n, x, i, f, t, tot, top;12 13 while (~scanf("%d", &n))14 {15 f = top = tot = 0;16 memset(s, 0, sizeof(... 阅读全文

posted @ 2012-03-18 23:39 getgoing 阅读(486) 评论(0) 推荐(0) 编辑

HDOJ 2473 并查集
摘要:就知道会超时,时限 8ms,1 ≤ N ≤ 105 , 1 ≤ M ≤ 106,循环查找当然超时;有点新意的并查集。 1 # include <stdio.h> 2 3 # define MAXN 100005 4 5 int father[MAXN]; 6 7 int main() 8 { 9 int N, M, i, cnt, ch, x, y, f, tmp, p, clo = 0;;10 11 while (1)12 {13 scanf("%d%d", &N, &M);14 getchar();1... 阅读全文

posted @ 2012-03-18 17:41 getgoing 阅读(248) 评论(0) 推荐(0) 编辑

HDOJ1213 并查集
摘要:格式要求是:DO NOT output any blanks.千万不要理解成:DO NOT output any blanks or blank lines.我理解成:不要输出多余的行。# include <stdio.h># define MAXN 1002int father[MAXN];int main(){ int N, M, x, y, T, i, cnt; scanf("%d", &T); while (T--) { cnt = 0; scanf("%d%d", &N, &M); ... 阅读全文

posted @ 2012-03-18 17:11 getgoing 阅读(337) 评论(2) 推荐(1) 编辑

HDOJ1232 并查集
摘要:1 /* 呀 博客园支持C代码了 */ 2 # include <stdio.h> 3 4 # define MAXN 1002 5 6 int father[MAXN]; 7 int N, M, i, x, y, cnt; 8 9 int main()10 { 11 while (1)12 {13 scanf("%d%d", &N, &M);14 if (N == 0) break;15 cnt = 0;16 for (i = 1; i <= N; ++i)1... 阅读全文

posted @ 2012-03-18 16:55 getgoing 阅读(206) 评论(0) 推荐(0) 编辑

Product:java高精度乘法
摘要:还是 java 简单! 1 import java.io.*; 2 import java.math.*; 3 import java.util.*; 4 public class Main 5 { 6 public static void main(String[] args) 7 { 8 BigDecimal a,b; 9 Scanner cin=new Scanner(System.in);10 while(cin.hasNextBigDecimal())11 {12 a=cin.ne... 阅读全文

posted @ 2012-03-17 12:48 getgoing 阅读(1104) 评论(0) 推荐(0) 编辑

WA: Product
摘要:和前面的高精度加法一样,实在找不到原因,之前的大数从来没这样的。。/* UVa 10106 - Product */# include <stdio.h># include <string.h># define MAXN 252char a[MAXN], b[MAXN];char ss[2 * MAXN];void pro(char *s);int main(){ int lena, lenb, i, j, tmp; while (~scanf("%s", a)) { scanf("%s", b); lena = str... 阅读全文

posted @ 2012-03-17 10:47 getgoing 阅读(213) 评论(0) 推荐(0) 编辑

华丽的大数类,华丽的AC:Integer Inquiry
摘要:参考《算法竞赛入门经典》(刘汝佳)中大数类的实现,终于 AC 了;使用大数类写着感觉很轻松,AC 得也很轻松。。不过还没找到 C 代码 WA 的原因。 1 /* UVa 424 - Integer Inquiry */ 2 # include <iostream> 3 # include <string> 4 # include <cstring> 5 # include <algorithm> 6 7 using namespace std; 8 9 const int maxn = 105;10 11 struct bign{12 int l 阅读全文

posted @ 2012-03-17 00:09 getgoing 阅读(238) 评论(1) 推荐(0) 编辑

Integer Inquiry
摘要:高精度加法,不知错在哪里。、 1 # include <stdio.h> 2 # include <string.h> 3 4 # define MAXN 110 5 6 char s[MAXN], sum[MAXN]; 7 8 int main() 9 {10 int i, len, c, tmp;11 char ch;12 13 while (scanf("%s", s) == 1)14 {15 len = strlen(s);16 17 if (len == 1 && s[0] == ... 阅读全文

posted @ 2012-03-16 23:40 getgoing 阅读(325) 评论(0) 推荐(0) 编辑

Artificial Intelligence
摘要:读取题目要求格式的字符串是个问题。 1 /* UVa 537 - Artificial Intelligence? */ 2 # include <stdio.h> 3 # include <ctype.h> 4 # include <stdlib.h> 5 6 typedef struct { 7 double val; 8 int vis; 9 }phy;10 11 phy U, I, P, x;12 int T;13 char tmp[50];14 15 void getval(void);16 17 int main()18 {19 int i... 阅读全文

posted @ 2012-03-16 13:23 getgoing 阅读(273) 评论(0) 推荐(0) 编辑

csu 1148 词典
摘要:快排、二分查找,对于我来说是道好题,对大牛来说,瞄一眼就跳过了。。。 1 # include <stdio.h> 2 # include <string.h> 3 # include <stdlib.h> 4 5 # define MAXN 100005 6 7 typedef struct { 8 char s[12]; 9 char ss[12];10 } word;11 12 word dic[MAXN];13 word key;14 15 int cmp(const void *a, const void *b)16 { 17 return... 阅读全文

posted @ 2012-03-16 00:23 getgoing 阅读(256) 评论(0) 推荐(0) 编辑

csu 1019 Simple Line Editor
摘要:找到之前的错误了: else if (ch=='#' && i>=0) --i;初始条件是 i = -1; 这样i = - 1;时,如果输入“#”,应该被忽略,但这句话其实没有做到。应该这样写: else if (t[i]=='#') j = (j>=0 ? j-1:-1);由于题目中没有提到一行文本到底有多少字符,干脆定义最大为 100 吧,什么?竟然通过了!# include <stdio.h># include <string.h># define MAXN 100char t[MAXN], s[MAXN 阅读全文

posted @ 2012-03-15 21:31 getgoing 阅读(360) 评论(0) 推荐(0) 编辑

Automatic Poetry
摘要:考虑到每段串中字母顺序不能反序,没有使用栈;为了方便,也没有在输入的同时输出;感觉虽然麻烦点,但是如果加上思考的时间,是比较省时的做法。 1 /* UVa 10361 - Automatic Poetry */ 2 # include <stdio.h> 3 4 # define N 105 5 6 char L1[N], L2[N]; 7 int m[2], n[2]; 8 9 int main()10 {11 int T, i;12 13 scanf("%d", &T);14 getchar();15 while (T--)16 {17... 阅读全文

posted @ 2012-03-15 13:33 getgoing 阅读(396) 评论(0) 推荐(0) 编辑

Where's Waldorf?
摘要:使用函数,结构会更清晰一些;fgets() 使用错,改为 scanf();边界判断错误(每一列下标都是从 0 开始,以 n-1 结尾),判断是否超出边界时,错写为 len*d[s][0];d[8][2] 的第5项错写成 {0,1}了。不管咋样,反正 AC 了。。 1 /* UVa 10010 - Where's Waldorf? */ 2 # include <stdio.h> 3 # include <string.h> 4 # include <ctype.h> 5 6 # define IN(up, x, low) (((x)<=(up) 阅读全文

posted @ 2012-03-15 11:55 getgoing 阅读(334) 评论(0) 推荐(0) 编辑

Palindromes
摘要:要注意:单个字符是没有对应反序串的。几个测试: 1 NOTAPALINDROME -- is not a palindrome. 2 3 ISAPALINILAPASI -- is a regular palindrome. 4 5 2A3MEAS -- is a mirrored string. 6 7 ATOYOTA -- is a mirrored palindrome. 8 9 B -- is a regular palindrome.10 11 E -- is a regular palindrome.12 13 8 -- is a mirrored palindrome.我... 阅读全文

posted @ 2012-03-15 10:07 getgoing 阅读(200) 评论(0) 推荐(0) 编辑

内置类型开方
摘要:迭代公式:x[n+1] = x[n] + {a/x^(k-1)-x[n]}/k发现BUG,谢谢联系!函数sqrtn() 中,参数x是被开方数, k是开方次数,x0是迭代初值,n是迭代次数;pow() 使用的应该不是迭代法。 1 # include <stdio.h> 2 # include <math.h> 3 4 long double sqrtn(double x, int k, double x0, int n); 5 6 int main() 7 { 8 int k, n; 9 double A, x0;10 11 while (~scanf("%l. 阅读全文

posted @ 2012-03-13 21:31 getgoing 阅读(195) 评论(0) 推荐(0) 编辑

[noj 1002]囧:求最大值最小值 WA 8次
摘要:没有思考,直接按成法来,第一组数据都没通过。最关键的在于 if else 的结构没有考虑到如果输入的数都相等怎么办,另外如果输入的数是按增序排列也会得到错误的结果: 1 # include <stdio.h> 2 3 int main() 4 { 5 int x, N, min, max; 6 7 while (~scanf("%d", &N)) 8 { 9 max = 0;10 min = 100;11 while (N--)12 {13 sca... 阅读全文

posted @ 2012-03-13 14:37 getgoing 阅读(277) 评论(0) 推荐(0) 编辑

csu 1049 第一次串讲——输入输出练习题
摘要:C++好处理一些,C语言使用 log10(double x) 计算 x 整数部分的位数(结果要 +1): 1 # include <stdio.h> 2 # include <math.h> 3 4 int main() 5 { 6 int n, j, tmp; 7 double x, tot; 8 9 freopen("in.txt", "r", stdin);10 freopen("out.txt", "w", stdout);11 12 while (~scanf("%d%d 阅读全文

posted @ 2012-03-13 11:48 getgoing 阅读(244) 评论(0) 推荐(0) 编辑

HDOJ 1284 钱币兑换问题
摘要:很久很久以前 WA 过,今天找到了原因:位运算的优先级比 + 低!,重新改了一下结构(for 变 while),稍有提速(31ms—>15ms)。 1 # include <stdio.h> 2 3 int main() 4 { 5 int N, tot; 6 7 while (~scanf("%d", &N)) 8 { 9 tot = 0; 10 while (N >= 0)11 {12 tot += (N>>1)+1;13 N -=... 阅读全文

posted @ 2012-03-13 11:18 getgoing 阅读(226) 评论(0) 推荐(0) 编辑

csu 1011 Counting Pixels
摘要:没想象中的复杂;先将园分为四块以减少计算量(还可以再细分为八块,略显复杂);判断圆是如何穿过一个像素的:对于右上方的点,只有三种情况:从右边穿过,从右下角的顶点穿过,从下边穿过;先给出一个初始像素(被穿过),然后根据穿过的情况向下一个被穿过的像素扩展,并根据情况计数;像素的表示:右上方定点的坐标:Language: CResult: AcceptedTime:236 msMemory:740 kb最快的只有128ms,求指点。 1 # include <stdio.h> 2 3 int x, y, r; 4 5 int state(int x, int y); // 1: ri.. 阅读全文

posted @ 2012-03-13 00:00 getgoing 阅读(455) 评论(0) 推荐(0) 编辑

最长回文子串
摘要:看了刘汝佳的白书,才发觉最长回文子串不是那么难,关键是将思路转化为代码;之前看到一种通过在两个元素之间加入分隔符的方法,将奇偶情况统一起来,其实并没有减少运算量,感觉没直接的好,虽然代码长点,但是思路清晰;另外学习了如何使用 fgets() 以及为什么不推荐使用 gets() 。# include <stdio.h># include <string.h> # include <ctype.h># define MAXN 1000char ch[MAXN], s[MAXN];int main(){ int len, i, j, max, sta, end; 阅读全文

posted @ 2012-03-12 21:09 getgoing 阅读(283) 评论(0) 推荐(0) 编辑

WA : csu1019 simple line editor
摘要:查了很多遍,不知道哪里错了,数组还不够大? 1 # include <stdio.h> 2 # include <ctype.h> 3 4 # define MAXN 10005 5 6 char a[MAXN]; 7 int i = -1, j, T; 8 char ch; 9 10 int main()11 {12 freopen("in.txt", "r", stdin);13 freopen("out.txt", "w", stdout);14 15 scanf("%d&q 阅读全文

posted @ 2012-03-12 15:23 getgoing 阅读(199) 评论(0) 推荐(0) 编辑

csu 1214 三个数字
摘要:1、2、3三个数字组成的序列,要求把所有的2放在前面,所有的3放在后面,输出结果。下面的代码提交了好几次(修改不大)都是WA,错误的原因是输入结束(EOF)前不一定有换行,这样最后一组测试数据就没法输出 1 和 3。 1 # include <stdio.h> 2 3 char ch; 4 int cnt1, cnt3; 5 6 int main() 7 { 8 while ((ch=getchar()) != EOF) 9 {10 if (ch == '2') putchar(ch);11 else if (ch == '1'... 阅读全文

posted @ 2012-03-12 13:29 getgoing 阅读(249) 评论(0) 推荐(0) 编辑

HDOJ 1040 qsort
摘要:注意 compare 函数的写法,行末不留空格。/* qsort */# include <stdio.h># include <stdlib.h>int compare(const void * a, const void * b){ return (*(int *)a) - (*(int *)b); }int main(){ int T, N, i; int a[1001]; scanf("%d", &T); while (T--) { scanf("%d", &N); for (i = 0; i < N 阅读全文

posted @ 2012-03-12 10:48 getgoing 阅读(184) 评论(0) 推荐(0) 编辑

HDOJ 1042 N!
摘要:万位制,先计算10000!的位数,然后考虑分配多大的数组!Accepted 1984MS 240K 1 # include <stdio.h> 2 # include <memory.h> 3 4 # define N 8200 5 # define BASE 100000 6 7 int main() 8 { 9 int n, i, j;10 long long c, tmp;11 int a[N];12 13 freopen("in.txt", "r", stdin);14 freopen("out.txt" 阅读全文

posted @ 2012-03-12 01:14 getgoing 阅读(311) 评论(0) 推荐(0) 编辑

斯特林公式
摘要:题目来源:HDOJ1018(求阶乘的位数)斯特林公式简介(维基百科):斯特灵公式是一条用来取n阶乘近似值的数学公式。一般来说,当n很大的时候,n阶乘的计算量十分大,所以斯特灵公式十分好用,而且,即使在n很小的时候,斯特灵公式的取值已经十分准确。公式为:这就是说,对于足够大的整数n,这两个数互为近似值。当n增加时,(ln n!)与o (n ln n − n)之比趋于1。几乎超时: 1 # include <stdio.h> 2 # include <math.h> 3 4 int main() 5 { 6 int T, x, i; 7 double sum; 8 9... 阅读全文

posted @ 2012-03-11 23:43 getgoing 阅读(1292) 评论(0) 推荐(0) 编辑

优化:一个简单小题
摘要:题目来源于HDOJ1013把一个正整数的各位数字之和加起来,如果得到一个个位数,就打印它,否则重复对和的各位数字相加,直到得到个位数,打印它;若输入为 0 则结束。这个题要处理比较大的数(字符串表示)放弃了在 while((ch=getchar() != EOF)) 下分情况讨论后(详情自己试),容易想到下面的方法: 1 # include <stdio.h> 2 3 int main() 4 { 5 char ch; 6 int ans; 7 8 while (ch=getchar() != '0') 9 {10 ans = (ch... 阅读全文

posted @ 2012-03-11 23:01 getgoing 阅读(214) 评论(0) 推荐(0) 编辑

[转载]zoj 分类
摘要:转载自lonelycatcher的博客ZOJ POJ 题目分类初学者题:1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 1334 1337 1338 1350 1365 1382 1383 1394 1402 1405 1414 1494 1514 1622 1715 1730 1755 1760 1763 1796 1813 1879 1889 1904 1915 1949 2001 2022 2099 2104 2108 2172 2176 2201 2208 2321 阅读全文

posted @ 2012-03-10 17:39 getgoing 阅读(877) 评论(0) 推荐(0) 编辑

csu 1242 碱基配对
摘要:我的思路是先求出类似dp求LCS的数组c,然后计算c的对角线上元素和的最大值,由于和枚举的计算量相同,并且多了给c赋值的过程,耗时要多些,另外也多分配了c这样一个比较大的空间同样的代码分别用C和C++提交耗时分别为720ms、380ms,内存占用2700kb(C少一些,相差500kb),不解。。所有AC中最快的只有64ms,内存占用740kb。。对角线边界的判断还是需要点思考的。。/* csu 1242 */# include <stdio.h># include <string.h># define MAXN 1002char a[MAXN], b[MAXN];sho 阅读全文

posted @ 2012-03-09 13:36 getgoing 阅读(256) 评论(0) 推荐(0) 编辑

csu 1242 碱基配对——一个错误的解答
摘要:早上刚看了如何用 dp 解决LCS,想到了这道题,无奈这道题和LCS不同,还要求对应位置匹配。还是遇到了一个小问题:序列是从1开始的,但是数组的下标是从0开始的,于是想到了gets(a+1)这样的方法,这时要对a[0]赋以非 0 初值/* csu 1242 错误:非LCS问题*/# include <stdio.h># include <string.h># define MAXN 1002char a[MAXN], b[MAXN];short c[MAXN][MAXN];short int solve(void){ short ans, m, n, i, j, tmp 阅读全文

posted @ 2012-03-09 12:54 getgoing 阅读(329) 评论(0) 推荐(0) 编辑

csu 1104 Fibonacci Numbers
摘要:超过8位只输出高低四位,中间用...隔开:Fibonacci numbers get large pretty quickly, so whenever the answer has more than 8 digits, output only the first and last 4 digits of the answer, separating the two parts with an ellipsis (“...”). 高四位的输出:利用通项公式,先求出取对数的小数部分,然后取前四位即可。第四位:直接递推会严重超时!!所以只能用矩阵幂的方法(其实题目提示了用线性代数知识:Use y 阅读全文

posted @ 2012-03-09 00:31 getgoing 阅读(268) 评论(0) 推荐(0) 编辑

各位数字之和——一个神奇的结论
摘要:今天下午翻了翻XX大学的ACM模版,作为新手,当然挑能看懂的来看,结果就看到了这样一个简单的问题:题目是这样的,输入一个正整数,将它的各位数字加起来,如果得到的不是一位数,将这个和的各位数字再加起来,如此循环,直到得到一个一位数,最后输出这个个位数。最简单的方法是使用递归模拟上述求和过程,直到和小于10:# include <stdio.h>int dig_sum(int x){ int sum = x; if (x < 10) return sum; else { sum = 0; while (x > 0) { ... 阅读全文

posted @ 2012-03-06 17:38 getgoing 阅读(1084) 评论(0) 推荐(0) 编辑

csu 1087 斐波那契数列
摘要:晕,题目上描述是 N<=20,但是建个20大小的表,提交是WA,用递归提价一下AC了。猜测是为了练习,不让大家偷懒。。猜测测试数据最大的是21。。为了偷懒,建了个25大小的表(注释部分),一提交果然AC,彻底晕倒了,要卡的话应该给个稍大一点的啊。。当然如果特别大,时间限制也要改大一点,谅解了。。。/* 1087: 斐波那契数列 *///用递归法计算第N个斐波那契数,并输出所用到的递归次数//斐波那契数列排列如下:1,1,2,3,5,8,……# include <stdio.h>int fun(int n, int *cnt);//const int f[] = {0,1,1, 阅读全文

posted @ 2012-03-06 10:15 getgoing 阅读(399) 评论(0) 推荐(0) 编辑

原创题目
摘要:1. 用二分法找一个数的整数部分,这个数在区间[1, 9]内。2. 如何通过位运算对一个数(10的倍数)除以10,这种方法真的比直接除以10快吗?3. 一串不超过 k 的序列,由四个字母组合而成:u l r d ,代表着一个3×3棋盘上一个棋子的移动序列,u l r d 分别表示向上、下、左、右四个方向移动,现在限定棋子只能在棋盘内移动,问有多少个这样的不同序列? 提示:不能连续向着一个方向移动超过 3 次(中间可以有非相反方向的移动),即对其中任一连续子序列所有的 u(l) 方向移动次数 p 减去所有的 d(r) 方向移动次数 q 满足 |p - q| <= 2,例如 uuu 阅读全文

posted @ 2012-03-06 09:17 getgoing 阅读(185) 评论(0) 推荐(0) 编辑

[转载]scanf和gets的几个区别
摘要:以下内容转载自:scanf 和 gets 读取字符串深入了解scanf()/getchar()和gets()等函数scanf与gets函数读取字符串的区别今天看到一段话,大致是说gets比scanf()快,有点吃惊,搜了一下,scanf()和gets的区别大致有着几条:1.scanf() 会忽略行开头的所有空格,并以空格、换行符结束输入; 使用getchar()读取scanf语句执行后,缓冲区留下的换行符, gets读入以任何字符开始的字符串,以换行符结束,但之后会丢弃换行符并以'\0'代替;2. 在数据大量的情况下,用gets读取快于scanf()10倍以上(注:来自pudn 阅读全文

posted @ 2012-03-06 09:13 getgoing 阅读(12704) 评论(0) 推荐(2) 编辑

csu 1054 约瑟夫环
摘要:约瑟夫环的一个特殊情形:m = 2;"我们把k设为2,然后给n个人,如果最后留下来的人是编号为n的,那么n就叫fix number.现在的问题是,如果给出一个数,要求输出是不是fix number.如果是fix number的话,就输出"Oh,It's a fix number",如果不是的话就输出"Sorry,It isn't a fix number""开始我怀疑形如 2^n-1 是 fix number(充要),只是通过拿 7 和 3 试验感觉到的,后来看了维基百科中的一段话(对k=2情况的讨论)后确信了。下面的 阅读全文

posted @ 2012-03-05 20:21 getgoing 阅读(344) 评论(0) 推荐(0) 编辑

约瑟夫环问题(基本)
摘要:n个人围成圈,依次编号为1,2,..,n,现在从1号开始依次报数,当报到m时,报m的人退出,下一个人重新从1报起,循环下去,问最后剩下那个人的编号是多少?递归法参见百度百科:Josephus(约瑟夫)问题的数学方法递推式: 将这些人的编号用对总人数取模所得余数代替(即原编号减一),其中 Fn 表示n个人的环,最后剩下的那个人的编号。由这个递推式不难写出相应代码:/* 约瑟夫环问题 */# include <stdio.h>int main(){ int m, n, i, s; while (~scanf("%d%d", &m, &n)) { s 阅读全文

posted @ 2012-03-05 19:11 getgoing 阅读(3858) 评论(0) 推荐(0) 编辑

TLE: csu 1211 大整数开平方练习
摘要:模拟手工开方,必须使用大数,否则只用sqrt()即可。代码有点乱,结果应该是正确的,但是。。TLE了。。。接下来应该考虑把代码变短些,除去不必要的运算。要还是不行,怎么办啊。。。。欢迎各位大牛测试,谢谢指点!/* 大整数开方 */# include <stdio.h># include <string.h># include <stdlib.h># include <math.h># include <time.h># define MAXN 1001void bigN_sqrt(char *s);int bigN_cmp(char 阅读全文

posted @ 2012-03-03 17:19 getgoing 阅读(501) 评论(0) 推荐(0) 编辑

[转载]位运算小技巧总结
摘要:转载自Hurricane的百度空间:http://220.181.111.15/%B7%E7%C0%D7%D1%B8%C1%D2/blog/item/bd38bb9e24790f116e068cd1.html(原文格式有点乱,重新排版了一下,如果大家发现了更好玩的,不妨一起分享一下)(1) 整型数循环移位a = (a<<k) | (a>>sizeof(int)-k) // int 型变量循环左移k次a = (a>>k) | (a<<sizeof(int)-k) // int 型变量循环右移k次 (2) 整数的平均值 :对于两个整数x,y,如果用 阅读全文

posted @ 2012-03-01 12:16 getgoing 阅读(206) 评论(0) 推荐(0) 编辑

计算素数
摘要:高效的方法(1000以内)int isPrime(int n){ int i; if (n % 2 == 0) return (n == 2); if (n % 3 == 0) return (n == 3); if (n % 5 == 0) return (n == 5); for (i = 7; i*i <= n; i += 2) if (n % i == 0) return 0; return 1;}测试:(可以先生成1~10000,然后逐个判断,计时)/* isPrime */# include <stdio.h>int isPrime... 阅读全文

posted @ 2012-03-01 00:34 getgoing 阅读(243) 评论(0) 推荐(0) 编辑

导航

点击右上角即可分享
微信分享提示