100枚硬币,最初全部朝下,第一次将所有硬币反转过来, 第二次反转位置是2的倍数的硬币,第三次反转3的倍数,.....执行一百次,问最终共有多少个硬币面朝上?

来源:互联网 发布:淘宝新规店铺负责人 编辑:程序博客网 时间:2024/05/29 17:47
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <WTYPES.H>#include <memory.h>/****************************************************************************************************************************************************************lxmuyu****begin:2012 11 21********100枚硬币,最初全部朝下,第一次将所有硬币反转过来, 第二次反转位置是2的倍数的硬币,第三次反转3的倍数,.....执行一百次,问最终共有多少个硬币面朝上?********end  :      2012 11 21****   ************************************************************************************************************************************************************/int countFactors(int data){int res = 0, i;for(i = 1; i <= data; i++){if(data % i == 0){res++;}}return res;}void main(){int count = 0, i, upCount = 0;while(count <= 0){printf("输入硬币个数 :");scanf("%d", &count);if(count >= 0){break;}printf("输入有误!\r\n");}for(i = 1; i <= count; i++){if(countFactors(i) % 2 != 0){upCount++;}}printf("朝上的硬币有 %d 个", upCount);}