100 examples of C language classic programming questions (61~80)

Table of contents

  • 61. Exercise 7-7 Matrix operations

  • 62. Exercise 7-8 Square matrix circularly shifted to the right

  • 63. Exercise 6-1 Count the number of characters by category

  • 64. Exercise 6-2 Use functions to find the sum of special a strings

  • 65. Exercise 6-4 Use the function to output the Fibonacci numbers within the specified range

  • 66. Exercise 6-5 Use functions to verify Goldbach's conjecture

  • 67. Exercise 6-6 Use a function to output the inverse of an integer

  • 68. Exercise 8-2 Calculate the sum and difference of two numbers

  • 69. Exercise 7-9 Calculate the number of days

  • 70. Exercise 7-10 Find the specified character

61. Exercise 7-7 Matrix operations


Given an n×n square matrix, this question requires to calculate the sum of all elements of the matrix except the subdiagonal, the last column and the last row. The sub-diagonal is the line from the upper right corner to the lower left corner of the matrix.

Input format:

Enter the first line to give a positive integer n (1<n≤10); then n lines, each line gives n integers, separated by spaces.

Output format:

Gives the sum of all elements of this matrix in one row except the subdiagonal, last column, and last row.

Input sample:

4
2 3 4 1
5 6 1 1
7 1 8 1
1 1 1 1

Sample output:

35

code:

#include<stdio.h>
int main()
{
    int n;
    int arr[11][11];
    scanf("%d",&n);
    int i,j;
    int sum=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",&arr[i][j]);
            sum+=arr[i][j];   //全部数据相加
        }
    }
    for(i=0;i<n;i++)
    {
        sum-=arr[i][n-1];  //减去最后一列
        sum-=arr[n-1][i];  //减去最后一行
    }
    sum+=arr[n-1][n-1];   //元素arr[n-1][n-1]被减两次,因此加一次
    int l=n-1;
    for(i=0;i<n;i++)
    {
        sum-=arr[i][l];   //减去副对角线上的元素
        l--;
    }
    sum+=arr[0][n-1];    //元素arr[0][n-1]被减两次,因此加一次
    sum+=arr[n-1][0];    //元素arr[n-1][0]被减两次,因此加一次
    printf("%d\n",sum);
    return 0;
}

62. Exercise 7-8 Square matrix circularly shifted to the right


This question requires writing a program to move each element in a given n×n square matrix to the right by m positions, that is, to transform columns 0, 1, ⋯, n−1 into n−m, n−m+1 , ⋯, n−1, 0, 1, ⋯, n−m−1 columns.

Input format:

The first line of input gives two positive integers m and n (1≤n≤6). Next, there are n lines in total, and each line contains n integers, representing a square matrix of order n.

Output format:

Output the shifted square matrix according to the input format: output n lines, each line contains n integers, and outputs a space after each integer.

Input sample:

2 3
1 2 3
4 5 6
7 8 9

Sample output:

2 3 1
5 6 4
8 9 7

code:

#include<stdio.h>
int main()
{
    int m,n;
    scanf("%d %d",&m,&n);
    int i,j;
    int arr[7][7];
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",&arr[i][j]);
        }
    }
    int k;
    int index;
    for(k=1;k<=m;k++)
    {
        for(i=0;i<n;i++)
        {
            index=arr[i][n-1];
            for(j=n-1;j>0;j--)
            {
                arr[i][j]=arr[i][j-1];
            }
            arr[i][j]=index;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
           printf("%d ",arr[i][j]);
        }
        printf("\n");
    }
    return 0;
}

63. Exercise 6-1 Count the number of characters by category


This question requires the implementation of a function to count the number of English letters, spaces or carriage returns, numeric characters and other characters in a given string.

Function interface definition:

void StringCount( char s[] );

Where char s[] is the string passed in by the user. The function StringCount must be in one line according to

letter = the number of English letters, blank = the number of spaces or carriage returns, digit = the number of numeric characters, other = the number of other characters

format output.

Sample referee test procedure:

#include <stdio.h>
#define MAXS 15
    
void StringCount( char s[] );
void ReadString( char s[] ); /* 由裁判实现,略去不表 */
    
int main()
{
    char s[MAXS];
    
    ReadString(s);
    StringCount(s);
    
    return 0;
}
    
/* Your function will be put here */

Input sample:

aZ &
09 Az

Sample output:

letter = 4, blank = 3, digit = 2, other = 1

code:

void StringCount( char s[] )
{
    int i;
    int letter=0;
    int blank=0;
    int digit=0;
    int other=0;
    for(i=0;i<strlen(s);i++)
    {
       if(s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z') letter++;
       else if(s[i]==' '||s[i]=='\n') blank++;
       else if(s[i]>='0'&&s[i]<='9') digit++;
       else other++;
    }
    printf("letter = %d, blank = %d, digit = %d, other = %d\n",letter,blank,digit,other);
}

64. Exercise 6-2 Use functions to find the sum of special a strings


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

Function interface definition:

int fn( int a, int n ); int SumA( int a, int n );

Among them, the function fn must return the number composed of n a; SumA returns the required sum.

Sample referee test procedure:

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

Input sample:

2 3

Sample output:

fn(2, 3) = 222
s = 246

code:

int fn( int a, int n )
{
  int i;
  int sum=0;
  for(i=1;i<=n;i++)
  {
    sum*=10;
    sum+=a;
  }
  return sum;
}
int SumA( int a, int n )
{
  int i,j;
  int sum=0;
  int t;
  for(i=1;i<=n;i++)
  {
     t=0;
     for(j=1;j<=i;j++)
     {
        t*=10;
        t+=a;
     }
     sum+=t;
  }
  return sum;
}

65. Exercise 6-4 Use the function to output the Fibonacci numbers within the specified range


This question requires implementing a simple function to calculate Fibonacci numbers, and using it to implement another function to output all Fibonacci numbers between two positive integers m and n (0<m≤n≤10000). The so-called Fibonacci sequence is a sequence that satisfies that any number is the sum of the first two (the first two are defined as 1).

Function interface definition:

int fib( int n );
void PrintFN( int m, int n );

Among them, the function fib must return the nth Fibonacci number; the function PrintFN should output all Fibonacci numbers in the given range [m, n] in one line, there should be a space between adjacent numbers, and there should be no extra space at the end of the line. If there is no Fibonacci number in the given interval, output a line of "No Fibonacci number".

Sample referee test procedure:

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

Input sample 1:

20 100 7

Output sample 1:

fib(7) = 13
21 34 55 89

Input sample 2:

2000 2500 8

Output sample 2:

fib(8) = 21
No Fibonacci number

code:

int fib( int n )
{
    int a=1;
    int b=1;
    if(n==1||n==2) return 1;
    else
    {
       int c;
       int temp=2;
       while(1)
        {
          c=a+b;
          temp++;
          a=b;
          b=c;
          if(temp>=n) break;
        }
        return c;
    }

}
void PrintFN( int m, int n )
{
    int i;
    int arr[100];
    int tt=0;
    for(i=1;;i++)
    {
        int temp=fib(i);
        if(temp>=m&&temp<=n)
        {
           arr[tt++]=temp;
        }
        if(temp>n) break;
    }
    if(tt==0) printf("No Fibonacci number\n");
    else
    {
        for(i=0;i<tt;i++)
         {
            if(i==tt-1) printf("%d\n",arr[i]);
            else printf("%d ",arr[i]);
         }
    }
}

66. Exercise 6-5 Use functions to verify Goldbach's conjecture


This question requires the implementation of a simple function to determine prime numbers, and use this function to verify Goldbach's conjecture: any even number not less than 6 can be expressed as the sum of two odd prime numbers. 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 );
void Goldbach( 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 Goldbach outputs the prime number decomposition of n according to the format "n=p+q", where p≤q is a prime number. And because such a decomposition is not unique (for example, 24 can be decomposed into 5+19, and can also be decomposed into 7+17), it is required to output the solution with the smallest p among all the solutions.

Sample referee test procedure:

#include <stdio.h>
#include <math.h>
     
int prime( int p ); void Goldbach( int n );
     
int main() {
    int m, n, i, cnt;
     
    scanf("%d %d", &m, &n);
    if ( prime(m) != 0 ) printf("%d is a prime number\n", m);
    if ( m < 6 ) m = 6;
    if ( m%2 ) m++;
    cnt = 0;
    for( i=m; i<=n; i+=2 ) {
        Goldbach(i);
        cnt++;
        if ( cnt%5 ) printf(", ");
        else printf("\n");
    }
     
    return 0;
}
     
/* 你的代码将被嵌在这里 */

Input sample:

89 100

Sample output:

89 is a prime number
90=7+83, 92=3+89, 94=5+89, 96=7+89, 98=19+79
100=3+97,

code:

int prime( int p )
{
    if(p<2) return 0;
    else{
    int i;
    int n=p;
    int l=1;
    for(i=2;i<=sqrt(p);i++)
    {
        if(n%i==0)
        {
            l=0;break;
        }
    }
    return l;}
}
void Goldbach( int n )
{
    int i;
    int temp;
    for(i=2;;i++)
    {
       temp=prime(i);
       if(temp==1)
       {
           int tt=n-i;
           if(prime(tt)) {printf("%d=%d+%d",n,i,tt);break;}
       }
    }
}

67. Exercise 6-6 Use a function to output the inverse of an integer


This question requires the implementation of a simple function to find the inverse of an integer.

Function interface definition:

int reverse( int number );

The function reverse must return the reverse sequence number of the integer number passed in by the user.

Sample referee test procedure:

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

Input sample:

-12340

Sample output:

-4321

code:

int reverse( int number )
{
    int l=1;
    if(number<0) {l=-1;number=-number;}
    int sum=0;
    int temp;
    while(number)
    {
       temp=number%10;
       sum=sum*10+temp;
       number/=10;
    }
    sum*=l;
    return sum;
}

68. Exercise 8-2 Calculate the sum and difference of two numbers


This question requires the implementation of a simple function that calculates the sum and difference of two input numbers.

Function interface definition:

void sum_diff( float op1, float op2, float *psum, float *pdiff );

Among them, op1 and op2 are the two input real numbers, and *psum and *pdiff are the calculated sum and difference.

Sample referee test procedure:

#include <stdio.h>
    
void sum_diff( float op1, float op2, float *psum, float *pdiff );
    
int main()
{
    float a, b, sum, diff;
    
    scanf("%f %f", &a, &b);
    sum_diff(a, b, &sum, &diff);
    printf("The sum is %.2f\nThe diff is %.2f\n", sum, diff);
     
    return 0; 
}
    
/* 你的代码将被嵌在这里 */

Input sample:

4 6

Sample output:

The sum is 10.00
The diff is -2.00

Reference Code:

void sum_diff( float op1, float op2, float *psum, float *pdiff )
{
  *psum=op1+op2;
  *pdiff=op1-op2;
}

69. Exercise 7-9 Calculate the number of days


This question requires writing a program to calculate the number of days in a certain year, month, and day in that year.

Input format:

The input gives the date on one line in the format "yyyy/mm/dd" (i.e. "year/month/day"). Note: The criterion for leap year is that the year is divisible by 4 but not by 100, or by 400. February in a leap year has 29 days.

Output format:

Output the date on one line as the day of the year.

Input example 1:

2009/03/02

Output sample 1:

61

Input example 2:

2000/03/02

Sample output 2:

62

code:

#include<stdio.h>

int fun(int yy,int mm)
{
    int temp=0;
    int sum=0;
    if((yy%4==0&&yy%100!=0)||(yy%400==0)) temp=1;
    int arr[13]={31,28,31,30,31,30,31,31,30,31,30,31};
    if(temp==1) arr[1]=29;
    int i;
    for(i=0;i<mm-1;i++)
    {
        sum+=arr[i];
    }
    return sum;
}
int main()
{
    int yyyy; //年
    int mm;  //月
    int dd;  //日
    scanf("%d/%d/%d",&yyyy,&mm,&dd);
    int temp=fun(yyyy,mm);
    printf("%d\n",temp+dd);
    return 0;

}

70. Exercise 7-10 Find the specified character


This question requires writing a program to find a specified character from a given string.

Input format:

The first line of input is a character to be searched for. The second line is a non-empty string (up to 80 characters) terminated by a carriage return.

Output format:

If found, output the maximum subscript corresponding to the character in the string according to the format "index = subscript" in one line (the subscript starts from 0); otherwise output "Not Found".

Input sample 1:

m programming

Output sample 1:

index = 7

Input sample 2:

a 1234

Output sample 2:

Not Found

code:

#include<stdio.h>
#include<math.h>
int main()
{
    char c;
    char str[81];
    scanf("%c",&c);
    getchar();
    gets(str);
    //printf("c==%c\n",c);
    //puts(str);
    int len=strlen(str);
    int i;
    int l=0;
    for(i=len-1;i>=0;i--)
    {
        if(str[i]==c)
        {
            l=1;break;
        }
    }
    if(l==0)printf("Not Found\n");
    else printf("index = %d\n",i);
    return 0;
}

71. Exercise 7-11 String reverse order


Input a string, reverse the string, and output the reversed string.

Input format:

input Gives a carriage return-terminated non-empty string of no more than 80 characters in length on one line.

Output format:

Output the reversed string in one line.

Input sample:

Hello World!

Sample output:

!dlroW olleH

code:

#include<stdio.h>
#include<math.h>
int main()
{
    char str[81];
    gets(str);
    int len=strlen(str);
    int i;
    for(i=len-1;i>=0;i--)
    {
        printf("%c",str[i]);
    }
    printf("\n");
    return 0;
}

72. Exercise 7-1 Selection method sorting


This question requires that the given n integers be sorted from largest to smallest and then output.

Input format:

Input the first line to give a positive integer n not exceeding 10. The second line gives n integers separated by spaces.

Output format:

Output the sequence of numbers ordered from largest to smallest in one line, there is a space between adjacent numbers, and there must be no extra spaces at the end of the line.

Input sample:

4
5 1 7 6

Sample output:

7 6 5 1

code:

#include<stdio.h>
int main()
{
  int n;
  int arr[11];
  int i,j,index,t;
  scanf("%d",&n);
  for(i=0;i<n;i++)
  scanf("%d",&arr[i]);
  for(i=0;i<n-1;i++)
  {
    index=i;
    t=arr[i];
    for(j=i+1;j<n;j++)
    {
      if(arr[j]>=arr[index]) index=j;
    }
    arr[i]=arr[index];
    arr[index]=t;
  }
  for(i=0;i<n;i++)
  {
    if(i==n-1) printf("%d\n",arr[i]);
    else printf("%d ",arr[i]);
  }
  return 0;
}

73. Exercise 7-2 Find the single-digit number that appears most in a batch of integers


Given a batch of integers, analyze each digit of each integer and find the single digit with the most occurrences. For example, given three integers 1234, 2345, and 3456, the numbers that appear most frequently are 3 and 4, both of which appear 3 times.

Input format:

Enter a positive integer N (≤1000) in the first line, and N non-negative integers not exceeding the integer range in the second line, and the numbers are separated by spaces.

Output format:

Output in one line according to the format "M: n1 n2 ...", where M is the maximum number of times, n1, n2, ... are the single-digit numbers with the most occurrences, arranged in ascending order. Numbers are separated by spaces, but must not have extra spaces at the end.

Input sample:

3
1234 2345 3456

Sample output:

3: 3 4

code:

#include<stdio.h>

void fun(int*arr,int temp)
{
    while(temp)
    {
        arr[temp%10]++;
        temp/=10;
    }
}
int main()
{
    int N;
    scanf("%d",&N);
    int i;
    int temp;
    int arr[10]={0};
    for(i=0;i<N;i++)
    {
        scanf("%d",&temp);
        fun(&arr,temp);
    }
    int index=0;
    for(i=1;i<10;i++)
    {
        if(arr[i]>=arr[index]) index=i;
    }
    printf("%d:",arr[index]);
    for(i=0;i<10;i++)
    {
        if(arr[i]==arr[index]) printf(" %d",i);
    }
    return 0;
}

74. Exercise 8-8 Move letters


This question requires writing a function to move the first 3 characters of the input string to the end.

Function interface definition:

void Shift( char s[] );

Among them, char s[] is the string passed in by the user, and the title guarantees that its length is not less than 3; the function Shift must store the transformed string as required in s[].

Sample referee test procedure:

#include <stdio.h>
#include <string.h>
    
#define MAXS 10
    
void Shift( char s[] );
    
void GetString( char s[] ); /* 实现细节在此不表 */
    
int main()
{
    char s[MAXS];
    GetString(s);
    Shift(s);
    printf("%s\n", s);
    return 0; 
}
    
/* 你的代码将被嵌在这里 */

Input sample:

abcdef

Sample output:

defabc

code:


void Shift( char s[] )
{
  int len=strlen(s);
  if(len==3) ;
  else
  {
      int i;
      char str[100];
      int t=0;
      for(i=0;i<3;i++)
      {
          str[i]=s[i];
      }
      for(i=3;i<len;i++)
      {
          s[t++]=s[i];
      }
      for(i=0;i<3;i++)
      {
          s[t++]=str[i];
      }
  }
}

75. Exercise 8-1 Split the integer and fractional parts of real numbers


This question requires the implementation of a simple function that splits the integer and fractional parts of a real number.

Function interface definition:

void splitfloat( float x, int *intpart, float *fracpart );

Where x is the split real number (0≤x<10000), intpart and fracpart are the integer part and fractional part of the real number x split.

Sample referee test procedure:


#include <stdio.h>
    
void splitfloat( float x, int *intpart, float *fracpart );
    
int main()
{
    float x, fracpart;
    int intpart;
    
    scanf("%f", &x);
    splitfloat(x, &intpart, &fracpart);
    printf("The integer part is %d\n", intpart);
    printf("The fractional part is %g\n", fracpart);
    
    return 0;
}
    
/* 你的代码将被嵌在这里 */

Input sample:

2.718

Sample output:

The integer part is 2
The fractional part is 0.718

code:

void splitfloat( float x, int *intpart, float *fracpart )
{
  *intpart=(int)x;
  *fracpart=x-(int)x;
}

76. Exercise 7-3 Judge the upper triangular matrix


The upper triangular matrix refers to the matrix whose elements below the main diagonal are all 0; the main diagonal is the connection from the upper left corner of the matrix to the lower right corner.

This question requires writing a program to determine whether a given square matrix is ​​upper triangular or not.

Input format:

Input the first line to give a positive integer T, which is the number of matrices to be tested. Next, the information of T matrices is given: the first line of each matrix information gives a positive integer n not exceeding 10. Followed by n lines, each line gives n integers separated by spaces.

Output format:

The judgment result of each matrix occupies one row. If the input matrix is ​​an upper triangular matrix, output "YES", otherwise output "NO".

Input sample:

2
3
1 2 3
0 4 5
0 0 6
2
1 0
-8 2

Sample output:

YES
NO

code:

#include<stdio.h>

int fun(int arr[][11],int n)
{
    int i,j;
    int l=1;
    for(i=1;i<n;i++)
    {
        for(j=0;j<i;j++)
        {
            if(arr[i][j]!=0){l=0;break;}
        }
        if(l==0) break;
    }
    return l;

}
int main()
{
    int T,n;
    scanf("%d",&T);
    int t,i,j;
    int k=0;
    int arr[11][11];
    int temp[11];
    for(t=1;t<=T;t++)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                scanf("%d",&arr[i][j]);
            }
        }
        int t=fun(&arr,n);
        temp[k++]=t;
    }
    for(i=0;i<k;i++)
        if(temp[i]==1)printf("YES\n");
    else printf("NO\n");
    return 0;
}

77. Exercise 7-4 Find the sum of elements in each row of the matrix


This question requires writing a program to find the sum of elements in each row of a given m×n matrix.

Input format:

The first line of input gives two positive integers m and n (1≤m, n≤6). Then there are m lines, each line gives n integers, during which

separated by spaces.

Output format:

Each row of output corresponds to the sum of matrix row elements.

Input sample:

3 2
6 3
1 -8
3 12

Sample output:

9
-7
15

code:

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

78. Exercise 7-6 Statistics of uppercase consonants


English consonants are letters other than A, E, I, O, and U. This question requires writing a program to count the number of uppercase consonants in a given string.

Input format:

Input Gives a string of no more than 80 characters on one line, terminated by a carriage return.

Output format:

The output gives the number of uppercase consonants in the string on one line.

Input sample:

HELLO World!

Sample output:

4

code:

#include<stdio.h>
int main()
{
    char str[81];
    gets(str);
    int len=strlen(str);
    int i;
    int temp=0;
    for(i=0;i<len;i++)
    {
        if(str[i]>='A'&&str[i]<='Z')
        {
            if(str[i]!='A'&&str[i]!='E'&&str[i]!='I'&&str[i]!='O'&&str[i]!='U')
                temp++;
        }
    }
    printf("%d\n",temp);
    return 0;
}

79. Exercise 3-5 Triangle judgment


The coordinates of any three points on the given plane https://mmbiz.qlogo.cn/mmbiz_svg/aXUpZVUYfjyXo3UOYGdVXn48LBlzxkTNiaYDbVyavKvnmlO1B7iaDCtSJEIOrRYXo7XShsXaOScJY3UGb13QdX7aUplKiaojNRM/0?wx_f mt=svg , check if they form a triangle.

Input format:

The input gives six numbers in the range [−100,100] sequentially in one line, that is, the coordinates of three points.

Output format:

If these 3 points cannot form a triangle, output "Impossible" in one line; if possible, output the perimeter and area of ​​the triangle in one line, the format is "L = perimeter, A = area", output to the decimal point last 2 digits.

Input example 1:

4 5 6 9 7 8

Output sample 1:

L = 10.13, A = 3.00

Input example 2:

4 6 8 12 12 18

Sample output 2:

Impossible

code:


#include<stdio.h>
#include<math.h>
double fun(double x1,double y1,double x2,double y2)
{
    double temp1=x1-x2;
    double temp2=y1-y2;
    return sqrt(temp1*temp1+temp2*temp2);
}
int main()
{
    double x1,y1;
    double x2,y2;
    double x3,y3;
    scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3);
    double lon1=fun(x1,y1,x2,y2);
    double lon2=fun(x2,y2,x3,y3);
    double lon3=fun(x3,y3,x1,y1);
    if(lon1+lon2>lon3&&lon1+lon3>lon2&&lon2+lon3>lon1)
    {
        double p=(lon1+lon2+lon3)/2.0;
        double A=sqrt(p*(p-lon1)*(p-lon2)*(p-lon3));
        printf("L = %.2lf, A = %.2lf\n",lon1+lon2+lon3,A);
    }
    else printf("Impossible\n");
    return 0;
}
//定义:x1,y1,----x3,y3时,类型double int 出错

80. Exercise 4-3 Find the partial sum of a simple interleaved sequence with a given precision


This question requires writing a program to calculate the sequence part and 1 - 1/4 + 1/7 - 1/10 + ... until the absolute value of the last item is not greater than the given precision eps.

Input format:

The input gives a positive real number eps in one line.

Output format:

Output the value S of the partial sum in the format "sum = S" in one line, accurate to six decimal places. The title guarantees that the calculation result does not exceed the double precision range.

Input example 1:

4E-2

Output sample 1:

sum = 0.854457

Input example 2:

0.02

Sample output 2:

sum = 0.826310

code:

#include<stdio.h>
int main()
{
    double exp;
    scanf("%lf",&exp);
    int i;
    int l=1;
    double sum=0;
    double t;
    for(i=1;;i+=3)
    {
        t=1.0/i*l;
        sum+=t;
        if(fabs(t)<=exp) break;
        l=-l;
    }
    printf("sum = %.6lf\n",sum);
    return 0;
}

Guess you like

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