桑海

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

随笔分类 -  算法竞赛入门经典

算法学习
摘要:1 #include 2 #include 3 #include 4 using namespace std; 5 6 void great(string& word) 7 { 8 for(int i = 0; i != word.size(); ++i) 9 if(word[i] >= 'a' && word[i] > n;25 bool flag = true;26 string word, str, res;27 stringstream ss;28 cin.ignore();29 cout > word)35 ... 阅读全文
posted @ 2013-07-03 21:03 桑海 阅读(242) 评论(0) 推荐(0)

摘要:WA了三次;仅仅是因为输出输入数据时,先去了了前导0.罪过啊!! 1 #include<iostream> 2 #include<string> 3 #include<limits> 4 #include<sstream> 5 using namespace std; 6 7 inline string reverse(const string a) 8 { 9 string t(a.rbegin(), a.rend()); 10 return t; 11 } 12 string operator + (const string& a, 阅读全文
posted @ 2013-05-21 12:17 桑海 阅读(289) 评论(0) 推荐(0)

摘要:Bign 1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 using namespace std; 6 const int maxn = 1000 + 5; 7 8 class bign 9 { 10 public: 11 int len, s[maxn]; 12 bign() 13 { 14 len = 0; 15 memset(s, 0, sizeof(s)); 16 ... 阅读全文
posted @ 2013-04-04 17:05 桑海 阅读(3581) 评论(0) 推荐(0)

摘要:样例输入:4 23 410 12 28 12816 12345样例输出:127512325536358My Code 1 #include<iostream> 2 using namespace std; 3 const int maxn = 20; 4 5 int node[1<<maxn]; 6 int main() 7 { 8 int d, n; 9 while(cin >> d >> n)10 {11 int s = 1<<d;12 memset(node, 0, s);//初始化结点全部关闭13 int i;... 阅读全文
posted @ 2013-01-30 23:00 桑海 阅读(948) 评论(0) 推荐(2)

摘要:编写一个 函数solve,给定浮点数a, b, c, d, e, f,求解方程组af + by = c, dx + ey = f;任务1:使用assert宏,让解不唯一时异常退出。任务1 1 #include<iostream> 2 #include<cassert> 3 using namespace std; 4 5 double x, y; 6 7 void solve(double a, double b, double c, double d, double e, double f) 8 { 9 assert(b*d!=a*e);10 y = (c*d-a*f 阅读全文
posted @ 2013-01-27 22:48 桑海 阅读(485) 评论(0) 推荐(1)

摘要:输入不超过1000的正整数n,输出n!的精度结果。样例输入:30样例输出:265252859812191058636308480000000#include<iostream>#include<string.h>using namespace std;const int maxn = 3000;int f[maxn];void out(int f[]){ int i, j; for(j = maxn - 1; j >= 0; --j) if(f[j]) break; for(i = j; i >= 0; --i) cout << f[i]; co 阅读全文
posted @ 2012-12-27 20:08 桑海 阅读(377) 评论(0) 推荐(0)

摘要:如果n和n+2都是素数,则称他们是孪生素数。输入m,输出连个数均不超过m的最大孪生素数。5<=m<=10000.例如m=20时的答案是17、19,m=1000时的答案是881、883.Code 1 #include<iostream> 2 #include<cmath> 3 #include<assert.h> 4 using namespace std; 5 /* 6 bool is_primer(int x) 7 { 8 for(int i = 2; i*i <= x; ++i) //中间值可能会溢出 9 if(x%i == 0) re 阅读全文
posted @ 2012-12-25 14:01 桑海 阅读(863) 评论(0) 推荐(0)

摘要:期待给予指正与建议,愿共勉 习题3-1: 分数统计(stat) 输入一些学生的分数,那个分数出现的次数最多?如果有多个并列,从大到小输出。 任务一:分数均为不超过100的非负整数。 Code#include<iostream>#include<cstring>using namespace std;int main(){ int a[101], n; memset(a, 0, si... 阅读全文
posted @ 2012-12-22 11:47 桑海 阅读(1143) 评论(2) 推荐(0)

摘要:3-1 开灯问题 有n盏灯,编号为1~n。第一个人把所有打开,第2个人按下所有编号为2的倍数的开关(这些灯将被关掉),第3个人按下所有编号为3的倍数的开关 (其中关掉的灯被打开你,开着的灯将被关闭),一次类推。一共有k个人,问最后又哪些灯开着?输入 :n和k,输出开着的灯编号。k <= n <= 1000. 样例输入:7 3 样例输出:1 5 6 7 Code#include<stdio.h>#... 阅读全文
posted @ 2012-12-19 22:43 桑海 阅读(544) 评论(0) 推荐(0)

摘要:2-1 位数(digit)输入一个不超过10^9的正整数,输出它的位数。例如12735的位数是5.请不要任何数学函数,只用四则运算和循环语句实现。Code//#define LOCAL#include<stdio.h>int main(){#ifdef LOCAL freopen("data.in", "r", stdin); freopen("data.out", "w", stdout);#endif int a, cnt; FILE *fin, *fout; fin = fopen("d 阅读全文
posted @ 2012-12-18 20:03 桑海 阅读(1037) 评论(0) 推荐(0)

摘要:问题描述: 圆桌旁坐着n个人,没人有一定数量的金币,金币总数能被n整除。每个人可以给他左右相邻的人一些金币,最终每个人的金币数相等。你的任务是求出被转手的金币数量的最小值。比如,n = 4,且4个人的金币数量分别是1,2,5,4时,只需转移4枚金币(第3个人给第2个人两枚金币,第2个人和第4个人分别给第1个人1枚金币)即可实现每人手中的金币数目相等。 输入: 输入包含多组数据。魅族数据第一行为整数... 阅读全文
posted @ 2012-12-16 19:21 桑海 阅读(355) 评论(0) 推荐(0)

摘要:问题描述: 有n个部下,第i个需要花Bi时间交代任务,他需要花费Ji分钟完成.请选择交代任务的顺序,是所有任务完成是时间最短(不能同时给两个部下交代任务,但他们可以同时执行各自的任务) 基本思路:对各个部下执行任务时间按照非升序排列,如果上次执行任务与此次交代任务的剩余时间大于本次执行时间,就更新本次任务的执行时间. My Code: #include<iostream>#include<vect... 阅读全文
posted @ 2012-12-14 13:45 桑海 阅读(299) 评论(0) 推荐(0)

摘要:A particle has initial velocity and constant acceleration. If its velocity after certain time is v then what will its displacement be in twice of that time? Input The input will contain two integers i... 阅读全文
posted @ 2012-11-15 22:38 桑海 阅读(119) 评论(0) 推荐(0)

摘要:Write a complete program that will correctly decode a set of characters into a valid message. Your program should read a given file of a simple coded set of characters and print the exact message that... 阅读全文
posted @ 2012-11-15 22:36 桑海 阅读(200) 评论(0) 推荐(0)

摘要:he just calculates one thing, the difference between his soldier number and the opponent's soldier number. From this difference he decides whether to fight or not. Hashmat's soldier number is never greater than his opponent.InputThe input contains two integer numbers in every line. These two 阅读全文
posted @ 2012-11-15 22:27 桑海 阅读(172) 评论(0) 推荐(0)

摘要:明明题目上写着不要输出多余的空白行,可是最后还是需要一个空白行的,白白让我郁闷了多次WA: #include<iostream>using namespace std;int main(){ int i_case; cin >> i_case; while(i_case--) { int farmer; cin >> farmer; ... 阅读全文
posted @ 2012-11-15 22:21 桑海 阅读(240) 评论(0) 推荐(0)

摘要:这道题本来是想自己写一个判断是否是英文字符的函数的,错误了,现在还不是很明白,如下: #include<iostream>#include<string>#include<cctype>using namespace std;int main(){ string line; while(getline(cin, line)) { int cnt = 0, flag ... 阅读全文
posted @ 2012-11-15 21:50 桑海 阅读(213) 评论(0) 推荐(0)

摘要:刚开始想直接求出n的阶乘,在从其结果里面查找相应的素数,可是发现数字太大,越界了,经改正如下: My Code: #include<iostream>using namespace std;int prime[100];void prime_array() //生成素数表{ for(int i = 2, k = 0; i < 100; ++i) { int... 阅读全文
posted @ 2012-11-14 21:02 桑海 阅读(269) 评论(0) 推荐(0)

摘要:题目分析: 本题是由对角线由1起,每次递增以,交替循环。 1 2 6 7 15 16 3 5 8 14 17 4 9 13 18 10 12 19 11 20 21 如上表:红黑交替,每次递增1. 以以每条对角线为整体,前k条对角线一共有:1+2+3+4+5+6+7+8+9+……+k+……(k表示第k条对角线)个数。 现在要确定输入数n的所行与列... 阅读全文
posted @ 2012-11-14 19:58 桑海 阅读(233) 评论(0) 推荐(0)

摘要:1 //--------------------C_Style---------------------- 2 3 #include<stdio.h> 4 #include<string.h> 5 const int maxn = 3000; 6 7 int get_next(int n) 8 { 9 char str[10];10 sprintf(str, "%d", n); //将%d的n转换成字符串存入到str中11 int size = strlen(str);12 for(int ix = 1; ix != size; ++ix) ... 阅读全文
posted @ 2012-11-13 17:00 桑海 阅读(957) 评论(0) 推荐(0)