100 examples of C language classic programming questions (41~60)

Table of contents

  • 41. Exercise 4-4 The summation of special series a

  • 42. Exercise 4-6 Number of daffodils

  • 43. Exercise 4-7 Greatest common divisor and least common multiple

  • 44. Exercise 7-5 Find saddle point

  • 45. Exercise 5-1 Find the sum from m to n

  • 46. ​​Exercise 5-2 Find the largest of two numbers

  • 47. Exercise 5-3 Number Pyramid

  • 48. Exercise 5-1 Symbolic functions

  • 49. Exercise 5-2 Use functions to find sums of odd numbers

  • 50. Exercise 5-3 Use a function to calculate the distance between two points

  • 51. Exercise 5-4 Use functions to find the sum of prime numbers

  • 52. Exercise 5-5 Use functions to count the number of specified numbers

  • 53. Exercise 5-6 Use the function to output the number of daffodils

  • 54. Exercise 5-7 Use the function to find the approximate value of the cosine function

  • 55. Exercise 4-8 Dropping the ball from high altitude

  • 56. Exercise 4-10 Monkey eating peach problem

  • 57. Exercises 6-8 Count the number of words in a line of text

  • 58. Exercise 7-2 Find the maximum value and its subscript

  • 59. Exercise 7-3 Store the numbers in the array in reverse order

  • 60. Exercise 7-4 Find elements that are not common to two arrays


41. Exercise 4-4 The summation of special series a


Given two positive integers a and n, both of which do not exceed 9, it is required to write a program to find the sum of a+aa+aaa++⋯+aa⋯a (n a).

Input format:

The input gives positive integers a and n not exceeding 9 in one line.

Output format:

Output in the format "s = corresponding sum" on one line.

Input sample:

2 3

Sample output:

s = 246

code:

#include<stdio.h>
int main()
{
    int a,n;
    scanf("%d %d",&a,&n);
    int i;
    int sum=0;
    int t=a;
    for(i=1;i<=n;i++)
    {
        sum+=t;
        t=t*10+a;
    }
    printf("s = %d\n",sum);
    return 0;
}

42. Exercise 4-6 Number of daffodils


The daffodil number refers to an N-digit positive integer (N≥3), and the sum of the N powers of the numbers in each digit is equal to itself. For example: 153=. This question requires writing a program to calculate the number of all N-digit daffodils.

Input format:

The input is given a positive integer N (3≤N≤7) in one line.

Output format:

Output all N-digit daffodil numbers in increasing order, each number occupying a line.

Input sample:

3

Sample output:

153
370
371
407

code:

#include<stdio.h>
void fun1(int *m,int *n,int N)
{
    int i;
    int t1,t2;
    t1=t2=1;
    for(i=1;i<=N-1;i++)
        t1*=10;
    for(i=1;i<=N;i++)
        t2*=10;
    *m=t1;*n=t2-1;
}

int fun2(int n,int N)
{
    int i;
    int t;
    int sum=0;
    int tt=n;
    while(n)
    {
        t=1;
        int temp=n%10;
        for(i=1;i<=N;i++)
        {
            t*=temp;
        }
        sum+=t;
        n/=10;
    }
    if(tt==sum) return 1;
    else return 0;
}
int main()
{
    int N;
    scanf("%d",&N);
    int m,n;
    fun1(&m,&n,N);  //找出最小N位数和最大N位数
    //printf("%d %d\n",m,n);
    int i;
    for(i=m;i<=n;i++)
    {
        int temp=fun2(i,N);
        if(temp==1) printf("%d\n",i);
    }
    return 0;
}

43. Exercise 4-7 Greatest common divisor and least common multiple


This question requires the greatest common divisor and least common multiple of two given positive integers.

Input format:

The input gives two positive integers M and N (≤1000) in one line.

Output format:

Output the greatest common divisor and least common multiple of M and N sequentially in one line, and the two numbers are separated by 1 space.

Input sample:

511 292

Sample output:

73 2044

code:

#include<stdio.h>
int main()
{
    int M,N;
    scanf("%d %d",&M,&N);
    int a,b;
    a=M;
    b=N;
    int temp;
    temp=M%N;
    while(temp)   //辗转相除法求最大公约数
    {
        M=N;
        N=temp;
        temp=M%N;
    }
    printf("%d %d\n",N,a*b/N);
    return 0;

}

44. Exercise 7-5 Find saddle point


The "saddle point" of a matrix element means that the element value at this position is the largest on the row and the smallest on the column.

This question requires writing a program to find the saddle point of a given square matrix of order n.

Input format:

The first line of input gives a positive integer n (1≤n≤6). Followed by n lines, each line gives n integers separated by spaces.

Output format:

Output the position of the saddle point in a row in the format of "row subscript column subscript" (subscript starts from 0). If no saddle point exists, output "NONE". The title guarantees that the given matrix has at least one saddle point.

Input sample 1:

Input sample 1:

4
1 7 4 1
4 8 3 6
1 6 1 2
0 7 8 9

Output sample 1:

2 1

Input sample 2:

2
1 7
4 1

Output sample 2:

NONE

code:

#include<stdio.h>

int fun1(int arr[][7],int i,int n)
{
    int j;
    int index=0;
    for(j=1;j<n;j++)
    {
        if(arr[i][j]>=arr[i][index]) index=j;
    }
    return index;
}

int fun2(int arr[][7],int i,int n)
{
    int j;
    int index=0;
    for(j=1;j<n;j++)
    {
        if(arr[j][i]<=arr[index][i]) index=j;
    }
    return index;
}
int main()
{
   int n;
   scanf("%d",&n);
   int arr[7][7];
   int i,j;
   for(i=0;i<n;i++)
   {
       for(j=0;j<n;j++)
       {
           scanf("%d",&arr[i][j]);
       }
   }
   int l=0;
   int temp1,temp2;
   for(i=0;i<n;i++)
   {
       temp1=fun1(arr,i,n);
       temp2=fun2(arr,temp1,n);
       if(arr[i][temp1]==arr[temp2][temp1])
       {
           l=1;break;
       }
   }
   if(l==1) printf("%d %d\n",temp2,temp1);
   else printf("NONE\n");
   return 0;
}

45. Exercise 5-1 Find the sum from m to n


This question requires the implementation of a simple function that calculates the sum of all integers between m~n (m<n).

Function interface definition:

int sum( int m, int n );

Among them, m and n are the parameters passed in by the user, and it is guaranteed that m<n. The function returns the sum of all integers between m~n.

Sample referee test procedure:

#include <stdio.h>
    
int sum(int m, int n);
    
int main()
{    
   int m, n;
   scanf("%d %d", &m, &n);
   printf("sum = %d\n", sum(m, n));
   return 0;
}
    
/* 你的代码将被嵌在这里 */

Input sample:

-5 8

Sample output:

sum = 21

code:

int sum(int m, int n)
{
    int i;
    int sum=0;
    for(i=m;i<=n;i++)
    {
        sum=sum+i;
    }
    return sum;
}

46. ​​Exercise 5-2 Find the largest of two numbers


This question requires to output the larger number of two integers a and b.

Function interface definition:

int max( int a, int b );

Among them, a and b are the parameters passed in by the user, and the function returns the larger number of the two.

Sample referee test procedure:

#include <stdio.h>
    
int max( int a, int b );
    
int main()
{    
   int a, b;
   scanf("%d %d", &a, &b);
   printf("max = %d\n", max(a, b));
   return 0;
}

/* 你的代码将被嵌在这里 */

Input sample:

-5 8

Sample output:

max = 8

code:

int max( int a, int b )
{
  if(a>=b)return a;
  else return b;
}

47. Exercise 5-3 Number Pyramid


This question requires the implementation of the function to output n rows of digital pyramids.

Function interface definition:

void pyramid( int n );

Among them, n is the parameter passed in by the user, which is a positive integer of [1, 9]. The function is required to print out an n-row digital pyramid in the format shown in the example. Note that each number is followed by a space.

Sample referee test procedure:

#include <stdio.h>
    
void pyramid( int n );
    
int main()
{    
    int n;
    scanf("%d", &n);
    pyramid(n);
    return 0;
}
    
/* 你的代码将被嵌在这里 */

Input sample:

5

Sample output:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

code:

void pyramid( int n )
{
    int i;
    int temp=n;
    for(i=1;i<=n;i++)
    {
        int j=1;
        for(;j<temp;j++)
            printf(" ");
        for(j=1;j<=i;j++)
        {
            printf("%d ",i);
        }
        printf("\n");
        temp--;
    }
}

48. Exercise 5-1 Symbolic functions


This question requires the implementation of the symbolic function sign(x).

Function interface definition:

int sign( int x );

Where x is an integer parameter passed in by the user. The sign function is defined as: if x is greater than 0, sign(x) = 1; if x is equal to 0, sign(x) = 0; otherwise, sign(x) = −1.

Sample referee test procedure:

#include <stdio.h>
    
int sign( int x );
    
int main()
{
    int x;
    scanf("%d", &x);
    printf("sign(%d) = %d\n", x, sign(x));
    return 0;
}
    
/* 你的代码将被嵌在这里 */

Input sample:

10

Sample output:

sign(10) = 1

code:

int sign( int x )
{
  if(x>0) return 1;
  else if(x==0) return 0;
  else return -1;
}

49. Exercise 5-2 Use functions to find sums of odd numbers


This question requires the implementation of a function to calculate the sum of all odd numbers among N integers, and at the same time implement a function for judging parity.

Function interface definition:

int even( int n ); int OddSum( int List[], int N );

The function even will return the corresponding value according to the parity of the parameter n passed in by the user: when n is even, it returns 1, otherwise it returns 0. The function OddSum is responsible for calculating and returning the sum of all odd numbers in the N integers List[] passed in.

Sample referee test procedure:

#include <stdio.h>
#define MAXN 10
int even( int n );
int OddSum( int List[], int N );
int main() 
{
 int List[MAXN], N, i;
 scanf("%d", &N);
 printf("Sum of ( ");
 for ( i=0; i<N; i++ ) 
 {
  scanf("%d", &List[i]);
  if ( even(List[i])==0 )
      printf("%d ", List[i]);
 }
 printf(") = %dn", OddSum(List, N));
        return 0;
    }
/* 你的代码将被嵌在这里 */

Input sample:

6
2 -3 7 88 0 15

Sample output:

Sum of ( -3 7 15 ) = 19

code:

int even( int n )
{
  if(n%2==0) return 1;
  else return 0;
}

int OddSum( int List[], int N )
{
  int i;
  int sum=0;
  for(i=0;i<N;i++)
  {
    if(List[i]%2!=0) sum+=List[i];
  }
  return sum;
}

50. Exercise 5-3 Use a function to calculate the distance between two points


This question requires the implementation of a function to find the distance between any two points on a given plane (x~1~ ,y~1~ ) and (x~2~ ,y~2~).

Function interface definition:

double dist( double x1, double y1, double x2, double y2 );

The parameters passed in by the user are the coordinates (x1, y1) and (x2, y2) of two points on the plane, and the function dist should return the distance between the two points.

Sample referee test procedure:

#include <stdio.h>
#include <math.h>
    
double dist( double x1, double y1, double x2, double y2 );
    
int main()
{    
    double x1, y1, x2, y2;
    
    scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
    printf("dist = %.2f\n", dist(x1, y1, x2, y2));
    
    return 0;
}
    
/* 你的代码将被嵌在这里 */

Input sample:

10 10 200 100

Sample output:

dist = 210.24

code:

double dist( double x1, double y1, double x2, double y2 )
{
  return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

51. Exercise 5-4 Use functions to find the sum of prime numbers


This question requires the realization of a simple function for judging prime numbers and a function for calculating the sum of prime numbers in a given interval using this function.

A prime number is a positive integer that is only divisible by 1 and itself. Note: 1 is not a prime number, 2 is a prime number.

Function interface definition:

int prime( int p ); int PrimeSum( int m, int n );

The function prime returns 1 when the parameter p is a prime number passed in by the user, otherwise it returns 0; the function PrimeSum returns the sum of all prime numbers in the interval [m, n]. The topic guarantees that the parameter m≤n passed in by the user.

Sample referee test procedure:

#include <stdio.h>
#include <math.h>
    
int prime( int p );
int PrimeSum( int m, int n );
    
int main()
{
    int m, n, p;
    
    scanf("%d %d", &m, &n);
    printf("Sum of ( ");
    for( p=m; p<=n; p++ ) {
        if( prime(p) != 0 )
            printf("%d ", p);
    }
    printf(") = %d\n", PrimeSum(m, n));
    
    return 0;
}
    
/* 你的代码将被嵌在这里 */

Input sample:

-1 10

Sample output:

Sum of ( 2 3 5 7 ) = 17

code:

int prime( int p )
{
  if(p<2) return 0;
  else
  {
     int i;
     int l=1;
     for(i=2;i<=sqrt(p);i++)
     {
       if(p%i==0)
       {
         l=0;
         break;
       }
     }
     return l;
  }
}
int PrimeSum( int m, int n )
{
  int sum=0;
  int i;
  for(i=m;i<=n;i++)
  {
    int temp=prime(i);
    if(temp==1) sum+=i;
  }
  return sum;
}

52. Exercise 5-5 Use functions to count the number of specified numbers


This question requires the implementation of a simple function that counts the number of specified numbers in an integer.

Function interface definition:

int CountDigit( int number, int digit );

Among them, number is an integer not exceeding a long integer, and digit is an integer in the interval [0, 9]. The function CountDigit should return the number of occurrences of digit in number.

Sample referee test procedure:

#include <stdio.h>
    
int CountDigit( int number, int digit );
    
int main()
{
    int number, digit;
    
    scanf("%d %d", &number, &digit);
    printf("Number of digit %d in %d: %d\n", digit, number, CountDigit(number, digit));
    
    return 0;
}
    
/* 你的代码将被嵌在这里 */

Input sample:

-21252 2

Sample output:

Number of digit 2 in -21252: 3

code:

int CountDigit( int number, int digit )
{
 if(number<0) number=-number;
 if(number==0&&digit==0) return 1;
  else
  {
    int tt=0;
    while(number)
    {
      int temp=number%10;
      if(temp==digit) tt++;
      number/=10;
    }
    return tt;
  }
}

53. Exercise 5-6 Use the function to output the number of daffodils


The daffodil number refers to an N-digit positive integer (N≥3), and the sum of the N powers of the numbers in each digit is equal to itself. For example: 153=1^3^+5^3^+3^3^. This question requires writing two functions, one to judge whether a given integer is a number of daffodils, and the other to print out all the numbers of daffodils in a given interval (m, n) in ascending order.

Function interface definition:

int narcissistic( int number ); void PrintN( int m, int n );

The function narcissistic judges whether the number is a narcissus number, and returns 1 if it is, otherwise returns 0.

The function PrintN prints all the numbers of daffodils in the open interval (m, n), and each number occupies one line. The title is guaranteed to be 100≤m≤n≤10000.

Sample referee test procedure:

#include <stdio.h>
    
int narcissistic( int number );
void PrintN( int m, int n );
    
int main()
{
    int m, n;
    
    scanf("%d %d", &m, &n);
    if ( narcissistic(m) ) printf("%d is a narcissistic number\n", m);
    PrintN(m, n);
    if ( narcissistic(n) ) printf("%d is a narcissistic number\n", n);
    
    return 0;
}
    
/* 你的代码将被嵌在这里 */

Input sample:

153 400

Sample output:

153 is a narcissistic number
370
371

code:

int narcissistic( int number )
{
  int nu=number;
  int temp=0;
  while(nu)
  {
    nu/=10;
    temp++;
  }
  nu=number;
  int sum=0;
  int index;
  while(number)
  {
    int i;
    int t=number%10;
    index=1;
    for(i=1;i<=temp;i++)
     index*=t;
    sum+=index;
    number/=10;
  }
  if(sum==nu) return 1;
  else return 0;
}
void PrintN( int m, int n )
{
  int i;
  for(i=m+1;i<n;i++)
  {
     int temp=narcissistic(i);
     if(temp==1) printf("%d\n",i);
  }
}

54. Exercise 5-7 Use the function to find the approximate value of the cosine function


This question requires the implementation of a function, using the following formula to find the approximate value of cos(x), accurate until the absolute value of the last item is less than e: cos(x)=

Function interface definition:

double funcos( double e, double x );

The parameters passed in by the user are the error upper limit e and the independent variable x; the function funcos should return the approximate value of cos(x) calculated with the given formula and satisfying the error requirements. Both input and output are in double precision.

Sample referee test procedure:

#include <stdio.h>
#include <math.h>
    
double funcos( double e, double x );
    
int main()
{    
    double e, x;
    
    scanf("%lf %lf", &e, &x);
    printf("cos(%.2f) = %.6f\n", x, funcos(e, x));
    
    return 0;
}
    
/* 你的代码将被嵌在这里 */

Input sample:

0.01 -3.14

Sample output:

cos(-3.14) = -0.999899

code:

double funcos( double e, double x )
{
    int i,j;
    double temp1;
    double temp2;
    double temp;
    double sum=0;
    int l=1;
    for(i=0;;i+=2)
    {
        if(i==0)
        {
           temp=1.0;
        }
        else
        {
            temp1=1;
            temp2=1;
            for(j=1;j<=i;j++)
            {
                temp1*=x;
                temp2*=j;
            }
            temp=temp1/temp2;
        }
          sum+=(temp*l);
        if(temp<e) break;

        l=-l;

    }
    return sum;
}

55. Exercise 4-8 Dropping the ball from high altitude


The ball falls freely from a given height, bounces to half of its original height after touching the ground, falls again, bounces again, and so on. When the ball hits the ground for the nth time, how much distance has it traveled in the air? What is the height of the nth bounce?

Input format:

The input gives two non-negative integers on one line, the initial height of the ball and n, both in the range of long integers.

Output format:

In one line, sequentially output the distance traveled by the ball when it hits the ground for the nth time, and the height of the nth bounce, separated by a space, and retain one decimal place. The title guarantees that the calculation result does not exceed the double precision range.

Input sample:

33 5

Sample output:

94.9 1.0

code:

#include<stdio.h>
int main()
{
    double h;
    int n;
    scanf("%lf %d",&h,&n);
    int i;
    double sum=h;
    if(n==0)
    {
        printf("0.0 0.0\n");
    }
    else{
         for(i=1;i<=n-1;i++)
          {
            if(h==0) break;
            h=h/2.0;
            sum=sum+h+h;
          }
          printf("%.1lf %.1lf\n",sum,h/2);
      }
      return 0;
}

56. Exercise 4-10 Monkey eating peach problem


A monkey picked several peaches on the first day, ate half of them immediately, and was not satisfied, so he ate one more; the next morning he ate half of the remaining peaches, and ate another one. After that, I ate the remaining half of the previous day plus one every morning. When I wanted to eat again in the morning of the Nth day, I saw that there was only one peach left. Question: How many peaches were picked on the first day?

Input format:

The input is given a positive integer N (1<N≤10) in one line.

Output format:

Output how many peaches were picked on the first day in one line.

Input sample:

3

Sample output:

10

code:

#include<stdio.h>
int main()
{
    int N;
    scanf("%d",&N);
    int i;
    int t=1;
    for(i=N;i>1;i--)
    {
        t=(t+1)*2;

    }
    printf("%d\n",t);
}

57. Exercises 6-8 Count the number of words in a line of text


This topic requires writing a program to count the number of words in a line of characters. The so-called "word" refers to a continuous character string without spaces, each word is separated by spaces, and the number of spaces can be multiple.

Input format:

input gives a line of characters.

Output format:

Print the number of words on a line.

Input sample:

Let's go to room 209.

Sample output:

5

code:

#include<stdio.h>
int main()
{
    char str[1000];
    gets(str);
    int i,j;
    int len=strlen(str);
    i=0;
    j=len-1;
    while(str[i]==' ')
    {
        i++;
    }
    while(str[j]==' ')
    {
        j--;
    }
    if(i==len&&j<0) printf("0\n");
    else{
    int n1=i;
    int n2=j;
    int temp=0;
    int l=0;
    for(i=n1+1;i<=n2;i++)
    {
       if(str[i]==' '&&str[i-1]!=' ') {l=1;temp++;}
    }
   printf("%d\n",temp+1);}
    return 0;
}

58. Exercise 7-2 Find the maximum value and its subscript


This question requires writing a program to find the maximum value of the given n numbers and its corresponding minimum subscript (the subscript starts from 0).

Input format:

The input is given a positive integer n (1<n≤10) in the first line. Enter n integers in the second line, separated by spaces.

Output format:

Output the maximum value and the minimum subscript of the maximum value in one line, separated by a space.

Input sample:

6
2 8 10 1 9 10

Sample output:

10 2

code:

#include<stdio.h>
#include<math.h>
int main()
{
  int n;
  int a[100];
  scanf("%d\n",&n);
  int i;
  for(i=0;i<n;i++)
    scanf("%d\,",&a[i]);
  int j;
  int index;
  index=0;
  for(i=1;i<n;i++)
  {
      if(a[i]>a[index]) index=i;

  }
  printf("%d %d\n",a[index],index);
}

59. Exercise 7-3 Store the numbers in the array in reverse order


This question requires writing a program to store the given n integers in an array, store the n numbers in the array in reverse order, and then output the elements in the array in order.

Input format:

The input is given a positive integer n (1≤n≤10) in the first line. Enter n integers in the second line, separated by spaces.

Output format:

Output the processing results of these n integers in one line. Adjacent numbers are separated by a space, and there must be no extra spaces at the end of the line.

Input sample:

4
10 8 1 2

Sample output:

2 1 8 10

code:

#include<stdio.h>
int main()
{
    int arr[11];
    int n;
    scanf("%d",&n);
    int i;
    for(i=0;i<n;i++)
        scanf("%d",&arr[i]);

    for(i=0;i<n/2;i++)
    {
        int t=arr[i];
        arr[i]=arr[n-1-i];
        arr[n-1-i]=t;
    }
   // arr[n-1]=t;
    for(i=0;i<n;i++)
    {
        if(i==n-1) printf("%d\n",arr[i]);
        else printf("%d ",arr[i]);
    }
    return 0;
}

60. Exercise 7-4 Find elements that are not common to two arrays


Given two integer arrays, this question asks to find elements that are not common to both.

Input format:

The input gives two integer arrays in two lines, each line first gives a positive integer N (≤20), followed by N integers, separated by spaces.

Output format:

Output the elements that are not common to the two arrays in the order given by the numbers on one line, and the numbers are separated by spaces, but there must be no extra spaces at the end of the line. The title guarantees that at least one such number exists. The same number is not output repeatedly.

Input sample:

10 3 -5 2 8 0 3 5 -15 9 100
11 6 4 8 2 6 -5 9 0 100 8 1

Sample output:

3 5 -15 6 4 1

code:

#include<stdio.h>
int temp[100];
int tt=0;
void fun(int arr[][22],int n1,int n2)
{
    int i,j;
    int l;
    for(i=0;i<n1;i++)
    {
      int l=1;
      for(j=0;j<n2;j++)
      {
          if(arr[0][i]==arr[1][j]){l=0;break;}
      }
      if(l==1) temp[tt++]=arr[0][i];
    }
    for(i=0;i<n2;i++)
    {
      int l=1;
      for(j=0;j<n1;j++)
      {
          if(arr[1][i]==arr[0][j]){l=0;break;}
      }
      if(l==1) temp[tt++]=arr[1][i];
    }
}
int main()
{
    int arr[2][22];
    int N;
    int i,k;
    int n1,n2;
    for(k=0;k<2;k++)
    {
        scanf("%d",&N);
        if(k==0) n1=N;
        else n2=N;
        for(i=0;i<N;i++)
        {
           scanf("%d",&arr[k][i]);
        }
    }
    fun(&arr,n1,n2);
    int j;
    int l;
    for(i=0;i<tt-1;i++)
    {
        for(j=i+1;j<tt;j++)
        {
            if(temp[j]==temp[i])temp[j]=-999999;
        }
    }
    for(i=0;i<tt;i++)
    {
        if(temp[i]!=-999999)
        {
            if(i==0) printf("%d",temp[i]);
            else printf(" %d",temp[i]);
        }
    }
    printf("\n");
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_58045538/article/details/129011456