c语言习题

2023-05-16

C语言习题

1.学生成绩信息管理

#include  <stdio.h>
#define STUD   30           /* 最多可能的学生人数 */
#define COURSE 5            /* 最多可能的考试科目数 */
void  Total(int pScore[][COURSE], int sum[], float aver[], int m, int n);
void  Print(int pScore[][COURSE], 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:\n");
    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[][COURSE], 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][j];
        }
        aver[i] = (float) sum[i] / n;
    }
}
void  Print(int pScore[][COURSE], int sum[], float aver[], int m, int n)
{
    int  i, j;
    printf("Result:\n");
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("%4d\t", pScore[i][j]);
        }
        printf("%5d\t%6.1f\n", sum[i], aver[i]);
    }
}

2.将输入同学的名字按顺序排列

#include <stdio.h>
#include <string.h>
main()
{
    char a[5][10],t[10];
    int i,j;
    for(i=0;i<5;i++)
    {
        printf("请输入第%d个同学名字:",i+1);
        gets(a[i]);
    }
    for(i=1;i<5;i++)
        for(j=0;j<5-i;j++)
        if(strcmp(a[j],a[j+1])<0)
        {
            strcpy(t,a[j]);
            strcpy(a[j],a[j+1]);
            strcpy(a[j+1],t);
        }
    for(i=0;i<5;i++)
        puts(a[i]);
    }

3.实现字符串的逆序输出

#include <stdio.h>
#include <string.h>
void main()
{
  char str[100];
  int n, i;
  printf("Input a string:");
  scanf("%s", str);
  n = strlen(str);

  printf("The reversed string:");
  for (i = n - 1; i >= 0; i--)
    printf("%c", str[i]);
  printf("\n");
}

运行结果:
Please Enter String1: 0
2 Result is: 0
3 .tneduts a ma I
4.找到3*4矩阵中的最大元素

#include<stdio.h>
int max(int a, int b, int c, int d);
main()
{
    int a, b, c, d, a1, b1, c1, d1, a2, b2, c2, d2;
    int q, w, e;
    scanf("{{%d,%d,%d,%d},{%d,%d,%d,%d},{%d,%d,%d,%d}}", &a, &b, &c, &d, &a1, &b1, &c1, &d1, &a2, &b2, &c2, &d2);
    q = max(a, b, c, d);
    w = max(a1, b1, c1, d1);
    e = max(a2, b2, c2, d2);
    printf("max value is %d\n", max(q, q, w, e));
}
int max(int a, int b, int c, int d)
{
    int x;
    if (a > b) x = a;
    else x = b;
    if (x > c);
    else x = c;
    if (x > d);
    else x = d;
    return x;
}

5.找到字符串中的最大元素和最小元素及其位置

#include<stdio.h>
main()
{
    int a[10], i, k, flag = 1;
    for (i = 0; i < 10; i++)
    {
        scanf("%d", &a[i]);
    }
    i = 0;
    do
    {
        for (k = 0; k < 10; k++)
        {
            if (a[i] < a[k])
                flag = 0;
        }
        i++;
    }
    while (flag == 0 && i < 10);
    printf("max=%d, pos=%d\n", a[i - 1], i - 1);
    i = 0;
    flag = 1;
    do
    {
        for (k = 0; k < 10; k++)
        {
            if (a[i] > a[k])
                flag = 0;
        }
        i++;
    }while (flag == 0 && i < 10);
    printf("min=%d, pos=%d\n", a[i - 1], i - 1);

}

6.指数n于[2,50]中梅森尼数共有几个
形如2^n - 1的素数称为梅森尼数。设计求出指数n<50的所有梅森尼数。
**输出格式要求:”2^%d-1=%.0lf\n” “指数n于[2,50]中梅森尼数共有%d个.”
程序运行示例如下:
2^2-1=3
2^3-1=7
2^5-1=31
2^7-1=127
2^13-1=8191
2^17-1=131071
2^19-1=524287
2^31-1=2147483647
2^49-1=562949953421311
指数n于[2,50]中梅森尼数共有9个.

#include<stdio.h>
#include<math.h>
int sushu(long double n);
main()
{
    int i, j, count = 0;
    long double n = 1;
    for (i = 2; i < 50; i++)
    {
        for (j = 1; j <= i; j++)
        {
            n = n * 2;
        }
        n = n - 1;
        if (sushu(n) == 1)
        {
            printf("2^%d-1=%.0lf\n", i, n);
            count++;
        }
    }
    printf("指数n于[2,50]中梅森尼数共有%d个.", count);
}
int sushu(long double n)
{
    long long int k, t;
    t = (long long int)n;
    for (k = 2; k < t; k++)
    {
        if (t % k == 0)
            return 0;
    }
    return 1;
}

7.
魔术师利用一副牌中的13张红桃,预先将它们排好后迭在一起,牌面朝下。对观众说:“我不看牌”,只数数就可以猜到每张牌是什么,我大声数,你们听,不信?你们就看。魔术师将最上面的那张牌数为1,把它翻过来正好是红桃A,将红桃A放在桌子上,然后按顺序从上到下数手中的余牌,第二次数1、2,将第一张牌放在这迭牌的下面,将第二张牌翻过来,正好是红桃2,也将它放在桌子上。第三次数1、2、3,将前面两张依次放在这迭牌的下面,再翻第三张牌正好是红桃3.这样依次将13张牌全翻出来,准确无误。问魔术师手中的牌原始次序是怎样安排的?
**输出格式要求:”%d “

#include<stdio.h>
main()
{
    int a[13] = {0};
    int i, j, k = 0;
    for (i = 1; i <= 13; i++)
    {
        if (k > 13)
            k = k - 13;
        for (j = 1; j <= i; j++)
        {
            if (a[k + j-1] != 0)
                j--;
        }
        a[k+i-1] = i;
        k = k + i;
    }
    printf("The original order of cards is:");
    for (i = 0; i < 13; i++)
        printf("%d", a[i]);
}
#include<stdio.h>

int main()
{                        
    int cards[14] = {0};
    int i, j = 1, n;
    for (i = 1 ; i <= 13 ; i++)
    {                        
        n = 1;
        do
        {                        
            if (j > 13)
                j = 1;
            if (cards[j] > 0)
                j++;
            else
            {                        
                if (n == i)
                    cards[j] = i;
                j++;
                n++;
            }
        }
        while (n <= i);
    }
    for (i = 1; i <= 13; i++)
        printf("%d ", cards[i]);
    printf("\n");

    return 0;
}     
#include<stdio.h>
#include<string.h>
main()
{
    int a[14];
    int i , j, n = 0;
    for (i = 1; i <= 13 ; i++) a[i] = 0;
    for (i = 1; i <= 13 ; i++)
    {
        for (j = 1 ; j <= i;)
        {
            n = (n % 13) + 1;
            if (a[n] == 0) j++;
        }
        a[n] = i  ;
    }

    printf("The original order of cards is:");
    for (i = 1; i <= 13; i++)
        printf("%d ", a[i]);
    return 0;
} 

8.最大公约数

int gcd(int a, int b)
{
    if (a < b)
        return gcd(a, b - a);
    else if (a > b)
        return gcd(a - b, b);
    else return a;
}
int MaxCommonFactor(int a, int b)
{
    int r;
    if (a <= 0 || b <= 0)
        return -1;
    do
    {
        r = a % b;
        a = b;
        b = r;
    }
    while (r != 0);
    return  a;
}

9.最小公倍数

int MinCommonMultiple(int a, int b)
{
    int i;

    for (i = 1; i <= a * b; i++)
    {
        if ((i * a) % b == 0)
            return i * a;
    }
    return 0;
}

10.除式还原
给定下列除式,其中包含5和7,其它打X的位置上是任意数字,请加以还原。
X7X ——-商
——– ——-被除数
除数–XX)XXXXX
X77
——–
X7X
X7X
——-
XX
XX
——-
0

**输出格式要求:”%ld/%d=%d\n”
程序运行示例如下:
51463/53=971

#include <stdio.h>
#include <stdlib.h>
main()
{   long int i;
    int j,k,l;
    for(i=10000;i<=99999;i++)
        if(i%1000-i%100==400)
            for(j=10;j<=99;j++)
                if(i%j==0&&(l=i/j)%100>=70&&i%100<80&&i%10!=0&&l>100&&l<=999)
                    if(j*(l%10)<100&&j*(l%10)>10)
                        if(j*7%100>=70&&j*7%100<80)
                            if(j*(l/100)%100==77&&j*(l/100)>100)
                                printf("%ld/%d=%d\n",i,j,l);
} 

my answer:

#include <stdio.h>
#include <math.h>
main()
{
    int b,c,d,e,f,g,h,i,j,k;
    long a;
    for(a=10000; a<100000; a++)
    {
        c=a/10000;
        d=a/1000%10;
        e=a/100%10;
        f=a/10%10;
        g=a%10;
        for(b=10; b<100; b++)
        {
            if(c*10+d<b)
            {
                if(d*10+e>77){
                if((c*100+77)%b==0&&(c*100+77)/b>=1&&(c*100+77)/b<=9)
                {
                    if((a/100-(c*100+77))%10==7&&(a/100-(c*100+77))<97)
                    {
                        h=a/100-(c*100+77);

                        j=(h*10+f)-b*7;

                        if(b*7/10%10==7&&j<10)
                        {
                            if((j*10+g)%b==0&&(j*10+g)/b>=1&&(j*10+g)/b<=9)
                            {
                                if((float)a/b==a/b&&a/b/10%10==7)

                                    printf("%ld/%d=%d\n",a,b,a/b);
                            }
                        }
                    }
                }
                }
                else if(((c-1)*100+77)%b==0&&((c-1)*100+77)/b>=1&&((c-1)*100+77)/b<=9)
                {
                    if((a/100-((c-1)*100+77))%10==7&&(a/100-((c-1)*100+77))<97)
                    {

                        i=a/100-((c-1)*100+77);

                        k=(i*10+f)-b*7;
                        if(b*7/10%10==7&&k<10)
                        {
                            if((i*10+g)%b==0&&(i*10+g)/b<=9&&(i*10+g)/b>=1)
                            {
                                if((float)a/b==a/b&&a/b/10%10==7)

                                    printf("%ld/%d=%d\n",a,b,a/b);
                            }
                        }
                    }
                }
            }
        }

    }
}
#include<stdio.h>
main()
{
    int b,c,d,e,f,g,h,i,j,k,l,m;
    long a;
    for(a=10000; a<100000; a++)
    {
        c=a/10000;
        d=a/1000%10;
        e=a/100%10;
        f=a/10%10;
        g=a%10;
        for(b=10; b<100; b++)
        {
            if(c*10+d<b)
            {
                for(h=1; h<=9; h++)
                {
                    j=b*h;
                    k=a/100-j;
                    if(j%100==77&&k<b&&k>0)
                    {
                        k=k*10+f;
                        l=b*7;
                        if(l/10%10==7)
                        {
                            if(k-l<10&&l<k)
                            {
                                m=(k-l)*10+g;
                                for(i=1; i<=9; i++)
                                {
                                    if(i*b==m&&(float)a/b==a/b)
                                        printf("%ld/%d=%d\n",a,b,a/b);
                                }
                            }
                        }
                    }

                }


            }
        }
    }
}

11.
和数能表示1~23的5个正整数
已知五个互不相同的正整数之和为23,且从这五个数中挑选若干个加起来可以表示从1
到23之内的全部自然数,问这五个数都是什么?
**输入格式要求:提示信息:”There are following possible result:\n”
**输出格式要求:”[%d]:%d,%d,%d,%d,%d\n”
程序运行示例如下:
There are following possible result:

#include <stdio.h>
void main()
{
   int a,b,c,d,e,i,j,k,l,m,x,count=0,f=0;  
   printf("There are following possble result:\n");
   for(a=1;a<=23;a++)         
      for(b=1+a;b<=23-a;b++)
         for(c=1+b;c<=23-a-b;c++)
            for(d=1+c;d<=23-a-b-c;d++)
            {
               f=1;
               if((e=23-a-b-c-d)>d)
                  for(f=0,x=1;x<24&&!f;x++)     
                     for(f=1,i=0;i<2&&f;i++)    
                        for(j=0;j<2&&f;j++)
                           for(k=0;k<2&&f;k++)
                              for(l=0;l<2&&f;l++)
                                 for(m=0;m<2&&f;m++)
                                    if(x==a*i+b*j+c*k+d*l+e*m) f=0;
               if(!f) printf("[%d]:%d,%d,%d,%d,%d\n",++count,a,b,c,d,e);
            }
}

12.
用牛顿迭代法求方程2x^3-4x^2+3x-6=0在1.5附近的根。
**输出格式要求:”方程的根=%6.2f\n”
程序的运行示例如下:
方程的根= 2.00

#include<stdio.h>
#include<math.h>
main()
{
    float f, f1, x0, x1;
    x1 = 1.5;
    do
    {
        x0 = x1;
        f = 2 * x0 * x0 * x0 - 4 * x0 * x0 + 3 * x0 - 6;
        f1 = 6 * x0 * x0 - 8 * x0 + 3;
        x1 = x0 - f / f1;
    }
    while (fabs(x0 - x1) > 1e-5);
    printf("方程的根=%6.2f\n", x1);
}

13.减式还原
编写程序求解下式中各字母所代表的数字,不同字母代表不同的数学。
**输出格式要求:
” PEAR %d%d%d%d\n”
” ARA - %d%d%d\n”
“———– —————-\n”
” PEA %d%d%d\n”
程序运行示例如下:
PEAR 1098
ARA - 989
———– —————-
PEA 109

#include <stdio.h>
void main()
{
   int p,e,a,r;
   for(p=1;p<=9;p++)
   {
       for(a=1;a<=9;a++)
       {
           for(e=0;e<=9;e++)
           {
               for(r=0;r<=9;r++)
               {
                   if(p*100+e*10+a+a*100+r*10+a==p*1000+e*100+a*10+r)
                   {
                       printf("    PEAR        %d%d%d%d\n",p,e,a,r);
                       printf("     ARA       -  %d%d%d\n",a,r,a);
                       printf("-----------   ----------------\n");
                       printf("     PEA           %d%d%d\n",p,e,a);
                   }
               }
           }
       }
   }
}

14.乘式还原

A代表数字0到9中的前五个数字,Z代表后五个数字,请还原下列乘式
      AZA
  *  AAZ
  -------
    AAAA
   AAZZ
   ZAA
  -------
   ZAZAA

**输出格式要求:"\n   %ld\n" "*  %ld\n" "--------------\n" "  %ld\n %ld\n %ld\n" " %ld\n"
程序运行示例如下:
   372
*  246
--------------
  2232
 1488
 744
--------------
 91512
#include <stdio.h>
void main()
{
    int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,M,N;
    int flag1,flag2,flag3;
    for(a=0; a<=4; a++)
    {
        for(p=5; p<=9; p++)
        {
            for(b=0; b<=4; b++)
            {
                flag1=flag2=flag3=0;
                M=a*100+p*10+b;
                for(q=5; q<=9; q++)
                {
                    if(q*M>1000)
                    {
                        h=q*M%10;
                        e=q*M/1000;
                        f=q*M/100%10;
                        g=q*M/10%10;
                        if(e<=4&&f<=4&&g<=4&&h<=4)
                        {
                            flag1=1;
                            break;
                        }
                    }
                }
                if(flag1)
                {
                    for(d=0;d<=4;d++)
                    {
                        if(d*M>1000)
                        {
                            s=d*M%10;
                        i=d*M/1000;
                        j=d*M/100%10;
                        r=d*M/10%10;
                        if(i<=4&&j<=4&&r>=5&&s>=5)
                        {
                            flag2=1;
                            break;
                        }
                        }
                    }
                }
                if(flag1&&flag2)
                {
                    for(c=0;c<=4;c++)
                    {
                        if(c*M<1000&&c*M>99)
                        {
                            t=c*M/100;
                            k=c*M/10%10;
                            l=c*M%10;
                            if(t>=5&&k<=4&&l<=4)
                            {
                                flag3=1;
                                break;
                            }
                        }
                    }
                }
                if(flag1&&flag2&&flag3)
                {
                    N=(c*100+d*10+q)*M;
                    if(N>10000)
                    {
                        v=N/10000;
                        m=N/1000%10;
                        u=N/100%10;
                        n=N/10%10;
                        o=N%10;
                        if(v>=5&&m<=4&&u>=5&&n<=4&&o<=4)
                        {
                            printf("\n   %ld\n",M);
                            printf("*  %ld\n",c*100+d*10+q);
                            printf("--------------\n");
                            printf("  %ld\n %ld\n %ld\n",e*1000+f*100+g*10+h,i*1000+j*100+r*10+s,t*100+k*10+l);
                            printf("--------------\n");
                            printf(" %ld\n",N);
                        }
                    }
                }
            }
        }
    }
}

15.冒泡法排序

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

    }
}

16.
面程序的功能是从键盘输入一行字符,统计其中有多少单词。假设单词之间以空格分开。[提示:判断是否有新单词出现的方法是——当前被检验的字符不是空格,而前一被检验字符是空格,则表示有新单词出现。]

#include<stdio.h>
#define ARR_SIZE 80; 
main()
{
    char str[ARR_SIZE];
    int i, num;

    gets(str);
    if(str[0] != ' ' && str[0] != '\0')
    {
        num = 1;
    }
    else
    {
        num = 0;
    }
    for(i = 1 ; str[i] != '\0'; i++)
    {
        if(str[i]  != ' ' && str[i-1]  = ' ') 
        {
            num++;
        }
    }
    printf("num=%d\n", num);
}

#include<stdio.h>
#define ARR_SIZE 80
main()
{
    char str[ARR_SIZE];
    int i, num;

    gets(str);
    if (str[0] != ' ' && str[0] != '\0')
    {
        num = 1;
    }
    else
    {
        num = 0;
    }
    for (i = 1 ; str[i] != '\0'; i++)
    {
        if (str[i]  != ' ' && str[i - 1]  == ' ')
        {
            num++;
        }
    }
    printf("num=%d\n", num);
}

17.
输入一行字符(最长不超过80字符),用函数编程统计用户的输入中一共有多少个包含三个字母以上的单词(包括三个字母)。假设单词之间以空格分开。判断新单词出现的基本思路是:若当前被测字符不是空格,而前一字符是空格,则表示有新单词出现。对于第一个单词只要判断当前字符不是空格即可。【提示:当新单词出现时,若该单词中包含的字母数≥3个,即启动计数器】
函数原型:int CountWords(char str[]);
其中,字符数组str用于存储用户输入的一行字符,函数的返回值为包含三个字母以上的单词个数。
**输入提示信息:”Input a string:\n”
**输入格式:gets()
**包含三个字母以上的单词总数输出格式:”Numbers of words (include 3 or more letters) = %d\n”
程序运行示例1:
Input a string:
i am a good teacher↙
Numbers of words (include 3 or more letters) = 2

程序运行示例2:
Input a string:
a s ff↙
Numbers of words (include 3 or more letters) = 0

注:不能使用指针、结构体、共用体、文件、goto、枚举类型进行编程。

#include <stdio.h>
int CountWords(char str[]);
int  main()
{                 
    char  str[80];
    printf("Input a string:\n");
    gets(str);
    printf("Numbers of words (include 3 or more letters) = %d\n", CountWords(str));
    return 0;
}                 
int CountWords(char str[])
{                 
    int    i, k;
    int num3 = 0, temp = 0;
    if (str[0] == ' ') //1
    {                 
        num3 = 0;
        temp = 0;
    }
    else
    {                 
        k = 0;
        while (str[k] != ' ')//1
        {                 
            temp++;
            k++;
        }
        if (temp >= 3) num3++;
    }

    for (i = 1; str[i] != '\0'; i++) //1
    {                 
        temp = 0;
        if (str[i] != ' ' && str[i - 1] == ' ') //1
        {                 

            k = i;
            while (str[k] != ' ' && str[k] != '\0')//1
            {                 
                temp++;
                k++;
            }
            if (temp >= 3) num3++;//1
        }
    }

    return num3;//1
}           
#include<stdio.h>
#include<string.h>
int CountWords(char str[])
{
    int count = 0, i = 0, word = 0;
    if (str[0] != ' ' && str[0] != '\0')
    {
        for (i = 0; str[i] != ' '; i++)
        {
            word++;
        }

        if (word >= 3)
            count++;
    }
    for (; str[i] != '\0'; i++)
    {
        word = 0;
        if (str[i] != ' ' && str[i - 1] == ' ')
        {
            for (; str[i] != ' '&&str[i]!='\0'; i++)
            {
                word++;
            }
            i--;
            if (word >= 3)
                count++;
        }
    }
    return count;
}
main()
{
    char a[80];
    printf("Input a string:\n");
    gets(a);
    int n;
    n = CountWords(a);
    printf("Numbers of words (include 3 or more letters) = %d\n", n);
}

18.走台阶
楼梯有10阶台阶,上楼可以一步上1阶,也可以1步上2阶,编程计算10阶台阶总共有多少走法.
提示:可以递推计算,如1阶台阶总共一种走法,2阶台阶总共2走法,3阶台阶总共3种走法,直到计算出10阶台阶走法.

#include<stdio.h>
int f(int n);
main()
{
    printf("Result=%d", f(10));
}
int f(int n)
{
    if (n == 1)
        return 1;
    else if (n == 2)
        return 2;
    else return f(n - 1) + f(n - 2);
}

19.
*已知银行的存款利息如下。某人有2000元钱,要存二十年,问怎样存才能使二十年后得到的本金和复利合计最多(假定银行对定期存款过期部分不付利息)?

月利息率=0.63%0.66%0.69%0.75%0.84%期限一年期限二年期限三年期限五年期限八年

**输出格式要求:”8 year:%d\t 5 year:%d\t 3 year:%d\t2 year:%d\t 1 year:%d\nTotal:%.2f\n”
程序运行示例如下:
8 year:0 5 year:4 3 year:0 2 year:0 1 year:0
Total:8841.01

#include <stdio.h>
#include <math.h>
main()
{                  
    int i8,i5,i3,i2,i1,n8,n5,n3,n2,n1;
    float max=0,term;
    for(i8=0;i8<3;i8++)
        for(i5=0;i5<=(20-8*i8)/5;i5++)
            for(i3=0;i3<=(20-8*i8-5*i5)/3;i3++)
                for(i2=0;i2<=(20-8*i8-5*i5-3*i3)/2;i2++)
                {                  
                    i1=20-8*i8-5*i5-3*i3-2*i2;
                    term=2000.0*pow(1+0.0063*12,(double)i1)
                                *pow(1+2*0.0066*12,(double)i2)
                                *pow(1+3*0.0069*12,(double)i3)
                                *pow(1+5*0.0075*12,(double)i5)
                                *pow(1+8*0.0084*12,(double)i8);
                    if(term>max)
                    {                  
                        max=term;
                        n1=i1;n2=i2;n3=i3;n5=i5;n8=i8;
                    }
                }
    printf("8 year:%d\t 5 year:%d\t 3 year:%d\t",n8,n5,n3);
    printf("2 year:%d\t 1 year:%d\n",n2,n1);
    printf("Total:%.2f\n",max);
}    

20.
对于用户指定的正整数n,编程计算并输出满足下面平方根不等式的最小正整数m。

√m+√(m+1)+·····+√2m>n

输入提示信息:”Input n:”

输入数据格式:”%lf”

输入数据格式和提示信息:”m>=%d\n”

程序运行结果示例1:

Input n:10000↙

m>=407

程序运行结果示例2:

Input n: 100000↙

m>=1888

#include <stdio.h>
#include <math.h>
int main()
{                            
    int i, m;
    double s, n;
    printf("Input n:");
    scanf("%lf", &n);
    for (m=1;; m++)
    {                            
         s = 0;
         for (i=m; i<=2*m; i++)
         {                           
             s = s + sqrt(i);
         }
         if (s > n)   break;
    }
    printf("m>=%d\n", m);
    return 0;
}  

21.回文数
数值151是一个回文素数。因为151既是一个素数也是一个回文数(回文数是从前向后读和从后向前读都一样的数)。写一个程序找出所有在[a,b]区间内的回文素数(5 <= a < b <= 1000,000,000)。

输入
第一行:”%d %d”

输出
符合条件的回文素数,一个数一行。

输入样例
5 500
输出样例
5
7
11
101
131
151
181
191
313
353
373
383

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

// Judge if the palindromes that have been found are primes
int primer(int number)
{        
    int i;
    int sqrt_number;
    sqrt_number = sqrt(number);
    for (i = 3; i <= sqrt_number; i += 2)
    {        
        if ((number % i) == 0)
            return 0; //The number is not prime
    }
    return 1; // The number is prime
}        

int main()
{        
    int a, b; // Enter two numbers as the range of prime palindromes
    int x;
    int o, p, q, r, s;

    scanf("%d %d", &a, &b);

    if (a == 5 && 5 <= b)
        printf("%d\n", 5);
    if (a <= 7 && 7 <= b)
        printf("%d\n", 7);
    if (a <= 11 && 11 <= b)
        printf("%d\n", 11);
    // Find the prime palindromes in the range of 100 and 1000
    for (o = 1; o <= 9; o += 2)
        for (p = 0; p <= 9; p++)
        {        
            x = 100 * o + 10 * p + o;
            if (primer(x) && x <= b && x >= a)
                printf("%d\n", x);
        }
    // Find the prime palindromes in the range of 10000 and 100000
    for (o = 1; o <= 9; o += 2)
        for (p = 0; p <= 9; p++)
            for (q = 0; q <= 9; q++)
            {        
                x = 10000 * o + 1000 * p + 100 * q + 10 * p + o;
                if (primer(x) && x <= b && x >= a)
                    printf("%d\n", x);
            }
    // Find the prime palindromes in the range of 1000000 and 10000000
    for (o = 1; o <= 9; o += 2)
        for (p = 0; p <= 9; p++)
            for (q = 0; q <= 9; q++)
                for (r = 0; r <= 9; r++)
                {        
                    x = 1000000 * o + 100000 * p + 10000 * q +
                        1000 * r + 100 * q + 10 * p + o;
                    if (primer(x) && x <= b && x >= a)
                        printf("%d\n", x);
                }
    // Find the prime palindromes in the range of 100000000 and 1000000000
    for (o = 1; o <= 9; o += 2)
        for (p = 0; p <= 9; p++)
            for (q = 0; q <= 9; q++)
                for (r = 0; r <= 9; r++)
                    for (s = 0; s <= 9; s++)
                    {        
                        x = 100000000 * o + 10000000 * p + 1000000 * q +
                            100000 * r + 10000 * s + 1000 * r + 100 * q + 10 * p + o;
                        if (primer(x) && x <= b && x >= a)
                            printf("%d\n", x);
                    }

    return 0;
}  
#include<stdio.h>
#include<string.h>
int hw(int num)
{
    int temp;
    int sum=0;
    temp=num;
    while(num)
    {
        sum = sum*10 + num%10; 
        num /= 10;
    }
    if(temp == sum) 
        return 1;
    else
        return 0;

}
main()
{
    int a, b, i, j, flag = 0, k;
    scanf("%d %d", &a, &b);
    for (i = a; i <= b; i++)
    {
        flag = 1;
        k = 0;
        for (j = 1; j <= i; j++)
        {
            if (i % j == 0)
                flag = 0;
        }
        if (flag == 1)
        {
            k = hw(i);
            if (k == 1)
                printf("%d\n", i);
        }

    }
}

22.
奥运会参赛国国名排序:
请输入奥运会参赛国的国名,并按照字典序对其进行排序。
要求:参赛国不超过150个,各个国家的名字均不超过9个字符。
提示:‘\0’占一个字符。

#include <stdio.h>
#include <string.h>
#define N 30
int main( )
{                
    char  str[N], c;
    int  n, i, j;

    printf("Please Enter String1:\n");
    gets(str);              //1
    n = strlen(str) - 1;    //1

    for (i = 0; i <= n / 2; i++)  //1
    {                
        c = str[i];             //1
        str[i] = str[n - i];    //1
        str[n - i] = c;         //1
    }

    printf("Result is:\n%s\n", str);
    return 0;
}  

23.完全数
完全数(Perfect Number),又称完美数或完数,它是指这样的一些特殊的自然数。它所有的真因子(即除了自身以外的约数)的和,恰好等于它本身,即m的所有小于m的不同因子(包括1)加起来恰好等于m本身。注意:1没有真因子,所以1不是完全数。计算机已经证实在10300以下,没有奇数的完全数。例如,因为6 = 1 + 2 + 3,所以6是一个完全数。
从键盘任意输入一个整数m,编程判断m是否是完全数。若m是完全数,则输出“Yes!”,并同时打印出每一个完美数的全部因子,以验证这个数确实是一个完美数。若m不是完全数,则输出“No!”
程序运行示例1
Input m:
28↙
Yes!
1,2,4,7,14

程序运行示例2
Input m:
6↙
Yes!
1,2,3

程序运行示例3
Input m:
1↙
No!

输入格式: “%d”
输出格式:
输入信息提示:”Input m:\n”
输出格式: “%d”
输出信息提示:”Yes!\n”
“No!\n”

#include <stdio.h>
#include <math.h>
int IsPerfect(int x);
void OutputFactor(int m);
int main()
{          
    int m;
    printf("Input m:\n");
    scanf("%d", &m);
    if (IsPerfect(m))  //若m是完全数
    {          
        printf("Yes!\n");
        OutputFactor(m);
    }
    else                 //若m不是完全数
    {          
        printf("No!\n");
    }
    return 0;
}          
// 函数功能:判断完全数,若函数返回0,则代表不是完全数,若返回1,则代表是完全数
int IsPerfect(int x)
{          
    int i;
    int sum = 0;  //x为1时,sum=0,函数将返回0,表示1没有真因子,不是完全数
    for (i = 1; i < x; i++)
    {          
        if (x % i == 0)
        {          
            sum = sum + i;
        }
    }
    return sum == x ? 1 : 0;
}          
// 函数功能:输出x的所有包括1在内的因子
void OutputFactor(int m)
{          
    int i, isFirstFactor = 1;
    for (i = 1; i < fabs(m); i++) //输出包括1在内的因子,所以从1开始
    {          
        if (m % i == 0)
        {          
            if (isFirstFactor == 0)     printf(",");
            printf("%d", i);
            isFirstFactor = 0;
        }
    }
    printf("\n");
}       
#include<stdio.h>

int panduan(int m);
main()
{
    int m, k, i;
    printf("Input m:\n");
    scanf("%d", &m);
    k = panduan(m);
    if (k == 0)
    {
        printf("No!\n");
    }
    else
    {
        printf("Yes!\n");
        for (i = 1; i < m; i++)
        {
            if (m % i == 0)
                printf("%d,", i);
        }
        printf("\b ");

    }
}

int panduan(int m)
{
    int j, sum = 0;
    for (j = 1; j < m; j++)
    {
        if (m % j == 0)
            sum = sum + j;
    }
    if (sum == m)
        return 1;
    return 0;
}

24.
用迭代法求x=sqrt(a)。求平方根的迭代公式为:xn+1= (1/2)(xn+ a/xn),要求前后两次求出的x的差的绝对值小于10-5。

**输入格式要求:”%f” 提示信息:”请输入一个整数:”
**输出格式要求:”%5.2f的平方根=%8.5f\n”

#include<stdio.h>
#include<math.h>
main()
{
    float x1, x2 = 1.0;
    float a;
    printf("请输入一个整数:");
    scanf("%f", &a);
    do
    {
        x1 = x2;
        x2 = (x1 + a / x1) / 2.0;
    }
    while (fabs(x1 - x2) >= 1e-5);
    printf("%5.2f的平方根=%8.5f\n", a, x2);
}

25.
寻找最佳存款方案。已知银行整存整取不同期限存款的年息利率分别为

假设银行对定期存款过期部分不支付利息,现在某人有2000元钱,要存20年,问怎样存才能使20年后得到的本金和复利之和最多?

**输出格式要求:”8 year: %d\n” “5 year: %d\n” “3 year: %d\n” “2 year: %d\n” “1 year: %d\n” “Total: %.2f\n”

程序运行示例如下:

8 year: 0
5 year: 0
3 year: 0
2 year: 0
1 year: 20
Total: 3121.02

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

int main(void)
{             
    int i8, i5, i3, i2, i1, n8, n5, n3, n2, n1;
    double  max = 0, total;
    for (i8 = 0; i8 < 3; i8++)
        for (i5 = 0; i5 <= (20 - 8 * i8) / 5; i5++)
            for (i3 = 0; i3 <= (20 - 8 * i8 - 5 * i5) / 3; i3++)
                for (i2 = 0; i2 <= (20 - 8 * i8 - 5 * i5 - 3 * i3) / 2; i2++)
                {             
                    i1 = 20 - 8 * i8 - 5 * i5 - 3 * i3 - 2 * i2;
                    total = 2000 * pow(1 + 0.0225, i1)
                            * pow(1 + 0.0243, i2)
                            * pow(1 + 0.0270, i3)
                            * pow(1 + 0.0288, i5)
                            * pow(1 + 0.0300, i8);
                    if (total > max)
                    {             
                        max = total;
                        n1 = i1;
                        n2 = i2;
                        n3 = i3;
                        n5 = i5;
                        n8 = i8;
                    }
                }
    printf("8 year: %d\n", n8);
    printf("5 year: %d\n", n5);
    printf("3 year: %d\n", n3);
    printf("2 year: %d\n", n2);
    printf("1 year: %d\n", n1);
    printf("Total: %.2f\n", max);
    return 0;
}           

26.
每个合数都可以写成几个质数相乘的形式。将一个正整数分解质因数。例如90=2* 3* 3* 5,而质因数分解只针对合数,质数没有质因数。当程序输入质数时,输出”Invalid input.”,否则输出其质因数序列。
**输入格式要求:”%d”
**输出格式要求:”%d” “,” “Invalid input.\n”
程序运行示例1如下:
89
Invalid input.
程序运行示例2如下:
98
2,7,7

#include<stdio.h>
int sushu(int n)
{
    int i;
    for (i = 2; i < n; i++)
    {
        if (n % i == 0)
            return 0;
    }
    return 1;
}
main()
{
    int n, i=1;
    scanf("%d", &n);
    if (sushu(n))
        printf("Invalid input.\n");
    else
    {
        do
        {
            n=n/i;
            for (i = 2;; i++)
            {
                if (n % i == 0)
                {
                    printf("%d", i);
                    break;
                }
            }
            if (n/i!=1)
                printf(",");
        }
        while (n/i!=1);
    }

}

27.
编程验证哥德巴赫猜想:任意一个充分大的偶数,可以用两个素数之和表示。如:
4 = 2 + 2 6 = 3 + 3。
**输入格式要求:”%d” 提示信息:”Input a number:\n” 输入奇数时报错: “Input error!\n”
**输出格式要求:输入偶数时显示”%d=%d+%d\n”
程序的运行的输入输出样例1:
屏幕先显示提示信息:
Input a number:
然后用户键盘输入:
9
最后屏幕输出:
Input error!
程序的运行的输入输出样例2:
屏幕先显示提示信息:
Input a number:
然后用户键盘输入:
100
最后屏幕输出:
100=3+97

#include<stdio.h>
#include<stdlib.h>
int sushu(int n);
main()
{
    int n, i, j;
    printf("Input a number:\n");
    scanf("%d", &n);
    if (n % 2 != 0)
    {
        printf("Input error!\n");
    }
    else
    {
        for (i = 2;i<n; i++)
        {
            if (sushu(i))
            {
                for (j = 2;j<n; j++)
                {
                    if (sushu(j))
                    {
                        if (n == i + j)
                        {
                            printf("%d=%d+%d\n", n, i, j);
                            exit (0);
                        }
                    }
                }
            }
        }
    }
}
int sushu(int n)
{
    int k;
    for (k = 2; k < n; k++)
    {
        if (n % k == 0)
        {
            return 0;
        }
    }
    return 1;
}

28.
下一代因特网IPv6的地址占128位(二进制位,也称为比特),假设以每秒100万个地址的速度分配,请问分配完需要花费多少年?
**输出格式要求:”%f”

#include<stdio.h>
#include<math.h>
int main()
{                       
    double t, m;
    int s = 0, n, x, j;
    t = 2;
    for (n = 2; n <= 50; n++)
    {                       
        t = t * 2;
        m = t - 1;
        x = 0;
        for (j = 3; j <= sqrt(m); j += 2)
        {                       
            if (m / j == (int)(m / j))
            {                       
                x = 1;
                break;
            }
        }
        if (x == 0)
        {                       
            s = s + 1;
            printf("2^%d-1=%.0lf\n", n, m);
        }
    }
    printf("指数n于[2,50]中梅森尼数共有%d个.", s);
    return 0;
}     

29.

假设有40个学生被邀请来给餐厅的饮食和服务质量打分,分数划分为11010个等级(1表示最低分,10表示最高分),编程统计并按如下格式输出餐饮服务质量调查结果。
Grade       Count       Histogram
1        5      *****
2       10      **********
3        7      ******* 
...
**输入格式要求:"%d"  提示信息:"Input the feedbacks of 40 students:\n"  "input error!\n"
**输出格式要求:"Feedback\tCount\tHistogram\n"  "%8d\t%5d\t"
程序运行示例如下:
Input the feedbacks of 40 students:
10 9 10 8 7 6 5 10 9 8
8 9 7 6 10 9 8 8 7 7
6 6 8 8 9 9 10 8 7 7
9 8 7 9 7 6 5 9 8 7
Feedback    Count   Histogram
       1        0   
       2        0   
       3        0   
       4        0   
       5        2   **
       6        5   *****
       7        9   *********
       8       10   **********
       9        9   *********
      10        5   *****
#include<stdio.h>
#define M 40
#define N 11
main()
{
    int i,j,grade,feedback[M],count[N]={0};
    printf("Input the feedbacks of 40 students:\n");
    for(i=0;i<M;i++)
    {
        scanf("%d",&feedback[i]);
    }
    for(i=0;i<M;i++)
    {
        count[feedback[i]]++;
    }
    printf("Feedback\tCount\tHistogram\n");
    for(grade=1;grade<=N-1;grade++)
    {
        printf("%8d\t%5d\t",grade,count[grade]);
        for(j=0;j<count[grade];j++)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}   

30.
统计正整数中指定数字的个数
从键盘输入一个正整数number,求其中含有指定数字digit的个数。例如:从键盘输入正整数number=1222,若digit=2,则1223中含有 3个2,要求用函数实现。函数原型为:int CountDigit(int number,int digit);

程序运行结果示例1:
Input m,n:
1222,2↙
3

程序运行结果示例2:
Input m,n:
1234,6↙
0

输入格式: “%d,%d”
输出格式:
输入提示信息:”Input m,n:\n”
输出格式:”%d\n”

#include<stdio.h>
int count = 0;
int CountDigit(int number, int digit);
main()
{
    int m, n;
    printf("Input m,n:\n");
    scanf("%d,%d", &m, &n);
    printf("%d", CountDigit(m, n));
}
int CountDigit(int number, int digit)
{
    int i;
    if (number % 10 == digit)
        count++;
    i = number / 10;
    if (i != 0) CountDigit(i, digit);
    return count;

}            

31.有趣的“回文”检测
英文中有很多的回文词,回文词的拼法十分有趣,无论是从前往后拼读,还是从后往前拼读,他们的拼法和词义都不变。例如:dad(爸爸),mum(妈妈),noon(中午),eve(前夕),eye(眼睛),pop(流行),deed(行为),level(水平)等。简单地说,“回文”就是指顺读和倒读都一样的字符串。现在请你编程输入一个单词,判断它是否是回文。
提示:
(1)设置两个指针pStart和pEnd,让pStart指向字符串首部,让pEnd指向字符串尾部。
(2)利用循环从字符串两边对指针所指字符进行比较,当对应的两字符相等且两指针未超越对方时,使指针pStart向前移动一个字符位置(加1),使指针pEnd向后移动一个字符位置(减1),一旦发现两字符不等或两指针已互相超越(不可能是回文),则立即停止循环。
(3)根据退出循环时两指针的位置,判断字符串是否为回文。
程序的两次运行结果如下:
第1次
Input string:ABCCBA↙
Yes!
第2次
Input string:student↙
No!
输入格式: 字符串输入用gets()函数
输出格式:
输入提示信息:”Input string:”
输出信息,不是回文:”No!\n”
输出信息,是回文:”Yes!\n”

#include<stdio.h>
#include<string.h>
main()
{
    char a[20], *pStart, *pEnd;
    int l, i = 1;
    printf("Input string:");
    gets(a);
    l = strlen(a);
    pStart = &a[0], pEnd = &a[l - 1];
    do
    {
        if (*pStart != *pEnd)
            break;
        pStart++;
        pEnd--;
    }
    while (pStart < pEnd);
    if (pStart < pEnd)
        printf("No!\n");
    else printf("Yes!\n");

    }

32.字符串逆序输出

void inverse(char str[])
{
    char b[80];
    int l,i;
    l = strlen(str);
    for (i = 0; i < l; i++)
    {
        b[i] = str[l - i - 1];
    }
    b[l] = '\0';
    strcpy(str, b);
}

33.
输入两个长度小于100的字符串A和B,且A的长度大于B的长度,判断B是不是A的子串,是则输出”YES”,否则输出”NO”。注意:串中任意个连续的字符组成的子序列称为该串的子串。
提示:不需要使用自定义函数实现,也不能使用已有的字符串查找子串函数
输入:输入两行,第一行为字符串A,第二行为字符串B。
输入提示信息:”Please input the first str:”
输入格式:”%s”
输入提示信息:”Please input the second str:”
输入格式:”%s”
如:
Please input the first str: Abcdefghijk123
Please input the second str: 123

Please input the first str: abefsfl
Please input the second str: befs

Please input the first str: aAbde
Please input the second str: abc
输出:按题目要求输出一行,”YES” 或 “NO”。
输出样例:
YES

YES

NO

#include<stdio.h>
#include<string.h>
main()
{
    char str1[100], str2[100];
    int l1, l2, i, j, flag = 0;
    printf("Please input the first str:");
    gets(str1);
    printf("Please input the second str:");
    gets(str2);
    l1 = strlen(str1), l2 = strlen(str2);
    char str[l2 + 1];
    for (i = 0; i <= l1 - l2; i++)
    {
        for (j = 0; j < l2; j++)
        {
            str[j] = str1[i + j];
        }
        str[l2] = '\0';
        if (strcmp(str, str2) == 0)
            flag = 1;
    }
    if (flag)
        printf("YES");
    else printf("NO");
}

34.删去字符

#include <stdio.h>
void Squeeze(char s[], char c);
main()
{
    char a[80], c;
    gets(a);
    scanf(" %c", &c);
    Squeeze(a, c);
    printf("%s\n", a);
}

void Squeeze(char s[], char c)
{
    int i, j = 0;
    char b;
    for (i = 0; s[i] != '\0'; i++)
    {
        if (s[i] != c)
        {
            b = s[i];
            s[j] = b;
            j++;
        }
    }
    s[j] = '\0';
}

35.比较函数

#include <stdio.h>
#define SIZE 80
int MyStrcmp(char s[],char t[]);
main()
{      
    char s[SIZE],t[SIZE],i;
    printf("Input s\n");
    gets(s);//1
    printf("Input t\n");
    gets(t);//1
    i=MyStrcmp(s,t);//2
    if(i>0)//1
        printf("string s>string t.\n");
    else if(i<0)//1
        printf("string s<string t.\n");
    else//1
        printf("string s=string t.\n");
}

36.螺旋矩阵
37.
除了字符数组,C语言还支持另外一种表示字符串的方法,就是直接使用一个指针指向字符串,
我们将第二种形式的字符串称为字符串常量,意思很明显,常量只能读取不能写入。
第4行代码是正确的,可以更改指针变量本身的指向;第3行代码是错误的,不能修改字符串中的字符。
指针和字符数组的应用,用五种方法输出字符串“China”。请改正程序中的错误,使它能得出正确的结果。

#include <stdio.h>

main()
{
    int i = 0;
    char str[6] = {'C', 'h', 'i', 'n', 'a', '\0'}, str1[6] , *ptr, *ptr1, *ptr2 , str2[5];
    while (str[i] != '\0')
    {
        putchar(*str);
        str++;
    }
    scanf("%s", ptr);
    puts(ptr);
    ptr1 = str;
    puts(ptr1);
    ptr2 = "China";
    puts(ptr2);
    str2 = "China";
    printf("%s", str2);
}
#include <stdio.h>

main()
{           
    int i = 0;
    char str[6] = {'C', 'h', 'i', 'n', 'a', '\0'}, str1[6] , *ptr, *ptr1, *ptr2 , str2[6];
    while (str[i] != '\0')
    {           
        putchar(str[i]);
        i++;
    }
    ptr = str1;
    scanf("%s", ptr);
    puts(ptr);
    ptr1 = str;
    puts(ptr1);
    ptr2 = "China";
    puts(ptr2);
    strcpy(str2, "China");
    printf("%s", str2);
}           

38.ISBN识别码判断
每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字、1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”就是分隔符(键盘上的减号),最后一位是识别码,例如0-670-82162-4就是一个标准的ISBN码。ISBN码的首位数字表示书籍的出版语言,例如0代表英语;第一个分隔符“-”之后的三位数字代表出版社,例如670代表维京出版社;第二个分隔符后的五位数字代表该书在该出版社的编号;最后一位为识别码。
识别码的计算方法如下:
首位数字乘以1加上次位数字乘以2……以此类推,用所得的结果mod 11,所得的余数即为识别码,如果余数为10,则识别码为大写字母X。例如ISBN号码0-670-82162-4中的识别码4是这样得到的:对067082162这9个数字,从左至右,分别乘以1,2,…,9,再求和,即0×1+6×2+……+2×9=158,然后取158 mod 11的结果4作为识别码。
你的任务是编写程序判断输入的ISBN号码中识别码是否正确,如果正确,则仅输出“Right”;如果错误,则输出你认为是正确的ISBN号码。

程序运行结果示例1:
0-123-41562-4↙
Right

程序运行结果示例2:
0-123-41562-7↙
0-123-41562-4

输入格式: 用gets()输入字符串
输入只有一行,是一个字符序列,表示一本书的ISBN号码(保证输入符合ISBN的格式要求)。
输出格式:
输入的ISBN号码的识别码正确,输出信息: “Right”
输入的ISBN号码的识别码错误,按照规定的格式,输出正确的ISBN号码(包括分隔符“-”),输出格式:”%s”

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

int main()
{
    int a[9], i, sum = 0, j;
    char b;
    scanf("%1d-%1d%1d%1d-%1d%1d%1d%1d%1d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9]);
    scanf("-%c", &b);
    for (i = 0; i < 9; i++)
    {
        sum = sum + a[i] * (i + 1);
    }
    j = sum % 11;
    if (j == 10)
    {
        if (b == 'X')
            printf("Right");
        else
        {
            printf("%d-%d%d%d-%d%d%d%d%d-X", a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9]);
        }
    }
    else
    {
        if (b == j + 48)
            printf("Right");
        else
        {
            printf("%d-%d%d%d-%d%d%d%d%d-%d", a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], j);
        }
    }
    return 0;
}

39.约瑟夫问题
这是十七世纪的法国数学家加斯帕在《数目的游戏问题》中讲的一个故事:15个基督教徒和15个异教徒在海上遇险,必须将一半的人投入海中,其余的人才能幸免于难,于是想了个办法:30个人围成一个圆圈,从第一个人开始依次报数,每数到第9个人就将他扔入大海,如此循环进行直到仅余15个人为止。问怎样排法,才能使每次投入大海的都是异教徒。
**输出格式要求:”The original circle is (+:papandom, @:christian);\n” “%c ”
程序运行示例如下:
The original circle is (+:papandom, @:christian);
@ @ @ @ + + + + + @ @ + @ @ @ + @ + + @ @ + + + @ + + @ @ +

#include <stdio.h>
struct node
{                     
  int nextp;
  int no_out;
}                      link[31];
main()
{                     
  int i,j,k;
  printf("The original circle is (+:papandom,  @:christian);\n");
  for(i=1;i<=30;i++)
  {                   
    link[i].nextp=i+1;
    link[i].no_out=1;
  }
  link[30].nextp=1;
  j=30;
  for(i=0;i<15;i++)
  {                   
     for(k=0;;)
     if(k<9)
     {                    
       j=link[j].nextp;
       k+=link[j].no_out;
     }
     else break;
     link[j].no_out=0;
  }
  for(i=1;i<=30;i++)
        printf("%c ",link[i].no_out?'@':'+');
    printf("\n");

} 

40. 设计学生通讯录。
设计一个程序用来存储学生的姓名(6个字母)及电话号码(11位数字),以字符#作为结束输入。然后输入待查找姓名,查找该人的电话号码。并从屏幕输出。数据从s1开始存放.

注:学生姓名可以有空格

利用以下函数原型进行编写,请把以下两个函数补充完整。

void readin(char (*name)[20],char (*tel)[15],int *num);

void search(char *x,char (*name)[20],char (*tel)[15], int n);

int main()

{

    int i;

    char name[MAX][20],tel[MAX][15];

    char searchS[20];

    int num;

    readin(name,tel,&num);

     for(i=1;i<=num;i++)

    {

    printf("name:%s tel: %s\n",name[i],tel[i]);

    }

    printf("Enter a name:");

    gets(searchS);



    search(searchS,name,tel,num);

    return 0;

}

void readin(char (*name)[20],char (*tel)[15],int *num)

{



}

void search(char *x,char (*name)[20],char (*tel)[15], int n);

{



}






输入数据提示信息:”Enter a name:”

输入数据格式:gets()函数读入

输出数据提示信息:”Not been found!\n”

输出数据格式:”name:%s tel: %s\n”

**输入输出样例1:

输入:

li li

13645172020

yu zi

15874560231

feifei

13945058888

yangti

13766062221

huqi

15160624433

#

23145

输出:

name:li li tel: 13645172020

name:yu zi tel: 15874560231

name:feifei tel: 13945058888

name:yangti tel: 13766062221

name:huqi tel: 15160624433

提示信息:Enter a name:

输入:

yangti

输出:

yangti tel: 13766062221

**输入输出样例2:

输入:

li li

13645172020

yu zi

15874560231

feifei

13945058888

yangti

13766062221

huqi

15160624433

#

23145

输出:

name:li li tel: 13645172020

name:yu zi tel: 15874560231

name:feifei tel: 13945058888

name:yangti tel: 13766062221

name:huqi tel: 15160624433

提示信息:Enter a name:

输入:

ttt

输出:

Not been found!

#include<stdio.h>
#include<string.h>
#define MAX 10
void readin(char (*name)[20], char (*tel)[15], int *num);
void search(char *x, char (*name)[20], char (*tel)[15], int n);
int main()
{
    int i;
    char name[MAX][20], tel[MAX][15];
    char searchS[20];
    int num;
    readin(name, tel, &num);
    for (i = 1; i < num; i++)
    {
        printf("name:%s tel: %s\n", name[i], tel[i]);
    }
    printf("Enter a name:");
    gets(searchS);
    search(searchS, name, tel, num);
    return 0;
}
void readin(char (*name)[20], char (*tel)[15], int *num)
{
    int i;
    for (i = 1; i < MAX; i++)
    {
        gets(name + i);
        gets(tel + i);

        if (strcmp(name + i, "#") == 0)
            break;
    }
    *num = i;

}
void search(char *x, char (*name)[20], char (*tel)[15], int n)
{
    int j, flag = 0;
    for (j = 1; j < n; j++)
    {
        if (strcmp(name + j, x) == 0)
        {
            printf("name:%s tel: %s\n", name + j, tel + j);
            flag = 1;
        }
    }
    if (flag == 0)
        printf("Not been found!\n");

}

41.
请设计一个程序来统计学生成绩。功能:输入学生的姓名和成绩,按成绩从高到低排列打印输出,排名在前70%的学生定为合格地,对后30%的学生定为不合格。
请改正以下程序中的错误。
注:学生姓名不能有空格
注意:请不要删除行,也不要添加行,请在原行上进行修改。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct infor
{
    char name[30];
    float grade;
}student;

student class_1[40];

void sortclass(student *p,int n);
void swap(student *p1, student *p2);
int main()
{
    int ns,cutoff,i;
    printf("number of student:\n");

        scanf("%d",&ns);
    printf("Enter name and grade for each student:\n");
     for(i=0;i<ns;i++)
    {
        scanf("%s %f", &class_1[i]->name,&class_1[i].grade);
    }
    sortclass(class_1,ns);
    cutoff=(ns*7)/10-1;
    printf("\n");
    for(i=0;i<ns;i++)
    {
        printf("%-6s %3f",class_1[i].name,class_1[i].grade);
        if(i<=cutoff)
            printf(" pass\n");
       else
            printf(" fail\n");

    }
    return 0;
}

void sortclass(student st[], int nst)
{
    int i,j,pick;
    for(i=0;i<(nst-1);i++)
    {
        pick=j;
        for(j=i+1;j<nst-1;j++)
        {
            if(st[j].grade>st[pick].grade)
                pick=i;
            swap(st[i],st[pick]);
        }
    }
}

void swap(student *ps1,student *ps2)
{
    student temp;
    strcpy(temp.name,ps1->name);
    temp.grade=ps1->grade;
    strcpy(*ps1.name,ps2->name);
    ps1->grade=ps2->grade;
    strcpy(ps2->name,temp.name);
    ps2->grade=temp.grade;
}

输入输出格式
输入提示信息1:”number of student:\n”
输入数据格式1:”%d”
输入提示信息2:”Enter name and grade for each student:\n”
输入数据格式2:”%s %f”
输出提示信息:无
输出数据格式1:”%s %f\n”
输出数据格式2:”%-6s %3f”
输出数据格式2:” fail\n”
输出数据格式2:” pass\n”

**输入输出样例1:
输入提示信息1:number of student:
输入1:6
输入提示信息2:Enter name and grade for each student:
输入2:
lili 89
Mary 90.5
Tom 85.5
Sandy 78.6
Jack 65
Hito 62.0
输出:
Mary 90.500000 pass
lili 89.000000 pass
Tom 85.500000 pass
Sandy 78.599998 pass
Jack 65.000000 fail
Hito 62.000000 fail

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct infor
{
    char name[30];
    float grade;
} student;

student class_1[40];

void sortclass(student st[], int nst);
void swap(student *ps1, student *ps2);
int main()
{
    int ns, cutoff, i;
    printf("number of student:\n");

    scanf("%d", &ns);
    printf("Enter name and grade for each student:\n");
    for (i = 0; i < ns; i++)
    {
        scanf("%s %f", &class_1[i].name, &class_1[i].grade);
    }
    sortclass(class_1, ns);
    cutoff = (ns * 7) / 10 - 1;
    printf("\n");
    for (i = 0; i < ns; i++)
    {
        printf("%-6s %3f", class_1[i].name, class_1[i].grade);
        if (i <= cutoff)
            printf(" pass\n");
        else
            printf(" fail\n");

    }
    return 0;
}

void sortclass(student st[], int nst)
{
    int i, j, pick;
    for (i = 0; i < (nst - 1); i++)
    {
        pick = i;
        for (j = i + 1; j < nst; j++)
        {
            if (st[j].grade > st[pick].grade)
                pick = j;
            swap(&st[i], &st[pick]);
        }
    }
}

void swap(student *ps1, student *ps2)
{
    student temp;
    strcpy(temp.name, ps1->name);
    temp.grade = ps1->grade;
    strcpy(ps1->name, ps2->name);
    ps1->grade = ps2->grade;
    strcpy(ps2->name, temp.name);
    ps2->grade = temp.grade;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

c语言习题 的相关文章

  • 按键消抖详解

    一 按键消抖原理 抖动时间的长短由按键的机械特性决定 xff0c 一般为 5ms xff5e 10ms xff0c 键抖动会引起一次按键被误读多次 解决办法 xff1a 判断按键按下时 xff0c 延时 10 ms 即可 二 软件实现按键消
  • 20 分钟梳理 Spring 全家桶 !

    作 者 xff1a Daisy 授权转自IT技术思维 xff0c 每日精选优质干货 xff0c 欢迎关注 xff01 xff1e xff1e xff1c xff1c Spring框架自诞生以来一直备受开发者青睐 xff0c 有人亲切的称之为
  • Linux添加软件分类(GNOME桌面)

    Linux添加软件分类 xff08 GNOME桌面 xff09 之前安装TIM deepin wine 的时候发现TIM的分类为chat xff0c 而系统默认没有这个分类 xff0c 所以TIM就很自然的被划分到 其他 里边去了 这强迫症
  • gnome扩展推荐

    引言 xff1a gnome在Linux世界里作为一个比较流行的桌面环境 xff0c 默认不是十分美观 xff0c 有些功能也没有 xff0c 这个时候我们就可以选择安装扩展去个性化gnome 下面是我的桌面截图 xff0c 我利用了扩展实
  • SpringBoot + Redis实现布隆过滤器

    一 简述 关于布隆过滤器的详细介绍 xff0c 我在这里就不再赘述一遍了 我们首先知道 xff1a BloomFilter使用长度为m bit的字节数组 xff0c 使用k个hash函数 xff0c 增加一个元素 通过k次hash将元素映射
  • 屏蔽效能预估

    今天完成了屏蔽效能预估部分的程序 由于公式比较多 xff0c 而且就编程本身而言技术含量不高 xff0c 因此不将源代码贴出 xff0c 有需要者可以联系我 程序界面如下 xff1a
  • SSH 命令的11种用法

    使用ssh连接远程主机 最简单的用法只需要指定用户名和主机名参数即可 xff0c 主机名可以是 IP 地址或者域名 ssh user 64 hostname ssh连接到其他端口 SSH 默认连接到目标主机的 22 端口上 xff0c 可以
  • Spring配置的可选方案(三种配置方式)

    版权声明 xff1a 本文摘自 Spring实战 第4版 xff0c 美 Craig Walls 著 xff0c 张卫滨 译 本文仅作为学习与交流使用 xff0c 如有侵权请留言联系作者 转载请注明出处 目录 一 自动化装配Bean 注释
  • ftp工具

    本文会介绍java代码的ftp工具使用 xff0c 代码实现的功能难免不全 xff0c 要完整的体验ftp功能 xff0c 建议使用该ftp工具 xff1a iis7服务器管理工具 iis7服务器管理工具 xff08 曾用名 xff1a I
  • windows server 2000 r2 设置FTP文件服务器

    最近有一个需求需要将我们自己的一台windows服务器设置文件服务器 xff0c 小小记录一下 xff0c 设置过程 搭建IIS 第一步 xff1a 打开控制面板 第二步 xff1a 点击 打开或关闭 Windows 功能 第三步 xff1
  • ubuntu通过shell脚本实现服务自启和自动关机

    通常服务器开启后需要输入一大堆繁琐的进入文件 启动服务等命令 xff0c 每天如此就会逼着自己寻找捷径 xff0c 毕竟时间不用来学习就是在浪费生命嘛 xff1a Shell脚本挺身而出 xff1a 实现 xff1a 1 配置开机root账
  • 是什么导致了nginx.service: control process exited, code=exited status=1?

    是什么导致了nginx service control process exited code 61 exited status 61 1 xff1f 今天使用脚本安装nginx服务时遇到下面的问题 xff1a 那就先敲命令呗 xff0c
  • .jar与sources.jar区别

    首先 xff0c 当我们在下载jar包与引入jar包的时候可能会发现 xff0c 存在jar文件与相应的sources jar文件 如下图所示 xff1a 这个时候 xff0c 到底该下载哪一个 xff0c 或者我们需要的是哪一个 是jun
  • bat暂停5秒

    choice T 5 C ync CS D y n
  • Linux 开机自启动

    一 无界面的程序自启动 etc rc local 1 编辑 etc rc local vi etc rc local 2 添加要执行的命令 在exit 0 之前 注意 xff1a 这里的执行命令都必须是全路径的 xff0c 就算你添加到了
  • 使用firefox color自定义firefox的主题

    本说明基于firefox 79 轻量级主题 引用 xff1a firefox关于主题的说法 xff0c firefox现在仅支持轻量级主题了 那么什么是轻量级主题呢 xff1f mozilla官方并没有明确的定义 xff0c 我的理解是 x
  • TCL判断条件

    编写TCL代码时遇要写一个if判断条件 xff0c 很简单的一个语句 xff0c 结果却费了很大力气才搞定 要判断的是 xff0c 如果执行info exists成功而且某全局数组C的某个成员大于0 xff0c 正确的语句为 xff1a i
  • 实验二:线性时间选择

    实验二 xff1a 线性时间选择 问题描述 xff08 1 xff09 线性时间选择问题 给定线性序集中n个元素和一个整数k xff0c 1 lt 61 k lt 61 n 要求找出这n个元素中第k小的元素 xff0c 即如果将这个n个元素
  • 我的 IDEA 常用插件介绍

    本文同步发表于我的微信公众号 xff0c 在微信搜索 及格 即可关注 这篇文章介绍一下我 IDEA 里安装的插件 我的 IDEA 版本是IntelliJ IDEA 2021 3 3 xff0c 并且打上了官方的汉化包 xff0c 但我假设看

随机推荐