Nanjing Normal University re-examination ten training program

91. The establishment of a dynamic array, student performance input 5, wherein further with a function to check whether or below 60, and outputs the results failed

#include <stdio.h>
#include <stdlib.h>

void check(int *p)    //打印成绩<60的
{
    int i;
    for(i=0; i<5; i++)
        if(*(p+i)<60)
            printf("%d ",*(p+i));
    printf("\n");
}

int main()
{
    int *p1,i;
    p1=(int *)malloc(5*sizeof(int));  //动态开辟空间
    for(i=0; i<5; i++)
        scanf("%d",p1+i);  //p1+i为地址
    check(p1);
    return 0;
}

operation result:

92. Verify: Goldbach conjecture: an even number of not less than 6 can be expressed as the sum of two primes. Example: 34 = 3 + 31

#include <stdio.h>
#include <math.h>
int prime(int m)  //判断是否为素数
{
    int i,k;
    k=sqrt(m);
    for(i=2; i<=k; i++)
        if(m%i==0)
            break;
    if(i>k)
        return 1;
    else
        return 0;
}

void godbaha(int n)
{
    int a,b;
    for(a=3; a<=n/2; a+=2)   //a为第一个素数,一定不大于n/2
    {
        if(prime(a))
        {
            b=n-a;       //判断第二个是否为素数
            if(prime(b))
                printf("%d=%d+%d\n",n,a,b);
        }
    }
}

int main()
{
    int number;
    printf("输入一个不小于6的偶数:");
    scanf("%d",&number);
    godbaha(number);
    printf("\n");
    return 0;
}

operation result:

93. Enter the 10 student's name, student number and scores, failing which the person's name, student number and output results

#include <stdio.h>

int main()
{
    char name[10][10];   //姓名
    int no[10];       //学号
    float score[10];   //成绩
    int i;
    for(i=0; i<10; i++) //三者通过数组下标相联系
        scanf("%s %d %f",name[i],&no[i],&score[i]);
    printf("打印成绩不及格的学生:\n");
    for(i=0; i<10; i++)
        if(score[i]<60)
            printf("%s %d %.2f\n",name[i],no[i],score[i]);

    return 0;
}

operation result:

94. The definition of the structure, membership information Print

#include <stdio.h>

struct Student
{
    long int num;   //学号
    char name[20]; //姓名
    char sex;   //性别
    char addr[30];  //地址
} s1= {10101,"Li Lin",'M',"123 Beijing Road"};
int main()
{
    printf("No:%ld\nname:%s\nsex:%c\naddress:%s\n",s1.num,s1.name,s1.sex,s1.addr);  //输出
    return 0;
}

operation result:

95. There are three candidates, each voter can only vote one person, compile a program, get voting results

#include <stdio.h>
#include <string.h>
struct Person
{
    char name[30];  //姓名
    int count;  //票数
} leader[3]= {"Li",0,"Zhang",0,"Sun",0};
int main()
{
    int i,j;
    char leader_name[30];
    for(i=0; i<10; i++)   //依次输入10个票选
    {
        scanf("%s",leader_name);
        for(j=0; j<3; j++)   //输入一个判断一次
            if(strcmp(leader_name,leader[j].name)==0)  //将候选人票数加1
                leader[j].count++;
    }
    printf("Result:\n");   //输出各候选人票数
    for(i=0; i<3; i++)
        printf("%5s:%d\n",leader[i].name,leader[i].count);
    printf("\n");
    return 0;
}

operation result:

96. There are n number of students, according to results of the output level of student information

#include <stdio.h>

struct Student   //学生结构体
{
    int num;
    char name[20];
    float score;
};
int main()
{
    struct Student stu[5]= {{10101,"Zhang",78},{10102,"Wang",98.5},{10106,"Li",86},{10108,"Ling",73.5},{10110,"Sun",100}};
    struct Student temp;
    const int n=5;
    int i,j;
    for(i=0; i<n-1; i++)   //根据成绩进行冒泡排序
        for(j=0; j<n-i-1; j++)
            if(stu[j].score>stu[j+1].score)
            {
                temp=stu[j];   //结构体可以直接进行赋值
                stu[j]=stu[j+1];
                stu[j+1]=temp;
            }
    for(i=0; i<n; i++)  //输出排序后的结果
        printf("%6d %8s %6.2f\n",stu[i].num,stu[i].name,stu[i].score);
    printf("\n");
    return 0;
}

operation result:

97. There are n structure variable containing student number, name and three scores. Requirements: Output highest average score of student information

#include <stdio.h>
#define N 3
struct Student   //学生结构体
{
    int num;  //学号
    char name[20];  //姓名
    float score[3]; //三门成绩
    float aver;  //平均成绩
};

struct Student max(struct Student stu[])   //求平均成绩最高的学生
{
    int i,m=0;
    for(i=0; i<N; i++)
        if(stu[i].aver>stu[m].aver)
            m=i;
    return stu[m];
}

void input(struct Student stu[])  //输入学生信息及求平均成绩
{
    int i;
    for(i=0; i<N; i++)
    {
        scanf("%d %s %f %f %f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
        stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;
    }
}

void print(struct Student stud)  //打印学生信息
{
    printf("%d\n%s\n%5.1f\n%5.1f\n%5.1f\n%6.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver);
}

int main()
{
    struct Student stu[N],*p=stu;
    input(p);  //直接用stu相同
    printf("平均成绩最高的学生:\n");
    print(max(p));  //直接用stu相同
    return 0;
}

operation result:

98. A variable pointer to a structure

#include <stdio.h>
#include <string.h>
struct Student
{
    long num;
    char name[20];
};
int main()
{
    struct Student stu_1;
    struct Student *p;   //定义指向结构体变量的指针
    p=&stu_1;
    stu_1.num=10101;
    strcpy(stu_1.name,"Li Ming");     //下述三种方法结果相同
    printf("直接调用:\n");
    printf("No:%ld\nname:%s\n",stu_1.num,stu_1.name);
    printf("结构体变量指针调用用法:\n");
    printf("No:%ld\nname:%s\n",(*p).num,(*p).name);  //用*p调用
    printf("No:%ld\nname:%s\n",p->num,p->name);   //用p->调用

    return 0;
}

operation result:

99. The structure of the variable pointers Usage

#include <stdio.h>

struct Student
{
    long num;
    char name[20];
};
int main()
{
    struct Student stu[3]= {{10101,"Li Min"},{10102,"Zhang Zi"},{10103,"Wang Min"}};
    struct Student *p;
    for(p=stu; p<stu+3; p++)   //和普通数组相同用法
        printf("%5ld %-20s\n",p->num,p->name);

    return 0;
}

operation result:

100. write a function to establish a three-way dynamic list of student data

#include <stdio.h>
#include <stdlib.h>    //malloc
struct Student
{
    long num;
    float score;
    struct Student *next;
};//定义学生结构体

struct Student * Create(void)  //创建学生链表
{
    struct Student *head=NULL,*p1,*p2;
    p1=p2=(struct Student *)malloc(sizeof(struct Student));
    while(scanf("%ld,%f",&p1->num,&p1->score)&&p1->num!=0&&p1->score!=0)   //输入学生信息直到输入"0,0"
    {
        if(head==NULL)    //一开始链表为空
            head=p1;
        else    //链表不为空
            p2->next=p1;
        p2=p1;   //p2为循环指针
        p1=(struct Student *)malloc(sizeof(struct Student));
    }
    p2->next=NULL;
    return head;
}

void print(struct Student *head)  //打印学生链表
{
    struct Student *p;
    p=head;
    if(p!=NULL)
        while(p!=NULL)
        {
            printf("%ld %5.1f\n",p->num,p->score);
            p=p->next;
        }
}
int main()
{
    struct Student *pt;
    pt=Create();
    printf("输出学生数据:\n");
    print(pt);
    return 0;
}

operation result:

Published 462 original articles · won praise 55 · views 320 000 +

Guess you like

Origin blog.csdn.net/LY_624/article/details/105030562