华科C语言程序设计头歌代码-上(22级)

C语言程序设计实验课编程作业

  第1关 2-1 :字母大小写

#include <stdio.h>
 
 int main() 
 {
    char word;
    word!='\0';
    while((word=getchar())!=EOF){
      
       
       putchar(word>='A'&&word<='Z'?word+=32:word);
    }
    
     return 0;
 }

 第2关 2-2 :位运算

#include <stdio.h>
 
 int main(void)
 {
    unsigned short x,m,n;
    scanf("%x %hu %hu",&x,&m,&n);
    if((m<=15&&m>=0)&&(n>=1&&n<=16-m)){
    x=x>>m;
    x=x<<(16-n);
    printf("%x",x);
    }
    else printf("error\n");
     return 0;
 }

第3关 实验2-3:IP地址

int main()
 {
     unsigned long w,h,g,j,k;
     while(scanf("%lu",&w)!=EOF){
     h=4278190080;
     g=16711680;
     j=65280;
     k=255;
     h=(w&h)>>24;
     g=(w&g)>>16;
     j=(w&j)>>8;
     k=w&k;
     printf("%d.%d.%d.%d\n",h,g,j,k);
     }
     return 0;
 }

  第4关 实验3-1:空格合并 

 #include <stdio.h>
enum { COPY, DELETE };
int main()
{
	int state = COPY;
	char a;
	while ((a = getchar()) != EOF) {
		switch (state) {
		case COPY:if (a == ' ') {
			putchar(a);
			state = DELETE;
			break;
		}
		else putchar(a);
			state = COPY;
			break;
		case DELETE:if (a == ' ') {
			
			break;
		}
		else 
			putchar(a);
			state = COPY;
			break;
		}
		
	}
	return 0;
}

第5关 实验3-2 :杨辉三角形

 #include <stdio.h>
 
 int main()
{
	int max();
	int i, j, n,q;
	int a[9][9]={};
	scanf("%d", &n);
	for (i = 1; i <= n+1; i++) {
		for (j = 1; j <= i; j++) {
			if (1 == j || j == i) {
				a[i][j] = 1;
			}
		}

	}
	for (i = 3; i <= n+1; i++) {
		for (j = 2; j <= i; j++) {
			a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
		}

	}
	    
        
        
        
        for (i = 1; i <= n+1; i++) {
		
        for(q=1;q<3*(n+1)-i*2;q++){
            printf(" ");
        }
        
        
        
        for (j = 1; j <=i; j++) {
			
            
            printf("%-4d", a[i][j]);
			
        }
        
        if(i==n+1){
            break;
        }
	    printf("\n");
        
	    }
         return 0;
}

第6关 实验3-3:特别数   

#include <stdio.h>
 
 int main(void)
 {
     int i=100,k;
     do{
        k=((i*i)/1000)*1000;
        if((i*i-k)==i){
            printf("%d\n",i);
        }
        i++;
     }while(i<=999);
     return 0;
 }

第7关 实验4-1:歌德巴赫猜想

#include <stdio.h>
int max(int n) {
    int x, y, t,flag,a;
    if (n == 4)flag = 2;
    else if (n != 4) {
        for (x = 2; x < n; x++) {
            for (t = 2; t < x - 1; t++) {
                if (x % t == 0)break;

            }
            for (y = n - x, a = 2; a < y - 1; a++) {
                if (y % a == 0)break;
            }
            if (x % t != 0 && y % a != 0 && x < y) {
                flag = x;
                break;
            }
        }
    }
    return flag;
}
int main(void)
{
    int n;
    while(scanf("%d",&n)!=EOF){
    
    printf("%d=%d+%d\n",n,max(n),n-max(n));
    }
    return 0;
}

第8关 实验4-2:完全数

#include <stdio.h>
void shit(int q) {
    int i,sum,n,flag,j;
    for(n=4; n<q;n++) {
      for (i = 2, sum = 1; i <=n / 2; i++) {
            if (n % i == 0) {
                sum += i;
            }
            if (sum == n && i == n / 2) {
                flag=n;
                printf("%d=", flag);
                for (j = 1; j <= n / 2; j++) {
                    if (n % j == 0){
                        if(j!=n/2){
                        printf("%d+", j);
                        }
                        else if(j==n/2){
                            printf("%d",j);
                        }
                    }
                }
                printf("\n");
            }
      }
    }
   

}
int main() 
{
    int q=10000;
   
    shit(q);
    return 0;
}

第9关 实验4-3:自幂数

#include <stdio.h>
#include <math.h>


int main()
{
    int k;
    while (scanf("%d", &k) != EOF) {
        int i = 1;
        int h, q ;
        int j, l, a, sum;
        if(k==0){
            return 0;
        }
        else if (k == 3)printf("3位的水仙花数共有");
        else if (k == 4)printf("4位的四叶玫瑰数共有");
        else if (k == 5)printf("5位的五角星数共有");
        else if (k == 6)printf("6位的六合数共有");
        else if (k == 7)printf("7位的北斗星数共有");
        else if (k == 8)printf("8位的八仙数共有");
        i = pow(10, k - 1);
        j = 10 * i - 1;
        for (q=0,l = i; l < j; l++) {
            for (sum = 0, a = l; a > 0; a /= 10) {
                h = pow((a % 10), k );
                sum += h;
                if ((sum == l) &&( a<10) ){
                    q = q + 1;
                }
            }
        }
        printf("%d个", q);
          for (l = i; l < j; l++) {
            for (sum = 0, a = l; a > 0; a /= 10) {
                h = pow((a % 10), k);
                sum += h;
                if ((a < 10) && (sum == l)) {
                    printf("%d", l);
                    if (q >=1) {
                        if(q==1){
                            printf("\n");
                            break;
                        }
                        printf(",");
                        q--;
                    }
                }
            }

        }
    }
    printf("\n");
}

 第10关 实验4-4:三角形面积

#include <stdio.h>
#include <math.h>
#define S(s,a,b,c) s=(a+b+c)/2
#define AREA(s,a,b,c) s*(s-a)*(s-b)*(s-c)


    int main()
    {
        float s, a, b, c,area;
        scanf("%f %f %f", &a, &b, &c);
        s = S(s, a, b, c);
        area = pow(AREA(s, a, b, c),0.5);
        printf("s=%.2f\n", S(s, a, b, c));
        printf("area=%.2f",area);
        return 0;
    }

第11关 实验5-1:整数二进制表示 

#include <stdio.h>

int main() {
	int n, a = 0;
	int temp;
	unsigned int i[32];
	scanf("%d", &n);
	if (n > 0) {
		do {
			temp = n % 2;
			n /= 2;
			i[a++] = temp;
		} while (n != 0);
		for (int q = 32 - a; q > 0; q--) {
			printf("0");
		}
		for (int j = a - 1; j >= 0; j--) {
			printf("%d", i[j]);
		}
	}
	else if (n < 0) {
		int k = -n;
		do {
			temp = (k) % 2;
			k/= 2;
			i[a++] = temp;
		} while (k!= 0);
		for (int q = 0; q < a; q++) {
			if (i[q] == 0)i[q] = 1;
			else if(i[q] == 1)i[q] = 0;
		}
		if (i[0] == 0) {
			i[0] = 1;
			for (int q = 0; q < 32 - a; q++) {
				printf("1");
			}
			for (int q = a - 1; q >= 0; q--) {
				printf("%d", i[q]);
			}
		}
		else if (i[0] == 1) {
			for (int q = 0; q < 32 - a; q++) {
				printf("1");
			}
			for (int q = 0; i[q] = 1; q++) {
				i[q] = 0;
			}
			for (int q = a - 1; q >= 0; q--) {
				printf("%d", i[q]);
			}
		}
		
	}
	else if (n == 0)printf("00000000000000000000000000000000");
	return 0;
}

 第12关 实验5-2:学生成绩管理

#include <stdio.h>
int score[20];
char name[20][20];
void one(int n)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		scanf("%s",&name[i]);
		scanf("%d",&score[i]);
	}
	printf("%d records were input!\n",n);	
}
void two(int n)
{
	int i,j,k;
    char t;
	for(i=0;i<n-1;i++)
	{
		k=i;
		for(j=i;j<n;j++)
		{
			if(score[j]>score[k])
			{
				k=j;
			}
		}
		score[n]=score[k];
		score[k]=score[i];
		score[i]=score[n];
		strcpy(name[n],name[k]);
		strcpy(name[k],name[i]);
		strcpy(name[i],name[n]);
    }
    printf("Reorder finished!\n");
}
void three(int n)
{
	int i;
	for(i=0;i<n;i++)
    {
    	printf("%s %d\n",name[i],score[i]);
    	
	}
}
void four(int n)
{
	int left=0,right=n-1,mid,score1;
	scanf("%d",&score1);
	while(left<=right)
	{
		mid=(left+right)/2;
		if(score1>score[mid])
		{
			right=mid-1;
		}
		else if(score1<score[mid])
		{
			left=mid+1;
		}
		else
		{
			break;
		}
	
	}
	if(score1==score[mid])
		{
			printf("%s %d",name[mid],score1);
		}
		else
		{
			printf("not found!");
		}
	
}
int main(void)
{
 	int num,i;
 	scanf("%d",&i);
	scanf("%d",&num);
 	while(i!=0)
 	{
 		if(i==1)
 		{
 			one(num);
		}
		else if(i==2)
		{
			two(num);
		}
		else if(i==3)
		{
			three(num);
		}
		else if(i==4)
		{
			four(num);
		}
		scanf("%d",&i);
	}
 	return 0;
}

 第13关 实验5-3:N皇后

#include <stdio.h>
int queen[10];
int count = 0;
int available(int currow,int curcol) {
    for (int row = 0; row < currow; row++) {
        if (curcol == queen[row])return 0;
        if ((currow - row) == (curcol - queen[row]))return 0;
        if ((currow - row) == (queen[row]-curcol))return 0;
    }
    return 1;
}
void putqueen(int row,int n) {
    int col;
    if (row == n) {
        count++;
        return;
    }
    for (col = 0; col < n; col++) {
        if (available(row, col)) {
            queen[row] = col;
            putqueen(row + 1,n);
        }
    }
}
int main()
{
    int n;
    scanf("%d", &n);
    putqueen(0, n);
   if(count!=0){
    printf("%d", count);
   }
   else printf("无解");
    return 0;
}

 第14关 实验5-4:车厢座位分配(此代码有些许问题)

#include <stdio.h>
int chair[20] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 };
int left[100];
int line[5];
void special(int n) {
	int last, first;
	for (int q = 0; q < n; q++) {
		for(int i=0;i<100;i++){
        if (left[i] > 0) {
            last = 5-left[i];
			left[i] -= 1;
			first = i;
			if (last == 0) {
				if (i == n - 1) {
					printf("%dA", chair[first]);
				}
				else printf("%dA ", chair[first]);
			}
			else if (last == 1) {
				if (i == n - 1) {
					printf("%dB", chair[first]);
				}
				else printf("%dB ", chair[first]);
			}
			else if (last == 2) {
				if (i == n - 1) {
					printf("%dC", chair[first]);
				}
				else printf("%dC ", chair[first]);
			}
			else if (last == 3) {
				if (i == n - 1) {
					printf("%dD ", chair[first]);
				}
				else printf("%dD ", chair[first]);
			}
			else if (last == 4) {
				if (i == n - 1) {
					printf("%dF", chair[first]);
				}
				else printf("%dF ", chair[first]);
			}
            break;
          }
		}
	}
}
void judge(int n) {
	static int last = 0;
	static int t = 0;
	int first = 0;
	static int f = 0;
	int q = 0;

	if (f != 0&&f<=20) {
		for (int i = 0; i < f; i++) {
			if (n <= left[i]) {
				first = i;
				last = line[f] + 2;
				break;
			}
			else {
				q++;
			}
		}
		if (q == f) {
			first = f;
			last = 0;
		}
	}
	for (int i = 0; i < n; i++) {//一次买几张票 t票数

		if (last == 0) {
			if (i == n - 1) {
				printf("%dA", chair[first]);
			}
			else printf("%dA ", chair[first]);
			last++;
			t++;
		}
		else if (last == 1) {
			if (i == n - 1) {
				printf("%dB", chair[first]);
			}
			else printf("%dB ", chair[first]);
			t++;
			last++;
		}
		else if (last == 2) {
			if (i == n - 1) {
				printf("%dC", chair[first]);
			}
			else printf("%dC ", chair[first]);
			last++;
			t++;
		}
		else if (last == 3) {
			if (i == n - 1) {
				printf("%dD", chair[first]);
			}
			else printf("%dD ", chair[first]);
			last++;
			t++;
		}
		else if (last == 4) {
			if (i == n - 1) {
				printf("%dF", chair[first]);
			}
			else printf("%dF ", chair[first]);
			last++;
			t++;
		}
	}
	left[f] = 5 - t;//某行剩余座位
	line[f++] = last;
	t = 0;
	last = 0;

}
int main() {

	int times;
	scanf("%d", &times);
	int pieces[30];
	for (int i = 0; i < times; i++) {
		scanf("%d", &pieces[i]);
	}
	
	for (int i = 0; i <times; i++) {
	if(i>=20){
    special(pieces[i]);
    } 		
	else {
        judge(pieces[i]);
	    printf("\n");
    }
    }
}

 第15关 实验5-5:交换数组两段数据

#include<stdio.h>
int a[40];
int m[2];
int n[2];
int q;
void change(int n, int y, int x, int j) {


	if (y - n < j - x) {
		for (int i = 0, f = 0; i < x - y - 1; i++) {
			a[y + x - y - 1 + j - x - y + n + f] = a[y + x - y - 1 + f];
			f--;
		}
		for (int i = n, h = x; i <= y + j - x - y + n; i++) {
			a[i] = h + 1;
			h++;
		}
		for (int i = x, h = n; i < j; i++) {
			a[i + j - x - y + n] = h + 1;
			h++;
		}
	}
	else if (y - n > j - x) {
		for (int i = 0,w=y-n-j+x; i <j-y-1; i++) {//
			a[y+1-y+n+j-x+i] = a[y+1+i];
		}
		for (int i = 0, h = x; i <j - x + 1; i++) {
			a[i + n] = h + 1;
			h++;
		}
		for (int i = x - y + n + j - x, h = n; i <=j; i++) {
			a[i] = h + 1;
			h++;
		}
	}
	else if (y - n == j - x) {
		for (int i = n, h = x; i <y+1; i++) {
			a[i] = h + 1;
			h++;
		}
		for (int i = x - y + n + j - x, h = n; i <= j; i++) {
			a[i] = h + 1;
			h++;
		}
	}
}
int main(void)
{
	scanf("%d", &q);
	for (int i = 0; i < q; i++) {
		scanf("%d", &a[i]);
	}
	scanf("%d %d %d %d", &m[0], &n[0], &m[1], &n[1]);
	change(m[0], n[0], m[1], n[1]);
	for (int i = 0; i < q; i++) {
		printf("%d ", a[i]);
	}
}

 第16关 实验6-1:整形16进制表示

#include<stdio.h>
int main() {
    long c;
    scanf("%d", &c);
    char* p =(char*)&c;
    char high[4], low[4];
    for (int i = 0; i<4; i++) {
        low[i] = (*p) & 0x0f;
        if (low[i] < 10) {
            low[i] |= '0';
        }
        else {
            low[i] = (low[i] - 10) + 'A';
        }
        high[i] = (*p >> 4) & 0x0f;
        if (high[i]< 10) {
            high[i] |= '0';
        }
        else {
            high[i] = (high[i] - 10) + 'A';
        }
        
        p++;
    }
    for (int i = 3; i >= 0; i--) {
        printf("%c%c", high[i], low[i]);
    }
    
}

 第17关 实验6-2:图像旋转

#include <stdio.h>
int hang;
int lie;
void zhuan(int (*one)[4],int (*two)[10],int hang,int lie){
       for (int i = 0; i < hang; i++) {
        for (int q = 0; q < lie; q++) {
            scanf("%d", &one[i][q]);
        }
    }
    for (int i = 0; i < lie; i++) {
        for (int q = 0; q < hang; q++) {
            two[i][q] = one[q][lie - i - 1];
        }
    }
    for (int i = 0; i < lie; i++) {
        for (int j = 0; j < hang; j++) {
            if (j != (hang - 1))
                printf("%d ", two[i][j]);
            else printf("%d", two[i][j]);
        }
        printf("\n");
    }

}
int main()
{
    
    scanf("%d %d", &hang, &lie);
    int one[hang][4];
    int two[lie][10];
    zhuan(one,two,hang,lie);
}

 第18关 实验6-3:删除空字符

#include<stdio.h>
#include<stdio.h>
#include<string.h>
#include<malloc.h>
void change(char*,int);
int main() 
{
	int n;
    scanf("%d",&n);
    char* s[80];
	int length;
	for (int i = 0; i <=n; i++) {
		char a[80];
		gets(a);
		length= strlen(a);
		change(a, length+1);
		length = strlen(a);
		s[i]= (char*)malloc(length + 1);
		strcpy(s[i],a);
	}
	for (int i = 1; i <=n; i++) {
		if(i==n)printf("%s",s[i]);
        else printf("%s\n",s[i]);
	}
}
void change(char* s,int length) {
	for (int i = 0; i < length; i++) {
		if (s[i] == ' ') {
           for(;s[i+1]==' ';){
               for(int q=0;q<length-i;q++){
                   s[i+q]=s[i+q+1];
               }  
           }
		}
	}
    for(int q=0;q<3;q++){
    if(s[0]==' '){
        for(int i=0;s[i+1]==' ';i++){
                s[i]=s[i+1];
           }
        }
    if(s[0]==' '){
        for(int i=0;i<length;i++){
            s[i]=s[i+1];
        }
    }
    if(s[0]=='\t'){
        for(int i=0;s[i+1]=='\t';i++){
                s[i]=s[i+1];
           }
        }
	if(s[0]=='\t'){
        for(int i=0;i<length;i++){
            s[i]=s[i+1];
        }
    }
    }
}

 第19关 实验6-4:多任务调度

#include <stdio.h>
void task0() {
    printf("task0 is called!\n");
}
void task1() {
    printf("task1 is called!\n");
}
void task2() {
    printf("task2 is called!\n");
}
void task3() {
    printf("task3 is called!\n");
}
void task4() {
    printf("task4 is called!\n");
}
void task5() {
    printf("task5 is called!\n");
}
void task6() {
    printf("task6 is called!\n");
}
void task7() {
    printf("task7 is called!\n");
}

void execute(char* a, int n) {
    for (int i = 0; i < n; i++) {
        if (a[i] =='0') task0();
        if (a[i] =='1') task1();
        if (a[i] =='2') task2();
        if (a[i] =='3') task3();
        if (a[i] =='4') task4();
        if (a[i] =='5') task5();
        if (a[i] =='6') task6();
        if (a[i] =='7') task7();
    }
}
void scheduler() {
    char a[40];
    int i = 0;
    while ((a[i] = getchar()) !=EOF) {
        i++;
    }
    execute(a, i + 1);
}
int main()
{
    scheduler();
    return 0;
}

 第20关 实验6-5:Z字形扫描

#include<stdio.h>
int main() {
	int *n[40][40];
	int h, l;
	scanf("%d %d", &h, &l);
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < l; j++) {
			scanf("%d", &n[i][j]);
		}
	}
	int qink=2;
	int i=0, j=0;
	while (1) {
		if (0 <= i && i < h && 0 <= j && j < l) {
			printf("%2d", n[i][j]);
		}
		if (i == h - 1 && j == l - 1)goto end;
			switch (qink) {
			case 1:
				i++, j--;
				break;
			case 2:
				i--, j++;
				break;
			}
		
		if (j < 0) {
			qink = 2;
			j = 0;
		}
		if (i < 0) {
			qink = 1;
			i = 0;

		}
		
	}
end:
	return 0;
}

 第21关 实验6-6:查找最长串

#include<stdio.h>
#include<string.h>
#include<malloc.h>
int strlength;
char* part[100];

char* calculate(int n){
	int est=0;
    int q;
	for(int i=0;i<n;i++){
		strlength=strlen(part[i]);
	    if(est<strlength){
	    est=strlength;
	    q=i;
        }
    }
    return (part[q]); 
}
int main() {
	char temp[9000]="\0";
	gets(temp);
	const char p[2] =" ";
	int n = 0;
	part[n++]= strtok(temp, p);
	while ((part[n]=strtok(NULL,p))!= NULL) {
		n++; 
	}
	printf("%s %d",calculate(n),strlen(calculate(n)));
}

 第22关 实验7-1:班级成绩单1

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
int n;//个数 
struct Scores  {//单链成绩列表 
	char no[70];
	char name[6];
	int one;
	int two;
	int three;
	int four;
	double average;
	struct Scores *next;
}*head,*p,*tail;
void creatlist(){
	head=NULL;
	tail=NULL;
	scanf("%d",&n);
	for(int i=0;i<n;i++){//每次重新用p 
	    p=(struct Scores*)malloc(sizeof(struct Scores));
	    scanf("%s %s %d %d %d %d", p->no, p->name, &p->one, &p->two, &p->three, &p->four);
	    if(head==NULL){
	    	head=p;
		}
		else {
		    tail->next=p;
		}
		tail=p;
	} 
	if(tail!=NULL) tail->next=NULL;
	printf("完成了%d位同学的成绩输入\n",n); 
	return head;
} 
void printlist(){
	struct Scores*p;
	p=head;
	while(p){
		printf("%s %s %d %d %d %d\n",p->no,p->name,p->one,p->two,p->three,p->four);
		p=p->next;
	}
} 
void changeMessage(){
	struct Scores*p;
	p=head;
    int temp;
	char whoseNo[7];//学号 
	int which;//序号  代表改哪一列 
	scanf("%s%d",whoseNo,&which);
	while(p!=NULL){
		if(!strcmp(whoseNo,p->no)){
			if(which==0){
				char changename[6];
				scanf("%s",changename);
				strcpy(p->name,changename);
			}
			if(which==1){
				scanf("%d",&temp);
                p->one=temp;
			}
			if(which==2){
				scanf("%d",&temp);
                p->two=temp;
			}
			if(which==3){
				scanf("%d",&temp);
                p->three=temp;
			}
			if(which==4){
				scanf("%d",&temp);
                p->four=temp;
			}
            printf("%s %s %d %d %d %d",p->no,p->name,p->one,p->two,p->three,p->four);
            break;
		}
        p=p->next;
	}
}
void calculateaverage(){
	struct Scores*p;
	p=head;
	while(p){
		int sum=p->one+p->two+p->three+p->four;
		p->average=1.0*sum/4;
		printf("%s %s %.2f\n",p->no,p->name,p->average);
		p=p->next;
	}
}
void printAndcalculate(){
	struct Scores*p;
	p=head;
	while(p){
        int sum=p->one+p->two+p->three+p->four;
		double average=p->one+p->two+p->three+p->four;
		printf("%s %s %d ",p->no,p->name,sum);
		printf("%.2f\n",average/=4);
		p=p->next;
	}
} 
int main(void)
{
    int option;
    while(scanf("%d",&option)!=EOF){
	    if (option == 1) {
			creatlist();
		}
		if (option == 2) {
			printlist();
		}
		if (option == 3) {
			changeMessage();
		}
		if (option == 4) {
			calculateaverage();
		}
		if (option == 5) {
			printAndcalculate();
		}
        if (option == 0)   {
            break;
        }
    }
	 return 0;
}

 第23关 实验7-2:班级成绩单2(有些注释可能有误)

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
int n;//个数 
struct Scores {//单链成绩列表 
	char no[70];
	char name[6];
	int one;
	int two;
	int three;
	int four;
	double average;
	struct Scores* next;
}*head, * tail, * p;
struct Scores* creatlist() {//构造链表 
	head = NULL;
	tail = NULL;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {//每次重新用p 
		p = (struct Scores*)malloc(sizeof(struct Scores));
		scanf("%s %s %d %d %d %d", p->no, p->name, &p->one, &p->two, &p->three, &p->four);
		if (head == NULL) {
			head = p;
		}
		else {
			tail->next = p;
		}
		tail = p;
	}
	if (tail != NULL) tail->next = NULL;
	printf("完成了%d位同学的成绩输入\n", n);
	return head;
}
void printlist() {
	struct Scores* p;
	p = head;
	while (p) {
		printf("%s %s %d %d %d %d\n", p->no, p->name, p->one, p->two, p->three, p->four);
		p = p->next;
	}
}
void printsec() {
	struct Scores* p;
	p = head;
	while (p) {
		printf("%s %s %.2f\n", p->no, p->name, p->average);
		p = p->next;
	}
}
void changeMessage() {
	struct Scores* p;
	p = head;
	int temp;
	char whoseNo[7];//学号 
	int which;//序号  代表改哪一列 
	scanf("%s%d", whoseNo, &which);
	while (p != NULL) {
		if (!strcmp(whoseNo, p->no)) {
			if (which == 0) {
				char changename[6];
				scanf("%s", changename);
				strcpy(p->name, changename);
			}
			if (which == 1) {
				scanf("%d", &temp);
				p->one = temp;
			}
			if (which == 2) {
				scanf("%d", &temp);
				p->two = temp;
			}
			if (which == 3) {
				scanf("%d", &temp);
				p->three = temp;
			}
			if (which == 4) {
				scanf("%d", &temp);
				p->four = temp;
			}
			printf("%s %s %d %d %d %d", p->no, p->name, p->one, p->two, p->three, p->four);
			break;
		}
		p = p->next;
	}
}
void calculateaverage() {//计算输出平均值 
	struct Scores* p;
	p = head;
	while (p) {
		int sum = p->one + p->two + p->three + p->four;
		p->average = sum / 4;
		printf("%s %s %d %.2f\n", p->no, p->name, sum, p->average);
		p = p->next;
	}
}
void calaverage() {//计算平均值 
	struct Scores* p;
	p = head;
	while (p) {
		int sum = p->one + p->two + p->three + p->four;
		p->average = 1.0 * sum / 4;
		p = p->next;
	}
}
void printAndcalculate() {
	struct Scores* p;
	p = head;
	while (p) {
		double average = p->one + p->two + p->three + p->four;
		printf("%s %s %d %d %d %d ", p->no, p->name, p->one, p->two, p->three, p->four);
		printf("%.2f\n", average /= 4);
		p = p->next;
	}
}
void low1() {//冒泡法1 
	calaverage();
	char NO[70];
	char Name[6];
	struct Scores* tmp = head;
	while (tmp->next)
	{
		struct Scores* now = tmp;
		while (now->next)
		{
			if (tmp->average > now->next->average)
			{
				int s = tmp->average;
				tmp->average = now->next->average;
				tmp->next->average = s;
				strcpy(NO, tmp->no);
				strcpy(tmp->no, now->next->no);
				strcpy(now->next->no, NO);
				strcpy(Name, tmp->name);
				strcpy(tmp->name, now->next->name);
				strcpy(now->next->name, Name);
			}
			now = now->next;
		}
		tmp = tmp->next;
	}
	printsec();
}
void low0() {
	calaverage();
	char NO[70];
	char Name[6];
	struct Scores* tmp = head;
	while (tmp->next)
	{
		struct Scores* now = tmp;
		while (now->next)
		{
			if (now->average > now->next->average)
			{
				int s = now->average;
				now->average = now->next->average;
				now->next->average = s;
				strcpy(NO, now->no);
				strcpy(now->no, now->next->no);
				strcpy(now->next->no, NO);
				strcpy(Name, now->name);
				strcpy(now->name, now->next->name);
				strcpy(now->next->name, Name);
			}
			now = now->next;
		}
		tmp = tmp->next;
	}
	printsec();
}
void high1() {//冒泡 
	calaverage();
	char NO[70];
	char Name[6];
	struct Scores* tmp = head;
	int y = n;
	while (y)
	{
		struct Scores* now = tmp;
		while (now->next)
		{
			if (now->average < now->next->average)
			{
				int s = now->average;
				now->average = now->next->average;
				now->next->average = s;
				strcpy(NO, now->no);
				strcpy(now->no, now->next->no);
				strcpy(now->next->no, NO);
				strcpy(Name, now->name);
				strcpy(now->name, now->next->name);
				strcpy(now->next->name, Name);
			}
			now = now->next;
		}
		y--;
	}
	printsec();
}
void high0() {//选择法 
	calaverage();
	char NO[70];
	char Name[6];
	struct Scores* tmp = head;
	int y = n;
	while (y)
	{
		struct Scores* now = tmp;
		while (now->next)
		{
			if (now->average < now->next->average)
			{
				int s = now->average;
				now->average = now->next->average;
				now->next->average = s;
				strcpy(NO, now->no);
				strcpy(now->no, now->next->no);
				strcpy(now->next->no, NO);
				strcpy(Name, now->name);
				strcpy(now->name, now->next->name);
				strcpy(now->next->name, Name);
			}
			now = now->next;
		}
		y--;
	}
	printsec();
}
int main(void)
{
	int option;
	while (scanf("%d", &option) != EOF) {
		if (option == 1) {
			creatlist();
		}
		if (option == 2) {
			printlist();
		}
		if (option == 3) {
			changeMessage();
		}
		if (option == 4) {
			calculateaverage();
		}
		if (option == 5) {
			printAndcalculate();
		}
		if (option == 6) {
			int option1, option2;
			scanf("%d %d", &option1, &option2);
			if (option1 == 0) {
				if (option2 == 0) {
					low0();
				}
				if (option2 == 1) {
					low1();
				}
			}
			if (option1 == 1) {
				if (option2 == 0) {
					high0();
				}
				if (option2 == 1) {
					high1();
				}
			}
		}
		if (option == 0) {
			break;
		}
	}
	return 0;
}

第24关 实验7-3:班级成绩单3

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
int n;//个数 
struct Scores  {//单链成绩列表 
	char no[70];
	char name[6];
	int one;
	int two;
	int three;
	int four;
	double average;
	struct Scores *next;
}*head,*tail,*p;
struct Scores*creatlist(){//构造链表 
	head=NULL;
	tail=NULL;
	scanf("%d",&n);
	for(int i=0;i<n;i++){//每次重新用p 
	    p=(struct Scores*)malloc(sizeof(struct Scores));
	    scanf("%s %s %d %d %d %d", p->no, p->name, &p->one, &p->two, &p->three, &p->four);
	    if(head==NULL){
	    	head=p;
		}
		else {
		    tail->next=p;
		}
		tail=p;
	} 
	if(tail!=NULL) tail->next=NULL;
	printf("完成了%d位同学的成绩输入\n",n); 
	return head;
} 
void printlist(){
	struct Scores*p;
	p=head;
	while(p){
		printf("%s %s %d %d %d %d\n",p->no,p->name,p->one,p->two,p->three,p->four);
		p=p->next;
	}
} 
void printsec(){
	struct Scores*p;
	p=head;
	while(p){
		printf("%s %s %.2f\n",p->no,p->name,p->average);
		p=p->next;
	}
}
void changeMessage(){
	struct Scores*p;
	p=head;
    int temp;
	char whoseNo[7];//学号 
	int which;//序号  代表改哪一列 
	scanf("%s%d",whoseNo,&which);
	while(p!=NULL){
		if(!strcmp(whoseNo,p->no)){
			if(which==0){
				char changename[6];
				scanf("%s",changename);
				strcpy(p->name,changename);
			}
			if(which==1){
				scanf("%d",&temp);
                p->one=temp;
			}
			if(which==2){
				scanf("%d",&temp);
                p->two=temp;
			}
			if(which==3){
				scanf("%d",&temp);
                p->three=temp;
			}
			if(which==4){
				scanf("%d",&temp);
                p->four=temp;
			}
            printf("%s %s %d %d %d %d",p->no,p->name,p->one,p->two,p->three,p->four);
            break;
		}
        p=p->next;
	}
}
void calculateaverage(){//计算输出平均值 
	struct Scores*p;
	p=head;
	while(p){
		int sum=p->one+p->two+p->three+p->four;
		p->average=sum/4;
		printf("%s %s %d %.2f\n",p->no,p->name,sum,p->average);
		p=p->next;
	}
}
void calaverage(){//计算平均值 
    struct Scores*p;
	p=head;
	while(p){
		int sum=p->one+p->two+p->three+p->four;
		p->average=1.0*sum/4;
		p=p->next;
	}
} 
void printAndcalculate(){
	struct Scores*p;
	p=head;
	while(p){
		double average=p->one+p->two+p->three+p->four;
		printf("%s %s %d %d %d %d ",p->no,p->name,p->one,p->two,p->three,p->four);
		printf("%.2f\n",average/=4);
		p=p->next;
	}
} 
void low1(){//冒泡法1 
	calaverage();
    struct Scores *tmp = head ;
    while ( tmp->next )
    {
        struct Scores *now = tmp ;
        while ( now->next )
        {
            if( now->average > now->next->average ) 
            {
                struct Scores tmpp = *now ;
                tmpp.next = now->next->next ;
                now->next->next = now->next ;
                *now = *now->next ;
                *now->next = tmpp ;
            }
            now = now->next ;
        }
        tmp = tmp->next ;
    }
    printsec();
}
void low0(){//选择法 0
	calaverage();
    struct Scores *tmp = head ;
    while ( tmp )
    {
        struct Scores *now = tmp ;
        while ( now->next )
        {
            if( tmp->average > now->next->average ) 
            {
                struct Scores tmpp = *now ;
                tmpp.next = now->next->next ;
                now->next->next = tmp->next ;
                *tmp = *now->next ;
                *now->next = tmpp ;
            }
            now = now->next ;
        }
        tmp = tmp->next ;
    }
    printsec();
}
void high1(){//冒泡 
	calaverage();
    struct Scores *tmp = head ;
    while ( tmp )
    {
        struct Scores *now = tmp ;
        while ( now->next )
        {
            if( now->average < now->next->average ) 
            {
                struct Scores tmpp = *now ;
                tmpp.next = now->next->next ;
                now->next->next = now->next ;
                *now = *now->next ;
                *now->next = tmpp ;
            }
            now = now->next ;
        }
        tmp = tmp->next ;
    }
    struct Scores*p=(struct Scores*)malloc(sizeof(struct Scores));
    p->next=head;
    struct Scores*q=p;
    struct Scores*node1=p->next;
    struct Scores*node2=p->next->next;
    p->next=node2;
    node1->next=node2->next;
    node2->next=node1;
    p=node1;
    head=q->next;
    printsec();
}
void high0(){//选择法 
	calaverage();
    struct Scores *tmp = head ;
    while ( tmp )
    {
        struct Scores *now = tmp ;
        while ( now->next )
        {
            if( tmp->average < now->next->average ) 
            {
                struct Scores tmpp = *tmp ;
                tmpp.next = now->next->next ;
                now->next->next = tmp->next ;
                *tmp = *now->next ;
                *now->next = tmpp ;
            }
            now = now->next ;
        }
        tmp = tmp->next ;
    }
    printsec();
}
int main(void)
{
    int option;
    while(scanf("%d",&option)!=EOF){
	    if (option == 1) {
			creatlist();
		}
		if (option == 2) {
			printlist();
		}
		if (option == 3) {
			changeMessage();
		}
		if (option == 4) {
			calculateaverage();
		}
		if (option == 5) {
			printAndcalculate();
		}
		if(option==6){
			int option1,option2;
            scanf("%d %d",&option1,&option2);
            if(option1==0){
            	if(option2==0){
				    low0();
				   }
				if(option2==1){
					low1();
				} 
            }
            if(option1==1){
                if(option2==0){
				    high0();
				   }
				if(option2==1){
				    high1();
				} 
            }
		}
        if (option == 0) {
            break;
        }
    }
	return 0;
}

 第25关 实验7-4:约瑟夫问题

#include<stdio.h>
#include<malloc.h>
int n,s;
struct T{
	int x;
	struct T*next;
}*head,*tail;
void creatlist(){
	head=NULL;
	tail=NULL;
    int c=1;
	for(int i=1;i<=n;i++){
		struct T*p=(struct T*)malloc(sizeof(struct T));
		p->x=i;
		if(head==NULL){
			head=p;
		}
		else{
			tail->next=p;
		}
		tail=p;
	} 
	tail->next=head;
	struct T*move=head;
	struct T*h=NULL;
	while(move->next!=move){
		for(int i=2;i<=s;i++){
			h=move;
			move=move->next;
		}        
		if(c!=n-1)
        printf("%d ",move->x);  
        else
        printf("%d",move->x);  
		h->next=move->next;
        move=move->next;
        c++;
	}
	printf("\n%d",move->x);
} 

int main()
{
    scanf("%d %d",&n,&s);	
	creatlist();
	return 0;
}

 第26关 实验8-1:文件单词替换

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

int main()
{
	FILE* fp;
	char c1[20] = { 0 };
	char c2[20] = { 0 };
	char t[20] = { 0 };
	char s[1000] = { 0 }, c[1000] = { 0 };
	int n, b, a, num = 0, j = 0, m, q;
	scanf("%s%s",c1, c2);
	b = strlen(c1), a = strlen(c2);
	fp = fopen("EXP/exp801/source.txt", "r");
	fseek(fp, 0, 2);
	n = ftell(fp);
	rewind(fp);
	fread(s, n, 1, fp);
	s[n + 1] = '\0';
	for (int i = 0; s[i] != '\0'; i++) {
		if (s[i] != c1[0]) c[j++] = s[i];
		else {
			for (m = 0; m < b; m++) t[m] = s[i + m];
			if ((!strcmp(t, c1)) && (s[i + b] < 65)) {
				for (m = 0; m < a; m++) c[j++] = c2[m];
				i += b - 1;
				num++;
				for (q = 0; q < 20; q++) t[q] = '\0';
			}
			else
				c[j++] = s[i];
		}
	}
	printf("%d\n%s", num, c);
	return 0;

}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值