7道c练习题

花了我将近两个小时的时间。。。。。。。。。
/*
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;
}

你可能感兴趣的:(C++,c,C#,J#,ide)