随笔分类 -  算法

一些简单有趣算法
摘要:问题:Input输入数据有多组。每组数据的第一行有两个正整数n,m(0 #include #include #include #include #include using namespace std; const int MAX = 1003; const int dirx[5] =... 阅读全文
posted @ 2015-05-16 22:53 chaoer 阅读(347) 评论(0) 推荐(0)
摘要:问题:给你一个字符串,然后让你把所有的前缀给找出来,并把它们在字符串中的出现次数相加,输出这个和写出算法。InputThe first line is a single integer T, indicating the number of test cases.For each case, the... 阅读全文
posted @ 2015-05-14 13:34 chaoer 阅读(153) 评论(0) 推荐(0)
摘要:问题:When a number is expressed in decimal, the kth digit represents a multiple of 10k. (Digits are numbered from right to left, where the least signifi... 阅读全文
posted @ 2015-05-13 11:47 chaoer 阅读(352) 评论(0) 推荐(0)
摘要:问题:给定一个数值,判断他是否等于一连串素数之和(这些素数必须是连续的)。输出满足条件的组合的个数。题解:预先求出连续的素数和。然后找到不大于n的最大素数,那么所有的组合(连续不断的素数)只可能在此范围内。一些正整数可以通过和一个或多个连续的素数表示。有多少这样的陈述是一个给定的正整数吗?例如,整数... 阅读全文
posted @ 2015-05-13 11:39 chaoer 阅读(357) 评论(0) 推荐(0)
摘要:问题:给定两个正整数,计算这两个数的最小公倍数并写出算法。Input输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.Output对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。Sample Input10 14Sample Output70回答:#include ... 阅读全文
posted @ 2015-05-12 10:18 chaoer 阅读(249) 评论(0) 推荐(0)
摘要:问题:Consider a three-parameter recursive function w(a, b, c):if a 20 or b > 20 or c > 20, then w(a, b, c) returns:w(20, 20, 20)if a #include cons... 阅读全文
posted @ 2015-05-12 10:02 chaoer 阅读(168) 评论(0) 推荐(0)
摘要:问题:InputThe input consists of one or more sets of strings, followed by a final line containing only the value 0. Each set starts with a line containin... 阅读全文
posted @ 2015-05-12 09:42 chaoer 阅读(318) 评论(0) 推荐(0)
摘要:问题: 有三个正整数a,b,c(0int gcd(int a,int b){ returnb==0?a:gcd(b,a%b);}intmain(){ int n;scanf("%d",&n); inta,b,c,i;while(n--){scanf("%d%d",&a,&b);for(i=2;i<=... 阅读全文
posted @ 2015-05-11 16:17 chaoer 阅读(359) 评论(0) 推荐(0)
摘要:问题:InputThe input consists of n cases, and the first line consists of one positive integer giving n. The next n lines each contain 3 integers, r, e an... 阅读全文
posted @ 2015-05-11 16:10 chaoer 阅读(197) 评论(0) 推荐(0)
摘要:问题:一个 n 阶方阵的元素是1,2,...,n^2,它的每行,每列和2条对角线上元素的和相等,这样的方阵叫魔方。n为奇数时我们有1种构造方法,叫做“右上方” ,例如下面给出n=3,5,7时的魔方.38 1 63 5 74 9 2517 24 1 8 1523 5 7 14 164 6 13 20 ... 阅读全文
posted @ 2015-05-11 15:59 chaoer 阅读(549) 评论(1) 推荐(0)
摘要:问题:输入规格:每个输入文件包含一个测试用例。对于每一种情况下,有一行文字不超过长度1048576个字符的,由回车'\ N'终止。输入中包含的至少一个字母数字字符,即,从集合[0-9 AZ AZ]一个字符。输出规格:对于每一个测试的情况下,打印在一行中的输入文本最常发生的词,后跟一个空格和的时候,它... 阅读全文
posted @ 2015-05-10 23:11 chaoer 阅读(245) 评论(0) 推荐(0)
摘要:问题:Excel可以对一组纪录按任意指定列排序。现请你编写程序实现类似功能算法。Input测试输入包含若干测试用例。每个测试用例的第1行包含两个整数 N ( #include #include #define maxn 100005 using namespace std; struc... 阅读全文
posted @ 2015-05-10 22:05 chaoer 阅读(279) 评论(0) 推荐(0)
摘要:问题:完数的定义:如果一个大于1的正整数的所有因子之和等于它的本身,则称这个数是完数,比如6,28都是完数:6=1+2+3;28=1+2+4+7+14,本题的任务是判断两个正整数之间完数的个数。Input输入数据包含多行,第一行是一个正整数n,表示测试实例的个数,然后就是n个测试实例,每个实例占一行... 阅读全文
posted @ 2015-05-10 21:32 chaoer 阅读(616) 评论(0) 推荐(0)
摘要:问题: 速算24点相信绝大多数人都玩过。就是随机给你四张牌,包括A(1),2,3,4,5,6,7,8,9,10,J(11),Q(12),K(13)。要求只用'+','-','*','/'运算符以及括号改变运算顺序,使得最终运算结果为24(每个数必须且仅能用一次)。游戏很简单,但遇到无解的情况往往让人... 阅读全文
posted @ 2015-05-07 23:21 chaoer 阅读(966) 评论(0) 推荐(0)
摘要:问题:Cipher textA B C D E F G H I J K L M N O P Q R S T U V W X Y ZPlain textV W X Y Z A B C D E F G H I J K L M N O P Q R S T UInputInput to this probl... 阅读全文
posted @ 2015-05-06 09:36 chaoer 阅读(429) 评论(0) 推荐(0)
摘要:问题:样例输入210:37:4900:00:01样例输出1 011001100010100011 001010100101110001 2 000000000000000001 000000000000000001题意:将时间按照题意竖向和横向输出回答:#include #include #in... 阅读全文
posted @ 2015-05-06 08:44 chaoer 阅读(215) 评论(0) 推荐(0)
摘要:问题:读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值并写出算法。回答:#include#include#includeusing namespace std;char Precede(char a,char b);double operate(double a1,cha... 阅读全文
posted @ 2015-05-06 08:20 chaoer 阅读(426) 评论(0) 推荐(0)
摘要:问题:比赛时间又一次开始,看到气球漂浮是多么令人兴奋啊!但是,要告诉你一个秘密,法官最喜欢的时间是猜测最受欢迎的问题。当测试结束后,他们将计数每一种颜色的气球,再找出结果。今年,他们决定把最“可爱”的问题留给你。输入: 输入多组测试用例,每个用例以一个数字N(0#include #includ... 阅读全文
posted @ 2015-05-05 16:36 chaoer 阅读(466) 评论(0) 推荐(0)
摘要:问题:如题回答:#include #include int p(int n) { int ans,r; if(n==1) return 1; ans=0; r=(int)(sqrt(8*n+1)-1)/2; ans+=pow(2,r)-1; ans+=2*p(n-r); retur... 阅读全文
posted @ 2015-05-05 16:17 chaoer 阅读(123) 评论(0) 推荐(0)
摘要:问题:1~n编号的彩票,要买全,等概率条件下平均要买几张要求写出算法。回答:已经买了m张时,买中剩下的概率为1-m/n,则要买的张数为1/(1-m/n)n=2,s=1+1/(1-1/2);n=3,s=1+1/(1-1/3)+1/(1-2/3)s=1+1/(1-1/n)+1/(1-2/n)+1/(1-... 阅读全文
posted @ 2015-05-05 15:51 chaoer 阅读(346) 评论(0) 推荐(0)