#include<string.h>#define_CRT_SECURE_NO_WARNINGSvoidInputKeyWords(){char psw[10]="";int i =0;for(i =0; i <3; i++){//输入密码printf("please input:\n");scanf("%s",psw);//判断密码是否正确if(strcmp(psw,"password")==0)break;}if(i ==3){printf("exit\n");}else{printf("log in\n");}}
将三个数按从小到大输出。
//交换2个数voidexchange(int x,int y){int tmp = x;
x = y;
y = tmp;}voidMaxNum(int a,int b,int c){printf("a = %d b = %d c = %d \n", a, b, c);//比较a和b的大小,大的往后移if(a > b){int tmp = a;
a = b;
b = tmp;}if(a > c){int tmp = a;
a = c;
c = tmp;}if(b > c){int tmp = b;
b = c;
c = tmp;}printf("a = %d b = %d c = %d", a, b,c);}//三目运算符找出三个数中的最大值voidMaxNum(int a,int b,int c){printf("a = %d b = %d c = %d \n", a, b, c);int max_num =0;//将较大的数存储在临时变量
max_num =(a > b ? a : b)> c ?(a > b ? a : b): c;}
两个数的最大公约数
voidGongYueShu01(int x,int y){//24,18最大不超过18//找出2个数的最小值,假设最大公约数的最大值为2个数的最小值。int max =0;
max =(x < y)? x : y;while(1){//如果最大的公约数能整除2个数,直接找到,否则往下找if(x % max ==0&& y % max ==0){printf("最大公约数 = %d\n", max);break;}
max--;}}voidGongYueShu02(int x,int y){//相当于辗转相除法int t =0;while(t = x % y){
x = y;
y = t;}printf("最大公约数 = %d\n", y);}
打印100~200之间的素数。
#include<stdio.h>#include<math.h>#define_CRT_SECURE_NO_WARNINGSvoidSuShu(){int i =0;int count =0;for(i =100; i <=200; i++){//判断i是不是素数//从2->i-1之间的数去试除,看能不能整除//优化m = a*b a和b中至少有一个数字小于等于开平方m;//从2->开平方i-1之间的数去试除,看能不能整除int j =0;int flag =1;//假设i是素数的标记for(j =2; j <=sqrt(i); j++){if(i % j ==0){
flag =0;break;}}if(flag ==1){
count++;printf("%d ", i);}}printf("\n count = %d\n", count);}
判断1000年—2000年之间的闰年
#include<stdio.h>voidRunNian(){int year =0;for(year =1000; year <=2000; year++){//判断闰年的两种方法:1,能被4整除,不能被100整除是闰年;//2.能被400整除是闰年if(((year %4==0)&&(year %100!=0))||(year %400==0)){printf("%d ",year);}}}