目录
-
d对比两个数找出最大值
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
Max(int x, int y) //定义函数
{
if (x > y)
return x;
else
return y;
}
int main()
{
int num1 = 10;
int num2 = 20;
int max = 0;
max = Max(num1, num2);
printf("max=%d\n", max);
return 0;
}
计算n的阶乘
//n的阶乘
int main()
{
int n = 0;
int i = 0;
int ret = 1;
printf("计算n的阶乘,n=");
scanf("计算n的阶乘,%d", &n);
for (i= 1; i < n + 1; i++)
{
ret = i*ret;
}
printf("n的阶乘为:%d\n", ret);
return 0;
}
排序算法
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
#include<Windows.h>
#include<stdlib.h>
//range from large to small number
int main()
{
int a = 0;
int b = 0;
int c = 0;
int temp= 0;
printf("please enter 3 numbers:");//scanf中%d前面不能跟字符,会出错
scanf("%d%d%d", &a, &b, &c);//需要另外写入printf
if (a < b)
{
temp = a;
a = b;
b = temp;
}
if (a < c)
{
temp = a;
a = c;
c = temp;
}
if (b < c)
{
temp = b;
b = c;
c = temp;
}
printf("排序结果为:%d %d %d\n", a, b, c);
return 0;
}
打印3的倍数
int main()
{
int i = 0;
for (i = 1; i < 101; i++)
{
if (i % 3 == 0)
{
printf("%d\n", i);
}
}
return 0;
}
求最大公因数--辗转相除法
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
#include<Windows.h>
#include<stdlib.h>
//辗转相除法
int main()
{
int m = 24;
int n = 18;
int r = 0;
printf("please enter 2 numbers:\n");
scanf("%d%d",&m,&n);
while (m%n)
{
r = m%n;
m = n;
n = r;
}
printf("%d\n", n);
return 0;
}
打印闰年
//打印闰年
int main()
{
int year = 0;
int count = 0;
for(year = 1000; year < 2001;year++)
{
//judge year is 闰年:能被4整除且不能被100整除
//或者能被400整除是闰年
if (year % 4 == 0 && year % 100 != 0)
{
printf("%d ", year);
count++;
}
else if (year % 400 == 0)
{
printf("%d ", year);
count++;
}
}
printf("\n闰年个数=%d", count);
return 0;
}
int main()
{
int year = 0;
int count = 0;
for (year = 1000; year < 2001; year++)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
printf("%d ", year);
count++;
}
}
printf("\n闰年个数=%d", count);
return 0;
}
判断是否为素数
方法:1.试除法:产生2到i-1的数字
//打印素数
int main()
{
int i = 0;
int j = 0;
int count = 0;
for (i = 100; i <= 200; i++)
{
//试除法
for (j = 2; j < i; j++)
{
if (i%j == 0)
{
break;
}
}
if (j == i)
{
printf("%d ", i);
count++;
}
}
printf("\n一共有素数%d个", count);
return 0;
}
试除法的优化:
1.如果i能够携程的形式(即不是素数),则a和b之中至少有一个数字小于等于
则试除的数无须从2试到
2.偶数不可能为素数
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
#include<Windows.h>
#include<stdlib.h>
#include<math.h>//为了使用sqrt
//打印素数
int main()
{
int i = 0;
int j = 0;
int count = 0;
for (i = 101; i <= 200; i+=2)
{
//试除法
for (j = 2; j<=sqrt(i); j++)
{
if (i%j == 0)
{
break;
}
}
if (j > sqrt(i))
{
printf("%d ", i);
count++;
}
}
printf("\n一共有素数%d个", count);
return 0;
}
计算1/1-1/2+1/3-1/4+……
int main()
{
int i = 0;
int flag = 1;
double sum = 0.0;
for (i = 1; i <= 100; i++)
{
sum += flag*1.0 / i;
flag = -flag;
}
printf("%lf\n", sum);
return 0;
}
找出数组的最大值
//求最大值
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int max = arr[0];//不能赋max初值为0;否则如果数组均为负数,则最大的数则为零
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < sz; i++)
{
if (arr[i]>max)
{
max = arr[i];
}
}
printf("max=%d\n", max);
return 0;
}
在屏幕上输出9*9乘法口诀表
//在屏幕上输出9*9乘法口诀表
int main()
{
int i = 0;
for (i = 1; i <= 9; i++)
{
int j = 1;
for (j = 1; j <= i; j++)
{
printf("%d*%d=%2d ", i, j, i*j);//%2d为右对齐;%-2d为左对齐
}
printf("\n");
}
return 0;
}
二分查找
//half finding
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int k = 20;
int sz = sizeof(arr) / sizeof(arr[0]);//calculate the number of element in array
int left = 0;//the left index start from zero
int right = sz - 1;
while (left<=right)
{
int mid = (left + right) / 2;
if (arr[mid] > k)
{
right = mid - 1;
}
else if (arr[mid] < k)
{
left = mid + 1;
}
else
{
printf("the number have been found,the index is:%d\n", mid);
break;
}
}
if (left>right)
{
printf("The number can not be found.\n");
}
return 0;
}
猜数字小游戏
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
#include<Windows.h>
#include<stdlib.h>
#include<math.h>//为了使用sqrt
#include<stdlib.h>//为了使用随机生成
#include<time.h>//使用时间戳
//猜数字游戏
//1.电脑生成随机数
//2.猜数字
void menu()//菜单函数
{
printf("**********************************\n");
printf("******** 1.play 0.exit ******\n");
printf("**********************************\n");
}
//RAND_MAX生成0-32767之间
void game()//游戏函数
{
//生成随机数
int ret = 0;
int guess = 0;
ret = rand() % 100+1;//生成1-100
//printf("%d\n",ret);
while (1)
{
printf("please guess the number:>");
scanf("%d", &guess);
if (guess > ret)
{
printf("猜大了\n");
}
else if (guess < ret)
{
printf("猜小了\n");
}
else
{
printf("恭喜你,猜对了\n");
break;
}
}
}
int main()
{
int input = 0;
//srand是用来设置一个随机系列
srand((unsigned int)time(NULL));//时间戳来设置随机数的生成起点
//time_t time(time_t*timer)
//time_t
do
{
menu();//菜单函数
printf("请选择>:");
scanf("%d", &input);
switch (input)
{
case 1:
game();//函数,猜数字游戏
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);//如果input为1/非零数字,则为真,就会再循环一次
return 0;
}