C语言基础题

1.闰年问题

  例 :输入年、月判断该月的天数。

 1 #include<stdio.h>
 2 int a[2][6] = {31,28,31,30,31,30,31,31,30,31,30,31};
 3 int main()
 4 {
 5     int p1,p2;    
 6     int *p;
 7     p=&a[0][0];
 8     printf("请依次输入年、月:
");
 9     printf("请输入年份:");
10     scanf("%d",&p1);
11     printf("请输入月份:");
12     scanf("%d",&p2);
13     if(p1%4==0&&p1%100!=0||p1%400==0)
14     {
15         *(p+1)=29;
16     }
17     if(p2 > 12 )
18     {
19         printf("月份次数超限!
");
20     }
21     printf("这个月的天数为:%d
",*(p+p2-1));
22     return 0;
23 
24 }

2.素数问题

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int i,input,flag=0;
 5     printf("请输入一个数
");
 6     scanf("%d",&input);
 7     for(i=2;i<input;i++)
 8     {
 9         if(input%i==0)
10         {
11             flag=1;
12             break;
13         }
14     }
15     if(flag==0)
16             printf("%d是素数
",input);
17     else
18         printf("%d不是素数
",input);    
19 }

3.斐波那契数列问题

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int i,x1,x2,x,input;
 5     printf("你想输出多少个斐波那契数?
");
 6     scanf("%d",&input);
 7     x1=1;
 8     x2=1;
 9     printf("%-5d%-5d",x1,x2);
10     for(i=3;i<=input;i++)
11     {
12         x=x1+x2;
13         printf("%-5d",x);
14         x2=x1;
15         x1=x;
16     }
17     printf("
");
18     return 0;
19 }

4.水仙花数问题

例:输出所有三位的水仙花数。

 1 #include<stdio.h>
 2 #define    SUM a*a*a+b*b*b+c*c*c
 3 int main()
 4 {
 5     int i,a,b,c;
 6     for(i=100;i<1000;i++)
 7     {
 8         a=i/100;
 9         b=i/10%10;
10         c=i%10;
11         if(SUM==i)
12             printf("%d是水仙花数!
",i);
13     }
14 }

5.冒泡排序问题

例:输入十个成绩,对成绩进行排序。

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int data[10],i,j,t;
 5     printf("请输入10个成绩:
");
 6     for(i=0;i<10;i++)
 7     {
 8         scanf("%d",&data[i]);
 9     }
10     for(i=0;i<9;i++)
11     {
12         for(j=0;j<9-i;j++)
13         {
14             if(data[j]>data[j+1])
15             {
16                 t=data[j];
17                 data[j]=data[j+1];
18                 data[j+1]=t;
19             }
20         }
21     }
22     printf("排序后的成绩为:
");
23     for(i=0;i<10;i++)
24     {
25         printf("%-3d",data[i]);
26     }
27     printf("
");
28     return 0;
29 }

 6. 链表的创建与输入

/* 创建一个链表,用来动态添加学生的信息*/
#include<stdio.h>
#include<stdlib.h>
/* 定义学生结构体 */
struct stunode
{
    char no[20];
    char name[10];
    int score;
    struct stunode *next;
};

/* 定义头结点 */
struct stunode head;

int main()
{
    int i,n;
    /* 定义链表操作所需要的指针变量 */
    struct stunode *p;
    /* 定义释放链表结构体指针变量 */
    struct stunode *freep;
    /* 定义指向链表尾节点的指针变量 */
    struct stunode *tail;
    /* 将头结点的成员next指向NULL */
    head.next=NULL;
    /* 初始建立链表tail指向头结点head */
    tail = &head;
    
    printf("请输入学生的人数:
");
    scanf("%d",&n);
    printf("请输入%d个学生的成绩",n);
    for(i=0;i<n;i++)
    {
        p=(struct stunode *)malloc(sizeof(struct stunode));
        printf("请输入学号");
        scanf("%s",p->no);
        printf("请输入姓名");
        scanf("%s",p->name);
        printf("请输入成绩");
        scanf("%d",&p->score);
        tail->next=p;
        p->next=NULL;
        tail=p;
        
    }
    printf("输入的学生信息为:
");
    /* 输出 并 释放链表 */
    p=head.next;
    while(p!=NULL)
    {
        printf("%s %s %d",p->no,p->name,p->score);
        freep = p;
        p=p->next;
        free(freep);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/battlecry/p/6103516.html