提交 5bba3383 编写于 作者: 独孤过's avatar 独孤过 提交者: Gitee

删除文件 algorithm

上级 3f8faa06
#include <stdio.h>
#define N 9
int bsearch(int *array, int num, int key)
{
int low = 0, high = num - 1, mid;
while (low <= high)
{
mid = (low + high) / 2;
int temp = array[mid];
if (temp == key)
return mid;
else if (temp > key)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
int main()
{
int array[N], key;
printf("输入升序排列的%d个数:", N);
for (int index = 0; index < N; ++index)
scanf("%d", &array[index]);
printf("输入查找数:");
scanf("%d", &key);
int index = bsearch(array, N, key);
if (index >= 0)
printf("array[%d] = %d\n", index, array[index]);
else
puts("不存在查找数");
return 0;
}
#include <stdio.h>
#define N 9
void sort(int *array, int num)
{
for (int i = num - 1; i > 0; --i)
for (int j = 0; j < i; ++j)
if (array[j] > array[j + 1])
{
int temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
int main()
{
int array[N], x;
printf("%d\n", N);
for (int i = 0; i < N; ++i)
scanf("%d", &array[i]);
sort(array, N);
for (int i = 0; i < N; ++i)
printf("%d ", array[i]);
return 0;
}
#include <stdbool.h>
#include <stdio.h>
bool leapYear(int year)
{
return year % 4 == 0 && year % 100 != 0 \
|| year % 400 == 0;
}
int main()
{
int year;
printf("ݣ");
scanf("%d", &year);
if (leapYear(year))
puts("");
else
puts("ƽ");
return 0;
}
#include <stdbool.h>
#include <stdio.h>
bool leapYear(int year)
{
return year % 4 == 0 && year % 100 != 0 \
|| year % 400 == 0;
}
const unsigned dayForMonth[12] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 };
unsigned days(int year, unsigned month, unsigned day)
{
int days = 0;
for (int index = 1; index < month; ++index)
days += dayForMonth[index];
days += day;
if (month >= 3 && leapYear(year))
++days;
return days;
}
int main()
{
int year;
unsigned month, day;
printf("输入年月日,以斜线、中横线或者空格间隔:");
if (scanf("%d%*c%u%*c%u", &year, &month, &day) == 3)
printf("这天是此年第%u天\n", days(year, month, day));
return 0;
}
#include <stdio.h>
//// ݹ
//unsigned permutate(unsigned n, unsigned m)
//{
// if (m > n)
// return 0;
// else if (m == 0)
// return 1;
// else if (m == 1)
// return n;
// else
// return n * permutate(n - 1, m - 1);
//}
//
unsigned permutate(unsigned n, unsigned m)
{
if (m > n)
return 0;
unsigned result = 1;
for (; m > 0; --m, --n)
result *= n;
return result;
}
int main()
{
unsigned m, n;
printf("n = ");
scanf("%u", &n);
printf("m = ");
scanf("%u", &m);
printf("A(%u,%u) = %u\n", n, m, permutate(n, m));
return 0;
}
/*
* 枚举法又称为穷举法,先列举所有可能出现的情况,再一一进行测试,找出符合条件的所有结果。
* 例如计算一个古典数学问题——“百钱买百鸡”。
* 一百个铜钱买一百只鸡,公鸡每只5钱,母鸡每只3钱,小鸡3只1钱,问公鸡、母鸡和小鸡各买几只?
* 假设公鸡x只,母鸡y只,小鸡z只。根据题意可列出以下方程组:
* 5x+3y+z/3=100(百钱);x+y+z=100(百鸡)
* 由于2个方程式之中有3个未知数,属于无法直接求解的不定方程,故采用“枚举法”进行试根。
* 此处x、y、z均为正整数,且z是3的倍数;由于鸡和钱的总数都是100,可以确定x,y,z的取值范围:
* x的取值范围为[1,20]
* y的取值范围为[1,33]
* z的取值范围为[3,99],步长为3
* 逐一测试各种可能的x、y、z组合,并输出符合条件者。
*/
#include <stdio.h>
int main()
{
for (int x = 1, y, z; x <= 20; ++x)
for (y = 1; y <= 33; ++y)
{
z = 100 - x - y;
if (z % 3 == 0 \
&& 5 * x + 3 * y + z / 3 == 100)
printf("公鸡%d只,母鸡%d只,小鸡%d只\n", x, y, z);
}
return 0;
}
#include <stdio.h>
unsigned combine(unsigned, unsigned);
int main()
{
unsigned m, n;
printf("n = ");
scanf("%u", &n);
printf("m = ");
scanf("%u", &m);
printf("C(%u,%u) = %u\n", n, m, combine(n, m));
return 0;
}
unsigned permutate(unsigned n, unsigned m)
{
if (m > n)
return 0;
unsigned result = 1;
for (; m > 0; --m, --n)
result *= n;
return result;
}
unsigned factorial(unsigned n)
{
unsigned result = 1;
for (; n > 1; --n)
result *= n;
return result;
}
unsigned combine(unsigned n, unsigned m)
{
return permutate(n, m) / factorial(m);
}
#include <stdio.h>
//// 递归
//unsigned factorial(unsigned n)
//{
// if (n <= 1)
// return 1;
// else
// return factorial(n - 1)*n;
//}
// 递推
unsigned factorial(unsigned n)
{
unsigned result = 1;
for (; n > 1; --n)
result *= n;
return result;
}
int main()
{
printf("输入一个正整数:");
unsigned x;
scanf("%u", &x);
printf("%u! = %u\n", x, factorial(x));
return 0;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册