⽐较有趣的C语⾔⼩程序1.判断是否是闰年:
#include <stdio.h>
int main(void)
{
int year, a;
printf("Please enter the year:\n");
scanf("%d",&year);
if (year % 400 == 0)
a = 1;
else
{
if (year % 4 == 0 && year % 100 != 0)
a = 1;
else
a = 0;
}
if (a == 1)
printf("%d is a leap year\n", year);
else
printf("%d not a leap year\n", year);
return0;
}
2.判断⼀个⽇期是周⼏,有两种算法
1 #include "stdio.h"
2int day_of_week1(int y, int m, int d)//使⽤的是蔡勒公式
3 {
4if (m < 3)
5 {
6 y--;
7 m += 12;
8 }
9return ((y%100+(y%100)/4+(y/100)/4-2*(y/100)+(26*(m+1))/10+d-1)%7);
10 }
11
12int day_of_week2(int y, int m, int d)//
13 {
14int t[12]={0,3,2,5,0,3,5,1,4,6,2,4};
15 y -= m < 3;
16return ((y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7);
17 }
18
19int main(void)
20 {
21int a = 3, y=2020, m=1, d=1, w1 = 0,w2;
22 printf("%d-%d-%d\n", y, m, d);
23 w1 = day_of_week1(y, m, d);
24 printf("星期%d", w);
25 w2 = day_of_week2(y, m, d);
26 printf("星期%d", w);
27return0;
28 }
3.打印⼼代码(转⾃⼀位不知道的⼤佬)
1 #include "stdio.h"
2 #include <math.h>
3float f(float x, float y, float z)
4 {
5float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
6return a * a * a - x * x * z * z *z- 9.0f / 80.0f * y * y * z * z * z;
7 }
8float h(float x, float z)
9 {
10for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
11if (f(x, y, z) <= 0.0f)
12return y;
13return0.0f;
14 }
15int main(void)
16 {
17for (float z = 1.5f; z > -1.5f; z -= 0.05f)
18 {
19for (float x = -1.5f; x < 1.5f; x+= 0.025f)
20 {
21float v = f(x, 0.0f, z);
22if(v <= 0.0f)
23 {
24float y0 = h(x,z);
25float ny = 0.01f;
26float nx = h(x + ny, z) - y0;
27float nz = h(x, z+ny) - y0;
28float nd = 1.0f /sqrtf(nx*nx + ny*ny + nz*nz);
29float d = (nx+ny-nz) * nd *0.5f + 0.5f;
30 putchar(".:-=+*#%@"[(int)(d*5.0f)]);
31 }
32else
33 putchar(' ');
34 }
35 putchar(' ');
36 }
37for (int i = 5; i >0;){}
38 }
有趣的java小程序4.这个建议⾃⼰试试,很好玩。
#include <stdio.h>
#include <stdlib.h>
#define decode(p,r,i,n,t,f) r##f##r##i##t##p
#define puts decode(m,s,t,o,e,y)
int main()
{
puts((char*)(const double[]){1.3553894309652565e+224,6.952439113111912e-308}); return0;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论