zoukankan      html  css  js  c++  java
  • C编程(C语言程序设计,大连理工大学MOOC)

    仅作参考,请指正(都提交通过,但是也有错误,第22题的错误,已经修正)

    1、Hello World!

    1 #include <stdio.h> 
    2 int main()
    3 {
    4     printf("Hello World!"); 
    5     return 0;
    6 }
    View Code

    2、我爱编程

    1 #include <stdio.h> 
    2 int main()
    3 {
    4     printf("Hello C!
    "); 
    5     printf("I like programming."); 
    6     return 0;
    7 }
    View Code

    3、计算两个整数的乘积

    1 #include <stdio.h> 
    2 int main()
    3 {
    4     int a,b;
    5     scanf("%d%d",&a,&b);
    6     printf("%d",a*b); 
    7     return 0;
    8 }
    View Code

    4、计算圆的面积

    1 #include <stdio.h> 
    2 #include <string.h>
    3 #define PI 3.1415
    4 int main()
    5 {    
    6     printf("Area=%5.2f
    ", PI*4.3*4.3);
    7     return 0;
    8 }
    View Code

    5、计算平方根

    1 #include <stdio.h> 
    2 #include <math.h>
    3 int main()
    4 {
    5     double x;    
    6     scanf("%lf", &x);
    7     printf("%f
    ", sqrt(x));
    8     return 0;
    9 }
    View Code

    6、输出逆序数

     1 #include <stdio.h> 
     2 #include <string.h>
     3 #include <math.h>
     4 int main()
     5 {
     6     int x,y = 0;    
     7     scanf("%d", &x);
     8     while(x)
     9     {
    10         y = y*10 + x%10;
    11         x /= 10;
    12     }
    13     printf("y=%d
    ", y);
    14     return 0;
    15 }
    View Code

    7、交换两个变量

     1 #include<stdio.h>
     2 #include<math.h>
     3 
     4 int main()
     5 {
     6     int a,b,t;
     7     scanf("%d,%d",&a,&b);
     8     t = a;
     9     a = b;
    10     b = t;
    11     printf("%d,%d",a,b);
    12     return 0;
    13 }
    View Code

    8、输出字符ASCII码

     1 #include<stdio.h>
     2 #include<math.h>
     3 
     4 int main()
     5 {
     6     int a;
     7     a = getchar();
     8     printf("ASCII=%d",a);
     9     return 0;
    10 }
    View Code

    9、求一元二次方程的两个实根

     1 #include<stdio.h>
     2 #include<math.h>
     3 
     4 int main()
     5 {
     6     float x1,x2;
     7     x1 = (-1 + sqrt(1+4*2))/2.0;
     8     x2 = (-1 - sqrt(1+4*2))/2.0;
     9     printf("x1=%.4f
    ",x1);
    10     printf("x2=%.4f
    ",x2);
    11     return 0;
    12 }
    View Code

    10、计算银行存款本息

     1 #include<stdio.h>
     2 #include<math.h>
     3 
     4 int main()
     5 {
     6     int money,year;
     7     double rate,sum = 0;
     8     scanf("%d%d%lf",&money,&year,&rate);
     9     sum = money*pow((1+rate),year);
    10     
    11     printf("sum=%7.2f",sum);
    12     return 0;
    13 }
    View Code

    11、大小写字母转换

     1 #include <stdio.h>
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4  
     5 int main()
     6 {
     7     char ch;
     8     ch = getchar();
     9     if(ch>='a'&&ch<='z')
    10         ch -= 32;
    11     else
    12         ch += 32;
    13     putchar(ch);
    14     return 0;
    15 }
    View Code

    12、计算分段函数

     1 #include <stdio.h> 
     2 int main(void)
     3 {
     4     double x, y; 
     5     scanf("%lf",&x);
     6     if ( x<0 )
     7         y=0;
     8     else if( x<=15 )
     9         y=4*x/3;
    10     else
    11         y=2.5*x-10.5;
    12     printf("y=%.2f",y);   
    13     return 0;
    14 }
    View Code

    13、百分制成绩转换为五级分制成绩

     1 #include <stdio.h>
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4  
     5 int main()
     6 {
     7     float achievment;
     8     scanf("%f",&achievment);
     9     
    10     int a = achievment;
    11     char ch;
    12     
    13     switch(a/10){
    14         case 10:
    15         case 9:ch = 'A';break;
    16         case 8:ch = 'B';break;
    17         case 7:ch = 'C';break;
    18         case 6:ch = 'D';break;
    19         default:ch = 'E';
    20     }
    21     printf("grade:%c
    ",ch);
    22     
    23     return 0;
    24 }
    View Code

    14、输出水仙花数

     1 #include <stdio.h>
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4  
     5 int main()
     6 {
     7     int i;
     8     scanf("%d",&i);
     9     
    10     int a = i%10, b = i/10%10, c = i/100;
    11     if(a*a*a + b*b*b + c*c*c == i)
    12         printf("%d是水仙花数!",i);
    13     else
    14         printf("%d不是水仙花数!",i);
    15     
    16     return 0;
    17 }
    View Code

    15、用for语句实现判断并求和

     1 #include<stdio.h>
     2 
     3 int main()
     4 {
     5     int sum = 0;
     6     for(int i=1; i<=100;  ++i)
     7     {
     8         if(i%3==0 && i%5==1)
     9             sum += i;
    10     }
    11     printf("sum=%d",sum);
    12     
    13     return 0;
    14 }
    View Code

    16、用do while 语句实现从键盘输入一组整数,求平均值

     1 #include<stdio.h>
     2 
     3 int main()
     4 {
     5     int i=0, x;
     6     float sum = 0;
     7     do{
     8         scanf("%d",&x);
     9         if(x<0) break;
    10         sum += x;
    11         i++;
    12     }while(x>=0);
    13     printf("AVE=%.2f
    ",sum/i);    
    14     return 0;
    15 }
    View Code

    17、用while语句实现计算圆周率 pi (3.14) 的近似值

     1 #include<stdio.h>
     2 #include <math.h>
     3 #define EPS  1e-5
     4 int main()
     5 {
     6     float pi = 0, sign = 1, x = 1;
     7     
     8     while(fabs(sign/x) > EPS)
     9     {
    10         pi += sign/x;
    11         x += 2;
    12         sign = -sign;    
    13     }
    14     printf("pi=%.2f
    ",pi*4);    
    15     return 0;
    16 }
    View Code

    18、求两个整数的最大公约数

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 
     4 int GCD(int x, int y);
     5 int main()
     6 {
     7     int a,b;
     8     scanf("%d%d",&a,&b);
     9     printf("GCD=%d
    ",GCD(a,b));
    10     return 0;
    11 }
    12 
    13 int GCD(int x, int y){
    14     return y == 0 ? x : GCD(y, x%y); 
    15 }
    View Code

    19、找素数

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 
     4 int isPrime(int x);
     5 int main()
     6 {
     7     int a;
     8     scanf("%d",&a);
     9     for(int i=a+1;;++i){
    10         if(isPrime(i)){
    11             printf("prime=%d
    ",i);
    12             break;
    13         }            
    14     }   
    15     return 0;
    16 }
    17 
    18 int isPrime(int x){
    19     for(int i=2; i<x; ++i)
    20         if(x%i==0)
    21             return 0;
    22     return 1;
    23 }
    View Code

    20、斐波那契数列求和

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 
     4 int main()
     5 {
     6     int sum = 0, a=1,b=1;
     7     for(int i=0;i<20;i+=2){
     8         sum += a+b;
     9         a = a+b;
    10         b = a+b;
    11     } 
    12     printf("sum=%d
    ",sum);
    13     return 0;
    14 }
    View Code

    21、逆序存放数据

     1 #include<stdio.h>
     2 #define N 100
     3 int main()
     4 {
     5     int arr[N] = {0}, n=6;
     6     for(int i=n-1; i>=0; --i)
     7         scanf("%d",&arr[i]);
     8     for(int j=0; j<n; ++j)
     9         printf("%d
    ",arr[j]);
    10     return 0;
    11 }
    View Code

    22、找出一维整型数组中的次最大值

    #include<stdio.h>
    #define N 5
    /*设次大值一定存在*/
    int main()
    {
        int i, j, arr[N+1] = {0};
        /* 输入5个数 */
        for(i=0; i<N; ++i)
            scanf("%d",&arr[i]);
        /* 冒泡排序 */
        for(i=0; i<N-1; ++i)
            for(j=0; j<N-i-1; ++j)
                if(arr[j]<arr[j+1]){
                    int t = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = t;
                }
        /* 求次最大值下标i*/
            for(i=1; arr[0]==arr[i] && i<N; ++i)
                ;
            /*if(i==N)
                printf("次大值不存在!
    ");*/
        printf("次最大值=%d",arr[i]);
        return 0;
    }
    View Code
    /*
        求最大值、次大值可以不用排序
     */
    #include <stdio.h>
    #define N 5
    int main()
    {
        int i, arr[N] = {0};
        int max1, max2;
        
        scanf("%d%d",&arr[0],&arr[1]);
        max1 = max2 = arr[0];
        arr[1] > max1 ? (max1 = arr[1]) : (max2  = arr[1]);
        
        for(i=2; i<N; ++i){
            scanf("%d",&arr[i]);
            if(arr[i]>max1){
                max2 = max1;
                max1 = arr[i];
            }
            else if(arr[i]>max2){
                max2 = arr[i];
            }
        }
    
            /*if(max1==max2)
                printf("次大值不存在!
    ");
            else
                printf("次最大值=%d",max2);*/
    
        /*次最大值 max2 是存在的 */
        printf("次最大值=%d",max2);
        return 0;
    }
    View Code
    #include <stdio.h>
    #include <limits.h>
    #define N 5
    int main()
    {
        int i, arr[N] = {0};
        int max1, max2;
        
        max1 = max2 = INT_MIN;
        
        for(i=0; i<N; ++i){
            scanf("%d",&arr[i]);
            if(arr[i]>max1){
                max2 = max1;
                max1 = arr[i];
            }
            else if(arr[i]>max2){
                max2 = arr[i];
            }
        }    
        /* 次最大值 max2 */
        if(max1  == INT_MIN)
            printf("次大值不存在!
    ");
        else
            printf("次最大值=%d",max2);
        return 0;
    }
    View Code

    23、调整最大值的位置

     1 #include<stdio.h>
     2 #define N 100
     3 int main()
     4 {
     5     int arr[N] = {0}, n=6, max=0, maxi=0;
     6     scanf("%d",&arr[0]);
     7     max=arr[0];
     8     
     9     for(int i=1; i<n; ++i){
    10         scanf("%d",&arr[i]);
    11         if(arr[i]>max){
    12             max = arr[i];
    13             maxi = i;
    14         }
    15             
    16     }
    17     int t = arr[n-1];
    18     arr[n-1] = arr[maxi];
    19     arr[maxi] = t;
    20     
    21     for(int j=0; j<n; ++j)
    22         printf("%d
    ",arr[j]);
    23     return 0;
    24 }
    View Code

    24、连接两个字符串

     1 #include<stdio.h>
     2 #include<string.h>
     3 #define N 100
     4 int main()
     5 {
     6     char a[2*N] = "", b[N] = "";
     7     scanf("%s%s",a,b);
     8     /* strcpy(&a[strlen(a)],b);//没有用strcat用的strcpy
     9     */
    10     //不用函数
    11     int i;
    12     for(i=0; a[i]; ++i);//求字符串''下标
    13     char *p = a+i, *q = b;
    14     while(*q)
    15     {
    16         *p++ = *q++;
    17     }
    18     *p = ''; 
    19     
    20     printf("%s
    ",a);
    21     return 0;
    22 }
    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 #define N 100
     4 int main()
     5 {
     6     char a[2*N] = "", b[N] = "";
     7     scanf("%s%s",a,b);
     8    
     9     char *p = a, *q = b;
    10     while(*p) p++;
    11     while((*p++=*q++)!='');
    12    
    13     printf("%s
    ",a);
    14     return 0;
    15 }
    View Code

    25、求矩阵外围元素之和

     1 #include <stdio.h>
     2 #include <malloc.h>
     3 int main()
     4 {
     5     int sum=0, n=3;    
     6     int *arr = (int*)malloc(sizeof(int)*n*n); //一维模拟二维数组
     7     
     8     for(int i=0; i<n*n; ++i)
     9     {
    10         scanf("%d",&arr[i]);
    11     } 
    12     
    13     for(int i=0; i<n; ++i)
    14     {
    15         for(int j=0; j<n; ++j)
    16         {
    17             if(i==0||i==n-1 || j==0|| j==n-1 )
    18             {
    19                 sum += arr[i*n+j]; //一行n个
    20             }         
    21         }      
    22     }
    23     
    24     printf("%d",sum);
    25     
    26     return 0;
    27 }
    View Code

    26、求矩阵主对角线和副对角线元素之和

     1 #include <stdio.h>
     2 #define N 5
     3 int main()
     4 { 
     5     char str[N][N];
     6         int i,j,sum=0;
     7 
     8         for(i=0; i<N; ++i)
     9             for(j=0; j<N; ++j)
    10                 scanf("%d",&str[i][j]);
    11 
    12         for(i=0; i<N; ++i)
    13             for(j=0; j<N; ++j)
    14             {
    15                 if(i==j)
    16                     sum += str[i][j];
    17                 if(i==N-j-1)
    18                     sum += str[i][j];
    19             }
    20         printf("sum=%d
    ",sum);
    21     return 0;
    22 }
    View Code

    27、将字符串的小写字母转换为大写字母

     1 #include <stdio.h>
     2 #define N 1000
     3 int main()
     4 {
     5     char str[N]="";
     6     int i=0;
     7     gets(str);
     8 
     9     while(str[i]){
    10         if(str[i]>='a'&&str[i]<='z')
    11             str[i] -= 32;
    12         i++;
    13     }
    14     printf("%s
    ",str);
    15     return 0;
    16 }
    View Code

    28、编写函数,计算银行存款本息并输出

     1 #include <stdio.h> 
     2 #include <stdlib.h>
     3 double fun(int money,int year, double rate)
     4 {
     5     double sum = money;
     6     while(year--){
     7         sum = sum*(1+rate);
     8     }
     9     return sum;
    10 }
    11 int main()
    12 {
    13     int money, year;
    14     double rate, sum=0;
    15     scanf("%d%d%lf",&money,&year,&rate);
    16     sum = fun(money,year,rate);
    17     printf("sum=%.2f",sum);
    18     return 0;
    19 }
    View Code

    29、数值转换

     1 /*位运算*/
     2 #include <stdio.h>
     3 void fun(int x)
     4 {
     5     int j=31;/*按32位定义*/   
     6     while(!((x&(1<<j))>>j))/*去前面的0*/
     7         j--;
     8     for(; j>=0; j--)/*按位输出*/
     9         printf("%d", (x&(1<<j))>>j);
    10 }
    11 int main()
    12 {
    13     int x;
    14     scanf("%d",&x);
    15     fun(x);
    16     return 0;
    17 }
    View Code

    30、求一组数中大于平均值的数的个数

     1 #include <stdio.h>
     2 #define N 100
     3 
     4 int main()
     5 {    
     6     int num[N] = {0},count=0;
     7     double sum = 0;
     8     for(int i=0; i<10; ++i){
     9         scanf("%d",&num[i]);
    10         sum += num[i];
    11     }
    12     for(int j=0; j<10; ++j)
    13         if(num[j]>sum/10)
    14             count++;
    15     printf("%d",count);
    16     return 0;
    17 }
    View Code

    31、求x的n次方

     1 #include <stdio.h>
     2 double cube(double x,int n)
     3 {
     4     if(n ==1)
     5         return x;
     6     else
     7         return x*cube(x,n-1);
     8 }
     9 int main()
    10 {
    11     double x,y;
    12     int n;
    13     scanf("%lf%d",&x,&n);
    14     y=cube(x,n);
    15     printf("%f",y);
    16     return 0;
    17 }
    View Code

    32、输出最大、最小数

     1 #include <stdio.h>
     2 void fun(int *arr,int *max,int *min){
     3     *max = *min = arr[0];
     4     for(int i=1; i<10; ++i){
     5         if(arr[i]>*max)
     6             *max = arr[i];
     7         if(arr[i]<*min)
     8             *min = arr[i];
     9     }
    10 }
    11 int main()
    12 {
    13     int max,min;
    14     int arr[10] = {0};
    15     for(int i=0; i<10; ++i){
    16         scanf("%d",&arr[i]);
    17     }    
    18     fun(arr,&max,&min);
    19     printf("max=%d,min=%d",max,min);
    20     return 0;
    21 }
    View Code

    33、任意整数转换为千分位分隔的字符形式

     1 #include <stdio.h>
     2 #define N 100
     3 int main()
     4 {
     5     int n;
     6     scanf("%d",&n);
     7     int arr[N] = {0},index = 0;
     8     while(n)
     9     {
    10         arr[index++] = n%10;
    11         n /= 10;
    12     }
    13     for(int i=index-1; i>=0; --i)        
    14     {
    15         printf("%d",arr[i]);
    16         if(i%3==0&&i)
    17             printf(",");
    18     }
    19     return 0;
    20 }
    View Code

    34、利用指针编写程序,统计字符串的长度

     1 #include <stdio.h>
     2 #define N 100
     3 int main()
     4 {    
     5     char str[N] = "";
     6     gets(str);
     7     char *p = str;
     8     int len = 0;
     9     while(*p)
    10     {
    11         len++;
    12         p++;
    13     }
    14     printf("Length of the string is %d
    ",len);
    15     return 0;
    16 }
    View Code

    35、利用指针编程,将数组中所有元素的值对称交换

     1 #include <stdio.h>
     2 #define N 10
     3 int main()
     4 {    
     5     int arr[N] = {0};
     6     for(int i=0; i<N; ++i)
     7         scanf("%d",&arr[i]);
     8     int *p = arr, *q = arr+N-1;
     9     while(p<q)
    10     {
    11         int t = *p;
    12         *p = *q;
    13         *q = t;
    14         p++, q--;
    15     }
    16     for(int j=0; j<N; ++j)
    17         printf("%2d",arr[j]);
    18     return 0;
    19 }
    View Code

    36、利用指针编写程序,求一维数组中的最大和最小的元素值

     1 #include <stdio.h>
     2 #define N 10
     3 int main()
     4 {    
     5     int arr[N] = {0};
     6     for(int i=0; i<N; ++i)
     7         scanf("%d",&arr[i]);
     8     int max, min;
     9     max = min = arr[0];
    10     for(int j=1; j<N; ++j)
    11     {
    12         if(arr[j]>max)
    13             max = arr[j];
    14         if(arr[j]<min)
    15             min = arr[j];
    16     }
    17     printf("max=%d,min=%d",max,min);
    18     return 0;
    19 }
    View Code

    37、利用指针编写一函数,输入3个整数,按由大到小的顺序将它们输出

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #define N 256
     5 
     6 void swap(int *a, int *b)
     7 {
     8     int t = *a;
     9     *a = *b;
    10     *b = t;
    11 }
    12 int main( )
    13 {    
    14     int arr[N] = {0};
    15     for(int i=0; i<3; ++i)
    16         scanf("%d",&arr[i]);
    17     if(arr[0]<arr[1])
    18         swap(&arr[0],&arr[1]);
    19     if(arr[0]<arr[2])
    20         swap(&arr[0],&arr[2]);
    21     if(arr[1]<arr[2])
    22         swap(&arr[1],&arr[2]);
    23     for(int j=0;j<3; ++j)
    24         printf("%3d",arr[j]);
    25     return 0;
    26 }
    View Code

    38、用指向数组的指针变量实现输出指定学生的全部成绩

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #define N 256
     5 
     6 int main( )
     7 {    
     8     float score[][4]={{60,70,80,90},{56,89,67,88},{34,78,90,66}};
     9     float (*p)[4] = score;
    10     int n;
    11     scanf("%d",&n);
    12     for(int i=0;i<4;++i)
    13         printf("%8.2f",p[n][i]);
    14     return 0;
    15 }
    View Code

    39、某班有5名同学,建立一个学生的简单信息表,包括学号、姓名、3门课程的成绩,编写程序,计算每名学生的平均成绩及名次

     1 /* 
     2 测试数据
     3 201701 zhao 29.8 85.4 65.4
     4 201702 qian 96.5 87.5 65.4
     5 201703 sun 85.4 65.4 84.6
     6 201704 li 63.4 95.4 86.3
     7 201705 zhou 65.9 84.6 97.5 
     8 */
     9 #include<stdio.h>
    10 
    11 struct student{
    12     long no;
    13     char name[20];
    14     float score[3];
    15 }stu[5],*p;
    16 
    17 int rank(float *ave, float score){
    18     int rank = 5;
    19     for(int i=0; i<5; ++i)
    20         if(ave[i]<score)
    21             rank--;
    22     return rank;
    23 }
    24 
    25 int main()
    26 { 
    27     float ave[5] = {0.0};
    28     int index = 0;
    29     for(p=stu; p<stu+5; ++p){ //stu+5
    30         scanf("%ld %s %f %f %f",
    31             &p->no,p->name,&p->score[0],&p->score[1],&p->score[2]);
    32         ave[index++] = (p->score[0]+p->score[1]+p->score[2])/3.0;
    33     }
    34     
    35     printf("Number:  Name:        score:       Average:  rank:
    ");
    36     for(index=0,p=stu; p<stu+5; ++p){
    37         printf("%-9ld%-10s%-5.1f%-5.1f%-8.1f%-10.1f%-d
    ",
    38             p->no,p->name,p->score[0],p->score[1],
    39             p->score[2],ave[index++],rank(ave,ave[index]));
    40     }
    41     return 0;
    42  }
    View Code

    40、某班有10名同学,建立学生结构体类型,包括学号、姓名、3门课程的成绩,编写程序,读入已准备好的文本文件student1.txt,为结构体数组变量赋值,计算每名学生的平均成绩及名次。结果输出到屏幕的同时保存到student2.txt文件中

     1 #include <stdio.h>
     2  
     3 /* 结构体定义 */
     4 struct student{
     5     long no;
     6     char name[20];
     7     float score[3];
     8 }stu[10],*p;
     9  
    10 /* 计算名次 */
    11 int rank(float *ave, float score, int n){
    12     int rank = n;
    13     for(int i=0; i<n; ++i)
    14         if(ave[i]<score)
    15             rank--;
    16     return rank;
    17 }
    18  
    19 int main()
    20 {
    21     /* 打开文件 */   
    22     FILE *fp1,*fp2;
    23     fp1 = fopen("student1.txt","r");
    24     fp2 = fopen("student2.txt","w");
    25      
    26     /* 读取数据 计算平均值 */
    27     float ave[10] = {0.0};
    28     int index = 0; 
    29     for(p=stu; p<stu+10; ++p){ //stu+10
    30         fscanf(fp1,"%ld %s %f %f %f",
    31             &p->no,p->name,&p->score[0],&p->score[1],&p->score[2]);
    32         ave[index++] = (p->score[0]+p->score[1]+p->score[2])/3.0;
    33     }
    34     /* 写入文件 */
    35     for(index = 0,p=stu; p<stu+10; ++p){
    36         fprintf(fp2,"%-9ld%-10s%-5.1f%-5.1f%-8.1f%-10.1f%-d
    ",
    37             p->no,p->name,p->score[0],p->score[1],p->score[2],ave[index++],rank(ave,ave[index],10));
    38     }   
    39     /* 关闭文件 */
    40     fclose(fp1);
    41     fclose(fp2);
    42     return 0;
    43 }
    View Code
     1 测试数据 student1.txt
     2 201701 zhao 29.8 85.4 65.4
     3 201702 qian 96.5 87.5 65.4
     4 201703 sun 85.4 65.4 84.6
     5 201704 li 63.4 95.4 86.3
     6 201705 zhou 65.9 84.6 97.5
     7 201706 wu 39.8 85.4 75.4
     8 201707 zheng 96.5 82.5 75.9
     9 201708 wang 75.4 85.4 94.6
    10 201709 feng 73.4 95.4 56.3
    11 2017010 chen 75.9 74.6 77.5
    测试数据
     1 /* 
     2 测试数据 student1.txt
     3 201701 zhao 29.8 85.4 65.4
     4 201702 qian 96.5 87.5 65.4
     5 201703 sun 85.4 65.4 84.6
     6 201704 li 63.4 95.4 86.3
     7 201705 zhou 65.9 84.6 97.5
     8 201706 wu 39.8 85.4 75.4
     9 201707 zheng 96.5 82.5 75.9
    10 201708 wang 75.4 85.4 94.6
    11 201709 feng 73.4 95.4 56.3
    12 2017010 chen 75.9 74.6 77.5
    13  */
    14 #include <stdio.h>
    15 struct student{
    16     long no;
    17     char name[20];
    18     float score[3];
    19 }stu[10],*p;
    20 
    21 int rank(float *ave, float score){
    22     int rank = 10;
    23     for(int i=0; i<10; ++i)
    24         if(ave[i]<score)
    25             rank--;
    26     return rank;
    27 }
    28 
    29 int main()
    30 { 
    31     FILE *fp;
    32     fp = fopen("d:\student1.txt","r");
    33     float ave[10] = {0.0};
    34     int index = 0;
    35     for(p=stu; p<stu+10; ++p){ //stu+10
    36         fscanf(fp,"%ld %s %f %f %f",
    37             &p->no,p->name,&p->score[0],&p->score[1],&p->score[2]);
    38         ave[index++] = (p->score[0]+p->score[1]+p->score[2])/3.0;
    39     }
    40     printf("Number:  Name:        score:       Average:  rank:
    ");
    41     for(index = 0, p=stu; p<stu+10; ++p){
    42         printf("%-9ld%-10s%-5.1f%-5.1f%-8.1f%-10.1f%-d
    ",
    43             p->no,p->name,p->score[0],p->score[1],p->score[2],ave[index++],rank(ave,ave[index]));
    44     }
    45     fclose(fp);
    46     /* ---------------------------------------------- */
    47     fp = fopen("d:\student2.txt","w");
    48     for(index = 0, p=stu; p<stu+10; ++p){
    49         fprintf(fp,"%-9ld%-10s%-5.1f%-5.1f%-8.1f%-10.1f%-d
    ",
    50             p->no,p->name,p->score[0],p->score[1],p->score[2],ave[index++],rank(ave,ave[index]));
    51     }
    52     fclose(fp);
    53     return 0;
    54  }
    View Code

    41、输入并保存5名职工的信息,计算并输出平均工资,再按照工资从高到低的顺序输出

     1 #include <stdio.h> 
     2 #include <stdlib.h>
     3 typedef struct employee EMPLOYEE;
     4 struct employee{
     5     int no;
     6     char name[10];
     7     double salary;
     8 };
     9 int compare(const void * a,const void *b)/*比较函数*/
    10 {
    11     return (*(EMPLOYEE*)a).salary < (*(EMPLOYEE*)b).salary;
    12 }
    13 
    14 int main()
    15 {
    16     double sum = 0;
    17     EMPLOYEE e[5];
    18     for(int i=0; i<5; ++i){
    19         scanf("%d%s%lf",&e[i].no,e[i].name,&e[i].salary);
    20         sum += e[i].salary;
    21     }
    22     qsort(e,5,sizeof(EMPLOYEE),compare);
    23     printf("The Average=%-6.1f
    ",sum/5);
    24     for(int i=0; i<5; ++i){
    25         printf("%-10d%-10s%-6.1f
    ",e[i].no,e[i].name,e[i].salary);
    26     }        
    27     return 0;
    28 }
    View Code
     1 #include <stdio.h> 
     2 typedef struct employee EMPLOYEE;
     3 struct employee{
     4     int no;
     5     char name[10];
     6     double salary;
     7 };
     8 /* 选择排序 */
     9 void sortSalary(EMPLOYEE e[]){
    10     for(int i=0; i<5-1; ++i)
    11         for(int j=i+1; j<5; ++j)
    12             if(e[i].salary<e[j].salary)
    13             {
    14                 EMPLOYEE t = e[i];
    15                 e[i] = e[j];
    16                 e[j] = t;
    17             }
    18 }
    19 
    20 int main()
    21 {
    22     double sum = 0;
    23     EMPLOYEE e[5];
    24     for(int i=0; i<5; ++i){
    25         scanf("%d%s%lf",&e[i].no,e[i].name,&e[i].salary);
    26         sum += e[i].salary;
    27     }
    28     sortSalary(e);
    29     printf("The Average=%-6.1f
    ",sum/5);
    30     for(int i=0; i<5; ++i){
    31         printf("%-10d%-10s%-6.1f
    ",e[i].no,e[i].name,e[i].salary);
    32     }        
    33     return 0;
    34 }
    View Code
    1 测试数据
    2 19610901 zhao 8524
    3 20170205 qian 6605
    4 20100320 sun 6750
    5 19730205 wang 8230
    6 19800725 zhou 7590
    测试数据

    42、编写程序键盘输入整型变量n,从低位开始取出整型变量n偶数位上的数依次放在整型变量s中,然后输出s

     1 #include<stdio.h>
     2 
     3 int main()
     4 {
     5     int n;
     6     scanf("%d",&n);
     7     int s=0,i=1;
     8     while(n)
     9     {
    10         n/=10;
    11         s += n%10*i;
    12         i *= 10;
    13         n/=10;
    14     }
    15     printf("%d",s);
    16     return 0;
    17 }
    View Code
     1 #include <stdio.h> 
     2 #define N 100
     3 int main()
     4 {
     5     int n,s=0,i=1;//n的偶数位依次放在s中
     6     scanf("%d",&n);
     7     while(n>10)//确保不取奇数位
     8     {
     9         s += i*(n/10%10); //取偶数位累加    
    10         n /= 100;         //去两位
    11         i *= 10;          //升位
    12     }  
    13     printf("%d
    ",s);
    14     return 0;
    15 }
    View Code
     1 #include <stdio.h> 
     2 #define N 100
     3 int main()
     4 {
     5     int s;
     6     scanf("%d",&s);
     7   
     8     int arr[N] = {0};
     9     int i=0;
    10     while(s>10)
    11     {
    12         arr[i++] = s/10%10;    
    13         s /= 100;
    14     }
    15     for(int j=i-1; j>=0; --j)
    16         printf("%d",arr[j]);
    17     return 0;
    18 }
    View Code

    其他课程相关

    1、字符串a下标值为偶数的元素由小到大排序,其他元素不变

     1 #include<stdio.h>
     2 int main()
     3 {
     4     char a[]="labchmfye",t;
     5     int len = 9;
     6     
     7     int i,j;
     8     for(i=0;i<len-2;i+=2)
     9     {
    10         for(j=i+2;j<len; j+=2){
    11             if(a[i]>a[j])
    12             {
    13                 t=a[i];
    14                 a[i]=a[j];
    15                 a[j]=t;
    16             }
    17         }        
    18     }        
    19     puts(a);
    20     printf("
    ");
    21     return 0;
    22 }

    2、十进制正整数转换为二进制1

     1 /*位运算*/
     2 #include <stdio.h>
     3 void fun(int x)
     4 {
     5     int j=31;/*按32位定义*/   
     6     while(!((x&(1<<j))>>j))/*去前面的0*/
     7         j--;
     8     for(; j>=0; j--)/*按位输出*/
     9         printf("%d", (x&(1<<j))>>j);
    10 }
    11 int main()
    12 {
    13     int x;
    14     scanf("%d",&x);
    15     fun(x);
    16     return 0;
    17 }

    3、十进制正整数转换为二进制2

     1 #include <stdio.h>
     2 #define N 100
     3 void fun(int n, int base)
     4 {
     5     int num[N] = {0}, i=0;
     6     while(n)
     7     {
     8         num[i++] = n%base;        
     9         n /= base;
    10     }
    11     for(int j=i-1;j>=0; --j)
    12         printf("%d",num[j]);
    13 }
    14 int main()
    15 {    
    16     int n;
    17     scanf("%d",&n);
    18     fun(n,2);
    19     return 0;
    20 }

    4、螺旋二维数组

     1 #include<stdio.h>
     2 int main()
     3 {   /* 6*6二维数组,i,j控制行列,x,y控制元素 */
     4     int arr[6][6]={0},count = 1,i,j,x,y;
     5     /* 输入 */
     6     for(i=0,j=5;i<=5&&j>=0; ++i,--j)
     7     {   //左右(x不变,y变)
     8         for(x=y=i; y<=j; ++y){
     9             arr[x][y] = count++;
    10         }
    11         //上下(x变,y不变)
    12         for(x=i+1; x<=j; ++x){
    13             arr[x][y-1] = count++;
    14         }
    15         //右左(x不变,y变)
    16         for(y=j-1; y>=i; --y){
    17             arr[x-1][y] = count++;
    18         }
    19         //下上(x变,y不变)
    20         for(x=j-1; x>i; --x){
    21             arr[x][y+1] = count++;
    22         }
    23     }
    24     /* 输出 */
    25     for(int m=0; m<=5; ++m){
    26         for(int n=0; n<=5; ++n){
    27             printf("%4d",arr[m][n]);
    28         }
    29         printf("
    ");
    30     }
    31     return 0;
    32  }

    5、整形变量的交换,位异或

     1 #include <stdio.h>
     2 int main()
     3 {
     4     int a=49,b=37;
     5     a = a^b;//a包含b
     6     b = a^b; //取出a赋值给b
     7     a = a^b; //取出b赋值给a
     8     printf("%d %d",a,b);
     9     return 0;
    10 }
  • 相关阅读:
    AIoT 2020 年分析
    TensorFlow解析常量、变量和占位符
    TensorFlow编程结构
    对端边缘云网络计算模式:透明计算、移动边缘计算、雾计算和Cloudlet
    Harmony生命周期
    立体显示与BCN双稳态手性向列相
    显示技术示例
    SystemML大规模机器学习,优化算子融合方案的研究
    改天有空看看 1
    gradle 123123
  • 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/11727392.html
Copyright © 2011-2022 走看看