基础练习01
2018-03-23 09:38
183 查看
1. 打印100~200 之间的素数
//方法1
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i=0;
for (i = 100; i < 200; i++)
{
int j = 0;
int count = 0;
for (j = 2; j < i/2; j++)
{
if (i%j == 0)
{
break;
}
}
if (j == i)
{
printf("%5d", i);
count++;
}
}
printf("\ncount=%d\n", count);
system("pause");
return 0;
}
//方法2
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int i = 0;
//计算素数的个数
int count = 0;
//循环判断素数,排除所有偶数缩小范围
for (i = 101; i < 200; i += 2)
{
int j = 0;
//将数字与它开平方以内的数字求余缩小范围
for (j = 2; j <= sqrt(i); j++)
{
//求余为0,则不为素数,跳出循环
if (i%j == 0)
{
break;
}
}
if (j > sqrt(i))
{
printf("%5d", i);
count++;
}
}
printf("\ncount=%d\n", count);
system("pause");
return 0;
}
2. 输出乘法口诀表
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, j, chengji;
for (i = 0; i <= 9; i++)
{
for (j = 1; j <= i; j++)
{
chengji = i*j;
printf("%d*%d=%-5d", i, j, chengji);
}
printf("\n");
}
system("pause");
return 0;
}
3. 判断1000年-- - 2000年之间的闰年
//方法一
#include<stdio.h>
#include<stdlib.h>
int main()
{
int y;
for (y = 1000; y <= 2000; y++)
{
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
{
printf("%5d", y);
}
}
system("pause");
return 0;
}
//方法二
#include<stdio.h>
#include<stdlib.h>
int IsLeapYear(int y)
{
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
return 1;
else
return 0;
}
int main()
{
int i;
for (i = 1000; i <= 2000; i++)
{
if (IsLeapYear(i))
{
printf("%5d", i);
}
}
system("pause");
return 0;
}
//方法1
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i=0;
for (i = 100; i < 200; i++)
{
int j = 0;
int count = 0;
for (j = 2; j < i/2; j++)
{
if (i%j == 0)
{
break;
}
}
if (j == i)
{
printf("%5d", i);
count++;
}
}
printf("\ncount=%d\n", count);
system("pause");
return 0;
}
//方法2
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int i = 0;
//计算素数的个数
int count = 0;
//循环判断素数,排除所有偶数缩小范围
for (i = 101; i < 200; i += 2)
{
int j = 0;
//将数字与它开平方以内的数字求余缩小范围
for (j = 2; j <= sqrt(i); j++)
{
//求余为0,则不为素数,跳出循环
if (i%j == 0)
{
break;
}
}
if (j > sqrt(i))
{
printf("%5d", i);
count++;
}
}
printf("\ncount=%d\n", count);
system("pause");
return 0;
}
2. 输出乘法口诀表
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, j, chengji;
for (i = 0; i <= 9; i++)
{
for (j = 1; j <= i; j++)
{
chengji = i*j;
printf("%d*%d=%-5d", i, j, chengji);
}
printf("\n");
}
system("pause");
return 0;
}
3. 判断1000年-- - 2000年之间的闰年
//方法一
#include<stdio.h>
#include<stdlib.h>
int main()
{
int y;
for (y = 1000; y <= 2000; y++)
{
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
{
printf("%5d", y);
}
}
system("pause");
return 0;
}
//方法二
#include<stdio.h>
#include<stdlib.h>
int IsLeapYear(int y)
{
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
return 1;
else
return 0;
}
int main()
{
int i;
for (i = 1000; i <= 2000; i++)
{
if (IsLeapYear(i))
{
printf("%5d", i);
}
}
system("pause");
return 0;
}
相关文章推荐
- 算法-蓝桥杯-基础练习 01字串
- 基础练习 01字串
- 蓝桥杯:基础练习 01字串【JAVA算法实现】
- 基础练习 01字串
- 黑马程序员--02.Java语言基础--01.赋值语句练习2【个人总结】
- 基础练习 01字串
- (蓝桥杯)基础练习 01字串
- 第五届蓝桥杯——基础练习:01字串
- 蓝桥杯 基础练习 01字串
- 蓝桥杯 基础练习 01字串
- 蓝桥杯基础练习 01字串
- “蓝桥杯”基础练习:01字串
- 基础练习 01字串
- 蓝桥杯-01字串(基础练习)
- “蓝桥杯”基础练习:01字串
- 基础练习 01字串
- 蓝桥杯习题集_ 基础练习 01字串
- 蓝桥杯基础练习 01字串
- 基础练习2_01字串
- 基础练习 01字串(二进制)