Computer Common algorithm------second Zhang Yi (1) enumeration overview
Enumeration method, also known as enumerating and exhaustive methods, makes the concrete manifestation of brute force strategy, also known as brute force method.
Enumeration is a simple and straightforward way to solve a problem.
(2) The basic idea of enumeration is:
逐一列举问题所涉及的所有清醒,并根据问题提出条件的条件检验哪些是问题的解,那些应予以排除。枚举法常用于解决“是否存在”或“有多少种可能”等问题。
(3) enumeration is characterized by
算法设计比较简单,只要一一列举问题所涉及的所以情形即可。
(4) Enumeration mode
实施枚举通常是应用循环结构来实现
There are two kinds:
1. Interval Enumeration
区间枚举:通过枚举循环的上下限控制枚举区间,而在循环体中完成各个运算操作,然后根据所求解的具体条件,应用选择结构实施判别与筛选,求得所要求的解。
Framework description for Interval enumeration design:
框架描述:n = 0;for(k = <区间下限>;k<=<区间上限>;k++){ //根据实际情况控制枚举范围 <运算操作序列>; if(<约束条件>){ //根据约束条件实施筛选 printf(<满足要求的解>); //逐一输出问题的解 n++; //统计解的个数 } }printf(<解的个数>);
**2.递增枚举:**
有些问题没有明确的范围限制,可根据问题的具体情况试探地从某一起点开始增值枚举,对每一个数进行操作与判别,若满足条件即输出结果
The framework description for incrementing the enumeration design:
k = 0;while(1){ k++; //设置循环,枚举变量k递增; <运算操作序列>; if(<约束条件>){ //根据约束条件实施筛选与结束 printf(<满足要求的解>); //输出问题的解 return ; //返回结束 } }
Enumeration implementation steps:
(1)根据问题的具体情况确定枚举量(简单变量或数组)(2)根据问题的具体情况确定枚举范围,设置枚举循环(3)根据问题的具体要求确定筛选条件(4)设计枚举并运行、调试,对运行结果进行分析与讨论。
(5) Statistics and summation
All-Vegetarian Group
··· Primes, also known as prime numbers, are integers that cannot be divisible by integers other than 1. If 2,3,5,7 2 is also an even prime number
··· Composite is also called the plural, an integer divisible by an integer other than 1.
··· Note: 1 is neither prime nor even
Examples: For example, input n=15, the output 3+5+11=19 is a prime number 3 5 11 is called a total of 15-based group
Statistical summation application Trial quotient discriminant Prime #include<stdio.h> #include <math.h>int main () {int i,j,k,i2,j2,k2,n,s,t,w,z,max,p[9000],q [1500]; Long m; printf ("Please enter an integer n:"); scanf ("%d", &n); for (int i= 0;i<= 3*n;i = i+2) {t = 1; z = (int) sqrt (i); for (int j = 3;j<= z;j++) {if (i%j==0) {t = 0; Break } if (t = = 1) {//Odd I is a prime number when tagged p[i]=1 p[i] = 1; } w = 0; for (int i = 3;i<= n;i=i+2) {if (p[i] = = 1) {w++; Q[W] = i; A total of W is not greater than N of the odd prime number assigned to the Q Array}}}} m = 0; max = 0; for (int i = 1;i<= w-2;i++)//Set triple loop enumerate all three prime array for (int j = i+1;j<= w-1;k++) for (in t k = j+1;k<=w;k++) {s = q[i]+q[j]+q[k]; Statistics of the sum of three elements if (p[s] = = 1) {m++; if (S>max) {//Compare and record Max Total group max = s; I2 = Q[i]; K2 = Q[k]; }}} printf ("Total%ld units \ n", m); if (m>0) printf ("A maximum whole group is:%d+%d+%d=%ld\n", I2,j2,k2,max);}
Minimal true fraction (numerator is less than denominator and numerator denominator is not common factor)
#include<stdio.h>#include<math.h>int main(){ int a,b,i,j,t,u; long m = 0; double s; printf("最简真分母在[a,b]内,请确定a、b:"); scanf("%d,%d",&a,&b);//输入区间上下限 s = 0; for(int j = a;j<= b;j++) //枚举分母 for(int i = 1;i<= j-1;i++){ //枚举分子 for(t = 0,u = 2;u<= i;u++){ //枚举因数 if(j%u == 0 && i%u == 0){ t = 1; break; //分子分母有公因数,舍去 } if(t == 0) { m++;//统计最简真分数个数 s+=(double)i/j; //求最简真分数的和 } } } printf("最简真分数共m=%ld个。\n",m); printf("其和s=%.5f\n",s);}
(6) Solution equation
Pell equation
X^2+ny=1 (where n is a non-squared positive integer)
常把x、y中有一个为零的解称为平凡解x、y满足方程的最小正数的解又称为基本解
Programming:
#include<stdio.h>#include<math.h> void main(){ double a,m,n,x,y; printf("解佩尔方程:x^2-ny^2=1.\n"); printf("请输入非平方整数n:"); scanf("%lf",&n); m = floor(sqrt(n+1)); if(m*n == n){ printf("n为平方数,方程无正整数解"); return; } y = 1; y++; a = n*y*y; x = floor(sqrt(a+1)); if(x*x == a+1){ printf("方程x^2-%.0fy^2 = 1的基本解为:\n",n); printf("x = %.0f,y = %.0f\n",x,y); break; } }}
(7) Solving inequalities
Programming:
#include<stdio.h>#include<math.h>int main(){ long c,d,i,m1,m2; double s; printf("请输入正整数m1,m2(m1<m2):"); scanf("%ld,%ld",&m1,&m2); i = 0; s = 0; while(s<= m1){ i = i+1; s = s+sqrt(i)/(i+1); } c = i; do{ i = i+1; s = s+sqrt(i)/(i+1); } while(s<=m2){ d = i-1; printf("满足不等式的正整数n为:%ld<=n<=%ld\n",c,d);
2017.12.15 Computer algorithm analysis and design enumeration