c语言程序设计精髓(哈尔滨工业大学)

仅作参考,请指正

一、编程题

1、学分绩计算

#include  <stdio.h>
int main()
{
    printf("Input math1, English and math2:");
    int math1, English,math2;
    scanf("%d,%d,%d",&math1,&English,&math2);
    double score =(math1 * 5 + English * 1.5 + math2 * 3.5) / 10;
    printf("Final score = %.2f
",score);
    return 0;
}
View Code

2、一尺之捶,日取其半

#include  <stdio.h>
#include <math.h>
int main()
{
    printf("Input length and days:");
    float length;
    int days;
    scanf("%f,%d",&length,&days);
    printf("length=%.5f
",length*pow(0.5,days));
    return 0;
}
View Code

3、网购打折商品V1.0

#include  <stdio.h>
int main()
{
    printf("Input payment p:");
    float payment,price;
    scanf( "%f",&payment);
    price = payment*(1-0.08);
    printf("price = %.1f
",price);
    return 0;
}
View Code

4、计算时间差V1.0

#include <stdio.h>
//#include <math.h>
#include <stdlib.h>
int main()
{
      int h1,h2,m1,m2,r;
      printf("Input time one(hour, minute):");
      scanf("%d,%d",&h1,&m1);
      printf("Input time two(hour, minute):");
      scanf("%d,%d",&h2,&m2);
      r = abs((h1*60+m1) - (h2*60+m2));
      printf("%d hour %d minute
", r/60,r%60);
      return 0;
}
View Code

5、分数比较

#include <stdio.h>
#include <math.h>
int main()
{
    printf("Input a/b, c/d:");
    int a,b,c,d;
    scanf("%d/%d,%d/%d",&a,&b,&c,&d);
    double e = (double)a/b-(double)c/d;
    if(fabs(e)<=1e-6){
        printf("%d/%d=%d/%d
",a,b,c,d);
    }
    else if(e>1e-6){
        printf("%d/%d>%d/%d
",a,b,c,d);
    }
    else{
        printf("%d/%d<%d/%d
",a,b,c,d);
    }

    return 0;
}
View Code

6、存款利率计算器v2.0

#include <stdio.h>
#include <math.h>
int main()
{
    printf("Input rate, year, capital:");
    int year;
    double rate,capital;
    scanf("%lf,%d,%lf",&rate,&year,&capital);
    printf("Compound interest (Y/N)?" );
    char ch;
    scanf(" %c",&ch);
    if(ch=='Y'||ch=='y'){
        printf("deposit = %.4f
",capital*pow((1+rate),year));
    }
    else{
        printf("deposit = %.4f
",capital* (1 + rate * year));
    }

    return 0;
}
View Code

7、存款利率计算器v3.0

#include <stdio.h>
#include <math.h>
int main()
{
    //1.
    printf("Input capital, year:");
    double capital,rate,deposit;
    int year;
    scanf( "%lf,%d",&capital,&year);

    //2.
    int flag = 0;
    if( year == 1){
        rate = 0.0225;
    }
    else if(year == 2){
        rate = 0.0243;
    }
    else if(year == 3){
        rate = 0.0270;
    }
    else if(year == 5){
        rate =  0.0288;
    }
    else if(year == 8){
        rate = 0.0300;
    }
    else{
        flag = 1;
    }

    //3.
    printf("Compound interest (Y/N)?");
    char ch;
    scanf(" %c",&ch);
    if(ch=='Y'||ch=='y'){
        deposit = capital*pow((1+rate),year);
    }
    else{
        deposit = capital* (1 + rate * year);
    }

    //4.
    if(flag){
        printf( "Error year!
");
    }
    else{
        printf("rate = %.4f, deposit = %.4f
",rate,deposit);
    }

    return 0;
}
View Code

8、博弈论之Best Response

#include <stdio.h>
#include <math.h>
int main()
{
    printf("Input percent of A and B:");
    double aPercent,bPercent;
    scanf("%lf%lf",&aPercent,&bPercent);
    double complete = 10*aPercent+6*bPercent;
    double standard = 8*aPercent+10*bPercent;
    printf("compete = %.4f
standard = %.4f
",complete,standard);

    if(complete-standard>1e-6){
        printf("The Best Response is compete!");
    }
    else{
        printf("The Best Response is standard!");
    }

    return 0;
}
View Code

9、马克思手稿中的趣味数学题

#include<stdio.h>
int main()
{
    int men,women,baby;
    printf("Man   Women   Children
"); //!
    for(men=0;men<=10;men++)//men+women+baby=30  3men+2women+baby=50
    {
        women=20-2*men;
        baby=30-men-women;
        if(3*men+2*women+baby==50)
        { //printf("Man   Women   Children
"); //
            printf("%3d%8d%8d
",men,women,baby);
            //break;
        }
    }
    return 0; //!
}
View Code

10、猜神童年龄

#include <stdio.h>
int tenBit(int n,int *a);
int main()
{
    for(int x=10;x<=22;++x)
    {
        int a[10]={0};
        int boo = tenBit(x*x*x,a)&&tenBit(x*x*x*x,a);
        if(boo){
            printf("age=%d
",x);
            break;
        }
    }
    return 0;
}

int tenBit(int n,int *a){
    while(n)
    {
        if(++a[n%10]>1)
            return 0;
        n/=10;
    }
    return 1;
}
View Code

11、闰年相关的问题v3.0——计算有多少闰年

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

int main()
{
    int count=0,birthYear,thisYear;
    printf("Input your birth year:");
    scanf("%d",&birthYear);
    printf("Input this year:");
    scanf("%d",&thisYear);
    for(int i=birthYear;i<=thisYear;++i){
        if(i%4==0&&i%100!=0||i%400==0){
            printf("%d
",i);
            count++;
        }

    }
    printf("count=%d
",count);
    return 0;
}
View Code

12、闰年相关的问题v4.0——计算心跳数

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

int main()
{
    int birthYear,thisYear,cnt=0;

    printf("Input your birth year:");
    scanf("%d",&birthYear);
    printf("Input this year:");
    scanf("%d",&thisYear);
    int year = thisYear-birthYear;

    for(int i=birthYear;i<thisYear;++i){
        if(i%4==0&&i%100!=0||i%400==0){
            cnt++;
        }
    }

    printf("The heart beats in your life: %lu",(year*365+cnt)*24*60*75);
    return 0;
}
View Code

13、计算阶乘的和v2.0

#include  <stdio.h>
long Fact(int n);
int main()
{
    for(int m=100;m<=999;++m)
    {
        int a = m/100;
        int b = m/10%10;
        int c = m%10;
        if(m==Fact(a)+Fact(b)+Fact(c)){
            printf("%d
",m);
        }
    }

    return 0;
}
long Fact(int n){

    int result = 1;
    for(int i = 1;i<=n; ++i){
        result *= i;
    }

    return result;
}
View Code

14、计算最大的三位约数

#include  <stdio.h>
int Func(int n);
int main()
{
    printf("Input n:");
    int n;
    scanf("%d",&n);
    if(n<1000||n>1000000){
        printf("Input error!
");
    }
    else{
        printf("%d
",Func(n));
    }

    return 0;
}
int Func(int n){
    for(int i=999; i>=100; --i){
        if(n%i==0)
            return i;
    }
    return 1;
}
View Code

15、孔融分梨

#include <stdio.h>
int Gcd(int a, int b);
int main()
{
     printf("Input m,n:");
     int m, n, x;
     scanf("%d,%d", &m, &n);
     x = Gcd(m,n);

     //if ( m<1 || n>10000)
     if(-1 == x)
     {
        printf("Input error!
");
     }
     else
     {
        printf("%d/%d
", m/x,n/x);
     }

     return 0;
}

int Gcd(int a, int b)
{
     int r;
     //if (a<=0 || b<=0){
     if ( a<1 || b>10000){
        return -1;
     }

     do{
          r = a % b;
          a = b;
          b = r;
     }while (r!=0);

     return  a;
}
View Code

16、素数求和

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

int IsPrime(int x);

int main()
{
     printf("Input n:");
     int n;
     scanf("%d",&n);

     int sum =0;
     while(n>=2){
        if(IsPrime(n)){
            sum += n;
        }
        n--;
     }

     printf("sum=%d
",sum);

     return 0;
}

int IsPrime(int x){
    for(int i=2;i <= sqrt(x);++i){
        if(x%i==0){
            return 0;
        }
    }
    return 1;
}
View Code

17、n层嵌套平方根的计算

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

double Y(double x, int n)
{
    if (n == 0){
        return 0;
    }
    else{
        return (sqrt(x +Y(x,n-1)));
    }
}

int main()
{
    printf("Please input x and n:");
    double x;
    int n;
    scanf("%lf,%d",&x,&n);

    printf("Result=%.2f
",Y(x,n));

    return 0;
}
View Code

18、递归法求和

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

int sum(int n){
    if(n==1){
        return 1;
    }
    else{
        return n+sum(n-1);
    }
}

int main()
{
    printf("Please input n:");
    int n;
    scanf("%d",&n);
    if(n<1){
        printf("data error!
");
    }
    else{
        printf("sum=%d
",sum(n));
    }

    return 0;
}
View Code

19、猴子吃桃程序_扩展3

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

int Monkey(int n, int x);

int main()
{
    printf("Input days n:");
    int n;
    scanf("%d",&n);

    int x = 1;
    printf("x=%d
",Monkey(n,x));

    return 0;
}

int Monkey(int n, int x){
    if(n==1){
        return x;
    }
    else{
        return Monkey(n-1,2*(x+1));
    }
}
View Code

20、网购打折商品V2.0

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

float Price(float payment);
int main()
{
    printf("Input payment:");
    float payment;
    scanf("%f",&payment);

    printf("price = %.1f
",Price(payment));

    return 0;
}

float Price(float payment){

    float rate;
    int c = payment/100;
    if(c<1){
        rate = 0.0;
    }
    else if(c<2&&c>=1){
        rate = 0.05;
    }
    else if(c<5&&c>=2){
        rate = 0.08;
    }
    else if(c<10&&c>=5){
        rate = 0.1;
    }
    else{
        rate = 0.15;
    }

    return payment - payment * rate;
}
View Code

21、摘苹果

#include <stdio.h>

int GetApple(int a[], int height, int n);
int main()
{
    int a[10]={0};
    for(int i=0;i<10;++i){
        scanf("%d",&a[i]);
    }

    int height;
    scanf("%d",&height);
    height += 30;
    int n = 10;

    int cnt = GetApple(a,height,n);
    printf("%d",cnt);

    return 0;
}

int GetApple(int a[], int height, int n){
    int cnt = 0;
    for(int i=0;i<n;++i){
        if(a[i]<=height){
            cnt++;
        }
    }
    return cnt;
}
View Code

22、好数对

#include <stdio.h>

int main()
{
    int a[1000] = {0};
    int n;
    scanf("%d",&n);

    for(int i=0;i<n;++i){
        scanf("%d",&a[i]);
    }

    int count = 0;
    for(int i=0;i<n-1;++i)
        for(int j=i+1;j<n;++j)
            for(int k=0; k<n; ++k)
                if(a[k] == a[i]+a[j])
                    count++;
    printf("%d",count);

    return 0;
}
View Code

23、组合三位数

#include<stdio.h>

int f(int array[],int a)
{
    while(a)
    {
        array[a%10]++;
        if(array[a%10]>1)
        {
            return 0;
        }
        a/=10;
    }
    return 1;
}

int main()
{
    int num1=0,num2=0,num3=0;
    for(int i=102;num1=i,num2=i*2,num3=i*3,i*=3,i<987;i=num1+1)
    {
        int array[10]={0};
        if(f(array,num1)&&f(array,num2)&&f(array,num3))
        {
            printf("%d,%d,%d
",num1,num2,num3);
        }
    }
    return 0;
}
View Code

24、求100以内的最大素数

#include<stdio.h>
#include<math.h>
int isPrime(int n){
    int k=(int)sqrt(n);
    for(int i=2;i<=k;i++){
        if(n%i==0){
            return 0;
        }
    }
    return 1;
}
int main()
{
    printf("Input n(n<=500):");
    int n;
    scanf("%d",&n);

    int count = 0,sum = 0;
    for(int i=n;i>1&&count!=10;--i){
        if(isPrime(i)){
            printf("%6d",i);
            count++;
            sum+=i;
        }
    }
    printf("
sum=%d
",sum);

    return 0;
}
View Code

25、重复数字检查

#include  <stdio.h>
int CountRepeatNum(int count[], int n);
int main()
{
    printf("Input n:
");
    long int n;
    scanf("%ld",&n);
    int num[10]={0};

    if(CountRepeatNum(num,n)){
        printf("Repeated digit!
");
    }
    else{
        printf("No repeated digit!
");
    }

    return 0;
}
int CountRepeatNum(int count[], int n)
{
    while(n)
    {
        if(++count[n%10]>1){
            return 1;
        }
        n/=10;
    }
    return 0;
}
View Code

26、教授的课

#include  <stdio.h>
#define N 32
int IsCancel(int a[], int n, int k);
int main()
{
    printf("Input n,k:
");
    int n,k;
    scanf("%d,%d",&n,&k);
    int array[N]={0};
    for(int i=0;i<n;++i){
        scanf("%d",&array[i]);
    }

    if(IsCancel(array,n,k)){
        printf("YES");
    }
    else{
        printf("NO");
    }

    return 0;
}
int IsCancel(int a[], int n, int k){
    int cnt=0;
    for(int i=0;i<n;++i){
        if(a[i]<=0){
            cnt++;
        }
    }
    if(cnt<k){
        return 1;
    }
    return 0;
}
View Code

27、寻找鞍点

#include  <stdio.h>
#define N 32
void FindSaddlePoint(int a[][N], int m, int n);
int main()
{
    printf( "Input m,n:
");
    int m,n;
    scanf("%d,%d",&m,&n);

    printf( "Input matrix:
" );
    int array[N][N]={0};
    for(int i=0;i<m;++i){
        for(int j=0;j<n;++j){
            scanf("%d",&array[i][j]);
        }
    }
    FindSaddlePoint(array,m,n);

    return 0;
}
void FindSaddlePoint(int a[][N], int m, int n)
{
    int find = 0;
    for(int i=0;i<m;++i)
    {
        int rowPos=i,columnPos=0,flag = 1;
        for(int j=1;j<n;++j)
        {
            if(a[i][j]>a[rowPos][columnPos]){
                rowPos = i;
                columnPos = j;
            }
        }
        for(int k=0;k<m;++k){
            if(a[k][columnPos]<a[rowPos][columnPos]){
                flag = 0;
                break;
            }
        }
        if(flag){
            printf("a[%d][%d] is %d
",rowPos,columnPos,a[rowPos][columnPos]);
            find = 1;
        }
    }
    if(!find){
        printf("No saddle point!
");
    }
}
View Code

28、计算三位阶乘和数

#include  <stdio.h>

int main()
{
    int array[10]={0};
    int result = 1;
    array[0]=1;
    for(int i=1;i<10;++i){
        result *= i;
        array[i] = result;
    }

    for(int j=100;j<=999;++j){
        int a = j/100,b=j/10%10,c=j%10;
        if(j==array[a]+array[b]+array[c]){
            printf("%ld
",j);
        }
    }

    return 0;
}
View Code

29、数字字符串转换为整型数

#include<stdio.h>
int Myatoi(char str[]);
int main(void)
{
    printf("Input a string:");
    char a[10] = "";
    scanf("%7s",a);
    printf("%d
",Myatoi(a));

    return 0;
}
int Myatoi(char str[]){
    int sum = 0;
    for(char *p = str; *p!=''; ++p){
        if(*p>=48&&*p<=57){
            sum = sum*10 + *p-48;
        }
    }
    return sum;
}
View Code

30、查找子串

#include<stdio.h>
#include<string.h>
int SearchString(char s[], char d[]);
int main(void)
{
    char a[80] = "",b[40] = "";
    printf("Input a string:");
    gets(a);
    printf("Input another string:");
    gets(b);

    int ret = SearchString(a,b);
    if(-1 == ret){
        printf( "Not found!
");
    }
    else{
        printf("Searching results:%d
",ret);
    }

    return 0;
}
int SearchString(char a[], char b[]){
    char *s1 = NULL, *s2 = NULL, *pa = a;
    while(*pa)/**< pa指向了字符串a的尾部退出循环*/
    {
        s1 = pa;
        s2 = b;
        while(*s1&&*s2&&!(*s1-*s2)){
            s1++, s2++;
        }
        if(!*s2)/**< s1指向了字符串a的尾部找到了*/
            return pa - a + 1;
        pa++;/**< 指针pa右移 */
    }
    return -1;
}
View Code

31、统计重复字符

#include<stdio.h>
#include<string.h>
int CountRepeatStr(char str[], int *tag);
int main(void)
{
    char a[80] = "";
    int tag = 0;
    printf("Input a string:
");
    gets(a);
    int max = CountRepeatStr(a,&tag);
    printf("%c:%d
",a[tag],max);
    return 0;
}
int CountRepeatStr(char str[], int *tag)
{
    int max = 1,count = 1;
    for(int i=1;i<strlen(str);++i)
    {
        if(str[i-1] == str[i]){
            count++;
        }

        if(count>max){
            max = count;
            *tag = i;
        }

        if(str[i-1] != str[i]){
            count = 1;
        }
    }
    return max;
}
View Code

32、凯撒密码

#include<stdio.h>
#include<string.h>
#define N 100
void Caesar(char c[]);
int main(void)
{
    char a[N] = "";
    printf("Input a string:");
    gets(a);
    Caesar(a);
    return 0;
}
void Caesar(char c[])
{
    char b[N] = "";
    for(int i=0;i<strlen(c);++i){
        b[i]=(c[i]-'a'+3)%26 + 'a';
    }
    puts(b);
}
View Code

33、山地训练

#include <stdio.h>
#include <string.h>
#define N 5000

long Fun(long M, long T, long U, long F, long D, char str[]);
int main()
{
    printf("Input M,T,U,F,D:");
    long M,T,U,F,D;
    scanf( "%ld%ld%ld%ld%ld",&M,&T,&U,&F,&D);

    printf("Input conditions of road:");
    char str[N] = {0};
    scanf("%s",str);

    long num = Fun(M,T,U,F,D,str);
    printf("num=%ld
",--num); /**< 难道路段计数从0开始! */

    return 0;
}
long Fun(long M, long T, long U, long F, long D, char str[])
{
    long int sum = 0, num = 0;
    while(M>sum&&num<T)/**< 往返时间足够,且最多跑完T个路段 */
    {
        if(str[num]=='u'||str[num]=='d'){
            sum += U+D;
        }
        else{
            sum += 2*F;
        }
        num++;/**< 往返一次 */

        if(M<sum){ /**< 时间不够用了,num-- */
            num--;
        }
    }

    return num;
}
View Code

34、奇偶数分离

#include <stdio.h>
#include <string.h>
#define N 100
void Seperate(int a[], int n);
int main()
{
    printf("Input n:");
    int n;
    scanf("%d",&n);
    printf("Input numbers:");
    int a[N] = {0};
    for(int i=0; i<n; ++i){
        scanf("%d",&a[i]);
    }

    Seperate(a,n);

    return 0;
}
void Seperate(int a[], int n)
{
    int cnt = 0;
    for(int i=0; i<n; ++i){
        if(a[i]%2!=0){
            cnt++;
            if(cnt!=1){
                printf(",");
            }
            printf("%d",a[i]);

        }
    }
    printf("
");
    cnt = 0;
    for(int i=0; i<n; ++i){
        if(a[i]%2==0){
            cnt++;
            if(cnt!=1){
                printf(",");
            }
            printf("%d",a[i]);
        }
    }
}
View Code

35、子串判断

#include <stdio.h>
#include <string.h>
#define N 100
int IsSubString(char a[], char b[]);
int main()
{
    printf( "Input the first string:");
    char a[N] = "";
    gets(a);
    printf("Input the second string:");
    char b[N] = "";
    gets(b);
    if(IsSubString(a,b)){
        printf("Yes
");
    }
    else{
        printf( "No
");
    }

    return 0;
}
int IsSubString(char a[], char b[])
{
    if(strstr(a,b)){
        return 1;
    }
    return 0;
}
View Code

36、星期查找

#include <stdio.h>
#include <string.h>
#define N 100
int IsSubString(char a[], char b[]);
int main()
{
    char *weekday[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    printf("Please enter a string:
");
    char b[N] = "";
    gets(b);
    int flag = 0;
    for(int i=0; i<7;++i){
        if(strcmp(weekday[i],b)==0){
            printf("%s is %d
",weekday[i],i);
            flag = 1;
        }
    }
    if(!flag){
        printf("Not found!
");
    }

    return 0;
}
View Code

37、计算时间差V2.0

#include <stdio.h>
#include <stdlib.h>
typedef struct clock
{
    int hour;
    int minute;
    int second;
} CLOCK;
CLOCK CalculateTime(CLOCK t1, CLOCK t2);
int main()
{
    CLOCK time1,time2;
    printf("Input time one:(hour,minute):");
    scanf("%d,%d",&time1.hour,&time1.minute);
    printf( "Input time two: (hour,minute):");
    scanf("%d,%d",&time2.hour,&time2.minute);
    CLOCK c = CalculateTime(time1,time2);
    printf("%dhour,%dminute
",c.hour,c.minute);
    return 0;
}
CLOCK CalculateTime(CLOCK t1, CLOCK t2)
{
    CLOCK res;
    int sum = abs((t1.hour * 60 + t1.minute) - (t2.hour * 60 + t2.minute));
    res.hour = sum / 60;
    res.minute = sum%60;
    return res;
}
View Code

38、奖学金发放

#include <stdio.h>
#define N 100
typedef struct winners
{
    char name[20];
    int finalScore;
    int classScore;
    char work;
    char west;
    int paper;
    int scholarship;
} WIN;

void Addup(WIN stu[], int n);
int FindMax(WIN student[], int n);
int main()
{
    int n;
    printf("Input n:");
    scanf("%d",&n);
    WIN stu[N];
    Addup(stu,n);
    int index = FindMax(stu,n);
    printf("%s get the highest scholarship %d
",stu[index].name,
           stu[index].scholarship);
    return 0;
}
void Addup(WIN stu[], int n)
{

    for(int i = 0; i<n; ++i)
    {
        printf("Input name:");
        scanf("%s",stu[i].name);

        printf("Input final score:");
        scanf("%d",&stu[i].finalScore);

        printf("Input class score:");
        scanf("%d",&stu[i].classScore);

        printf("Class cadre or not?(Y/N):");
        scanf(" %c",&stu[i].work);

        printf("Students from the West or not?(Y/N):");
        scanf(" %c",&stu[i].west);

        printf("Input the number of published papers:");
        scanf("%d",&stu[i].paper);

        stu[i].scholarship = 0;
        if(stu[i].finalScore>80&&stu[i].paper>=1){
            stu[i].scholarship += 8000;
        }
        if(stu[i].finalScore>85&&stu[i].classScore>80){
            stu[i].scholarship += 4000;
        }
        if(stu[i].finalScore>90){
            stu[i].scholarship += 2000;
        }
        if(stu[i].finalScore>85&&stu[i].west=='Y'){
            stu[i].scholarship += 1000;
        }
        if(stu[i].classScore>80&&stu[i].work=='Y'){
            stu[i].scholarship += 850;
        }
        printf("name:%s,scholarship:%d
",stu[i].name,stu[i].scholarship);
    }
}
int FindMax(WIN student[], int n)
{
    int index = 0;
    for(int i =1; i<n; ++i){
        if(student[i].scholarship>student[index].scholarship){
            index = i;
        }
    }
    return index;
}
View Code

39、评选最牛群主v1.0

#include <stdio.h>
#include <string.h>
#define N 3
typedef struct vote
{
    char name[20];
    int cnt;
} VOTE;

int main()
{
    enum{tom,jack,rose};
    VOTE v[N] = {{"tom",0},{"jack",0},{"rose",0}};

    printf("Input the number of electorates:");
    int n;
    scanf("%d",&n);

    char name[20];
    for(int i=0; i<n; ++i){
        printf("Input vote %d:",i+1);
        scanf("%s",name);
        if(strcmp(name,v[tom].name)==0){
            v[tom].cnt++;
        }
        else if(strcmp(name,v[jack].name)==0){
            v[jack].cnt++;
        }
        else if(strcmp(name,v[rose].name)==0){
            v[rose].cnt++;
        }
    }
    printf("Election results:
");
    int index=0, max = 0;
    for(;index<3; ++index){
        printf("%s:%d
",v[index].name,v[index].cnt);
        if(v[index].cnt>v[max].cnt) max = index;
    }
    printf("%s wins
",v[max].name);

    return 0;
}
View Code

40、星期判断

#include <stdio.h>
#include <string.h>
int main()
{
    char ch1, ch2 = '';
    char *weekday[] = {"sunday","monday","tuesday",
        "wednesday","thursday","friday","saturday"};
    char **p = weekday;

    printf("please input the first letter of someday:
");
    scanf(" %c",&ch1);

    if(ch1>='A'&&ch1<='Z'){
        ch1 += 32;
    }

    int n = 1;
    if('s'== ch1||'t'== ch1){
        n = 2;
        printf("please input second letter:
");
        scanf(" %c",&ch2);
    }

    char str[3] = {ch1,ch2};
    int cnt = sizeof(weekday)/sizeof(char*);
    int flag = 0;

    while(cnt--)
    {
        if(strncmp(*p,str,n)==0){
            printf("%s
",*p);
            flag = 1;
            break;
        }
        p++;
    }

    if(!flag){
        printf("data error
");
    }

    return 0;
}
View Code

二、练兵区

1、hello world!

#include  <stdio.h>
int main()
{
    printf("hello world!
");
    return 0;
}
View Code

2、在屏幕上输出多行信息

#include  <stdio.h>
int main()
{
    printf("hello world!
hello hit!
hello everyone!
");
    return 0;
}
View Code

3、计算半圆弧长及半圆的面积

#include  <stdio.h>
#define  PI  3.14159
#define  R   5.3
int main()
{
    printf( "Area=%.5f
", PI * R * R/2);
    printf("circumference=%.5f
", 2 * PI * R/2);
    return 0;
}
View Code

4、计算长方体体积

#include  <stdio.h>
int main()
{
    const double l = 1.2;
    const double w = 4.3;
    const double h = 6.4; 
    printf( "volume=%.3f
", l * w * h);
    return 0;
}
View Code

5、输出逆序数

#include <stdio.h>
#include <math.h>
int main()
{
    int  x,y = 0;
    printf("Input x:
");
    scanf("%d",&x);
    x = fabs(x);
    y = y*10 + x%10;
    x/=10;
    y = y*10 + x%10;
    x/=10;
    y = y*10 + x%10;
    printf("y=%d
",y);
    return 0;
}
View Code

6、计算总分和平均分

#include <stdio.h>
#include <math.h>
int main()
{
    double total,average;
    total = (86+74+92+77+82)*0.3 + (81+87+90+62+88)*0.7;
    average = total/5;
    printf("total=%.2f
",total);
    printf("average=%.2f
",average);
    printf("average=%d
",(int)average);

    return 0;
}
View Code

7、存款利率计算器V1.0

#include <stdio.h>
#include <math.h>
int main()
{
    double rate,capital;
    int n;
    printf("Please enter rate, year, capital:
");
    scanf("%lf,%d,%lf", &rate, &n, &capital);
    double deposit = capital*pow((1+rate),n);
    printf("deposit=%.3f
",deposit);

    return 0;
}
View Code

8、数位拆分v1.0

#include <stdio.h>
#include <math.h>
int main()
{
    int n= 4321;
    int a = n/100;
    int b = n%100;
    printf("a=%d,b=%d
",a,b);
    printf("a+b=%d
",a+b);
    printf("a-b=%d
",a-b);
    printf("a*b=%d
",a*b);
    printf("a/b=%.2f
",(float)a/b);
    printf("a%%b=%d
",a%b);

    return 0;
}
View Code

9、求正/负余数

#include <stdio.h>
#include <math.h>
int main()
{
    printf("negative: %d
",(-11)%5);
    printf("positive: %d
",(-11)%5+5);

    return 0;
}
View Code

10、身高预测

#include <stdio.h>
#include <math.h>
int main()
{
    int faHeight_m=175, moHeight_m=162;
    int faHeight_f=169, moHeight_f=153;
    int h1 = (faHeight_m + moHeight_m)*0.54;
    int h2 = (faHeight_f*0.923 + moHeight_f)/2;
    printf("Height of xiao ming:%d
",h1);
    printf("Height of xiao hong:%d
",h2);
    return 0;
}
View Code

11、求一元二次方程的根

#include <stdio.h>
#include <math.h>
int main()
{
    float a =2.0,b = 3.0,c=1.0;
    float x1,x2;
    x1 = -b/(2*a) + sqrt(b*b-4*a*c)/(2*a);
    x2 = -b/(2*a) - sqrt(b*b-4*a*c)/(2*a);
    printf("x1=%.4f
",x1);
    printf("x2=%.4f
",x2);
    return 0;
}
View Code

12、日期显示

#include <stdio.h>
#include <math.h>
int main()
{
     printf("Enter a date (mm/dd/yy):
");
      int m,d,y;
      scanf( "%d/%d/%d",&m,&d,&y);
      printf("You entered the date: %04d.%02d.%02d
",y,m,d);
      return 0;
}
View Code

13、产品信息格式化

#include <stdio.h>
#include <math.h>
int main()
{
      printf("Enter item number:
");
      int n;
      scanf("%d",&n);
      printf("Enter unit price:
");
      float price;
      scanf("%f",&price);
      printf("Enter purchase date (mm/dd/yy):
");
      int m,d,y;
      scanf("%d/%d/%d",&m,&d,&y);
      printf("Item      Unit     Purchase
");
      printf("%-9d$ %-9.2f%02d%02d%02d
",n,price,m,d,y);

      return 0;
}
View Code

14、计算两个数的平方和

#include <stdio.h>
#include <math.h>
int main()
{
      printf("Please input x and y:
");
      float x,y;
      scanf("%f,%f",&x,&y);
      float result = pow(x,2)+pow(y,2);
      printf("Result=%.2f
",result);

      return 0;
}
View Code

15、逆序数的拆分计算

#include <stdio.h>
#include <math.h>
int main()
{
      printf("Input x:
");
      int x;
      scanf( "%d",&x);
      x = abs(x);
      int y = 0;
      y = y*10+x%10;
      x/=10;
      y = y*10+x%10;
      x/=10;
      y = y*10+x%10;
      x/=10;
      y = y*10+x%10;
      x/=10;
      printf("y=%d
",y);
      int a,b;
      a = y/100;
      b = y%100;
      printf("a=%d,b=%d
",a,b);
      int result = pow(a,2)+pow(b,2);
      printf("result=%d
",result);

      return 0;
}
View Code

16、拆分英文名

#include <stdio.h>
#include <math.h>
int main()
{
      printf("Input your English name:
");
      char a,b,c;
      scanf( "%c%c%c",&a,&b,&c);
      printf("%c%c%c
",a-32,b,c);
      printf("%c:%d
",a,a-'a'+1);
      printf("%c:%d
",b,b-'a'+1);
      printf("%c:%d
",c,c-'a'+1);

      return 0;
}
View Code

17、计算体指数

#include <stdio.h>
#include <math.h>
int main()
{
      printf("Input weight, height:
");
      int weight,height;
      scanf( "%d,%d",&weight,&height);
      printf("weight=%d
",weight*2);
      printf("height=%.2f
",height/100.0);
      printf("t=%.2f
",weight/pow(height/100.0,2));
      return 0;
}
View Code

18、检测用户错误输入

#include <stdio.h>
#include <math.h>
int main()
{
    int a,b;
    if(2 == scanf("%d %d",&a,&b)){
        printf("a = %d, b = %d
",a,b);
    }
    else{
        printf("Input error!");
    }

    return 0;
}
View Code

19、闰年判断

#include <stdio.h>
#include <math.h>
int main()
{
    int year;
    if(1 == scanf("%d",&year)&&year>0){
        if(year%4==0&&year%100!=0||year%400==0){
            printf("Yes
");
        }
        else{
            printf("No
");
        }
    }
    else{
        printf("Input error!
");
    }

    return 0;
}
View Code

20、程序改错v1.0

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int score;
    printf("Please input score:
");

    if (scanf("%d", &score) == 0
        ||score < 0 || score > 100  )
        printf("Input error!
");
    else if (score >= 90)
        printf("grade: A
");
    else if (score >= 80)
        printf("grade: B
");
    else if (score >= 70)
        printf("grade: C
");
    else if (score >= 60)
        printf("grade: D
");
    else
        printf("grade: E
");

    return 0;
}
View Code

21、字符类型判断

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("Input simple:
");
    char ch;
    scanf("%c",&ch);
    if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z'){
        printf("It is an English character.
");
    }
    else if(ch>='0'&&ch<='9'){
        printf("It is a digit character.
");
    }
    else{
        printf("It is other character.
");
    }
    
    return 0;
}
View Code

22、快递费用计算

#include <stdio.h>
#include <math.h>
int main()
{
    int area;
    float price,weight;
    scanf("%d,%f",&area,&weight);
    int w = ceil(weight)-1;
    switch(area){
        case 0:
            price = 10+w*3;
            break;
        case 1:
            price = 10+w*4;
            break;
        case 2:
            price = 15+w*5;
            break;
        case 3:
            price = 15+w*6.5;
            break;
        case 4:
            price = 15+w*10;
            break;
        default:
            price = 0;
            printf("Error in Area
");
    }

    printf("Price: %5.2f
",price);


    return 0;
}
View Code

23、数位拆分v2.0

#include <stdio.h>
#include <math.h>
int main()
{
    printf("Please input n:
");
    int n;
    scanf("%d",&n);
    int a = n/100;
    int b = n%100;
    printf("%d,%d
",a,b);
    printf("sum=%d,sub=%d,multi=%d
",a+b,a-b,a*b);
    if(b==0){
        printf("The second operator is zero!");
    }
    else{
        printf("dev=%.2f,mod=%d
",(float)a/b,a%b);
    }

    return 0;
}
View Code

24、出租车计价

#include <stdio.h>
#include <math.h>
int main()
{
     printf("Input distance and time:");
    float distance,fee;
    int time;
    scanf("%f,%d",&distance,&time);
 
    int d = distance;
    if(d<=3){
        fee = 8;
    }
    else if(d>3&&d<=10){
        fee = 8 + (distance-3)*2;
    }
    else if(d>10){
        fee = 8 + (10-3)*2 + (distance-10)*3;
    }
 
    if(time>=5){/**< 重复代码合并处理 */
        fee +=time/5*2;
    }
    printf("fee = %.0f
" ,fee);
return 0;
}
View Code

25、数据区间判断

#include <stdio.h>
#include<math.h>
int main(){
    printf("Please enter the number:
");
    int n;
    scanf( "%d",&n);
    if(n>=10000||n<=0){
        printf("error!
");
    }
    else if(n/10==0){
        printf("%d: 0-9
",n);
    }
    else if(n/100==0){
        printf("%d: 10-99
",n);
    }
    else if(n/1000==0){
        printf("%d: 100-999
",n);
    }
    else{
        printf("%d: 1000-9999
",n);
    }

    return 0;
}
View Code

26、计算一元二次方程的根v2.0

#include <stdio.h>
#include<math.h>
int main(){
    printf("Please enter the coefficients a,b,c:
");
    float a,b,c,x1,x2;
    scanf("%f,%f,%f",&a,&b,&c);
    float delte = b*b-4*a*c;

    if(delte>1e-6||fabs(delte)<=1e-6){
        x1 = -b/(2*a)+pow(delte,0.5)/(2*a);
        x2 = -b/(2*a)-pow(delte,0.5)/(2*a);
        printf( "x1=%7.4f, x2=%7.4f
",x1,x2);
    }
    else{
        printf("error!
");
    }

    return 0;
}
View Code

27、判断一个整型数据有几位v2.0

#include  <stdio.h>
#include <stdlib.h>
int main()
{
    printf("Please enter the number:
");
    int x,cnt=0;
    int num[10]={0};

    scanf("%d",&x);
    int t = x;

    t = abs(t);
    while(t){
        num[t%10]++;
        t /=10;
        cnt++;
    }

    printf("%d: %d bits
",x,cnt);
    for(int i=0;i<10;i++){
        if(num[i]>0){
            printf("%d: %d
",i,num[i]);
        }
    }

    return 0;
}
View Code

28、奖金计算

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

int main()
{
    long profits,bonus;
    scanf("%ld",&profits);
    int bonus1,bonus2,bonus3,bonus4,bonus5;

    bonus1 = 100000*0.1;
    bonus2 = bonus1 + (200000-100000)*0.075;
    bonus3 = bonus2 + (400000-200000)*0.05;
    bonus4 = bonus3 + (600000-400000)*0.03;
    bonus5 = bonus4 + (1000000-600000)*0.015;
    
    if(profits<=100000){
        bonus = profits*0.1;
    }
    else if(profits<=200000){
        bonus = bonus1 + (profits-100000)*0.075;
    }
    else if(profits<=400000){
        bonus = bonus2 + (profits-200000)*0.05;
    }
    else if(profits<=600000){
        bonus = bonus3 + (profits-400000)*0.03;
    }
    else if(profits<=1000000){
        bonus = bonus4 + (profits-600000)*0.015;
    }
    else{
        bonus = bonus5 + (profits-1000000)*0.01;
    }


    printf("bonus=%ld
",bonus);

    return 0;
}
View Code

29、程序修改—1

#include  <stdio.h>
int main()
{
    int i, j, sum = 0, n=100;
    scanf("%d",&n);
    for (i=0,j=n; i<=j; i++,j--)
    {
       sum = sum + i + j;
    }
    printf( "sum = %d" , sum);

    return 0;
}
View Code

30、程序修改—2

#include  <stdio.h>
  int main()
  {
      int sum = 0, m;
      printf( "Input m:
");
      scanf("%d", &m);

      while (m != 0){
          sum = sum + m;
          printf( "sum = %d
", sum);
          printf( "Input m:
");
          scanf("%d", &m);
      }
      return 0;
  }
View Code

31、程序改错-1

#include <stdio.h>
  int main()
  {
      int x, y, z;
      for (x=0; x<=20; x++)
      {
          for (y=0; y<=33; y++)
          {
              z = 100 - x - y;
              if (z%3==0&&5*x + 3*y + z/3 == 100)
              {
                  printf("x=%d, y=%d, z=%d
", x, y, z);
              }
          }
      }
      return 0;
  }
View Code

32、程序改错-2

#include <stdio.h>
#include <math.h>
int main()
{
    int n, i;
    printf("Input n:
");
    scanf("%d", &n);
    int flag = 1;
    if(n<=1){
        flag = 0;
        goto EXIT;
    }
    for (i=2; i<=sqrt(n); i++)
    {
        if (n % i == 0)
        {
           flag = 0;
           break;
        }
    }
    EXIT:
    if(flag){
        printf("Yes!
");
    }
    else {
        printf("No!
");
    }

    return 0;
}
View Code

33、程序改错-3

#include <stdio.h>
  int main()
  {
    int x1, x2;
    int n;
    do{
      printf("Input x1, x2:
");

      if((n=scanf( "%d,%d", &x1, &x2))!=2){
        while(getchar()!='
');
      }
    }while (x1 * x2 >= 0||n!=2);
    printf( "x1=%d,x2=%d
", x1, x2);
   
    return 0;
  }
View Code

34、猴子吃桃程序_扩展1

#include <stdio.h>
int main()
{
    printf("Input days:
");
    int days,x=1;
    scanf("%d",&days);
    while(--days){
        x = 2*(x+1);
    }

    printf("x=%d
",x);
    return 0;
}
View Code

35、猴子吃桃程序_扩展2

#include <stdio.h>
int main()
{

    int days,x=1,n=0;

    do{
        printf("Input days:
");
        if((n=scanf("%d",&days))==0){
            while(getchar()!='
');
        }
    }while(n==0||days<1);
    while(--days){
        x = 2*(x+1);
    }

    printf("x=%d
",x);
    return 0;
}
View Code

36、6位密码输入检测

#include <stdio.h>
int main()
{
    printf("Input your password:
");
    char ch;
    int n = 6,cnt=0;
    while(n){
        scanf("%c",&ch);
        if(ch>='0'&&ch<='9'){
            n--;
            cnt++;
            printf("%c, you have enter %d-bits number
",ch,cnt);
        }
        else{
            printf("error
");
        }
        getchar();
    }

    return 0;
}
View Code

37、判断一个整型数据有几位v1.0

#include <stdio.h>
int main()
{
    printf("Please enter the number:
");
    int x,n;
    scanf("%d",&n);
    int cnt=0;
    x=n;
    while(x--){
        x/=10;
        cnt++;
    }
    printf("%d: %d bits
",n,cnt);

    return 0;
}
View Code

38、检测输入数据中奇数和偶数的个数

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

int main()
{
    printf("Please enter the number:
");

    int x,totalOdd=0,totalEven=0;
    scanf( "%d",&x);

    if(x==-1){
        printf("over!
");
    }
    
    while(x!=-1){
       if(x%2==0){
          printf("%d:even
",x);
          totalEven++;
       }
       else{
          printf("%d:odd
",x);
          totalOdd++;
       }
       scanf( "%d",&x);
    }

    printf("The total number of odd is %d
",totalOdd);
    printf("The total number of even is %d
",totalEven);

    return 0;
}
View Code

39、计算球的反弹高度

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

int main()
{
    printf("Input:
");
    int n,times=1;
    float length=100,heigh=100/2;
    scanf("%d",&n);
    while(--n){
        length +=heigh*2;
        heigh /=2;
        times++;
    }
    printf("%d times:
",times);
    printf("%.3f
",length);
    printf("%.3f
",heigh);

    return 0;
}
View Code

40、绘制金字塔

/*绘制金字塔*/
#include <stdio.h>
#include <stdlib.h>
void PrintChar(char letter){

    int line=0;
    for(;line<=letter-'A';line++)/**< 逐行打印 */
    {
        //1.输出一行"-"
        for(int i=0;i<=letter-'A'-line;i++){
            printf(" ");
        }

        //2.升序输出字母
        char ch='A';
        for(;ch<=('A'+line);ch++){
            printf("%c",ch);
        }

        //3.降序输出字母
        ch-=2;
        while(ch!='A'-1){
            printf("%c",ch--);
        }

        //4.
        printf("
");
    }
}

int main()
{
    printf("Please input a capital:
");
    char letter;
    scanf("%c",&letter);
    PrintChar(letter);

    return 0;

}
View Code

41、循环嵌套的应用

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

int main()
{
   int line=0;
    for(;line<='F'-'A';line++)/**< 逐行打印 */
    {
        char ch='F';
        for(int i=0;i<=line;++i){/**< 每行打印字母数 */
            printf("%c",ch--);
        }
        printf("
");
    }

    return 0;
}
View Code

42、利用泰勒级数计算sinx的值

#include<stdio.h>
#include<math.h>
int main()
{
    int n=1,count=1;
    float x;
    double sum,term;

    printf("Input x:
");
    scanf("%f",&x);
    sum = x;
    term = x;

    do{
        term = -term * x *x/((n+1)*(n+2));
        sum += term;
        n+=2;
        count++;
    }while(fabs(term)>=1e-5);

    printf("sin(x)=%.3f,count=%d
",sum,count);

    return 0;
}
View Code

43、计算100~200之间的所有素数之和

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

int fun(int m);

int main()
{
    int sum =0;
    for(int i=100;i<=200;++i){
        if(fun(i)){
            sum +=i;
        }
    }
    printf( "sum=%d
",sum);

    return 0;
}

int fun(int m){
    for(int i=2;i<=sqrt(m);++i){
        if(m%i==0){
            return 0;
        }
    }
    return 1;
}
View Code

44、编程实现一个输入指定范围内的整数的函数

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

int getint(int min, int max);

int main()
{
    printf("Please enter min,max:
");
    int min,max;
    scanf("%d,%d",&min,&max);

    printf("The integer you have entered is:%d
",getint(min,max));

    return 0;
}

int getint(int min, int max)
{
    int x;
    do{
        printf("Please enter an integer [%d..%d]:
",min,max);
        scanf("%d",&x);
    }while(x<min||x>max);

    return x;
}
View Code

45、程序改错v2.0

#include<stdio.h>
   int main()
   {
    int score;
    char grade;
    printf("Please input score:
");

    while(0==scanf("%d", &score) ||score < 0 || score > 100)
    {
        printf("Input error!
");
        printf("Please input score:
");
        getchar();
    }


    if (score >= 90)
        grade = 'A';
    else if (score >= 80)
        grade = 'B';
    else if (score >= 70)
        grade = 'C';
    else if (score >= 60)
        grade = 'D';
    else
        grade = 'E';
    printf("grade: %c
", grade);

    return 0;
}
View Code

46、编程计算a+aa+aaa+…+aa…a(n个a)的值

#include<stdio.h>
int main()
{
    printf ("Input a,n:
");
    int n,a;
    scanf ("%d,%d",&a,&n);
    
    long sum=0;
    int aa=0;
    while (n--){
      aa=aa*10+a;
      sum+=aa;
    }
    printf("sum=%ld
",sum);

    return 0;
}
View Code

47、搬砖问题

#include<stdio.h>
int main()
{
    printf( "Input n(27<n<=77):
");
    int n;
    scanf("%d",&n);

    int i,j,k;
    for(i=0;i<=77/4;++i){
        for(j=0;j<=77/3;++j){
            k = 36-i-j;
            if(k%2==0&&i*4+j*3+k/2==n){
                printf("men=%d,women=%d,children=%d
",i,j,k);
            }
        }
    }

    return 0;
}
View Code

48、编程输出某年某月有多少天(考虑到闰年)

#include<stdio.h>
int main()
{
    printf("Input year,month:
");
    int year,month;
    scanf("%d,%d",&year,&month);
    int flag = 0;
    if(year%4==0&&year%100!=0||year%400==0){
        flag = 1;
    }

    switch(month){
        case 1:
            printf("31 days
");
            break;
        case 2:
            if(flag){
                printf("29 days
");
            }
            else{
                printf("28 days
");
            }
            break;
        case 3:
            printf("31 days
");
            break;
        case 4:
            printf("30 days
");
            break;
        case 5:
            printf("31 days
");
            break;
        case 6:
            printf("30 days
");
            break;
        case 7:
        case 8:
            printf("31 days
");
            break;
        case 9:
            printf("30 days
");
            break;
        case 10:
            printf("31 days
");
            break;
        case 11:
            printf("30 days
");
            break;
        case 12:
            printf("31 days
");
        default:
            printf("Input error!
");
    }

    return 0;
}
View Code

49、谐均值计算

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

double Calculate(double x,double y);
int main()
{
    printf("Input two doubles:
");
    double x,y;
    scanf("%lf%lf",&x,&y);
    printf("1/((1/x+1/y)/2) = %0.3f
",Calculate(x,y));

    return 0;
}

double Calculate(double x,double y){
    return 1/((1/x+1/y)/2);
}
View Code

50、输出指定行列数的字符

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

void Chline(char ch, int column, int row);
int main()
{
    printf("input a char:
");
    char ch;
    scanf("%c",&ch);

    printf("input column and row:
");
    int column,row;
    scanf("%d%d",&column,&row);
    Chline(ch,column,row);

    return 0;
}

void Chline(char ch, int column, int row){
    while(row--){
        for(int i=0;i<column;++i){
            printf("%c",ch);
        }
        printf("
");
    }
}
View Code

51、魔术师猜数

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

int Magic(int m);
int main()
{
    int n;
    scanf("%d",&n);

    int number = Magic(n);
    if(-1 == number){
        printf("The sum you calculated is wrong!
");
    }
    else{
        printf("The number is %d
",number);
    }

    return 0;
}
int Magic(int m){
    int a,b,c;
    for(a=1;a<=9;++a){
        for(b=0;b<=9;++b){
            for(c=0;c<=9;++c){
                if((a+2*b+2*c)*100 + (2*a+b+2*c)*10 + 2*a+2*b+c==m)
                {
                    return a*100+b*10+c;
                }
            }
        }
    }
    return -1;
}
View Code

52、计算礼炮声响次数

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

int main()
{
    int count=0;
    for(int i=5;i<=7*21;++i){
        if(i%5==0&&i%6==0&&i<=105)count++;
        if(i%5==0&&i%7==0&&i<=105)count++;
        if(i%6==0&&i%7==0&&i<=126)count++;
        if(i%5==0&&i%6==0&&i%7==0&&i<=105)count+=2;
    }
    printf("n=%d",21*3-count);
    return 0;
}
View Code

53、水手分椰子

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

int coco(int t,int x,int n);
int main()
{
    printf("Input n(1<n<=5):
");
    int n;
    scanf("%d",&n);

    if(n<=1||n>5){
        printf("Error!
");
        return 0;
    }

    int t = n;  /**< n为 "n等分" ,程序中值不变,t为水手数 */
    int x = 1;  /**< x 为一个等分 假定初始值1 */

    int y = coco(t,x,n);/**< 从最后,前推上次留下来的椰子 */

    while(y == -1){/**< 一旦上次留下来的椰子在上次不能等分
                    那么,假设最后一次等分值x不正确,++ */
        x++; t = n; /**< 重新定义等分值 还原t */
        y = coco(t,x,n);/**< 再次计算上次留下来的椰子数 */
    }
    printf("y=%d
",y);

    return 0;
}

int coco(int t,int x,int n)
{
    if(t == 1){/**< 尾递归,第1个水手,直接返回*/
        return x*n+1;
    }
    else{
        if((x*n+1)%(n-1)!=0){/**< 上次留下来的椰子在上次是否可以等分
                即(x*n+1)为(n-1)整除 否则x的初值需要改变*/
            return -1;
        }
        else{
            return coco(t-1,(x*n+1)/(n-1),n);
        }
    }
}
View Code

54、递归法计算游戏人员的年龄

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

unsigned int ComputeAge(unsigned int n);
int main()
{
    unsigned int n;
    scanf("%u",&n);
    printf( "The person's age is %u
",ComputeAge(n));

    return 0;
}
unsigned int ComputeAge(unsigned int n){
    if(n==1){
        return 10;
    }
    else{
        return 2 + ComputeAge(n-1);
    }
}
View Code

55、递归法计算两个数的最大公约数

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

int Gcd(int a, int b);
int main()
{
    printf("Input a,b:");

    int a,b;
    scanf("%d,%d",&a,&b);
    if(a<=0||b<=0){
        printf("Input error!
");
    }
    else{
        printf("%d
",Gcd(a,b));
    }

    return 0;
}
int Gcd(int a, int b){
    if(a==b){
        return a;
    }
    else if(a>b){
        return Gcd(a-b,b);
    }
    else{
        return Gcd(a,b-a);
    }
}
View Code

56、寻找中位数v1.0

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

int mid(int a, int b, int c);
int main()
{
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    printf("The result is %d
",mid(a,b,c));

    return 0;
}
int mid(int a, int b, int c){
    if (a>b&&a<c||a>c&&a<b)return a;
    else if (b>a&&b<c||b>c&&b<a)return b;
    else return c;
}
View Code

57、还原算术表达式

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

int main()
{
    printf("Input n(n<1000):
");
    int n;
    scanf("%d",&n);

    int flag = 1;
    for(int x=1;x<=9;++x){
        for(int y=1;y<=9;++y){
            for(int z=0;z<=9;++z){
                if(2*z+10*(y+z)+100*(x+y)==n){
                    printf("X=%d,Y=%d,Z=%d
",x,y,z);
                    flag = 0;
                }
            }
        }
    }

    if(flag){
        printf("Invalid
");
    }

    return 0;
}
View Code

58、三天打渔两天晒网

#include<stdio.h>
int inputdate();
int main()
{
    int n = inputdate();
    if(-1==n){
        printf("Invalid input");
    }
    else if(n%5==0||n%5==4){
        printf("He is having a rest");
    }
    else{
        printf("He is working");
    }

    return 0;
}
int inputdate()
{
    int year,month,day;
    int n = scanf("%4d-%2d-%2d",&year,&month,&day);
    if(3 != n||year<1990||month>12||month<=0||day>31||day<=0){
        return -1;
    }

    int date[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
                     {31,29,31,30,31,30,31,31,30,31,30,31}};

    int sum = 0, flag =(year%4==0&&year%100!=0)||(year%400==0);
    for(int i=0;i<month-1;++i){
        sum +=date[flag][i];
    }

    return sum+day;
}
View Code

59、统计用户输入

#include<stdio.h>

int main()
{
    int space = 0,newline=0,others=0;
    printf("Please input a string end by #:
");
    char ch;
    do{
        ch = getchar();
        if(ch==' ')
            space++;
        else if(ch=='
')
            newline++;
        else if(ch!='#')
            others++;
    }while(ch!='#');

    printf( "space: %d,newline: %d,others: %d
",space,newline,others);

    return 0;
}
View Code

60、统计正整数中指定数字的个数

#include<stdio.h>

int main()
{
    printf("Input m,n:
");
    int m,n;
    scanf("%d,%d",&m,&n);
    int count = 0;
    while(m){
        if(m%10==n){
            count++;
        }
        m/=10;
    }
    printf( "%d
",count);
    return 0;
}
View Code

61、玫瑰花数

#include<stdio.h>
#include <math.h>
int main()
{
    for(int i=1000;i<=9999;++i){
        int n1=i/1000,n2=i/100%10,n3=i%100/10,n4=i%10;
//        if(i==(int)pow(n1,4)+(int)pow(n2,4)
//            +(int)pow(n3,4)+(int)pow(n4,4)){
//               printf("%d
",i);
//        }
        if(i==n1*n1*n1*n1+n2*n2*n2*n2+n3*n3*n3*n3+n4*n4*n4*n4)
            printf("%d
",i);
    }

    return 0;
}
View Code

62、四位反序数

#include<stdio.h>
#include <math.h>
int main()
{
    for(int i=1000;i<=9999;++i){
        int n = i,number=0;
        while(n){
           number =number*10+n%10;
           n/=10;
        }
        if(i*9==number){
            printf("%d
",i);
        }
    }

    return 0;
}
View Code

63、8除不尽的自然数

#include<stdio.h>
#include <math.h>
int main()
{
    for(int x = 8*64;;++x){
        if(x%8==1&&(x/8)%8==1&&(x/8/8)%8==7
           &&(x/8/8/8)*2==(x/17/17)&&x%17==4&&(x/17)%17==15)
        {
            printf("%d
",x);
            break;
        }
    }

    return 0;
}
View Code

64、矩阵转置v1.0

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

int main()
{
    printf("Input n:");
    int n;
    scanf("%d",&n);
    int array[n][n];

    printf("Input %d*%d matrix:
",n,n);
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            scanf("%d",&array[i][j]);
        }
    }

    for(int i=0;i<n-1;++i){
        for(int j=i+1;j<n;++j){
            int temp = array[i][j];
            array[i][j]=array[j][i];
            array[j][i]=temp;
        }
    }

    printf("The transposed matrix is:
");
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            printf("%4d",array[i][j]);
        }
        printf("
");
    }

    return 0;
}
View Code

65、兔子生崽问题

//兔子生崽问题
#include <stdio.h>
int rabbit (int n);
int main()
{ 
    printf ("Input n(n<=12):
");
    int n;
    scanf ("%d",&n);
    for (int i=1;i<=n;i++){
        printf ("%4d",rabbit(i));
    }
    printf ("
Total=%d
",rabbit(n));
         
    return 0;
}
int rabbit (int n){
    if (n==0||n==1){
        return 1;
    }
    else {    
        return rabbit(n-1)+rabbit(n-2);
    }
}
View Code

66、抓交通肇事犯

#include <stdio.h>

int main()
{ 
      int m,k;
    for (int i=0;i<=9;i++){
        for (int j=0;j<=9;j++){
            k=1000*i + 100*i + 10*j + j;
            for (m=32;m*m<=9999;m++){
                if (k==m*m){
                  printf ("k=%d,m=%d
",k,m);
                }
            }
        }
    }
           
    return 0;
}
View Code

67、检验并打印幻方矩阵

#include <stdio.h>
#define N 5

void input_square (int a[][N]);
int row_sum(int a[][N],int b[],int index);
int column_sum(int a[][N],int b[],int index);
int diagonal_sum(int a[][N],int b[],int index);
int  back_diagonal_sum(int a[][N],int b[],int index);
int judge_square (int a[][N],int b[]);
void print (int a[][N]);

int main()
{
    int a[N][N]={0};
    int b[2*(N+1)]={0};
    input_square (a);

    if (judge_square (a,b)){
        printf ("It is not a magic square!
");
    }
    else {
        printf ("It is a magic square!
");
        print (a);
    }

    return 0;
}
/**< 输入矩阵 */
void input_square (int a[][N]){
    for (int i=0;i<N;i++){
        for (int j=0;j<N;j++){
            scanf ("%d",&a[i][j]);
        }
    }
}
/**< 行元素和 */
int row_sum(int a[][N],int b[],int index){
   int sum = 0;
   for (int i=0;i<N;i++){
        for (int j=0;j<N;j++){
            sum += a[i][j];
        }
        b[index++]=sum;
        sum = 0;
   }
   return index;
}
/**< 列元素和 */
int column_sum(int a[][N],int b[],int index){
   int sum = 0;
   for (int i=0;i<N;i++){
        for (int j=0;j<N;j++){
            sum += a[j][i];
        }
        b[index++]=sum;
        sum = 0;
   }
   return index;
}
/**< 对角线之和 */
int diagonal_sum(int a[][N],int b[],int index){
    int i = 0,sum = 0;
    while(i<N){
        sum += a[i][i];
        i++;
    }
    b[index++]=sum;
    return index;
}
/**< 反对角线之和 */
int  back_diagonal_sum(int a[][N],int b[],int index){
    int i = N-1,j = 0,sum = 0;
    while(j<N){
        sum += a[i--][j++];
    }
    b[index++]=sum;
    return index;
}
/**< 判断矩阵 */
int judge_square (int a[][N],int b[]){
    int index = 0;
    index = row_sum(a,b,index);
    index = column_sum(a,b,index);
    index = diagonal_sum(a,b,index);
    index = back_diagonal_sum(a,b,index);

    for (int i=0;(i+1)<2*(N+1);i++){
        if (b[i]!=b[i+1]){
            return 1;
        }
    }

    return 0;
}
/**< 打印矩阵 */
void print (int a[][N]){
    for (int i=0;i<N;i++){
        for (int j=0;j<N;j++){
            printf ("%4d",a[i][j]);

        }
        printf ("
");
    }
}
View Code

68、二分法求根

#include<stdio.h>
#include<math.h>
#define EPS 1e-6
float Root(float x)
{
    return x*x*x - x - 1;
}
int main()
{
    float min,max,mid;
    scanf("%f,%f",&min,&max);

    do{
        mid=(max+min)/2.0;
        if( Root(mid) > EPS) max = mid;
        if( Root(mid) < EPS) min = mid;
    }while(fabs( Root(mid) ) > EPS);

    printf("x=%6.2f
",mid);

    return 0;
}
View Code

69、矩阵转置

#include <stdio.h>
#define N 5
int main()
{
     int array[N][N]={0};
     int n;
     scanf("%d",&n);

     printf("The original matrix is:
");
     for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            array[i][j] = i * n + j + 1;
            printf("%3d",array[i][j]);
        }
        printf("
");
     }

     for(int i=0;i<n;++i){
        for(int j=0;j<i;++j){
            int temp = array[i][j];
            array[i][j] = array[j][i];
            array[j][i] = temp;
        }
     }

     printf("The changed matrix is:
");
     for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){

            printf("%3d",array[i][j]);
        }
        printf("
");
     }

     return 0;
}
View Code

70、程序改错

#include  <stdio.h>
#define ARR_SIZE 10
void  MaxMinExchang(int *a, int n);
int main()
{
    int a[ARR_SIZE], i, n;
    printf("Input n(n<=10):
");
    scanf("%d", &n);
    printf("Input %d Numbers:
", n);
    for (i=0; i<n; i++)
    {
        scanf("%d", &a[i]);
    }
    MaxMinExchang(a, n);
    printf("After MaxMinExchange:
");
    for (i=0; i<n; i++)
    {
        printf("%d ", a[i]);
    }
    printf("
");
    return 0;
}
void  MaxMinExchang(int *a, int n)
{
    int  maxValue = a[0], minValue = a[0], maxPos=0, minPos=0;
    int  i, temp;
    for (i=0; i<n; i++)
    {
        if (a[i] > maxValue)
        {
            maxValue = a[i];
            maxPos = i;
        }
        if (a[i] < minValue)
        {
            minValue = a[i];
            minPos = i;
        }
    }
    temp = a[maxPos];
    a[maxPos] = a[minPos];
    a[minPos] = temp;
}
View Code

71、蛇形矩阵

#include <stdio.h>
#define N 100
int main()
{
    int a[N][N]={0};

    printf("Input n:
");
    int n;
    if(1 != scanf("%d",&n)||n>100||n<=0){
        printf("Input error!
");
        return 0;
    }

    int i,j,k,sum;
    for (i=1;i<=n;i++)
        for (j=1;j<=n+1-i;j++)
        {
            k=i+j-2;
            sum=(k+1)*k/2;
            if (k%2)//如果前面的对角线个数是奇数
                a[i][j]=sum+i;
            else
                a[i][j]=sum+j;
        }
    for (i=1;i<=n;i++)
        for (j=n-i+2;j<=n;j++)
            a[i][j]=n*n+1-a[n+1-i][n+1-j];

    for (i=1;i<=n;i++)
    {
        for (j=1;j<=n;j++)
            printf("%4d",a[i][j]);
        printf("
");
    }

    return 0;
}
View Code

72、亲密数_1

#include <stdio.h>
int sum(int num){
    int sum = 0;
    for(int i=1;i<num;++i){
        if(num%i==0){
            sum+=i;
        }
    }
    return sum;
}
int main()
{
    printf("Input m, n:
");
    int m,n;
    scanf("%d,%d",&m,&n);

    if(sum(m)==n&&sum(n)==m){
        printf("Yes!
");
    }
    else{
        printf( "No!
");
    }

    return 0;
}
View Code

73、亲密数_2

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

int sum(int num){
    int sum = 0;
    for(int i=1;i<num;++i){
        if(num%i==0){
            sum+=i;
        }
    }
    return sum;
}
int main()
{
    printf("Input n:
");
    int n;
    scanf("%d",&n);

    for(int A=2; A<n; A++)
    {
        int B = sum(A);
        if(A<B&&A==sum(B))
        {
            printf("(%d,%d)
",A,B);
        }
    }

    return 0;
}
View Code

74、完全数

#include <stdio.h>
#include <stdlib.h>
#define N 100
int main()
{
    printf("Input m:
");
    int m;
    scanf("%d",&m);

    int a[100] = {0},cnt = 0;
    int sum = 0;
    for(int i=1;i<m;++i){
        if(m%i==0){
            sum += i;
            a[cnt++] = i;
        }
    }
    if(sum==m){
        printf("Yes!
");
        for(int j=0;j<cnt;++j){
              if(j!=0){
                 printf(",");
              }
              printf("%d",a[j]);
        }
    }
    else{
        printf("No!
");
    }

    return 0;
}
View Code

75、回文素数

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 1000

int isPrime(int n){
    for(int i=2;i<sqrt(n);++i)
    {
        if(n%i==0)return 0;
    }
    return 1;
}

int main()
{
    printf("Input n:
");
    int n;
    scanf("%d",&n);
    int a[N] = {0};

    int i,count = 0;
    for(int j=11;j<n;j++){
        if(isPrime(j)){
            a[count++] = j;
        }
    }

    int cnt=0;
    for(i=0;i<count;i++){
        int t = a[i],sum = 0;

        while(t){
            sum = sum*10 + t%10;
            t /= 10;
        }

        if(sum==a[i]){

            if(cnt!=2)   //
            printf("%4d",sum);
            cnt++;
        }

    }
    if(cnt>1)cnt--; //
    printf("
count=%d
",cnt);
    return 0;
}
View Code

76、梅森尼数

#include <stdio.h>
int isPrime(double x){
   for(long j=3;j<sqrt(x)+1;j+=2)
      if(x/j == (int)(x/j))
      {
         return 0;
      }
    return 1;
}

void f(int n){
    double sum =2;int count=0;
    for(int i=2;i<n;++i){
        sum *= 2;
        if(isPrime(sum -1)){
            printf("2^%d-1=%.0f
",i,sum-1);
            count++;
        }
    }
    printf( "count=%d
",count);
}

int main()
{
    printf("Input n:
");
    int n;
    scanf("%d",&n);
    f(n);
    return 0;
}
View Code

77、工资统计

#include<stdio.h>

void Input(float wage[], int n);
float Compute(float wage[], int n, float *pmaxwage, float *pminwage);
int main()
{
    float wage[50],maxwage,minwage,avewage;
    int n;

    printf("Please input n:
");
    scanf("%d",&n);

    Input(wage,n);
    avewage=Compute(wage,n,&maxwage,&minwage);

    printf("maxwage=%.2f, minwage=%.2f, avewage=%.2f
",maxwage,minwage,avewage);

    return 0;
}

void Input(float wage[], int n)
{
    for(int i=0;i<n;++i){
        scanf("%f",&wage[i]);
    }
}
float Compute(float wage[], int n, float *pmaxwage, float *pminwage)
{
    *pmaxwage  = *pminwage = wage[0];
    float sum = wage[0];
    for(int i=1;i<n;++i){
        sum +=wage[i];
        if(wage[i]>*pmaxwage){
            *pmaxwage = wage[i];
        }
        if(wage[i]<*pminwage){
            *pminwage = wage[i];
        }
    }
    return sum/n;
}
View Code

78、有趣的“回文”检测

#include <stdio.h>
#include <string.h>
#define N 100

int isPalindrome(char a[]){
    char *pStart = a ,*pEnd = a + strlen(a)-1;
    while(pStart<=pEnd){
        if(*pStart!=*pEnd){
            return 0;
        }
        pStart++,pEnd--;
    }
    return 1;
}

int main()
{
    printf("Input string:");
    char a[N]="";
    gets(a);
    if(isPalindrome(a)){
        printf("Yes!
");
    }
    else{
        printf("No!
");
    }

    return 0;
}
View Code

79、学生成绩管理系统V1.0

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

/**< 定义结构 */
#define STU_NUM 100
typedef struct stu STUDENT;
struct stu
{
    long num;
    float score;
};
STUDENT student[100];

/**< 函数声明 */
int Menu();
void InputRecord(int n);
void TotalAverageScore(int n);
void SortRecord(int n, int (*f)(STUDENT,STUDENT));
int  DescendingScore(STUDENT x,STUDENT y);
int  AscendingNumber(STUDENT x,STUDENT y);
void SearchByNumber(int n);
int BinSearch(int n,STUDENT x,int (*f)(STUDENT,STUDENT));
int CompareNumber(STUDENT x,STUDENT y);
int CompareScore(STUDENT x,STUDENT y);
void StatisticAnalysis(int n);
void ListRecord(int n);

/**< 主函数 */
int main()
{
    /**< 输入人数 */
    printf("Input student number(n<30):
");
    int n;
    scanf("%d",&n);

    /**< 菜单选项 */
    int choice;
    do{
        choice= Menu();
        switch(choice)
        {
            case 0:printf("End of program!
");break;
            case 1:InputRecord(n);break;
            case 2:TotalAverageScore(n);break;
            case 3:printf("Sort in descending order by score:
");
                   SortRecord(n,DescendingScore);
                   ListRecord(n);break;
            case 4:printf("Sort in ascending order by number:
");
                   SortRecord(n,AscendingNumber);
                   ListRecord(n);break;
            case 5:SortRecord(n,AscendingNumber);
                   SearchByNumber(n);
                   break;
            case 6:StatisticAnalysis(n);break;
            case 7:ListRecord(n);break;
            default:printf("Input error!
");
        }
    }while(choice!=0);
    return 0;
}

/**< 菜单 */
int Menu()
{
    printf("Management for Students' scores
");
    printf("1.Input record
");
    printf("2.Caculate total and average score of course
");
    printf("3.Sort in descending order by score
");
    printf("4.Sort in ascending order by number
");
    printf("5.Search by number
");
    printf("6.Statistic analysis
");
    printf("7.List record
");
    printf("0.Exit
");
    printf("Please Input your choice:
");
    int choice;
    scanf("%d",&choice);
    return choice;
}
/**< 1.输入数据 */
void InputRecord(int n)
{
    printf("Input student's ID, name and score:
");
    for(int i=0; i<n; ++i){
        scanf("%ld%f",&student[i].num,&student[i].score);
    }
}

/**< 2.计算总分 平均分*/
void TotalAverageScore(int n)
{
    float sum = 0.0;
    for(int i=0; i<n; ++i){
        sum += student[i].score;
    }
    printf("sum=%.0f,aver=%.2f
",sum,sum/n);
}

/**< 3.4.排序(交换法) */
void SortRecord(int n, int (*f)(STUDENT,STUDENT))
{
    for(int i=0; i<n-1; ++i){
        for(int j=i+1; j<n;++j){
            if((*f)(student[i],student[j])){
                STUDENT temp = student[i];
                student[i] = student[j];
                student[j] = temp;
            }
        }
    }
}

/**< 按成绩升序 */
int  DescendingScore(STUDENT x,STUDENT y)
{
    return x.score<y.score;
}

/**< 按ID降序 */
int  AscendingNumber(STUDENT x,STUDENT y)
{
    return x.num>y.num;
}

/**< 5.按学号查找 */
void SearchByNumber(int n)
{
    printf("Input the number you want to search:
");
    STUDENT x;
    scanf("%ld",&x.num);
    int index = BinSearch(n,x,CompareNumber);
    if(index==-1){
       printf("Not found!
");
    }
    else{
       printf("%ld	%.0f
",student[index].num,student[index].score);
    }
}

/**< 二分查找(排序) */
int BinSearch(int n,STUDENT x,int (*f)(STUDENT,STUDENT))
{
    int low = 0, heigh = n-1, mid;
    while(low<=heigh)
    {
        mid = (low+heigh)/2;
        int compare = (*f)(x,student[mid]);
        if(compare==1){
            low = mid + 1;
        }
        else if(compare==-1){
            heigh = mid - 1;
        }
        else{
            return mid;
        }
    }
    return -1;
}

/**< 按学号比较  */
int CompareNumber(STUDENT x,STUDENT y)
{
    if(x.num>y.num) return 1;
    if(x.num<y.num) return -1;
    if(x.num==y.num) return 0;
}

/**< 按分数比较  */
int CompareScore(STUDENT x,STUDENT y)
{
    if(x.score>y.score) return 1;
    if(x.score<y.score) return -1;
    if(fabs(x.score-y.score)<=1e-6) return 0;
}
/**< 6.统计占比 */
void StatisticAnalysis(int n)
{
    int grade[6]={0};
    for(int i=0;i<n;++i){
        if(student[i].score<60)  grade[0]++;
        else if(student[i].score<70) grade[1]++;
        else if(student[i].score<80) grade[2]++;
        else if(student[i].score<90) grade[3]++;
        else if(student[i].score<100) grade[4]++;
        else if(student[i].score==100) grade[5]++;
    }
    float percent[6]={0};
    int j;
    for(j=0;j<6;++j){
        percent[j] = grade[j]*100.0/n;
    }
    int k=60;j = 0;
    printf("<60	%d	%.2f%%
",grade[j],percent[j]);
    for(j=1;k<100;k+=10,j++){
        printf("%d-%d	%d	%.2f%%
",k,k+9,grade[j],percent[j]);
    }
    printf("%d	%d	%.2f%%
",k,grade[j],percent[j]);
}

/**< 7.输出数据 */
void ListRecord(int n)
{
    for(int i=0; i<n; ++i){
        printf("%ld	%.0f
",student[i].num,student[i].score);
    }
}
View Code

80、程序改错——1

#include <stdio.h>
#include <string.h>
#define N 100   //1.
 
char* MyStrcat(char *dest, char *source);//2.
int main(void)
{
    char first[2*N+1]="", second[N]="", *result = NULL;//3.
    printf("Input the first string:
");
    gets(first);
    printf("Input the second string:
");
    gets(second);
    result = MyStrcat(first, second);
    printf("The result is : %s
", result);
    return 0;
}
char* MyStrcat(char *dest, char *source)
{
    int i = 0;
    while (*(dest+i)!='')   i++;
    for (int j=0; *(source+j)!=''; i++,j++)//4.
    {
        *(dest+i) = *(source+j);
    }
    return dest;
}
View Code

81、程序改错——2

#include<stdio.h>
#define  ARR_SIZE  5
void  YH(int a[][ARR_SIZE], int  n);
void  PrintYH(int a[][ARR_SIZE], int  n);
int main(void)
{
        int  a[ARR_SIZE][ARR_SIZE];
        YH(a, ARR_SIZE-1);  //!
        PrintYH(a, ARR_SIZE);
        return 0;
}
void YH(int a[][ARR_SIZE], int n)
{
        int  i, j ;
        for (i=1; i<=n; i++)
        {
             a[i][1] = 1;
             a[i][i] = 1;
        }
        for (i=3; i<=n; i++)
        {
            for (j=2; j<=i-1; j++)
            {
                 a[i][j] = a[i-1][j-1] + a[i-1][j];
            }
        }
}
void PrintYH(int a[][ARR_SIZE], int n)
{
        int i , j ;
        for (i=1; i<n; i++)
        {
            for (j=1; j<=i; j++)
            {
                printf("%4d", a[i][j]);
            }
             printf("
");
        }
}
View Code

82、出售金鱼

# include <stdio.h>

int main()
{
    int x = 11;
    int count = 5;
    x = (x*count+1)/(count-1);
    //printf("%d,%d
",x,count);

    while(--count>=2)
    {
        x = (x*count+1)/(count-1);
        //printf("%d,%d
",x,count);
    }
    printf("There are %d fishes at first.
",x);

    return 0;
}
View Code

83、找最值

#include<stdio.h>
#include<string.h>
int FindMax(int num[], int n, int *pMaxPos);
int FindMin(int num[], int n, int *pMinPos);
int main(void)
{
    printf("Input 10 numbers:
");
    int a[10]={0};
    for(int i=0;i<10;++i){
        scanf("%d",&a[i]);
    }

    int maxPos = 0,minPos = 0;
    FindMax(a,10,&maxPos);
    FindMin(a,10,&minPos);

    printf("Max=%d,Position=%d,Min=%d,Position=%d
",
           a[maxPos],maxPos,a[minPos],minPos);
    return 0;
}
int FindMax(int num[], int n, int *pMaxPos)
{
    int max = num[*pMaxPos];
    for(int i=*pMaxPos;i<n;++i){
        if(num[i]>max){
            max=num[i];
            *pMaxPos=i;
        }
    }
    return *pMaxPos;
}
int FindMin(int num[], int n, int *pMinPos)
{
    int min = num[*pMinPos];
    for(int i=*pMinPos;i<n;++i){
        if(num[i]<min){
            min=num[i];
            *pMinPos=i;
        }
    }
    return *pMinPos;
}
View Code

84、杨辉三角形

#include<stdio.h>
#define  ARR_SIZE  10
void  YH(int a[][ARR_SIZE], int  n);
void  PrintYH(int a[][ARR_SIZE], int  n);
int main(void)
{
        int  a[ARR_SIZE][ARR_SIZE];
        printf("Input n (n<=10):
");
        int n;
        scanf("%d",&n);
        YH(a, n+1);
        PrintYH(a, n+1);
        return 0;
}
void YH(int a[][ARR_SIZE], int n)
{
        int  i, j ;
        for (i=1; i<=n; i++)
        {
             a[i][1] = 1;
             a[i][i] = 1;
        }
        for (i=3; i<=n; i++)
        {
            for (j=2; j<=i-1; j++)
            {
                 a[i][j] = a[i-1][j-1] + a[i-1][j];
            }
        }
}
void PrintYH(int a[][ARR_SIZE], int n)
{
        int i , j ;
        for (i=1; i<n; i++)
        {
            for (j=1; j<=i; j++)
            {
                printf("%4d", a[i][j]);
            }
             printf("
");
        }
}
View Code

85、颠倒句子中的单词顺序

#include<stdio.h>
#include<string.h>
#define N 100
int Inverse(char str1[], char str2[][N]);
int main(void)
{
    printf("Input a sentence:");
    char str1[N] = "", str2[N][N] = {0};
    gets(str1);

    int ret = Inverse(str1, str2);

    for(int i=ret; i>0; --i){
        printf("%s ", str2[i]);
    }
    printf("%s
",str2[0]);/**< 因为处理了标点符号 */

    return 0;
}
int Inverse(char str1[], char str2[][N])
{
    int  i = 1, row = 0, column = 0;/**< 为了while里判断不越界 */
    str2[row][column++] = str1[0];

    while (str1[i]!='')
    {
        if (str1[i-1] != ' ' && str1[i] == ' ')
        {
            row++;         /**<行++, 列置0 */
            column = 0;
        }

        if(str1[i] != ' '){
            str2[row][column++] = str1[i];
        }

        i++;
    }

    str2[0][strlen(str2[0])] = str2[row][column-1]; /**< 对标点符号进行处理 */
    str2[row][column-1] = '';

    return row;
}
View Code

86、找出按字典顺序排在最前面的国名

#include <stdio.h>
#include <string.h>
#define N 100

int main()
{
    printf("Input five countries' names:
");
    char a[5][20];
    for(int i=0; i<5; ++i){
        gets(a[i]);
    }
    char *p = a[0];
    for(int i=0; i<5; ++i){
        if(strcmp(a[i],p)<0){
            p = a[i];
        }
    }
    printf("The minimum is:%s
",p);
    return 0;
}
View Code

87、学生成绩管理系统V2.0

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

/**< 定义结构 */
#define STU_NUM 100
typedef struct stu STUDENT;
struct stu
{
    long num;
    float score;
};
STUDENT student[100];

/**< 函数声明 */
int Menu();
void InputRecord(int n);
void TotalAverageScore(int n);
void SortRecord(int n, int (*f)(STUDENT,STUDENT));
int  DescendingScore(STUDENT x,STUDENT y);
int  AscendingScore(STUDENT x,STUDENT y);
int  AscendingNumber(STUDENT x,STUDENT y);
void SearchByNumber(int n);
int BinSearch(int n,STUDENT x,int (*f)(STUDENT,STUDENT));
int CompareNumber(STUDENT x,STUDENT y);
int CompareScore(STUDENT x,STUDENT y);
void StatisticAnalysis(int n);
void ListRecord(int n);

/**< 主函数 */
int main()
{
    /**< 输入人数 */
    printf("Input student number(n<30):
");
    int n;
    scanf("%d",&n);

    /**< 菜单选项 */
    int choice;
    do{
        choice= Menu();
        switch(choice)
        {
            case 0:printf("End of program!
");break;
            case 1:InputRecord(n);break;
            case 2:TotalAverageScore(n);break;
            case 3:printf("Sort in descending order by score:
");
                   SortRecord(n,DescendingScore);
                   ListRecord(n);break;
            case 4:printf("Sort in ascending order by score:
");
                   SortRecord(n,AscendingScore);
                   ListRecord(n);break;
            case 5:printf("Sort in ascending order by number:
");
                   SortRecord(n,AscendingNumber);
                   ListRecord(n);break;
            case 6:SortRecord(n,AscendingNumber);
                   SearchByNumber(n);
                   break;
            case 7:StatisticAnalysis(n);break;
            case 8:ListRecord(n);break;
            default:printf("Input error!
");
        }
    }while(choice!=0);
    return 0;
}

/**< 菜单 */
int Menu()
{
    printf("Management for Students' scores
");
    printf("1.Input record
");
    printf("2.Caculate total and average score of course
");
    printf("3.Sort in descending order by score
");
    printf("4.Sort in ascending order by score
");
    printf("5.Sort in ascending order by number
");
    printf("6.Search by number
");
    printf("7.Statistic analysis
");
    printf("8.List record
");
    printf("0.Exit
");
    printf("Please Input your choice:
");
    int choice;
    scanf("%d",&choice);
    return choice;
}
/**< 1.输入数据 */
void InputRecord(int n)
{
    printf("Input student's ID and score:
");
    for(int i=0; i<n; ++i){
        scanf("%ld%f",&student[i].num,&student[i].score);
    }
}

/**< 2.计算总分 平均分*/
void TotalAverageScore(int n)
{
    float sum = 0.0;
    for(int i=0; i<n; ++i){
        sum += student[i].score;
    }
    printf("sum=%.0f,aver=%.2f
",sum,sum/n);
}

/**< 3.4.5.排序(交换法) */
void SortRecord(int n, int (*f)(STUDENT,STUDENT))
{
    for(int i=0; i<n-1; ++i){
        for(int j=i+1; j<n;++j){
            if((*f)(student[i],student[j])){
                STUDENT temp = student[i];
                student[i] = student[j];
                student[j] = temp;
            }
        }
    }
}

/**< 按成绩升序 */
int  DescendingScore(STUDENT x,STUDENT y)
{
    return x.score<y.score;
}

/**< 按成绩降序 */
int  AscendingScore(STUDENT x,STUDENT y)
{
    return x.score>y.score;
}

/**< 按ID降序 */
int  AscendingNumber(STUDENT x,STUDENT y)
{
    return x.num>y.num;
}

/**< 6.按学号查找 */
void SearchByNumber(int n)
{
    printf("Input the number you want to search:
");
    STUDENT x;
    scanf("%ld",&x.num);
    int index = BinSearch(n,x,CompareNumber);
    if(index==-1){
       printf("Not found!
");
    }
    else{
       printf("%ld	%.0f
",student[index].num,student[index].score);
    }
}

/**< 二分查找(排序) */
int BinSearch(int n,STUDENT x,int (*f)(STUDENT,STUDENT))
{
    int low = 0, heigh = n-1, mid;
    while(low<=heigh)
    {
        mid = (low+heigh)/2;
        int compare = (*f)(x,student[mid]);
        if(compare==1){
            low = mid + 1;
        }
        else if(compare==-1){
            heigh = mid - 1;
        }
        else{
            return mid;
        }
    }
    return -1;
}

/**< 按学号比较  */
int CompareNumber(STUDENT x,STUDENT y)
{
    if(x.num>y.num) return 1;
    if(x.num<y.num) return -1;
    if(x.num==y.num) return 0;
}

/**< 按分数比较  */
int CompareScore(STUDENT x,STUDENT y)
{
    if(x.score>y.score) return 1;
    if(x.score<y.score) return -1;
    if(fabs(x.score-y.score)<=1e-6) return 0;
}
/**< 7.统计占比 */
void StatisticAnalysis(int n)
{
    int grade[6]={0};
    for(int i=0;i<n;++i){
        if(student[i].score<60)  grade[0]++;
        else if(student[i].score<70) grade[1]++;
        else if(student[i].score<80) grade[2]++;
        else if(student[i].score<90) grade[3]++;
        else if(student[i].score<100) grade[4]++;
        else if(student[i].score==100) grade[5]++;
    }
    float percent[6]={0};
    int j;
    for(j=0;j<6;++j){
        percent[j] = grade[j]*100.0/n;
    }
    int k=60;j = 0;
    printf("<60	%d	%.2f%%
",grade[j],percent[j]);
    for(j=1;k<100;k+=10,j++){
        printf("%d-%d	%d	%.2f%%
",k,k+9,grade[j],percent[j]);
    }
    printf("%d	%d	%.2f%%
",k,grade[j],percent[j]);
}

/**< 8.输出数据 */
void ListRecord(int n)
{
    for(int i=0; i<n; ++i){
        printf("%ld	%.0f
",student[i].num,student[i].score);
    }
}
View Code

88、月份表示

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define STU_NUM 100

int main()
{
    char *month[] = {"Illegal month","January","February","March","April",
        "May","June","July","August","September","October",
        "November","December"};
    printf("Input month number:
");
    int n;
    scanf("%d",&n);
    if(n>12||n<1){
        printf("%s
",month[0]);
    }
    else{
        printf("month %d is %s
",n,month[n]);
    }

    return 0;
}
View Code

89、程序改错——1

#include  <stdio.h>
#define STUD 30            //最多可能的学生人数
#define COURSE 5             //最多可能的考试科目数
void  Total(int *score, int sum[], float aver[], int m, int n);
void  Print(int *score, int sum[], float aver[], int m, int n);
int main(void)
{
         int     i, j, m, n, score[STUD][COURSE], sum[STUD];
         float   aver[STUD];
         printf("Enter the total number of students and courses:
");
         scanf("%d%d",&m,&n);
         printf("Enter score:
");
         for (i=0; i<m; i++)
         {
            for (j=0; j<n; j++)
            {
                scanf("%d", &score[i][j]);
            }
        }
        Total(*score, sum, aver, m, n);
        Print(*score, sum, aver, m, n);
        return 0;
}

void  Total(int *score, int sum[], float aver[], int m, int n)
{
        int  i, j;
        for (i=0; i<m; i++)
        {
            sum[i] = 0;
            for (j=0; j<n; j++)
            {
                sum[i] = sum[i] + *(score + i * COURSE + j);
            }
            aver[i] = (float) sum[i] / n;
        }
}

void  Print(int *score, int sum[], float aver[], int m, int n)
{
        int  i, j;
        printf("Result:
");
        for (i=0; i<m; i++)
        {
            for (j=0; j<n; j++)
            {
                printf("%4d", *(score + i * COURSE + j));
            }
            printf("%5d%6.1f
", sum[i], aver[i]);
     }
}
View Code

90、程序改错——2

#include  <stdio.h>
#include  <string.h>
#define   M  150 /* 最多的字符串个数 */
#define   N  10 /* 字符串最大长度 */
void SortString(char *ptr[], int n);
int main()
{
  int    i, n;
  char   *pStr[M];
  printf("How many countries?
");
  scanf("%d",&n);
  getchar();        /* 读走输入缓冲区中的回车符 */
  printf("Input their names:
");
  char ch[M][N];
  for (i=0; i<n; i++)
  {
      gets(ch[i]);  /* 输入n个字符串 */
      pStr[i] = ch[i];
  }
  SortString(pStr, n); /* 字符串按字典顺序排序 */
  printf("Sorted results:
");
  for (i=0; i<n; i++)
  {
      puts(pStr[i]);  /* 输出排序后的n个字符串 */
  }
  return 0;
}
void SortString(char *ptr[], int n)
{
  int   i, j;
  char  *temp = NULL;
  for (i=0; i<n-1; i++)
  {
      for (j=i+1; j<n; j++)
      {
         if (strcmp(ptr[j], ptr[i]) < 0)
         {
              temp = ptr[i];
              ptr[i] = ptr[j];
              ptr[j] = temp;
         }
      }
  }
}
View Code

91、找数组最值

#include  <stdio.h>
#include  <string.h>
#define N 100

void InputArray(int *p, int m, int n);
int  FindMax(int *p, int m, int n, int *pRow, int *pCol);
int main()
{
    printf("Input m,n:
");
    int m,n;
    scanf("%d,%d",&m,&n);
    printf( "Input %d*%d array:
",m,n);
    int array[N][N] = {0};
    InputArray(array[0],m,n);
    int row,col;
    int max = FindMax(array[0],m,n,&row,&col);

    printf("max=%d,row=%d,col=%d
",max,row,col);

    return 0;
}
void InputArray(int *p, int m, int n)
{
    for(int i=0; i<m; ++i){
        for(int j=0; j<n; ++j){
            scanf("%d",&p[i*N+j]);
        }
    }
}
int  FindMax(int *p, int m, int n, int *pRow, int *pCol)
{
    int max = p[0];
    for(int i=0; i<m; ++i){
        for(int j=0; j<n; ++j){
            if(p[i*N+j]>max){
                max = p[i*N+j];
                *pRow = i;
                *pCol = j;
            }
        }
    }
    return max;
}
View Code

92、冒泡排序

#include  <stdio.h>
#include  <string.h>
#define N 100

int main()
{
    printf("Input n:");
    int n;
    scanf("%d",&n);
    printf("Input %d numbers:",n);
    int a[N] = {0};
    for(int i=0; i<n; ++i){
        scanf("%d",&a[i]);
    }
    /**< 冒泡排序 */
    for(int i=0;i<n; ++i){
        for(int j=1; j<n-i; ++j){
            if(a[j]<a[j-1]){
                int temp = a[j-1];
                a[j-1] = a[j];
                a[j] = temp;
            }
        }
    }

    printf("Sorting results:");
    for(int i=0; i<n; ++i){
        printf("%4d",a[i]);
    }

    return 0;
}
View Code

93、删除字符串中与某字符相同的字符

#include  <stdio.h>
#include  <string.h>
#define N 100

void DelChar(char a[],char b)
{
    int i=0;
    for(; i<strlen(a); ++i){
        if(a[i]==b){
            while(a[i]){
                a[i] = a[i+1];
                ++i;
            }
        }
    }
    a[i] = '';
}
int main()
{
    printf( "Input a string:
");
    char a[N] = "";
    gets(a);
    printf("Input a character:
");
    char b = getchar();
    DelChar(a,b);
    printf("Results:%s
",a);

    return 0;
}
View Code

94、求最大数和最小数的最大公约数

#include  <stdio.h>
#include  <string.h>
#define N 100

int f(int a[],int *max,int *min);
int gcd(int a,int b);


int main()
{
    printf("Input 10 numbers:
");
    int a[10] = {0};
    for(int i=0; i<10; ++i){
        scanf("%d",&a[i]);
    }
    int max,min;
    int gcd = f(a,&max,&min);
    printf("maxNum=%d
",max);
    printf("minNum=%d
",min);
    if(gcd){
        printf("%d",gcd);
    }
    return 0;
}

int f(int a[],int *max,int *min)
{
    *max = *min = a[0];
    for(int i=0; i<10; ++i){
        if(a[i]>*max) *max = a[i];
        if(a[i]<*min) *min = a[i];
    }
    if(*min==0){
        return 0;
    }
    return gcd(*max,*min);
}

int gcd(int a,int b)
{
   return b==0?a:gcd(b,a%b);
}
View Code

95、数列合并

#include  <stdio.h>
#include <stdlib.h>
#include  <string.h>
#define N 100
void BubbleSort(int a[],int n);
void Merge(int a[], int b[], int c[], int m, int n);
int main()
{
    printf("Input m,n:");
    int m,n;
    scanf("%d,%d",&m,&n);
    printf("Input array a:");
    int a[N] = {0};
    for(int i=0; i<m; ++i){
        scanf("%d",&a[i]);
    }
    printf("Input array b:");
    int b[N] = {0};
    for(int j=0; j<n; ++j){
        scanf("%d",&b[j]);
    }
    int c[2*N] = {0};

    Merge(a,b,c,m,n);

    for(int i=0; i<m+n; ++i){
        printf("%4d",c[i]);
    }
    return 0;
}
void Merge(int a[], int b[], int c[], int m, int n)
{
    BubbleSort(a,m),BubbleSort(b,n);
    int *pa=a,*pb=b,i=0;
    while(*pa&&*pb){
        if(*pa>*pb){
            c[i++] = *pa++;
        }
        else if(*pa<*pb){
            c[i++] = *pb++;
        }
        else{
            c[i++] = *pa++;
        }
    }
    while(*pa&&!(*pb)){
        c[i++] = *pa++;
    }

    while(*pb&&!(*pa)){
        c[i++] = *pb++;
    }

}
void BubbleSort(int a[],int n)
{
     for(int i=0;i<n; ++i){
        for(int j=1; j<n-i; ++j){
            if(a[j]>a[j-1]){
                int temp = a[j-1];
                a[j-1] = a[j];
                a[j] = temp;
            }
        }
    }
}
View Code

96、大奖赛现场统分

#include <stdio.h>
#include <math.h>
#define N 100
/**< 评分,评委编号及评分 */
typedef struct judscore{
    int number;
    float score;
}JUDSCORE;
/**< 选手,选手编号、得分及平均分 */
typedef struct athletes{
    int number;
    JUDSCORE judge[N];
    float ave;
}ATHLETES;
/**< 评委,评委编号、评分误差及评价 */
typedef struct  judValue{
    int number;
    float score[N];
    float ave;
}JUDVALUE;


int main()
{
    printf("How many Athletes?
");/**< 输入选手人数、评委人数 */
    int n;
    scanf("%d",&n);
    printf("How many judges?
");
    int m;
    scanf("%d",&m);

    printf("Scores of Athletes:
");
    ATHLETES player[N];            /**< 选手及评委评分输入、计算平均分 */
    for(int i=0; i<n; ++i)
    {
        printf("Athlete %d is playing.
",i+1);
        printf("Please enter his number code:
");
        scanf("%d",&player[i].number);

        float min = 10, max = 0, sum = 0;
        for(int j=0; j<m; ++j)
        {
            printf("Judge %d gives score:
",j+1);
            player[i].judge[j].number = j+1;
            scanf("%f",&player[i].judge[j].score);

            if(player[i].judge[j].score>max){
                max = player[i].judge[j].score;
            }
            if(player[i].judge[j].score<min){
                min = player[i].judge[j].score;
            }
            sum += player[i].judge[j].score;
        }
        printf("Delete a maximum score:%.1f
",max);
        printf("Delete a minimum score:%.1f
",min);
        player[i].ave = (sum-max-min)/(m-2);
        printf("The final score of Athlete %d is %.3f
",
            player[i].number,player[i].ave);
    }

    /**< 选手按得分排序 */
    printf("Order of Athletes:
");
    for(int i=0; i<n; ++i)
    {
        for(int j=1; j<n-i; ++j)
        {
            if(player[j-1].ave<player[j].ave){
                ATHLETES temp = player[j-1];
                player[j-1] = player[j];
                player[j] = temp;
            }
        }
    }

    /**< 输出排序后的选手 */
    printf("order	final score	number code
");
    for(int i=0; i<3; ++i){
        printf("%5d	%11.3f	%6d
",i+1,player[i].ave,player[i].number);
    }

    /**< 评委,评分误差、定量评价计算*/
    JUDVALUE value[N];
    for(int j=0; j<m; ++j){
        float sum = 0;
        for(int i=0; i<n; ++i){
            value[j].number = player[i].judge[j].number;/**<编号  */
            float result = player[i].judge[j].score - player[i].ave;/**<误差  */
            value[j].score[i] = result*result;
            sum += value[j].score[i];/**< 误差汇总 */
        }
        value[j].ave = 10 -sqrt(sum/n) ;/**< 评价 */
    }

    /**< 按评价评委排序 */
    printf("Order of judges:
");
    for(int i=0; i<m; ++i)
    {
        for(int j=1; j<m-i; ++j)
        {
            if(value[j-1].ave<value[j].ave){
                JUDVALUE temp = value[j-1];
                value[j-1] = value[j];
                value[j] = temp;
            }
        }
    }

    /**< 评委输出 */
    printf("order	final score	number code
");
    for(int i=0; i<m; ++i){
        printf("%5d	%11.3f	%6d
",i+1,value[i].ave,value[i].number);
    }
    printf("Over!Thank you!
");
    return 0;
}
View Code

97、学生成绩管理系统V3.0

/**< 第十二周练习区编程题2,学生成绩管理系统V3.0 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

/**< 定义结构 */
#define STU_NUM 100
#define NAME_SIZE 30
typedef struct stu STUDENT;
struct stu
{
    long num;
    char name[NAME_SIZE];
    float score;
};
STUDENT student[STU_NUM];

int Menu();/**< 菜单 */
void InputRecord(int n);/**< 录入学生信息 */
void TotalAverageScore(int n);/**< 总分平均分 */

void SortRecord(int n, int (*f)(STUDENT,STUDENT));/**< 排序 */
int  DescendingScore(STUDENT x,STUDENT y);
int  AscendingScore(STUDENT x,STUDENT y);
int  AscendingNumber(STUDENT x,STUDENT y);
int  AscendingName(STUDENT x,STUDENT y);

void SearchBy(int n,STUDENT x, int(*f)(STUDENT,STUDENT));/**< 查询 */
STUDENT InputNumber();
STUDENT InputName();
int CompareNumber(STUDENT x,STUDENT y);
int CompareName(STUDENT x,STUDENT y);

void StatisticAnalysis(int n);/**< 成绩占比 */
void ListRecord(int n);/**< 输出 */

/**< 主函数 */
int main()
{
    /**< 输入人数 */
    printf("Input student number(n<30):
");
    int n;
    scanf("%d",&n);

    /**< 菜单选项 */
    int choice;
    do{
        choice= Menu();
        switch(choice)
        {
            case 0:printf("End of program!
");break;
            case 1:InputRecord(n);break;
            case 2:TotalAverageScore(n);break;
            case 3:printf("Sort in descending order by score:
");
                   SortRecord(n,DescendingScore);
                   ListRecord(n);break;
            case 4:printf("Sort in ascending order by score:
");
                   SortRecord(n,AscendingScore);
                   ListRecord(n);break;
            case 5:printf("Sort in ascending order by number:
");
                   SortRecord(n,AscendingNumber);
                   ListRecord(n);break;
            case 6:printf("Sort in dictionary order by name:
");
                   SortRecord(n,AscendingName);
                   ListRecord(n);break;
            case 7:SearchBy(n,InputNumber(),CompareNumber);
                   break;
            case 8:SearchBy(n,InputName(),CompareName);
                   break;
            case 9:StatisticAnalysis(n);break;
            case 10:ListRecord(n);break;
            default:printf("Input error!
");
        }
    }while(choice!=0);
    return 0;
}

/**< 菜单 */
int Menu()
{
    printf("Management for Students' scores
");
    printf("1.Input record
");
    printf("2.Caculate total and average score of course
");
    printf("3.Sort in descending order by score
");
    printf("4.Sort in ascending order by score
");
    printf("5.Sort in ascending order by number
");
    printf("6.Sort in dictionary order by name
");
    printf("7.Search by number
");
    printf("8.Search by name
");
    printf("9.Statistic analysis
");
    printf("10.List record
");
    printf("0.Exit
");
    printf("Please Input your choice:
");
    int choice;
    scanf("%d",&choice);
    return choice;
}
/**< 1.输入数据 */
void InputRecord(int n)
{
    printf("Input student's ID, name and score:
");
    for(int i=0; i<n; ++i){
        scanf("%ld%s%f",&student[i].num,student[i].name,
              &student[i].score);
    }
}

/**< 2.计算总分 平均分*/
void TotalAverageScore(int n)
{
    float sum = 0.0;
    for(int i=0; i<n; ++i){
        sum += student[i].score;
    }
    printf("sum=%.0f,aver=%.2f
",sum,sum/n);
}

/**< 3.4.5.6排序(交换法) */
void SortRecord(int n, int (*f)(STUDENT,STUDENT))
{
    for(int i=0; i<n-1; ++i){
        for(int j=i+1; j<n;++j){
            if((*f)(student[i],student[j])){
                STUDENT temp = student[i];
                student[i] = student[j];
                student[j] = temp;
            }
        }
    }
}

/**< 按成绩升序 */
int  DescendingScore(STUDENT x,STUDENT y)
{
    return x.score<y.score;
}

/**< 按成绩降序 */
int  AscendingScore(STUDENT x,STUDENT y)
{
    return x.score>y.score;
}

/**< 按学号降序 */
int  AscendingNumber(STUDENT x,STUDENT y)
{
    return x.num>y.num;
}

/**< 按姓名降序 */
int  AscendingName(STUDENT x,STUDENT y)
{
    return strcmp(x.name,y.name)>0;
}

/**< 7.8.查询 */
void SearchBy(int n, STUDENT x, int (*f)(STUDENT,STUDENT))
{
    int index = -1;
    for(int i=0;i<n;++i){
        if((*f)(student[i],x)){
            index = i; break;
        }
    }
    if(index==-1){
       printf("Not found!
");
    }
    else{
       printf("%ld	%s	%.0f
",student[index].num,
              student[index].name, student[index].score);
    }
}

/**< 输入姓名 */
STUDENT InputName()
{
    printf("Input the name you want to search:
");
    STUDENT x;
    scanf("%s",x.name);
    return x;
}

/**< 按姓名比较  */
int CompareName(STUDENT x,STUDENT y)
{
    return strcmp(x.name,y.name)==0;
}

/**< 输入学号  */
STUDENT InputNumber()
{
    printf("Input the number you want to search:
");
    STUDENT x;
    scanf("%ld",&x.num);
    return x;
}

/**< 按学号比较  */
int CompareNumber(STUDENT x,STUDENT y)
{
    return x.num == y.num;
}

/**< 9.统计占比 */
void StatisticAnalysis(int n)
{
    int grade[6]={0};
    for(int i=0;i<n;++i){
        if(student[i].score<60)  grade[0]++;
        else if(student[i].score<70) grade[1]++;
        else if(student[i].score<80) grade[2]++;
        else if(student[i].score<90) grade[3]++;
        else if(student[i].score<100) grade[4]++;
        else if(student[i].score==100) grade[5]++;
    }
    float percent[6]={0};
    int j;
    for(j=0;j<6;++j){
        percent[j] = grade[j]*100.0/n;
    }
    int k=60;j = 0;
    printf("<60	%d	%.2f%%
",grade[j],percent[j]);
    for(j=1;k<100;k+=10,j++){
        printf("%d-%d	%d	%.2f%%
",k,k+9,grade[j],percent[j]);
    }
    printf("%d	%d	%.2f%%
",k,grade[j],percent[j]);
}

/**< 10.输出数据 */
void ListRecord(int n)
{
    for(int i=0; i<n; ++i){
        printf("%ld	%s	%.0f
",student[i].num,
               student[i].name,student[i].score);
    }
}
View Code

98、单词接龙

#include <stdio.h>
#define N 16

int main()
{
    char a[30] = "", b[30] = "";
    scanf("%s%s",a,b);
    char *pa = a, *pb = b, *t = a;
    while(*t){
        pa = t;
        pb = b;
        while(*pa&&*pb&&!(*pa-*pb)){
            pa++,pb++;
        }
        if(!*pa){
            printf("%s
",t);
        }
        t++;
    }

    return 0;
}
View Code

99、分数比较

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int Compare(int a, int b, int c, int d)
{
    double x = (double)a/b;
    double y = (double)c/d;

    if(x>y)
        return 1;
    if(x<y)
        return -1;
    if(fabs(x-y)<=1e-6)
        return 0;
}
int main()
{
    printf("Input two FENSHU:
");
    int a, b, c, d;
    scanf("%d/%d,%d/%d",&a,&b,&c,&d);

    int ret = Compare(a,b,c,d);
    if(ret==1)
        printf("%d/%d>%d/%d
",a,b,c,d);
    if(ret==-1)
        printf("%d/%d<%d/%d
",a,b,c,d);
    if(ret==0)
        printf("%d/%d=%d/%d
",a,b,c,d);
    return 0;
}
View Code

100、百万富翁的换钱计划

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

int main()
{
    double toStranger = 0.0,toRichman = 0.0,
           ts = 0.01, tr = 100000;

    for(int i=1; i<=30; ++i){
        toStranger += ts;
        toRichman += tr;
        ts *=2;
    }

    printf("to Stranger: %.2f yuan
",toStranger);
    printf("to Richman: %.2f yuan
",toRichman);
    return 0;
}
View Code

101、用计数控制的循环实现正数累加求和

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

int main()
{
    int count = 0, sum = 0, number = 0;
    do{
        printf("Input a number:
");
        scanf("%d",&number);
        if(number>0){
            sum += number;
            count++;
        }
    }while(number!=0);
    printf("sum=%d,count=%d
",sum,count);

    return 0;
}
View Code

102、平方根表

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

int main()
{
    printf("Input n(n<=10):
");
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; ++i){
        printf("%7d",i);
    }
    printf("
");

    for(int i=0; i<n; ++i){
        printf("%d",i);
        int j = 0;
        while(j<n){
            printf("%7.3f",sqrt(10*i+j));
            j++;
        }
        printf("
");
    }

    return 0;
}
View Code

103、最大公约数

#include <stdio.h>
#include <string.h>
#define N 80
 int CommonFactors(int a, int b)
 {
     static int t,a1,b1,count;
     if(a<=0||b<=0){
        return -1;
     }

     if(a1 != a && b1 != b){
        a1 = a;
        b1 = b;
        t = a>b?b:a;
        count = 1;
     }

     while((a%t)||(b%t)){
        t--;
     }

     printf( "Common factor %d is %d
",count++,t);

     return t--;
 }
int main()
{
    printf("Input a and b:
");
    int a,b;
    scanf("%d,%d",&a,&b);

    int t = CommonFactors(a,b);
    while(t>1){
        t = CommonFactors(a,b);
    }

    return 0;
}
View Code

104、23根火柴游戏

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

int main()
{
    printf("Game start!
");
    printf("Note: the maximum number is 3
");
    int matches = 23;
    while(matches)
    {
        int youMove;
        do{
            printf("Please enter the number of matches you are moving:
");
            scanf("%d",&youMove);
            if(youMove>3){
               printf("The number you entered is wrong,please re-enter!
");
            }
        }while(youMove>3);

        matches -= youMove;
        if(!matches){
            printf("I'm sorry. You lost!
");
            break;
        }
        printf("The number of matches you are moving is:%d
",youMove);
        printf("The number of matches left is:%d
",matches);
        int computerMove = matches%3 + 1;
        if(matches<3){
            computerMove = 1;
        }
        printf("The number of matches that have been moved by the computer is:%d
",computerMove);
        matches -= computerMove;
        printf("The number of matches left is:%d
",matches);
        if(!matches){
            printf("Congratulations!You won!
");
            break;
        }
    }
    return 0;
}
View Code

105、学生成绩管理系统V4.0

/**< 第十二周练习区编程题2,学生成绩管理系统V4.0 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

/**< 定义结构 */
#define STU_NUM 30
#define MAX_LEN 10
#define COURSE_NUM 6
typedef struct stu STUDENT;
struct stu
{
    long num;
    char name[MAX_LEN];
    float score[COURSE_NUM];
    float sum;
    float ave;
};
STUDENT student[STU_NUM];
/**< 定义人数 科目数 */
int n,m;

int Menu();/**< 菜单 */
void InputRecord();/**< 录入学生信息 */
void TotalAverageScore();/**< 总分平均分 */
void TotalAverageScoreStu();/**< 总分平均分 */

void SortRecord(int (*f)(STUDENT,STUDENT));/**< 排序 */
int  DescendingScore(STUDENT x,STUDENT y);
int  AscendingScore(STUDENT x,STUDENT y);
int  AscendingNumber(STUDENT x,STUDENT y);
int  AscendingName(STUDENT x,STUDENT y);

void SearchBy(STUDENT x, int(*f)(STUDENT,STUDENT));/**< 查询 */
STUDENT InputNumber();
STUDENT InputName();
int CompareNumber(STUDENT x,STUDENT y);
int CompareName(STUDENT x,STUDENT y);

void StatisticAnalysis();/**< 成绩占比 */
void ListRecord();/**< 输出 */

/**< 主函数 */
int main()
{
    /**< 输入人数 */
    printf("Input student number(n<30):
");
    scanf("%d",&n);

    /**< 菜单选项 */
    int choice;
    do{
        choice= Menu();
        switch(choice)
        {
            case 0:printf("End of program!");break;
            case 1:InputRecord();break;
            case 2:TotalAverageScore();break;
            case 3:TotalAverageScoreStu();break;
            case 4:printf("Sort in descending order by score:
");
                   SortRecord(DescendingScore);
                   ListRecord();break;
            case 5:printf("Sort in ascending order by score:
");
                   SortRecord(AscendingScore);
                   ListRecord();break;
            case 6:printf("Sort in ascending order by number:
");
                   SortRecord(AscendingNumber);
                   ListRecord();break;
            case 7:printf("Sort in dictionary order by name:
");
                   SortRecord(AscendingName);
                   ListRecord();break;
            case 8:SearchBy(InputNumber(),CompareNumber);
                   break;
            case 9:SearchBy(InputName(),CompareName);
                   break;
            case 10:for(int i=0; i<m; ++i)
                        StatisticAnalysis(i);
                   break;
            case 11:ListRecord();break;
            default:printf("Input error!
");
        }
    }while(choice!=0);
    return 0;
}

/**< 菜单 */
int Menu()
{
    printf("Management for Students' scores
");
    printf("1.Input record
");
    printf("2.Caculate total and average score of every course
");
    printf("3.Caculate total and average score of every student
");
    printf("4.Sort in descending order by score
");
    printf("5.Sort in ascending order by score
");
    printf("6.Sort in ascending order by number
");
    printf("7.Sort in dictionary order by name
");
    printf("8.Search by number
");
    printf("9.Search by name
");
    printf("10.Statistic analysis
");
    printf("11.List record
");
    printf("0.Exit
");
    printf("Please Input your choice:
");
    int choice;
    scanf("%d",&choice);
    return choice;
}
/**< 1.输入数据 */
void InputRecord()
{
    printf("Input course number(m<=%d):
",n);
    scanf("%d",&m);

    printf("Input student's ID, name and score:
");
    for(int i=0; i<n; ++i){
        scanf("%ld%s",&student[i].num,student[i].name);
        for(int j=0; j<m; ++j){
            scanf("%f",&student[i].score[j]);
        }
    }
}

/**< 2.计算总分 平均分*/
void TotalAverageScore()
{
    for(int j=0; j<m; ++j){
        float sum = 0.0;
        for(int i=0; i<n; ++i){
            sum += student[i].score[j];
        }
        printf("course %d:sum=%.0f,aver=%.0f
",j+1,sum,sum/n);
    }
}

/**< 3.计算每个学生的总分 平均分 */
void TotalAverageScoreStu()
{
    for(int i=0; i<n; ++i){
        float sum = 0.0;
        for(int j=0; j<m; ++j){
            sum += student[i].score[j];
        }
        student[i].sum = sum;
        student[i].ave = sum/m;
        printf("student %d:sum=%.0f,aver=%.0f
",i+1,
               student[i].sum,student[i].ave);
    }
}

/**< 4.5.6.7排序(交换法) */
void SortRecord(int (*f)(STUDENT,STUDENT))
{
    for(int i=0; i<n-1; ++i){
        for(int j=i+1; j<n;++j){
            if((*f)(student[i],student[j])){
                STUDENT temp = student[i];
                student[i] = student[j];
                student[j] = temp;
            }
        }
    }
}

/**< 按每个学生的总分升序 */
int  DescendingScore(STUDENT x,STUDENT y)
{
    return x.sum<y.sum;
}

/**< 按每个学生的总分降序 */
int  AscendingScore(STUDENT x,STUDENT y)
{
    return x.sum>y.sum;
}

/**< 按学号降序 */
int  AscendingNumber(STUDENT x,STUDENT y)
{
    return x.num>y.num;
}

/**< 按姓名降序 */
int  AscendingName(STUDENT x,STUDENT y)
{
    return strcmp(x.name,y.name)>0;
}

/**< 8.9.查询 */
void SearchBy(STUDENT x, int (*f)(STUDENT,STUDENT))
{
    int index = -1;
    for(int i=0;i<n;++i){
        if((*f)(student[i],x)){
            index = i; break;
        }
    }
    if(index==-1){
       printf("Not found!
");
    }
    else{
       printf("%ld	%s	",student[index].num,
              student[index].name);
       for(int i=0; i<m; ++i)
            printf("%.0f	",student[index].score[i]);
       printf("%.0f	%.0f
",student[index].sum,
              student[index].ave);
    }
}

/**< 输入姓名 */
STUDENT InputName()
{
    printf("Input the name you want to search:
");
    STUDENT x;
    scanf("%s",x.name);
    return x;
}

/**< 按姓名比较  */
int CompareName(STUDENT x,STUDENT y)
{
    return strcmp(x.name,y.name)==0;
}

/**< 输入学号  */
STUDENT InputNumber()
{
    printf("Input the number you want to search:
");
    STUDENT x;
    scanf("%ld",&x.num);
    return x;
}

/**< 按学号比较  */
int CompareNumber(STUDENT x,STUDENT y)
{
    return x.num == y.num;
}

/**< 10.统计占比 */
void StatisticAnalysis(int index)
{
    printf("For course %d:
",index+1);
    int grade[6]={0};
    for(int i=0;i<n;++i){
        if(student[i].score[index]<60)  grade[0]++;
        else if(student[i].score[index]<70) grade[1]++;
        else if(student[i].score[index]<80) grade[2]++;
        else if(student[i].score[index]<90) grade[3]++;
        else if(student[i].score[index]<100) grade[4]++;
        else if(student[i].score[index]==100) grade[5]++;
    }
    float percent[6]={0};
    int j;
    for(j=0;j<6;++j){
        percent[j] = grade[j]*100.0/n;
    }
    int k=60;j = 0;
    printf("<60	%d	%.2f%%
",grade[j],percent[j]);
    for(j=1;k<100;k+=10,j++){
        printf("%d-%d	%d	%.2f%%
",k,k+9,grade[j],percent[j]);
    }
    printf("%d	%d	%.2f%%
",k,grade[j],percent[j]);
}

/**< 11.输出数据 */
void ListRecord()
{
    for(int i=0; i<n; ++i){
        printf("%ld	%s	",student[i].num,student[i].name);
        for(int j=0; j<m; ++j){
            printf("%.0f	",student[i].score[j]);
        }
        printf("%.0f	%.0f
",student[i].sum,
              student[i].ave);
    }
}
View Code

106、寻找最高分成绩的学生

#include  <stdio.h>
#include  <stdlib.h>
void InputScore(int *p, int m, int n);
int  FindMax(int *p, int m, int n, int *pRow, int *pCol);
int main()
{
    int  *pScore, m, n, maxScore, row, col;
    printf("Input array size m,n:
");
    scanf("%d,%d", &m, &n);
    pScore = (int *)malloc(m*n*sizeof(int)); /* 申请动态内存 */

    if (pScore == NULL)
    {
        printf("No enough memory!
");
        exit(0);
    }
    InputScore(pScore, m, n);
    maxScore = FindMax(pScore,m,n,&row,&col);

    printf("maxScore = %d, class = %d, number = %d
", maxScore, row+1, col+1);
    free(pScore);                                      /* 释放动态内存 */
    return 0;
}

/* 函数功能:输入m行n列二维数组的值 */
void InputScore(int *pScore, int m, int n)
{
    int i, j;
    printf("Input %d*%d array:
", m, n);
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            scanf("%d", &pScore[i*n+j]);
        }
    }
}
/*  函数功能:计算任意m行n列二维数组中元素的最大值,并指出其所在行列下标值 */
int  FindMax(int *p, int m, int n, int *pRow, int *pCol)
{
    int  i, j, max = p[0];
//    __________;
//
//    __________;

    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            if (p[i*n+j]>max)
            {
                max = p[i*n+j];
                *pRow = i;       /*记录行下标*/
                *pCol = j;             /*记录列下标*/
            }
        }
    }
    return max;
}
View Code

107、程序改错

#include  <stdio.h>
#define STUD   30      /* 最多可能的学生人数 */
#define COURSE 5       /* 最多可能的考试科目数 */
void  Total(int *pScore, int sum[], float aver[], int m, int n);
void  Print(int *pScore, int sum[], float aver[], int m, int n);
int main()
{
    int     i, j, m, n, score[STUD][COURSE], sum[STUD];
    float   aver[STUD];
    printf("How many students?
");
    scanf("%d", &m);
    printf("How many courses?
");
    scanf("%d", &n);
    printf("Input scores:
");

    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            scanf("%d", &score[i][j]);
        }
    }

    Total(*score, sum, aver, m, n);
    Print(*score, sum, aver, m, n);
     return 0;
}
void  Total(int *pScore, int sum[], float aver[], int m, int n)
{
    int  i, j;
    for (i=0; i<m; i++)
    {
        sum[i] = 0;
        for (j=0; j<n; j++)
        {
            sum[i] = sum[i] + pScore[i*COURSE+j];
        }
        aver[i] = (float) sum[i] / n;
    }
}
void  Print(int *pScore, int sum[], float aver[], int m, int n)
{
    int  i, j;
    printf("Result:
");
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            printf("%4d", pScore[i*COURSE+j]);
        }
        printf("%5d%6.1f
", sum[i], aver[i]);
    }
}
View Code

108、矩阵转置

#include <stdio.h>
#define M 10
#define N 10
void Transpose(int *a, int *at, int m, int n);
void InputMatrix(int *a, int m, int n);
void PrintMatrix(int *at, int n, int m);
int main()
{
    int s[M][N], st[N][M], m, n;
    printf("Input m, n:
");
    scanf("%d,%d", &m, &n);
    InputMatrix(*s, m, n);
    Transpose(*s,*st,m,n);
    printf("The transposed matrix is:
");
    PrintMatrix(*st, n,  m);
    return 0;
}
/* 函数功能:计算m*n矩阵a的转置矩阵at */
void Transpose(int *a, int *at, int m, int n)
{
    int i, j;
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            at[j*M+i] = a[i*N+j];
        }
    }
}
/* 函数功能:输入m*n矩阵a的值 */
void InputMatrix(int *a, int m, int n)
{
    int i, j;
    printf("Input %d*%d matrix:
", m, n);
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            scanf("%d", &a[i*N+j]);
        }
    }
}
/* 函数功能:输出n*m矩阵at的值 */
void PrintMatrix(int *at, int n, int m)
{
    int i, j;
    for (i=0; i<n; i++)
    {
        for (j=0; j<m; j++)
        {
            printf("%-5d", at[i*N+j]);
        }
        printf("
");
    }
}
View Code

109、在升序排序的数组中插入一个元素

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("Input array size:
");
    int n;
    scanf("%d",&n);
    int *p = (int *)malloc(sizeof(int)*n);

    if(p == NULL){
        return 0;
    }
    printf("Input array:
");
    for(int i=0; i<n; ++i){
        scanf("%d",&p[i]);
    }

    printf("Input x:
");
    int x;
    scanf("%d",&x);
    p = (int *)realloc(p,sizeof(int)*(n+1));

    if(p == NULL){
        return 0;
    }

    for(int j=0; j<n+1; ++j){
        if(p[j]>x){
            int i = n+1;
            while(i>=j){
                p[i] = p[i-1];
                --i;
            }
            p[j] = x;
            break;
        }
    }

    printf("After insert %d:
",x);
    for(int j=0; j<n+1; ++j)
        printf("%4d",p[j]);

    free(p);
    return 0;
}
View Code

110、计算平均数、中位数和众数

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("Input the feedbacks of 40 students:
");
    int sum = 0, array[11] = {0};
    int *p = (int *)malloc(sizeof(int)*40);
    for(int i=0; i<40; ++i){
        scanf("%d",&p[i]);
        sum += p[i];
        array[p[i]]++;
    }

    for(int i=0; i<40; ++i){
        for(int j=i+1; j<40; ++j){
            if(p[i]>p[j]){
                int temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }
    }

    int max = 0;
    for(int i=1; i<11; ++i){
        if(array[i]>array[max]){
            max = i;
        }
    }

    printf("Mean value=%d
",sum/40);
    printf("Median value=%d
",(p[20]+p[19])/2);
    printf("Mode value=%d
",max);

    free(p);
    return 0;
}
View Code

111、学生成绩管理系统V5.0

#include  <stdio.h>
#include  <stdlib.h>
#include  <string.h>
#define   MAX_LEN  10                        /* 字符串最大长度 */
#define   STU_NUM 30                       /* 最多的学生人数 */
#define   COURSE_NUM 6                     /* 最多的考试科目数 */
typedef struct student
{
        long num;            /* 每个学生的学号 */
        char name[MAX_LEN];                /* 每个学生的姓名 */
        float score[COURSE_NUM];    /* 每个学生COURSE_NUM门功课的成绩 */
        float sum;                       /* 每个学生的总成绩 */
        float aver;                   /* 每个学生的平均成绩 */
}STU;
int   Menu(void);
void  ReadScore(STU stu[], int n, int m);
void  AverSumofEveryStudent(STU stu[], int n, int m);
void  AverSumofEveryCourse(STU stu[], int n, int m);
void  SortbyScore(STU stu[],int n,int m,int (*compare)(float a,float b));
int   Ascending(float a, float b);
int   Descending(float a, float b);
void  AsSortbyNum(STU stu[], int n, int m);
void  SortbyName(STU stu[], int n, int m);
void  SearchbyNum(STU stu[], int n, int m);
void  SearchbyName(STU stu[], int n, int m);
void  StatisticAnalysis(STU stu[], int n, int m);
void  PrintScore(STU stu[], int n, int m);
int main()
{
    char  ch;
    int   n = 0, m = 0;  /* 学生人数为n,课程门数为m */
    STU   stu[STU_NUM];
    printf("Input student number(n<=30):
", STU_NUM);
        scanf("%d", &n);
    while (1)
    {
        ch = Menu();       /* 显示菜单,并读取用户输入 */
        switch (ch)
        {
            case 1:
                printf("Input course number(m<=%d):
",COURSE_NUM);
                scanf("%d", &m);
                ReadScore(stu,n,m);
                break;
            case 2:
                AverSumofEveryCourse(stu,n,m);
                break;
            case 3:
                AverSumofEveryStudent(stu,n,m);
                break;
            case 4:
                SortbyScore(stu,n,m,Descending);
                printf("Sort in descending order by score:
");
                PrintScore(stu,n,m);
                break;
            case 5:
                SortbyScore(stu,n,m,Ascending);
                printf("Sort in ascending order by score:
");
                PrintScore(stu,n,m);
                break;
            case 6:
                AsSortbyNum(stu,n,m);
                printf("Sort in ascending order by number:
");
                PrintScore(stu,n,m);
                break;
            case 7:
                SortbyName(stu,n,m);
                printf("Sort in dictionary order by name:
");
                PrintScore(stu,n,m);
                break;
            case 8:
                SearchbyNum(stu,n,m);
                break;
            case 9:
                SearchbyName(stu,n,m);
                break;
            case 10:
                StatisticAnalysis(stu,n,m);
                break;
            case 11:
                PrintScore(stu,n,m);
                break;
            case 0:
                printf("End of program!");
                exit(0);
            default: printf("Input error!
");
        }
    }
    return 0;
}

/*  函数功能:显示菜单并获得用户键盘输入的选项 */
int Menu(void)
{
    printf("Management for Students' scores
");
    printf("1.Input record
");
    printf("2.Caculate total and average score of every course
");
    printf("3.Caculate total and average score of every student
");
    printf("4.Sort in descending order by score
");
    printf("5.Sort in ascending order by score
");
    printf("6.Sort in ascending order by number
");
    printf("7.Sort in dictionary order by name
");
    printf("8.Search by number
");
    printf("9.Search by name
");
    printf("10.Statistic analysis
");
    printf("11.List record
");
    printf("0.Exit
");
    printf("Please Input your choice:
");
    int choice;
    scanf("%d",&choice);
    return choice;
}

/* 函数功能:输入n个学生的m门课成绩 */
void ReadScore(STU stu[], int n, int m)
{
    printf("Input student's ID, name and score:
");
    for(int i=0; i<n; ++i){
        scanf("%ld%s",&stu[i].num,stu[i].name);
        for(int j=0; j<m; ++j){
            scanf("%f",&stu[i].score[j]);
        }
    }
}

/* 函数功能:计算每个学生各门课程的总分和平均分 */
void AverSumofEveryStudent(STU stu[], int n, int m)
{
    for(int i=0; i<n; ++i){
        float sum = 0.0;
        for(int j=0; j<m; ++j){
            sum += stu[i].score[j];
        }
        stu[i].sum = sum;
        stu[i].aver = sum/m;
        printf("student %d: sum=%.0f,aver=%.0f
",i+1,
               stu[i].sum,stu[i].aver);
    }
}

/* 函数功能:计算每门课程的总分和平均分 */
void AverSumofEveryCourse(STU stu[], int n, int m)
{
    for(int j=0; j<m; ++j){
        float sum = 0.0;
        for(int i=0; i<n; ++i){
            sum += stu[i].score[j];
        }
        printf("course %d:sum=%.0f,aver=%.0f
",j+1,sum,sum/n);
    }
}
/* 函数功能:按选择法将数组sum的元素值排序 */
void SortbyScore(STU stu[], int n, int m, int (*compare)(float a, float b))
{
    for(int i=0; i<n-1; ++i){
        int k = i;
        for(int j=i+1; j<n;++j){
            if((*compare)(stu[j].sum,stu[k].sum))
                k = j;
        }
        if(k!=i){
            STU temp = stu[i];
            stu[i] = stu[k];
            stu[k] = temp;
        }
    }
}

/* 使数据按升序排序 */
int Ascending(float a, float b)
{
    return a < b;
}

/* 使数据按降序排序 */
int Descending(float a, float b)
{
    return a > b;
}

/* 函数功能:按选择法将数组num的元素值按从低到高排序 */
void AsSortbyNum(STU stu[], int n, int m)
{
    for(int i=0; i<n-1; ++i){
        int k = i;
        for(int j=i+1; j<n;++j){
            if(stu[j].num<stu[k].num)
                k = j;
        }
        if(k!=i){
            STU temp = stu[i];
            stu[i] = stu[k];
            stu[k] = temp;
        }
    }
}

/* 函数功能:交换法实现字符串按字典顺序排序 */
void SortbyName(STU stu[], int n, int m)
{
    for(int i=0; i<n-1; ++i){
        int k = i;
        for(int j=i+1; j<n;++j){
            if(strcmp(stu[j].name,stu[k].name)<0)
                k = j;
        }
        if(k!=i){
            STU temp = stu[i];
            stu[i] = stu[k];
            stu[k] = temp;
        }
    }
}

/* 函数功能:按学号查找学生成绩并显示查找结果 */
void SearchbyNum(STU stu[], int n, int m)
{
    printf("Input the number you want to search:
");
    long t;
    scanf("%ld",&t);
    int index = -1;
    for(int i=0;i<n;++i){
        if(stu[i].num == t){
            index = i; break;
        }
    }
    if(index==-1){
       printf("Not found!
");
    }
    else{
       printf("%ld	%s	",stu[index].num,
              stu[index].name);
       for(int i=0; i<m; ++i)
            printf("%.0f	",stu[index].score[i]);
       printf("%.0f	%.0f
",stu[index].sum,
              stu[index].aver);
    }
}

/* 函数功能:按姓名的字典顺序排出成绩表 */
void SearchbyName(STU stu[], int n, int m)
{
    printf("Input the name you want to search:
");
    char t[MAX_LEN] = "";
    scanf("%s",t);
    int index = -1;
    for(int i=0;i<n;++i){
        if(strcmp(stu[i].name,t)==0){
            index = i; break;
        }
    }
    if(index==-1){
       printf("Not found!
");
    }
    else{
       printf("%ld	%s	",stu[index].num,
              stu[index].name);
       for(int i=0; i<m; ++i)
            printf("%.0f	",stu[index].score[i]);
       printf("%.0f	%.0f
",stu[index].sum,
              stu[index].aver);
    }
}
/* 函数功能:统计各分数段的学生人数及所占的百分比 */
void StatisticAnalysis(STU stu[], int n, int m)
{
    for(int index=0; index<m; ++index)
    {
        printf("For course %d:
",index+1);
        int grade[6]={0};
        for(int i=0;i<n;++i){
            if(stu[i].score[index]<60)  grade[0]++;
            else if(stu[i].score[index]<70) grade[1]++;
            else if(stu[i].score[index]<80) grade[2]++;
            else if(stu[i].score[index]<90) grade[3]++;
            else if(stu[i].score[index]<100) grade[4]++;
            else if(stu[i].score[index]==100) grade[5]++;
        }
        float percent[6]={0};
        int j;
        for(j=0;j<6;++j){
            percent[j] = grade[j]*100.0/n;
        }
        int k=60;j = 0;
        printf("<60	%d	%.2f%%
",grade[j],percent[j]);
        for(j=1;k<100;k+=10,j++){
            printf("%d-%d	%d	%.2f%%
",k,k+9,grade[j],percent[j]);
        }
        printf("%d	%d	%.2f%%
",k,grade[j],percent[j]);
    }
}

/* 函数功能: 打印学生成绩 */
void PrintScore(STU stu[], int n, int m)
{
    for(int i=0; i<n; ++i){
        printf("%ld	%s	",stu[i].num,stu[i].name);
        for(int j=0; j<m; ++j){
            printf("%.0f	",stu[i].score[j]);
        }
        printf("%.0f	%.0f
",stu[i].sum,
              stu[i].aver);
    }
}
View Code

112、字符串中的字符排序

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

void InsertionSort( char *p);
int main()
{
    printf("Input a string:
");
    char *p = (char *)malloc(sizeof(char)*20);
    if(p==NULL){
        printf("No enough memory!
");
        exit(0);
    }

    gets(p);
    InsertionSort(p);

    printf("%s",p);
    free(p);

    return 0;
}
void InsertionSort( char *p)/* 插入排序 */
{
    int i, j;
    char Tmp;
    for ( j=1; p[j]; j++ ) {
        Tmp = p[j]; /* 取出未排序序列中的第一个元素*/
        for ( i=j; i>0 && p[i-1]>Tmp; i-- )
            p[i] = p[i-1]; /*依次与已排序序列中元素比较并右移*/
        p[i] = Tmp; /* 放进合适的位置 */
    }
}
View Code

113、纯数字字符串检验

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

int IsAllDigit( char *p);
int main()
{
    printf("Please input a string:
");
    char *p = (char *)malloc(sizeof(char)*20);
    if(p==NULL){
        printf("No enough memory!
");
        exit(0);
    }

    gets(p);
    if(!IsAllDigit(p))
        printf("The string is not digit string.");
    else
        printf("The string is digit string.");
    free(p);

    return 0;
}
int IsAllDigit( char *p)
{
    for (int j=0; p[j]; j++ ) {
        if(p[j]<'0'||p[j]>'9')
            return 0;
    }
    return 1;
}
View Code

114、孪生素数

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

long IsPrime( long c);
int main()
{
    printf("please input c,d(c>2):
");
    long c,d;
    scanf("%ld,%ld",&c,&d);

    int total = 0, x = 0, y = 0;
    while(c<=d)
    {
        while(!IsPrime(c))
            c++;
        y = c++;
        if(y-x == 2){
           total++;
           printf("(%ld,%ld)
",x,y);
        }
        x = y;
    }
    printf( "total=%d
",total);

    return 0;
}
long IsPrime( long c)
{
    for(long i=2; i<c;++i)
        if(c%i==0)
            return 0;
    return 1;
}
View Code

115、求解不等式

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

int main()
{
    printf("Input n:
");
    double n;
    scanf("%lf",&n);
    double s = 0.0;
    int m = 0;

    while(s<n){
        int t = ++m;
        s = 0.0;
        while(t<=2*m)
            s += sqrt(t++);
    }

    printf("Result:m>=%d
",m);
    printf("s=%.2f
",s);

    return 0;
}
View Code

116、计算零件数

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

int main()
{
    int x = 14;
    while(!(x%4==2&&x%7==3&&x%9==5))
        x++;
    printf("%d",x);

    return 0;
}
View Code

117、走台阶

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

int main()
{
    int a1 = 1, a2 = 2, t = 0;
    int n = 8;
    while(n--)
    {
        t = a2;
        a2 = a1 + a2;
        a1 = t;
    }
    printf("Result=%d",a2);

    return 0;
}
View Code

118、将数据按照奇偶排序

#include <stdio.h>
#include <string.h>
#define N 10
int main()
{  //freopen("E:\桌面\test.txt","r",stdin);
    int a[N] = {0}, b[N] = {0};
    printf("Input 10 numbers:
");
    for(int i=0; i<N; ++i){
        scanf("%d",&a[i]);
    }

    /**< 选择排序 */
    for(int i=0; i<N-1; ++i){
        int k = i;
        for(int j=i+1; j<N; ++j){
            if(a[k]>a[j])
            {
                k = j;
            }
        }
        if(i!=k){
            int t = a[i];
            a[i] = a[k];
            a[k] = t;
        }
    }

    int *p = b;
    for(int i=0; i<N; ++i){
        if(a[i]%2)
            *p++ = a[i];
    }
    for(int i=0; i<N; ++i){
        if(a[i]%2==0)
            *p++ = a[i];
    }

    printf("Output: ");
    int i=0;
    for(; i<N-1; ++i){
        if(i)
            printf(",");
        printf("%d",b[i]);
    }
    printf(",%d
",b[i]);
    return 0;
}
View Code

119、三色球分组

#include <stdio.h>
#include <string.h>
#define N 10
int Fun (void);
int main()
{  //freopen("E:\桌面\test.txt","r",stdin);
    printf("sum=%4d
",Fun());
    return 0;
}
int Fun (void)
{
    int sum = 0;
    printf("The result:
");
    for(int i=1; i<=3; ++i){
        for(int j=1; j<=5; ++j){
            int black = 8-i-j;
            printf("red:%4d white:%4d black:%4d
",i,j,black);
            sum++;
        }
    }
    return sum;
}
View Code

120、同构数

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

int main()
{
    int n = 10;
    for(int m=1; m<=99; ++m){
        if(m>=10)
            n = 100;
        if(m*m%n==m)
            printf("m=%3d		m*m=%6d
",m,m*m);
    }

    return 0;
}
View Code

三、课程相关

1、百鸡问题

#include <stdio.h>
int main()
{
    int x, y, z;
    for (x=0; x<=20; x++)
    {
        for (y=0; y<=33; y++)//1.
        {
                z = 100 - x - y;//2.
                if (5*x + 3*y + z/3.0 == 100)//3.
                {
                    printf("x=%d, y=%d, z=%d
", x, y, z);
                }
        }
    }
    return 0;
} 
View Code

2、最大值和最小值(指针变量参数)

#include <stdio.h>
int FindMax(int num[], int n, int *pMaxPos);
int FindMin(int num[], int n, int *pMinPos);    
int main()
{
    int num[10], maxValue, maxPos, minValue, minPos, i;
    printf("Input 10 numbers:
");
    for (i=0; i<10; i++)
    {
        scanf("%d", &num[i]);   // 输入10个数
    }                        
    maxValue = FindMax(num, 10, &maxPos);  // 找最大值及其所在下标位置 
    minValue = FindMin(num, 10, &minPos);  // 找最小值及其所在下标位置 
    printf("Max=%d,Position=%d,Min=%d,Position=%d
",
    maxValue, maxPos, minValue, minPos);
    return 0;
}
 
//函数功能:求有n个元素的整型数组num中的最大值及其所在下标位置,函数返回最大值
int FindMax(int num[], int n, int *pMaxPos)  //1.
{
    int i, max;
    max = num[0];//假设num[0]为最大值
    *pMaxPos = 0;//假设最大值在数组中的下标位置为0 //2.
    for (i=1; i<n; i++)
    {
        if (num[i] > max)
        {
            max = num[i];
            *pMaxPos = i;  //pMaxPos指向最大值数组元素的下标位置//3.
        }
    }
    return max ;//4.
}
 
//函数功能:求有n个元素的整型数组num中的最小值及其所在下标位置,函数返回最小值
int FindMin(int num[], int n, int *pMinPos)//5.
{
    int i, min;
    min = num[0];            //假设num[0]为最小
    *pMinPos = 0;            //假设最小值在数组中的下标位置为0 //6.
    for (i=1; i<10; i++)
    {
        if (num[i] < min)
        {
            min = num[i];
            *pMinPos = i;  //pMinPos指向最小值数组元素的下标位置//7.
        }
    }
    return min;//8.
}
View Code

3、10阶台阶

#include<stdio.h>
int main()
{
    int i = 0, a[10];
    a[0] = 1;
    a[1] = 2;
    for (i = 2; i < 10; ++i)//1
    {
        a[i] = a[i - 1] + a[i - 2];//2.
    }
    printf("Result=%d", a[9]);//3.
    return 0;
} 
View Code

4、最大公约数

#include <stdio.h>
int MaxCommonFactor(int a, int b);
int main()
{
     int a, b, x;
     printf("Input a,b:");
     scanf("%d,%d", &a, &b);
     x =MaxCommonFactor(a, b);//1.
      
     if (x != -1)
     {
          printf("MaxCommonFactor = %d
", x);
     }
     else
     {
          printf("Input error!
");
     }
      
     return 0;
}
//函数功能: 计算两个正整数的最大公约数,-1表示没有最大公约数
int MaxCommonFactor(int a, int b)
{
     int r;
     if (a<=0 || b<=0) return -1; // 保证输入的参数为正整数
          
     do{
          r = a % b;//2
          a = b;
          b = r;//3.
     }while (r != 0);//4.
      
     return  a;   
}    
View Code

5、判断素数

#include <math.h>
#include <stdio.h>
int IsPrimeNumber(int number);
int main()
{
     int n, ret;
     printf("Input n:");
     scanf("%d", &n);
     ret = IsPrimeNumber(n);
     if (ret != 0)//1.
     { 
          printf("%d is a prime number
", n);
     }
     else
     {
          printf("%d is not a prime number
", n);
     }
     return 0;
}
//函数功能:判断number是否是素数,函数返回非0值,表示是素数,否则不是素数
int IsPrimeNumber(int number)
{
     int i;
      
     if (number <= 1) return 0; // 负数、0和1都不是素数   
         for (i=2; i<=sqrt(number); i++)//2.
         {
              if (number % i == 0) // 被整除,不是素数 //3.
                  return 0;
     }
     return 1;
} 
View Code

6、四位分离之和

#include <stdio.h> 
#include <math.h> 
int main() 
{     
    int i1, i2, i3, i4, k, n;     
    printf("Input data is:");     
    scanf("%d", &n);     
    k = fabs(n);    //取绝对值     
    i1 = k / 1000; //分离出千位   //1.  
    i2 = (k - i1 * 1000) / 100; //分离出百位//2. 
    i3 = (k - i1 * 1000 - i2 * 100) / 10; //分离出十位//3.
    i4 = k % 10; //分离出个位//4.
    printf("The sum of the total bit is %d
", i1 + i2 + i3 + i4); 
    return 0;
} 
View Code

7、判断三角形

#include <stdio.h>
#include  <math.h>
int main()
{ 
      float  a, b, c, s, area;                
      printf("Input a,b,c:");
      scanf("%f,%f,%f", &a, &b, &c);
       
      if (a+b>c && b+c>a && a+c>b)//1.
        {
            s = (float)(a + b + c) / 2;
             
            area = sqrt(s * (s - a) * (s - b) * (s - c));//2.
             
            printf("area = %f
", area);
        }
     else
        {
            printf("It is not a triangle
");
        }
    return 0;
} 
View Code

8、判断闰年

#include  <stdio.h>
int main()
{
    int  year, flag;
    printf("Input a year:");
    scanf("%d", &year);
    
    if (year%4 == 0 && year%100 != 0 || year%400 == 0) //1.
     
         flag = 1;                 /* 如果year是闰年,则标志变量flag置1 */
    else  
         flag = 0;                 /* 否则,标志变量flag置0 */
          
    if (flag)//2.
      
         printf("%d is a leap year!
",year);      /* 打印“是闰年”*/
    else 
        printf("%d is not a leap year!
",year);  /* 打印“不是闰年”*/
         
    return 0;
} 
View Code

9、递归函数

int Sum(int n)
{
    if (n <= 0) printf("data error
");
    if (n == 1)return 1;//1.
    else return n+Sum(n-1);//2.
} 
View Code

10、累加和

#include <stdio.h>
int Fun(int b[],int m,int n)//1.
{ 
    int  i,s=0;
    for( i=m;i<=n;i++)  //2.
        s=s+b[i];
    return s;//3.
}
 
int main()
{ 
    int  x,a[]={1,2,3,4,5,6,7,8,9};
    x=Fun(a,3,7);
    printf("%d
",x);
    return 0;
} 
View Code

11、杨辉三角

#include<stdio.h>
#define  N  20
void  CaculateYH(int a[][N], int  n);
void  PrintYH(int a[][N], int  n);
int main()
{
    int  a[N][N] = {0}, n;
    printf("Input  n(n<20):");
    scanf("%d", &n);     
    CaculateYH(a, n);
    PrintYH(a, n);
    return 0;
}
 
/* 函数功能:计算杨辉三角形前n行元素的值 */
void CaculateYH(int a[][N], int n)//1.
{
    int  i, j;
    for (i=0; i<n; i++)
     {   
        a[i][0] = 1;
        a[i][i] = 1; //2.
     }
    for (i=2; i<n; i++)//3.
    {
        for (j=1; j<=i-1; j++)
        {
           a[i][j] = a[i-1][j-1] + a[i-1][j];//4.
        }
   } 
}
/* 函数功能:输出杨辉三角形前n行元素的值 */
void PrintYH(int a[][N], int n)
{
    int  i, j;
    for (i=0; i<n; i++)
    {
        for (j=0; j<=i; j++)//5.
        {
            printf("%4d", a[i][j]);
        }
        printf("
");
    }
}
View Code

12、邮票组合

#include <stdio.h>
int main()
{
    int i,j,k,s,n=0;
    int a[100]={0};
     
    for(i=0;i<=5;i++)//1.
        for(j=0;j<=5;j++)//2.
        {
            s=2*i+3*j;//3.
             
            for(k=0;a[k]!=0;k++)
                if(s==a[k])
                   break;//4.
                    
            if(a[k]==0&&s>0)
            {
                a[k]=s;//5.
                n++;
            }
        }
         
    printf("
 %d kinds:",n);
     
    for(k=0;a[k];k++)
        printf("%2d,",a[k]);
         
    return 0;
}
View Code

13、sizeof与strlen

#include <stdio.h>
#include <string.h>
int main( )
{ 
    char a[]="abcdefg",b[10]="abcdefg";
    printf("%d %d
",sizeof(a) ,sizeof(b));  /*1.测量字符数组a,b的长度,并输出*/
    printf("%d %d
",strlen(a) ,strlen(b));  /*2.测量字符数组a,b中存放的字符串的长度,并输出*/
    return 0;
}
View Code

14、矩阵转置

#include <stdio.h>
#define M 10
#define N 10
void Transpose(int a[][N], int at[][M], int m, int n);
void InputMatrix(int a[][N], int m, int n);
void PrintMatrix(int at[][M], int n, int m);
int main()
{
    int s[M][N], st[N][M], m, n;
    printf("Input m, n:");
    scanf("%d,%d", &m, &n);
    InputMatrix(s, m, n);
    Transpose(s, st, m, n);
    printf("The transposed matrix is:
");
    PrintMatrix(st, n,  m); 
    return 0;
}
/* 函数功能:计算m*n矩阵a的转置矩阵at */
void Transpose(int a[][N], int at[][M], int m, int n)   
{ 
    int i, j;
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            at[j][i] = a[i][j];//1.
        }
    }
}
/* 函数功能:输入m*n矩阵a的值 */
void InputMatrix(int a[][N], int m, int n)   
{
    int i, j;
    printf("Input %d*%d matrix:
", m, n);
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            scanf("%d", &a[i][j]); 
        }
    }
}
/* 函数功能:输出n*m矩阵at的值 */
void PrintMatrix(int at[][M], int n, int m)
{
    int i, j;
    for (i=0; i<n; i++)//2.
    {
        for (j=0; j<m; j++)//3.
        {
            printf("%d	", at[i][j]);
        }
        printf("
");//4.
    }
} 
View Code

15、同构数

#include <stdio.h>
int main()
{
    int m;
    for (m = 1; m <= 99; m++)
        {
            if ( m*m % 10 == m || m*m % 100 == m)//1.
            {
                printf("m=%3d		m*m=%6d
", m, m*m);
            }
        }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/GoldenEllipsis/p/13085022.html