比较有趣的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);
    return 0;
}

 

2.判断一个日期是周几,有两种算法

 1 #include "stdio.h"
 2 int day_of_week1(int y, int m, int d)//使用的是蔡勒公式
 3 {
 4     if (m < 3)
 5     {
 6         y--;
 7         m += 12;
 8     }
 9     return ((y%100+(y%100)/4+(y/100)/4-2*(y/100)+(26*(m+1))/10+d-1)%7);
10 }
11 
12 int day_of_week2(int y, int m, int d)//
13 {
14     int t[12]={0,3,2,5,0,3,5,1,4,6,2,4};
15     y -= m < 3;
16     return ((y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7);
17 }
18 
19 int main(void)
20 {
21     int 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);
27     return 0;
28 }

 

3.打印心代码(转自一位不知道的大佬)

 1 #include "stdio.h"
 2 #include <math.h>
 3 float f(float x, float y, float z)
 4 {
 5     float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
 6     return a * a * a - x * x * z * z *z- 9.0f / 80.0f * y * y * z * z * z;
 7 }
 8 float h(float x, float z)
 9 {
10     for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
11         if (f(x, y, z) <= 0.0f)
12             return y;
13     return 0.0f;
14 }
15 int main(void)
16 {
17     for (float z = 1.5f; z > -1.5f; z -= 0.05f)
18     {
19         for (float x = -1.5f; x < 1.5f; x+= 0.025f)
20         {
21             float v = f(x, 0.0f, z);
22             if(v <= 0.0f)
23             {
24                 float y0 = h(x,z);
25                 float ny = 0.01f;
26                 float nx = h(x + ny, z) - y0;
27                 float nz = h(x, z+ny) - y0;
28                 float nd = 1.0f /sqrtf(nx*nx + ny*ny + nz*nz); 
29                 float d = (nx+ny-nz) * nd *0.5f + 0.5f30                 putchar(".:-=+*#%@"[(int)(d*5.0f)]);
31             }
32             else 
33                 putchar(' ');
34         }
35         putchar(' ');
36     }
37     for (int i = 5; i >0;){}
38 }

 

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});
    return 0;
}

 

posted @ 2020-11-25 12:40  594C  阅读(1006)  评论(0编辑  收藏  举报