1 输入一个数值,输出其是否质数

2 验证哥德巴赫猜想,每个大于等于4的偶数都可以表示成2个质数之和

3 输入两个正整数m和n,求其最大公约数和最小公倍数

先求最大公约数:首先将m除以n(m>n)得到余数R,再用余数R去除原来的除数,得到新的余数,重复此过程直到余数为0时停止,此时的除数就是m和n的最大公约数

后求最小公倍数:m和n的积除以m和n的最大公约数

4 一个数恰好等于它的平方数的右端,这个数称为同构数。如5的平方是25,5是25中的右端的数,5就是同构数。找出1~1000之间的全部同构数

5 一个数如果恰好等于它的因子之和,这个数就成为“完数”。例如,6的因子是1、2、3,而6=1+2+3,因此6是“完数”。编程序找出1000之内的所有完数,并按照下面的格式输出其因子:

6 its factors are 1,2,3

6 一元二次方程

适用于受2个因素制约的问题

7 某单位排队形,开始排成3路纵队,末尾多出了2人。后改成5路纵队,末尾又多出了3人,最后改成7路纵队,正好没有余数。编程序求出该单位至少有多少人。

8 将一个数M分解为质因数(M≠0)。

9 求n!的末尾有多少个零。可以通过检查n!含有多少个10的因数来求它末尾零的个数。因为10=2×5,在n!中含有2的因数显然多于含有5的因数。 一种求n!中5的因数的个数的算法如下:

1) 输入正整数n;

2) 0=>k, n=>m;

3) 若m<5,转第5步,否则执行第4步;

4) m/5(取整)=>m, k+m=>k, 转第3步;

5) 输出k(n!末尾零的个数)。

10 有N张牌,计算机和你轮流取牌,每次只能取 1─2张,谁最后取完谁胜利。编写完成此功能的程序。

要求:

1) 让计算机先取。

2) 你取的张数由键盘输入。

3) 计算机第一次取牌时由键盘输入来确定是取1还是取2,以后它再取牌时由程序判断来决定取几张牌。

11 统计一个十进制的整数转换为二进制以后,有多少个1

方法1:按位与&

方法2:左移<<

12 人机搏弈。

有15颗棋子,你和计算机轮流取,每次只允许取1─3颗,直到取尽为止,谁手中的棋子总数为奇数者为赢。先由键盘输入来确定计算机先取还是你先取。

提示:取胜者秘诀是:第一次先取两颗,以后按下边规则进行:

1) 刚取的棋子数如果和已取得的棋子数加起来是个奇数,那么剩下的棋子数必须是1或8或9;

2) 刚取的棋子数如果和已取得的棋子数加起来是个偶数,那么剩下的棋子数必须是4或5。

思考: n颗棋子时,程序应如何修改? 

13 编程序求出应派谁去执行任务。

侦察班长准备在A、B、C、D、E、F 这6个人员中挑选若干人去执行任务,其人员配备条件是:

① A、B两人中至少去一人;

② A、B不能同去;

③ A、E、F三人中派两人去;

④ B、C两人都去或者都不去;

⑤ C、D中去一人;

⑥ 若D不去,则E也不去。

 

1 输入一个数值,输出其是否质数

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include<stdio.h>
 4 #include<math.h>
 5 
 6 int isprime(int num)//返回1是质数,0不是质数
 7 {
 8     int i;
 9     int flag = 1;//默认是质数
10 
11     if (num < 2)//负数,0,1不是质数
12     {
13         flag = 0;
14     }
15     else if (num == 2)//2是质数
16     {
17         flag = 1;
18     }
19     else
20     {
21         for (i = 2; i <= sqrt(num); i++)//计算开方
22         {
23             if (!(num%i))//如果找到能够整除num,不是素数
24             {
25                 flag = 0;
26                 break;
27             }
28         }
29     }
30 
31     return flag;
32 }
33 
34 main()
35 {
36     int num;
37 
38     scanf("%d", &num);
39 
40     if (isprime(num))//返回1是质数,0不是质数
41     {
42         printf("%d是素数\n", num);
43     }
44     else
45     {
46         printf("%d不是素数\n", num);
47     }
48 
49     system("pause");
50 }

 

2 验证哥德巴赫猜想,每个大于等于4的偶数都可以表示成2个质数之和

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 
 6 int isou(int num)//1代表偶数,0代表奇数
 7 {
 8     if (num % 2 == 0)
 9     {
10         return 1;
11     }
12     else
13     {
14         return 0;
15     }
16 }
17 
18 int sushu(int num)//1代表质数,0代表非质数
19 {
20     int i;
21     int flag = 1;
22 
23     if (num <= 1)//最小的质数是2
24     {
25         flag = 0;
26     }
27     else
28     {
29         for (i = 2;i < num;i++)
30         {
31             if (num%i == 0)
32             {
33                 flag = 0;
34                 break;
35             }
36         }
37     }
38 
39     return flag;
40 }
41 
42 main()
43 {
44     int num;
45     int i;
46 
47     scanf("%d", &num);
48 
49     printf("num=%d\n", num);
50 
51     if (isou(num) == 0)
52     {
53         printf("你输入的不是偶数");
54     }
55     else
56     {
57         if (num == 2)
58         {
59             printf("你输入的偶数太少");
60         }
61         else
62         {
63             for (i = 2;i < num;i++)
64             {
65                 if (sushu(i) == 1 && sushu(num - i) == 1)//判断两个数是否都是素数
66                 {
67                     printf("%d=%d+%d", num, i, num - i);
68                     break;
69                 }
70             }
71         }
72     }
73 
74     system("pause");
75 }

 

3 输入两个正整数m和n,求其最大公约数和最小公倍数

先求最大公约数:首先将m除以n(m>n)得到余数R,再用余数R去除原来的除数,得到新的余数,重复此过程直到余数为0时停止,此时的除数就是m和n的最大公约数

后求最小公倍数:m和n的积除以m和n的最大公约数

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include<stdio.h>
 4 
 5 int calculate_GCD(int m, int n)//最大公约数
 6 {
 7     int temp;
 8 
 9     if (m < n)
10     {
11         temp = m;
12         m = n;
13         n = temp;
14     }
15 
16     while (!n)
17     {
18         temp = m%n;
19         m = n;
20         n = temp;
21     }
22 
23     return m;
24 }
25 
26 int calculate_LCM(int m, int n)//最小公倍数
27 {
28     return m*n / calculate_GCD(m, n);
29 }
30 
31 main()
32 {
33     int m, n;
34 
35     scanf("%d%d", &m, &n);
36 
37     printf("%d和%d的最大公约数是:%d\n", m, n, calculate_GCD(m, n));
38 
39     printf("%d和%d的最小公倍数是:%d\n", m, n, calculate_LCM(m, n));
40 
41     system("pause");
42 }

 

传统方法

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int GongYueShu(int a, int b)//返回最大公约数
 5 {
 6     int n = a > b ? b : a;
 7 
 8     while (n)
 9     {
10         if (a%n == 0 && b%n == 0)
11         {
12             break;
13         }
14         n--;
15     }
16 
17     return n;
18 }
19 
20 int GongBeiShu(int a, int b)//返回最小公倍数
21 {
22     int n = a > b ? a : b;
23 
24     while (1)
25     {
26         if (n%a == 0 && n%b == 0)
27         {
28             break;
29         }
30         n++;
31     }
32 
33     return n;
34 }
35 
36 void main()
37 {
38     int a = 10;
39     int b = 7;
40 
41     printf("%d和%d的最大公约数是%d\n", a, b, GongYueShu(a, b));
42 
43     printf("%d和%d的最小公倍数是%d\n", a, b, GongBeiShu(a, b));
44     
45     system("pause");
46 }

 

4 一个数恰好等于它的平方数的右端,这个数称为同构数。如5的平方是25,5是25中的右端的数,5就是同构数。找出1~1000之间的全部同构数

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 
 6 int get10n(int n)
 7 {
 8     if (n == 0)
 9     {
10         return 1;
11     }
12     else
13     {
14         return get10n(n - 1) * 10;
15     }
16 }
17 
18 int getwei(int num)
19 {
20     if (num < 10)
21     {
22         return 1;
23     }
24     else
25     {
26         return getwei(num / 10) + 1;    
27     }
28 }
29 
30 int tgs(int num)
31 {
32     int flag = 0;//1是同构数,0不是同构数
33     int wei = getwei(num);//得到位数
34     int times = get10n(wei);//10的位数次方
35     //5,通过%10得到
36     //25,通过%100得到
37     //取模与位数有关
38 
39     if (num == num*num%times)//一个数恰好等于它的平方数的右端
40     {
41         flag = 1;
42     }
43 
44     return flag;
45 }
46 
47 main()
48 {
49     int i;
50 
51     for (i = 1; i <= 1000; i++)
52     {
53         if (tgs(i))//如果是同构数
54         {
55             printf("%d\n", i);
56         }
57     }
58 
59     system("pause");
60 }

 

5 一个数如果恰好等于它的因子之和,这个数就成为“完数”。例如,6的因子为1、2、3,而6=1+2+3,因此6是“完数”。编程序找出1000之内的所有完数,并按下面的格式输出其因子:

6 its factors are 1 2 3

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 
 6 int wanshu(int num)
 7 {
 8     int flag = 0;//1是完数,0不是完数
 9     int i;
10     int sum = 0;
11     int arr[100] = { 0 };//数组存放因子
12     int index = 0;//存放因子数量
13 
14     for (i = 1; i < num; i++)
15     {
16         if (!(num%i))//如果可以整除
17         {
18             sum += i;//因子相加
19             arr[index] = i;//数组存放因子
20             index++;//存放因子数量
21         }
22     }
23 
24     if (num == sum)//一个数如果恰好等于它的因子之和
25     {
26         flag = 1;//1是完数,0不是完数
27         printf("%d its factors are", num);
28         for (i = 0; i < index; i++)
29         {
30             printf("%5d", arr[i]);
31         }
32     }
33 
34     return flag;
35 }
36 
37 main()
38 {
39     int i;
40 
41     for (i = 1; i <= 1000; i++)
42     {
43         if (wanshu(i))//如果是完数
44         {
45             printf("\n");
46         }
47     }
48 
49     system("pause");
50 }

 

6 一元二次方程

适用于受2个因素制约的问题

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include<stdio.h>
 4 #include<stdio.h>
 5 #include<math.h>
 6 
 7 main()
 8 {
 9     double a, b, c;
10     double dt;
11     double x1, x2;
12 
13     scanf("%lf%lf%lf", &a, &b, &c);
14     printf("你要求解的方程式%f*x*x+%f*x+%f=0\n", a, b, c);
15 
16     if (a == 0)
17     {
18         printf("你要计算一元一次方程\n");
19         if (b == 0)
20         {
21             if (c == 0)
22             {
23                 printf("x为任意值\n");
24             }
25             if (c != 0)
26             {
27                 printf("x无解\n");
28             }
29         }
30         else
31         {
32             printf("x=%f", -1 * c / b);
33         }
34     }
35     else
36     {
37         printf("你要计算二元一次方程\n");
38         dt = b*b - 4 * a*c;
39         if (dt == 0)
40         {
41             printf("x1=x2=%f\n", -1.0*b / 2 / a);
42         }
43         else if (dt > 0)
44         {
45             printf("x1=%f,x2=%f\n", (-1.0*b + sqrt(dt)) / 2 / a, (-1.0*b - sqrt(dt)) / 2 / a);
46         }
47         else
48         {
49             double p = -1 * b / 2 / a;        /* 实数部分 */
50             double q = sqrt(-1 * dt) / 2 / a;        /* 虚数部分 */
51             printf("x1=%f+%fi,x2=%f-%fi\n", p, q, p, q);
52         }
53     }
54 
55     system("pause");
56 }

 

7 某单位排队形,开始排成3路纵队,末尾多出了2人。后改成5路纵队,末尾又多出了3人,最后改成7路纵队,正好没有余数。编程序求出该单位至少有多少人。

 

 1 #include <stdio.h>
 2 
 3 void main()
 4 {
 5     int i = 1;
 6 
 7     for (i = 1;; i++)
 8     {
 9         if (i % 3 == 2 && i % 5 == 3 && i % 7 == 0)
10         {
11             printf("该单位至少有%d人\n", i);
12             break;
13         }
14     }
15 }

 

8 将一个数M分解为质因数(M≠0)。

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 
 5 void main()
 6 {
 7     int i;
 8     int num = 100;
 9 
10     printf("%d=", num);
11 
12     for (i = 2; i <= num; i++)//从2开始,直到num
13     {
14         while (!(num%i))//如果发现因数,将num分解
15         {
16             num /= i;
17             printf("%d*", i);
18         }
19     }
20 
21     printf("%d", num);
22 }

 

9 求n!的末尾有多少个零。可以通过检查n!含有多少个10的因数来求它末尾零的个数。因为10=2×5,在n!中含有2的因数显然多于含有5的因数。 一种求n!中5的因数的个数的算法如下:

1) 输入正整数n;

2) 0=>k, n=>m;

3) 若m<5,转第5步,否则执行第4步;

4) m/5(取整)=>m, k+m=>k, 转第3步;

5) 输出k(n!末尾零的个数)。

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 
 5 void main()
 6 {
 7     int n, num, count = 0;
 8     int i;
 9     printf("innput n:");
10     scanf("%d", &n);
11     for (i = 5; i <= n; i += 5)
12     {
13         count++;
14         num = i / 5;
15         while (num % 5 == 0)
16         {
17             count++;
18             num /= 5;
19         }
20     }
21     printf("%d!末尾有%d个0。\n", n, count);
22     system("pause");
23 }

 

10 有N张牌,计算机和你轮流取牌,每次只能取 1─2张,谁最后取完谁胜利。编写完成此功能的程序。

要求:

1) 让计算机先取。

2) 你取的张数由键盘输入。

3) 计算机第一次取牌时由键盘输入来确定是取1还是取2,以后它再取牌时由程序判断来决定取几张牌。

 

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <time.h>
  5 
  6 #define N 10//一共10张牌
  7 
  8 void computerFirst();//1让计算机先取
  9 void playerFirst();//0让玩家先取
 10 
 11 void main()
 12 {
 13     int who = 0;//1让计算机先取 0让玩家先取
 14 
 15     printf("一共%d张牌\n", N);
 16 
 17     printf("请选择游戏选项 1让计算机先取 0让玩家先取\n");
 18     scanf("%d", &who);
 19     while (who != 1 && who != 0)
 20     {
 21         printf("请选择游戏选项 1让计算机先取 0让玩家先取\n");
 22         scanf("%d", &who);
 23     }
 24 
 25     if (who)//1让计算机先取
 26     {
 27         computerFirst();
 28     }
 29     else//0让玩家先取
 30     {
 31         playerFirst();
 32     }
 33 }
 34     
 35 void computerFirst()//1让计算机先取
 36 {
 37     time_t ts;
 38     srand((unsigned int)time(&ts));
 39     int count = N;//剩余牌数
 40     int temp = 0;//当前取牌数
 41     int flag = 1;//1游戏继续0游戏结束
 42 
 43     printf("计算机第一次取牌时由键盘输入来确定是取1还是取2\n");
 44     scanf("%d", &temp);
 45     while (temp != 1 && temp != 2)
 46     {
 47         printf("计算机第一次取牌时由键盘输入来确定是取1还是取2\n");
 48         scanf("%d", &temp);
 49     }
 50 
 51     count -= temp;//牌数减少
 52     printf("电脑取%d张牌,现在剩余%d张牌\n", temp, count);
 53 
 54     while (count > 1)//剩余牌数大于1,循环
 55     {
 56         printf("轮到玩家取牌,是取1还是取2\n");
 57         scanf("%d", &temp);
 58         while (temp != 1 && temp != 2)
 59         {
 60             printf("轮到玩家取牌,是取1还是取2\n");
 61             scanf("%d", &temp);
 62         }
 63 
 64         count -= temp;//牌数减少
 65         printf("玩家取%d张牌,现在剩余%d张牌\n", temp, count);
 66 
 67         if (!count)//如果剩余牌数为0,玩家赢
 68         {
 69             printf("玩家赢\n");
 70             flag = 0;//1游戏继续0游戏结束
 71             break;
 72         }
 73         else if (count == 1 || count == 2)//如果剩余牌数为1或者2,电脑赢
 74         {
 75             temp = count;//取牌
 76             count = 0;//没有牌
 77             printf("电脑取%d张牌,现在剩余%d张牌\n", temp, count);
 78             printf("电脑赢\n");
 79             flag = 0;//1游戏继续0游戏结束
 80             break;
 81         }
 82         else//如果剩余牌数大于2,电脑继续取牌
 83         {
 84             temp = (rand() % (2 - 1 + 1)) + 1;
 85             count -= temp;//牌数减少
 86             printf("电脑取%d张牌,现在剩余%d张牌\n", temp, count);
 87         }
 88     }
 89 
 90     if (flag)//1游戏继续0游戏结束
 91     {
 92         if (count)
 93         {
 94             temp = count;//取牌
 95             count = 0;//没有牌
 96             printf("玩家取%d张牌,现在剩余%d张牌\n", temp, count);
 97             printf("玩家赢\n");
 98         }
 99         else
100         {
101             printf("电脑赢\n");
102         }
103     }
104 }
105 
106 void playerFirst()//0让玩家先取
107 {
108     time_t ts;
109     srand((unsigned int)time(&ts));
110     int count = N;//剩余牌数
111     int temp = 0;//当前取牌数
112     int flag = 1;//1游戏继续0游戏结束
113 
114     while (count > 1)//剩余牌数大于1,循环
115     {
116         printf("轮到玩家取牌,是取1还是取2\n");
117         scanf("%d", &temp);
118         while (temp != 1 && temp != 2)
119         {
120             printf("轮到玩家取牌,是取1还是取2\n");
121             scanf("%d", &temp);
122         }
123 
124         count -= temp;//牌数减少
125         printf("玩家取%d张牌,现在剩余%d张牌\n", temp, count);
126 
127         if (!count)//如果剩余牌数为0,玩家赢
128         {
129             printf("玩家赢\n");
130             flag = 0;//1游戏继续0游戏结束
131             break;
132         }
133         else if (count == 1 || count == 2)//如果剩余牌数为1或者2,电脑赢
134         {
135             temp = count;//取牌
136             count = 0;//没有牌
137             printf("电脑取%d张牌,现在剩余%d张牌\n", temp, count);
138             printf("电脑赢\n");
139             flag = 0;//1游戏继续0游戏结束
140             break;
141         }
142         else//如果剩余牌数大于2,电脑继续取牌
143         {
144             temp = (rand() % (2 - 1 + 1)) + 1;
145             count -= temp;//牌数减少
146             printf("电脑取%d张牌,现在剩余%d张牌\n", temp, count);
147         }
148     }
149 
150     if (flag)//1游戏继续0游戏结束
151     {
152         if (count)
153         {
154             temp = count;//取牌
155             count = 0;//没有牌
156             printf("玩家取%d张牌,现在剩余%d张牌\n", temp, count);
157             printf("玩家赢\n");
158         }
159         else
160         {
161             printf("电脑赢\n");
162         }
163     }
164 }

 

11 统计一个十进制的整数转换为二进制以后,有多少个1

 

方法1:按位与&

 

 1 #include <iostream>
 2 
 3 void main()//按位与&
 4 {
 5     int num;
 6     int i = 0;//保存1的个数
 7 
 8     std::cin >> num;
 9     
10     while (num)
11     {
12         i++;
13         num &= num - 1;//不断与其减1的数按位与&
14     }
15 
16     std::cout << i << std::endl;//打印结果
17 
18     system("pause");
19 }

 

方法2:左移<<

 

 1 #include <iostream>
 2 
 3 int get1(int num)//返回一个十进制的整数转换为二进制以后,有多少个1
 4 {
 5     int count = 0;//表示位数
 6     unsigned int flag = 1;
 7 
 8     while (flag)
 9     {
10         std::cout << "num-" << num << " flag=" << flag << std::endl;//打印中间值
11 
12         if (num&flag)
13         {
14             count++;
15         }
16 
17         flag = flag << 1;
18     }
19 
20     return count;
21 }
22 
23 void main()//左移<<
24 {
25     int num;
26     int i = 0;
27 
28     std::cin >> num;
29 
30     i = get1(num);//返回一个十进制的整数转换为二进制以后,有多少个1    
31 
32     std::cout << i << std::endl;
33 
34     system("pause");
35 }

 

12 人机搏弈。

有15颗棋子,你和计算机轮流取,每次只允许取1─3颗,直到取尽为止,谁手中的棋子总数为奇数者为赢。先由键盘输入来确定计算机先取还是你先取。

提示:取胜者秘诀是:第一次先取两颗,以后按下边规则进行:

1) 刚取的棋子数如果和已取得的棋子数加起来是个奇数,那么剩下的棋子数必须是1或8或9;

2) 刚取的棋子数如果和已取得的棋子数加起来是个偶数,那么剩下的棋子数必须是4或5。

思考: n颗棋子时,程序应如何修改? 

 

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <time.h>
  5 
  6 #define N 15//一共15颗棋子
  7 
  8 void computerFirst();//1让计算机先取
  9 void playerFirst();//0让玩家先取
 10 
 11 void main()
 12 {
 13     int who = 0;//1让计算机先取 0让玩家先取
 14 
 15     printf("一共%d颗棋子\n", N);
 16 
 17     printf("请选择游戏选项 1让计算机先取 0让玩家先取\n");//先由键盘输入来确定计算机先取还是你先取
 18     scanf("%d", &who);
 19     while (who != 1 && who != 0)
 20     {
 21         printf("请选择游戏选项 1让计算机先取 0让玩家先取\n");
 22         scanf("%d", &who);
 23     }
 24 
 25     if (who)//1让计算机先取
 26     {
 27         computerFirst();
 28     }
 29     else//0让玩家先取
 30     {
 31         playerFirst();
 32     }
 33 }
 34 
 35 void computerFirst()//1让计算机先取
 36 {
 37     time_t ts;
 38     srand((unsigned int)time(&ts));
 39     int count = N;//剩余棋子数
 40     int countComputer = 0;//计算机的棋子数
 41     int countPlayer = 0;//玩家的棋子数
 42     int temp = 0;//当前取棋子数
 43     int round = 0;//回合
 44 
 45     round++;//回合
 46     printf("\n这是第%d次回合\n\n", round);
 47 
 48     temp = 2;//取胜者秘诀是:第一次先取两颗
 49     count -= temp;//棋子数减少
 50     countComputer += temp;//计算机的棋子数增加
 51     printf("电脑取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
 52 
 53     while (count)//剩余牌数大于0,循环
 54     {
 55         //这是玩家回合
 56         if (count >= 3)//剩余牌数大于等于3
 57         {
 58             printf("轮到玩家取棋子,是取1还是取2还是3\n");
 59             scanf("%d", &temp);
 60             while (temp != 1 && temp != 2 && temp != 3)
 61             {
 62                 printf("轮到玩家取棋子,是取1还是取2还是3\n");
 63                 scanf("%d", &temp);
 64             }
 65         }
 66         else if (count == 2)//剩余牌数等于2
 67         {
 68             printf("轮到玩家取棋子,是取1还是取2\n");
 69             scanf("%d", &temp);
 70             while (temp != 1 && temp != 2)
 71             {
 72                 printf("轮到玩家取棋子,是取1还是取2\n");
 73                 scanf("%d", &temp);
 74             }
 75         }
 76         else//剩余牌数等于1
 77         {
 78             temp = 1;//取棋子
 79         }
 80 
 81         count -= temp;//棋子数减少
 82         countPlayer += temp;//玩家的棋子数增加
 83         printf("玩家取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
 84 
 85         round++;//回合
 86         printf("\n这是第%d次回合\n\n", round);
 87 
 88         //提示:取胜者秘诀是:第一次先取两颗,以后按下边规则进行:
 89 
 90         //    1) 刚取的棋子数如果和已取得的棋子数加起来是个奇数,那么剩下的棋子数必须是1或8或9;
 91 
 92         //    2) 刚取的棋子数如果和已取得的棋子数加起来是个偶数,那么剩下的棋子数必须是4或5。
 93 
 94         //这是计算机回合
 95         if ((((countComputer + 3) % 2) && ((count - 3 == 1) || (count - 3 == 8) || (count - 3 == 9))) || ((!((countComputer + 3) % 2)) && ((count - 3 == 4) || (count - 3 == 5))))
 96         {
 97             printf("必胜\n");
 98             temp = 3;
 99             count -= temp;//棋子数减少
100             countComputer += temp;//计算机的棋子数增加
101             printf("计算机取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
102         }
103         else if ((((countComputer + 2) % 2) && ((count - 2 == 1) || (count - 2 == 8) || (count - 2 == 9))) || ((!((countComputer + 2) % 2)) && ((count - 2 == 4) || (count - 2 == 5))))
104         {
105             printf("必胜\n");
106             temp = 2;
107             count -= temp;//棋子数减少
108             countComputer += temp;//计算机的棋子数增加
109             printf("计算机取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
110         }
111         else if ((((countComputer + 1) % 2) && ((count - 1 == 1) || (count - 1 == 8) || (count - 1 == 9))) || ((!((countComputer + 1) % 2)) && ((count - 1 == 4) || (count - 1 == 5))))
112         {
113             printf("必胜\n");
114             temp = 1;
115             count -= temp;//棋子数减少
116             countComputer += temp;//计算机的棋子数增加
117             printf("计算机取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
118         }
119         else if (count > 3)//如果剩余棋子数大于3,电脑继续取棋子
120         {
121             temp = (rand() % (3 - 1 + 1)) + 1;
122             count -= temp;//棋子数减少
123             countComputer += temp;//计算机的棋子数增加
124             printf("计算机取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
125         }
126         else if (count == 2 || count == 3)//如果剩余棋子数为2或者3
127         {
128             if ((countComputer + count) % 2)//如果计算机的棋子数加上剩余的棋子数之和是奇数
129             {
130                 temp = count;//取棋子
131                 count -= temp;//棋子数减少
132                 countComputer += temp;//计算机的棋子数增加
133                 printf("计算机取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
134                 break;
135             }
136             else//如果计算机的棋子数加上剩余的棋子数之和不是奇数
137             {
138                 temp = count - 1;//取棋子
139                 count -= temp;//棋子数减少
140                 countComputer += temp;//计算机的棋子数增加
141                 printf("计算机取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
142             }
143         }
144         else if (count == 1)//如果剩余棋子数为1
145         {
146             temp = 1;//取棋子
147             count -= temp;//棋子数减少
148             countComputer += temp;//计算机的棋子数增加
149             printf("计算机取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
150             break;
151         }
152         else//如果剩余棋子数为0,判断
153         {
154             break;
155         }
156     }
157 
158     if (countComputer % 2)//谁手中的棋子总数为奇数者为赢
159     {
160         printf("计算机赢\n");
161     }
162     else
163     {
164         printf("玩家赢\n");
165     }
166 }
167 
168 void playerFirst()//0让玩家先取
169 {
170     time_t ts;
171     srand((unsigned int)time(&ts));
172     int count = N;//剩余棋子数
173     int countComputer = 0;//计算机的棋子数
174     int countPlayer = 0;//玩家的棋子数
175     int temp = 0;//当前取棋子数
176     int round = 0;//回合
177 
178     while (count)//剩余牌数大于0,循环
179     {
180         round++;//回合
181         printf("\n这是第%d次回合\n\n", round);
182 
183         //这是玩家回合
184         if (count >= 3)//剩余牌数大于等于3
185         {
186             printf("轮到玩家取棋子,是取1还是取2还是3\n");
187             scanf("%d", &temp);
188             while (temp != 1 && temp != 2 && temp != 3)
189             {
190                 printf("轮到玩家取棋子,是取1还是取2还是3\n");
191                 scanf("%d", &temp);
192             }
193         }
194         else if (count == 2)//剩余牌数等于2
195         {
196             printf("轮到玩家取棋子,是取1还是取2\n");
197             scanf("%d", &temp);
198             while (temp != 1 && temp != 2)
199             {
200                 printf("轮到玩家取棋子,是取1还是取2\n");
201                 scanf("%d", &temp);
202             }
203         }
204         else//剩余牌数等于1
205         {
206             temp = 1;//取棋子
207         }
208 
209         count -= temp;//棋子数减少
210         countPlayer += temp;//玩家的棋子数增加
211         printf("玩家取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
212         
213         //提示:取胜者秘诀是:第一次先取两颗,以后按下边规则进行:
214 
215         //    1) 刚取的棋子数如果和已取得的棋子数加起来是个奇数,那么剩下的棋子数必须是1或8或9;
216 
217         //    2) 刚取的棋子数如果和已取得的棋子数加起来是个偶数,那么剩下的棋子数必须是4或5。
218 
219         //这是计算机回合
220         if ((((countComputer + 3) % 2) && ((count - 3 == 1) || (count - 3 == 8) || (count - 3 == 9))) || ((!((countComputer + 3) % 2)) && ((count - 3 == 4) || (count - 3 == 5))))
221         {
222             printf("必胜\n");
223             temp = 3;
224             count -= temp;//棋子数减少
225             countComputer += temp;//计算机的棋子数增加
226             printf("计算机取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
227         }
228         else if ((((countComputer + 2) % 2) && ((count - 2 == 1) || (count - 2 == 8) || (count - 2 == 9))) || ((!((countComputer + 2) % 2)) && ((count - 2 == 4) || (count - 2 == 5))))
229         {
230             printf("必胜\n");
231             temp = 2;
232             count -= temp;//棋子数减少
233             countComputer += temp;//计算机的棋子数增加
234             printf("计算机取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
235         }
236         else if ((((countComputer + 1) % 2) && ((count - 1 == 1) || (count - 1 == 8) || (count - 1 == 9))) || ((!((countComputer + 1) % 2)) && ((count - 1 == 4) || (count - 1 == 5))))
237         {
238             printf("必胜\n");
239             temp = 1;
240             count -= temp;//棋子数减少
241             countComputer += temp;//计算机的棋子数增加
242             printf("计算机取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
243         }
244         else if (count > 3)//如果剩余棋子数大于3,电脑继续取棋子
245         {
246             temp = (rand() % (3 - 1 + 1)) + 1;
247             count -= temp;//棋子数减少
248             countComputer += temp;//计算机的棋子数增加
249             printf("计算机取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
250         }
251         else if (count == 2 || count == 3)//如果剩余棋子数为2或者3
252         {
253             if ((countComputer + count) % 2)//如果计算机的棋子数加上剩余的棋子数之和是奇数
254             {
255                 temp = count;//取棋子
256                 count -= temp;//棋子数减少
257                 countComputer += temp;//计算机的棋子数增加
258                 printf("计算机取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
259                 break;
260             }
261             else//如果计算机的棋子数加上剩余的棋子数之和不是奇数
262             {
263                 temp = count - 1;//取棋子
264                 count -= temp;//棋子数减少
265                 countComputer += temp;//计算机的棋子数增加
266                 printf("计算机取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
267             }
268         }
269         else if (count == 1)//如果剩余棋子数为1
270         {
271             temp = 1;//取棋子
272             count -= temp;//棋子数减少
273             countComputer += temp;//计算机的棋子数增加
274             printf("计算机取%d颗棋子,现在剩余%d颗棋子,计算机有%d颗棋子,玩家有%d颗棋子\n", temp, count, countComputer, countPlayer);
275             break;
276         }
277         else//如果剩余棋子数为0,判断
278         {
279             break;
280         }
281     }
282 
283     if (countComputer % 2)//谁手中的棋子总数为奇数者为赢
284     {
285         printf("计算机赢\n");
286     }
287     else
288     {
289         printf("玩家赢\n");
290     }
291 }

 

13 编程序求出应派谁去执行任务。

侦察班长准备在A、B、C、D、E、F 这6个人员中挑选若干人去执行任务,其人员配备条件是:

① A、B两人中至少去一人;

② A、B不能同去;

③ A、E、F三人中派两人去;

④ B、C两人都去或者都不去;

⑤ C、D中去一人;

⑥ 若D不去,则E也不去。

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 void main()
 5 {
 6     int a, b, c, d, e, f;//分别代表6个人
 7 
 8     for (a = 0; a < 2; a++)
 9     {
10         for (b = 0; b < 2; b++)
11         {
12             for (c = 0; c < 2; c++)
13             {
14                 for (d = 0; d < 2; d++)
15                 {
16                     for (e = 0; e < 2; e++)
17                     {
18                         for (f = 0; f < 2; f++)
19                         {
20                             if ((a + b == 1) && (a + e + f == 2) && ((b + c == 2) || (b + c == 0)) && (c + d == 1) && ((d + e == 0) || (d == 1)))
21                             {
22                                 printf("A=%d,B=%d,C=%d,D=%d,E=%d,F=%d\n", a, b, c, d, e, f);//1代表去,0代表不去
23                             }
24                         }
25                     }
26                 }
27             }
28         }
29     }
30 }

 

相关文章:

  • 2021-11-29
  • 2022-12-23
  • 2021-08-06
  • 2022-01-05
  • 2022-12-23
  • 2021-05-12
  • 2021-12-03
猜你喜欢
  • 2022-01-04
  • 2021-05-17
  • 2022-02-09
  • 2022-12-23
  • 2021-12-12
  • 2022-02-06
  • 2022-01-18
相关资源
相似解决方案