算法
2013年08月31日
⁄ 综合
⁄ 共 2308字 ⁄ 字号
小 中 大
- [code=C/C++]
-
- #include <stdio.h>
- #include <time.h>
-
- int M[10] = {0};
- int X[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- int Y[10] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
- int O[10] = {0};
- int ST = 52;
-
-
- void initM()
- {
- srand((unsigned int)time(0));
-
- for (int x=0; x<10; ++x)
- {
-
- M[x] = rand()%100;
- }
- }
-
-
- int active(int m, int x)
- {
- int o = m * x;
-
- if (o > ST)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
-
-
- void calcY()
- {
- for (int x=0; x<10; ++x)
- {
- O[x] = active(M[x], X[x]);
- }
- }
-
-
- int adjustM()
- {
- int err = 0;
- for (int x=0; x<10; ++x)
- {
- if (O[x] != Y[x])
- {
- err++;
-
- if (1 == O[x])
- {
- M[x] += X[x];
- }
- else
- {
- M[x] -= X[x];
- }
- }
- }
-
- return err;
- }
-
-
- void printM()
- {
- printf("/n最终训练结果:/n");
- for (int x=0; x<10; ++x)
- {
- printf("M[%i] = %i/n", x, M[x]);
- }
- }
-
-
- void test(int input)
- {
- if ( 0==active(M[input], X[input]) )
- {
- printf("%d 是 偶数 ", input+1);
- }
- else
- {
- printf("%d 是 奇数 ", input+1);
- }
-
- printf("/n/n");
- }
-
-
- int main()
- {
- printf("请输入阈值:");
- scanf("%d", &ST);
- printf("/n");
-
- initM();
-
- int n = 0;
-
- while (1)
- {
- n++;
- calcY();
- int err = adjustM();
- if (0 >=err)
- {
-
- break;
- }
- printf("第%0.2d次训练后的结果中存在的错误数 %d/n", n,err);
- }
-
- printM();
- printf("/n阈值=%d 训练次数=%d/n/n", ST, n);
-
- while (true)
- {
- int a = 0;
- printf("请输入范围为1~10的数字:");
- scanf("%d", &a);
- if (1 > a || 10 < a)
- {
- break;
- }
-
- test(a-1);
- }
-
- return 0;
- }
-
- [/code]
- 该日志由 molten 于11年前发表在综合分类下,最后更新于 2013年08月31日.
- 转载请注明: 算法 | 学步园 +复制链接