浏览 5156 次
锁定老帖子 主题:7道c练习题
精华帖 (0) :: 良好帖 (0) :: 新手帖 (24) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-04-28
最后修改:2009-04-28
/* author:zhanglu date:09/04/26 */ #include <stdio.h> /* 判断用户输入的数是否为质数? */ bool isPrimeNumber(int number) { bool flag = true; for(int i = 2;i < number;++i) { if(number % i == 0) { flag = false; break; } } return flag; } void testPrimeNumber() { int number,nClear; printf("请输入一个数字:\n"); scanf("%d", &number); while(number < 0) { printf("请重新输入一个数字:\n"); while((nClear = getchar()) != '\n' && nClear != EOF); scanf("%d", &number); } if(isPrimeNumber(number)) { printf("%d是质数!\n", number); }else { printf("%d不是质数!\n", number); } } /* 求1000以内的素数之和 */ void primeSum() { int nSumPrime = 0,i = 2; do { if(isPrimeNumber(i)) { nSumPrime += i; } ++i; }while(i <= 1000); printf("1000以内素数之和为:%d\n", nSumPrime); } /* 列出用户输入的某个数的全部因子 */ void splitNumber() { int a,nClear; printf("请输入一个数字:\n"); scanf("%d", &a); while(a < 0) { printf("请重新输入数字:\n"); while((nClear = getchar()) != '\n' && nClear != EOF); scanf("%d", &a); } int i = 2; while(a > 1) { while(a % i == 0) { a /= i; printf("%d\n", i); } ++i; } } /* 列出用户输入的某个数的分解和,分解为几个连续数的和 */ void listSplitNumer() { int number,nClear,nSum = 0; printf("请输入一个数字:\n"); scanf("%d", &number); while(number < 0) { printf("请重新输入一个数字:\n"); while((nClear = getchar()) != '\n' && nClear != EOF); scanf("%d", &number); } if(number % 2 == 0) { int halfNumber = number/2; printf("===================================\n"); for(int i = halfNumber;i > 0;--i) { nSum = i; for(int j = i - 1;j > 0;--j) { nSum += j; if(nSum == number) { printf("%d = ", number); for(int k = j;k <= i;++k) { if(k == i) { printf("%d\n", k); }else { printf("%d+ ", k); } } nSum = 0; break; }else { if(nSum > number) { nSum = 0; break; }else { if(j == 1) { nSum = 0; } } } } } }else { printf("===================================\n"); int halfNumber = number/2; printf("%d = %d + %d\n", number, halfNumber, halfNumber+1); for(int i = halfNumber;i > 0;--i) { nSum = i; for(int j = i - 1;j > 0;--j) { nSum += j; if(nSum == number) { printf("%d = ", number); for(int k = j;k <= i;++k) { if(k == i) { printf("%d\n", k); }else { printf("%d+ ", k); } } nSum = 0; break; }else { if(nSum > number) { nSum = 0; break; }else { if(j == 1) { nSum = 0; } } } } } } } /* 列出10000内的完全数 */ void perfectNumber() { int number = 10000; printf("%d = ", number); while(number != 1) { while(number % 2 == 0) { number /=2; if(number != 1) { printf("%d+ ", number); }else { printf("%d\n", number); } while(number != 1 && number % 2 != 0) { number /=2; number += 1; printf("%d+ ", number); } } } } /* 列出1000以内素数中的各位数字和为2的素数 */ void screeningPrime() { int primeDigit = 0; for(int i = 2;i <= 1000;++i) { if(isPrimeNumber(i)) { primeDigit = i; int baiw = primeDigit / 100; int shiw = primeDigit / 10; int gew = primeDigit % 10; int num; if(baiw != 0) { shiw = (primeDigit % 100)/10; gew = (primeDigit % 100) % 10; num = baiw + shiw + gew; while(num != 2) { baiw = num / 100; shiw = num / 10; gew = num % 10; if(baiw != 0) { shiw = (primeDigit % 100)/10; gew = (primeDigit % 100) % 10; num = baiw + shiw + gew; if(num == 2) { printf("%d\n", primeDigit); }else { if(num / 10 == 0) { break; } } }else if(shiw != 0) { gew = num % 10; num = shiw + gew; if(num == 2) { printf("%d\n", primeDigit); }else { if(num / 10 == 0) { break; } } }else { num = gew; if(num == 2) { printf("%d\n", primeDigit); }else { break; } } } }else if(shiw != 0) { gew = num % 10; num = shiw + gew; if(num == 2) { printf("%d\n", primeDigit); } }else { num = gew; if(num == 2) { printf("%d\n", primeDigit); } } } } } /* 7只老鼠围成一个圈,从第一只开始吃,隔一只吃一只,求最后被吃的老鼠是第几只老鼠? 1 2 3 4 5 */ void eatMice(int nMiceCout) { int nMice[100] = {0}; for(int j = 0;j < nMiceCout;j++) { nMice[j] = j+1; } int flag = 1; int i = 0, eatCount = 0, nMiceNumber; while(eatCount != nMiceCout) { if(i <= nMiceCout - 1) { if(flag == 1) { if(nMice[i] != 0) { nMice[i] = 0; ++eatCount; if(eatCount == nMiceCout) { nMiceNumber = i+1; } ++i; printf("flag = %d i = %d\n", flag, i); flag = 0; }else { ++i; } }else { if(nMice[i] != 0) { ++flag; } ++i; } }else { i = 0; } } printf("最后被吃的老鼠是第%d只老鼠\n", nMiceNumber); } int main(void) { //testPrimeNumber(); //primeSum(); //perfectNumber(); //screeningPrime(); eatMice(10); return 0; } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-05-18
学习了,我2个小时也弄不出来,我得2天,顺便问问C开发,一般用什么IDE?
|
|
返回顶楼 | |
发表时间:2009-05-19
最后修改:2009-05-19
一个小小的建议:
1.你说是C代码,那么C中是没有bool类型的。 2.判断一个数是不是素数,可以这样写,效率会高一些:(有写的不对的,多多包涵。) #include <stdio.h> #include <math.h> int IsPrime(int x) { if (x == 1)return 0; else if (x == 2)return 1; else if (x % 2 == 0) return 0; else { int i,bound = sqrt((double)x) + 1; for( i = 3; i < bound; i += 2) { if (x % i == 0) return 0; } return 1; } } int main() { int n; printf("Please input a positive integer:"); while (scanf( "%d",&n) && n) { if (n < 0) printf("Please input a positive integer:"); else { if(IsPrime(n)) printf("Yes\n"); else printf("No\n"); printf("Please input a positive integer:"); } } return 0; } 3.如果你想计算[0,n]中所有素数的和,那可以一边寻找,一遍构造素数数组,这个数组对后面整数的素数判断有好处,这样检查起来速度会更快。当然你可以先构造好数组,再计算,但是个人觉得,两者差不多。 #include <stdio.h> #include <math.h> int Ps[100]; int index = 2; int IsPrime(int x) { if (x == 1)return 0; else if (x == 2)return 1; else if (x % 2 == 0) return 0; else { int i,bound = sqrt((double)x) + 1; for( i = 1; Ps[i] < bound; i ++) { if (x % Ps[i] == 0) return 0; } Ps[index++] = x; return 1; } } int SumPrime(int n) { int i, sum = 0; for(i = 2; i <= n; i++) if(IsPrime(i)) sum += i; return sum; } int main() { int n; Ps[0] = 2;Ps[1] = 3; printf("Please input a positive integer:"); while (scanf( "%d",&n) && n) { if (n < 0) printf("Please input a positive integer:"); else { printf("The Sum of primers between 0 and %d is: %d\n",n,SumPrime(n)); printf("Please input a positive integer:"); } } return 0; } |
|
返回顶楼 | |
发表时间:2009-06-29
# /* # 7只老鼠围成一个圈,从第一只开始吃,隔一只吃一只,求最后被吃的老鼠是第几只老鼠? # 1 2 3 4 5 # 约瑟夫环问题 # */ int main() { int i, r=0, n=7, m=2; for(i=2; i<=n; i++) r = (r+m)%i; printf("%d", r+1); } |
|
返回顶楼 | |