当前位置:网站首页>Screenshot of the answers to C language exercises

Screenshot of the answers to C language exercises

2022-06-26 16:36:00 TUSTer_

One dimensional array and two dimensional array

The first 1 Turn off   Scheduling problem

#include<stdio.h>
int main()
{
    /*********Begin*********/
 int a[10];
 int i,j,t;
 printf("\n");
 for (i=0;i<10;i++) scanf("%d",&a[i]);
 printf("\n");
 for (j=0;j<9;j++)
     for(i=0;i<9-j;i++)
         if(a[i]>a[i+1])
         {
             t=a[i];a[i]=a[i+1];a[i+1]=t;
         }
 printf("\n");
 for(i=9;i>=0;i--) printf("%d ",a[i]);
 printf("\n");
 return 0;
}

The first 2 Turn off   Look up integers  

#include<stdio.h>
int a[1009];

int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++)
        scanf("%d", &a[i]);
    int pre;
    scanf("%d", &pre);
    for(int i = 1; i <= n; i ++)
    {
        if(a[i] == pre)
        {
            printf("%d", i);
            return 0;
        }
    }
    printf("-1");
    return 0;
}

The first 3 Turn off   Calculate the maximum value of the element in the array and its row and column subscript value

#include<stdio.h>
int a[1000][1000];
int main()
{
    printf("Input m, n:");
    int n, m;
    scanf("%d,%d", &n, &m);
    printf("Input %d*%d array:\n", n , m);
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= m; j ++)
            scanf("%d", &a[i][j]);
    int MAX = -999999;
    int r, c;
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= m; j ++)
            if(a[i][j] > MAX)
            {
                MAX = a[i][j];
                r = i;
                c = j;
            }
    printf("max=%d, row=%d, col=%d", MAX, r, c);
    return 0;
}

  The first 4 Turn off   Delete maximum

#include<stdio.h>
int main()
{
    int a[10];
    int MAX = -1;
    for(int i = 0; i < 10; i ++)
        scanf("%d", &a[i]);
    for(int i = 0; i < 10; i ++)
    {
        if(a[i] > MAX) MAX = a[i];
    }
    for(int i = 0; i < 10; i ++)
    {
        if(a[i] == MAX) continue;
        printf("%d ", a[i]);
    }
    return 0;
}

The first 5 Turn off   Yang hui triangle  

#include<stdio.h>
int a[10][10];

int main()
{
    int num = 1;
    for(int i = 0; i < 10; i ++) a[i][0] = 1;
    for(int i = 1; i < 10; i ++)
    {
        for(int j = 1; j < 10; j ++)
        {
            if(j == num)
            {
                num ++;
                a[i][j] = 1;
                break;
            }
            a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
        }
    }
    num = 1;
    for(int i = 0; i < 10; i ++)
    {
        for(int j = 0; j < 10; j ++)
        {
            if(j == num)
            {
                num ++;
                break;
            }
            if(j == num - 1) printf("%d", a[i][j]);
            else printf("%d ", a[i][j]);
        }
        printf("\n");
    }
    return 0;
}

The use of three loop statements  

The first 1 Turn off   Fractional sequence and

#include <stdio.h>
int main( )
{   
	int a, b, c, k, n = 5;
	double s;
	s = 0.0; a = 2; b = 1;
	
	for ( k = 1; k <= n; k++ ) 
	{
		
	/***** There is an error in the following line *****/
		s = s + (double)a / (double)b;
		
	/***** There is an error in the following line *****/
		c=a;a=b+c;b=c;
		
	} 
	printf( "s%d = %lf\n", n, s); 
	
	return 0;
}

  The first 2 Turn off   Formula calculation (1)

#include<stdio.h>
int main()
{
    int n,a=0;
    double s;
   
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        a=a+i;
        s=s+1/(double)a;
    }
    printf("s = %lf",s);
    return 0;
}

  The first 3 Turn off   Formula calculation (2)

/********** Begin **********/
#include<stdio.h>
#include<math.h>
int main()
{
    int m;
    scanf("%d",&m);
    double log(double x);
    double s=0;
    for(double i=1;i<=m;i++)
    {
        s=s+log(i);
    }
    s=sqrt(s);
    printf("s = %lf",s);
    return 0;
}

  The first 4 Turn off   Ask to be at the same time 7 or 11 Integers divided by integers

/********** Begin **********/
#include<stdio.h>
int main()
{
    int x;
    for(int i=1;i<=1000;i++)
    {
        if (i%7==0||i%11==0)
        {
            if(i%7==0&&i%11==0)
            {

            }
            else
            {
                x++;
                printf("%-5d",i);
            }
        }
        if(x==8)
        {
           printf("\n");
           x=0;
        }
    }
    return 0;
}

The first 5 Turn off   Formula calculation (3) 

/********** Begin **********/
#include<stdio.h>
int main()
{
    int n;
    double s=1,sum=1;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        sum=sum*i;
       s=s+1/sum;
    }
    printf("s = %.6lf",s);
    return 0;
}

Cyclic structure programming 1 

The first 1 Turn off   Small ball free fall movement

#include<stdio.h>
#include<math.h>
	int main(void)
	{  
	  /*********Begin*********/
	  float M,N,sum,high,x;
	  scanf("%f",&M);
	  scanf("%f",&N);
	  int i;
	  sum=M;
	  for (i=1;i<N;i++)
	  {
		  high=pow(0.5,i)*M;
		  x=pow(0.5,i)*M;
		  sum=sum+high+x;

	  }
	  high=pow(0.5,N)*M;
	  sum=sum;
	  printf("%.2f %.2f",high,sum);

	  
	  /*********End**********/ 
       return 0;
	}

  The first 2 Turn off   Find out n All can be 5 The product of positive integers divided by integers

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	  int N ,i,sum=1;
	  scanf("%d",&N);
	for(i=1;i<=N;i++)
	{
		if(i%5==0)
		{
			sum=sum*i;
		}
	}
	printf("%d",sum);
	  
	  /*********End**********/ 
       return 0;
	}

  The first 3 Turn off   The greatest common divisor and the least common multiple

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	  
	  int a,b,c,m,t;
	  scanf("%d%d",&a,&b);
	  if(a,b)
	  {
		  t=a;a=b;
		  b=t;
	  }
	  m=a*b;
	  c=a%b;
	  while(c!=0)
	  {
		  a=b;
		  b=c;
		  c=a%b;
	  }
	  printf(" The greatest common divisor is :%d\n",b);
	  printf(" The minimum common multiple is :%d",m/b);


	  /*********End**********/ 
       return 0;
	}

  The first 4 Turn off   Statistics of the number of various characters in the string

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	  char m;
	  int a=1;
	  int k=0,s=0,e=0,n=0;
	  while(a)
	  {
		  m=getchar();
		  if(m==' ')
		  k+=1;
		  else if((m>='a'&& m<='z')||(m>='A'&&m<='Z'))
		  s+=1;
		  else if(m>='0'&&m<='9')
		  n+=1;
		  else
		  e+=1;
		  if(m=='\n')
		  a=0;
	  }
	  printf("%d %d %d %d",s,n,k,e-1);
	  /*********End**********/ 
       return 0;
	}

  Sequential structure programming - Advanced

The first 1 Turn off   Digital separation

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	  int n, a, b, c;
      scanf("%d",&n);
      a=n%10;// The bits of this number 
      n/=10;
      b=n%10;// Ten digits of this number 
      n/=10;
      c=n;// The hundredth of this number 
      printf("%d %d %d",c,b,a);
	  
	  /*********End**********/ 
       return 0;
	}

  The first 2 Turn off   Find the area of the triangle

#include<stdio.h>
#include<math.h>// It's important here , Header file to add mathematical knowledge , Because the square function is called 
	int main(void)
	{  
	  /*********Begin*********/
	  double a,b,c;
      scanf("%lf %lf %lf",&a,&b,&c);
      double area;
      double s=(a+b+c)/2;
      area = s*(s-a)*(s-b)*(s-c);
      area = sqrt(area);// Notice that the square function is used here sqrt
      printf("%.3f",area);
	  
	  /*********End**********/ 
       return 0;
	}

  The first 3 Turn off   Calculate the greatest common divisor of two positive integers

#include<stdio.h>

int MaxCommonFactor( int a, int b)
{ 
   int c; 
   if(a<=0||b<=0) 
      return -1; 
   while(b!=0)
   { 
     c=a%b; 
     a=b;
     b=c;
   } 
  return a; 
}   
int main(void)
	{  
	  /*********Begin*********/
	  int a,b;
      scanf("%d,%d",&a,&b);
      int c=MaxCommonFactor(a,b);
      printf("%d\n",c);
	  
	  /*********End**********/ 
    return 0;
}

Sequential structure programming - Basics

The first 1 Turn off   Addition operation

#include<stdio.h>
	int main(void)
	{  
	  int a,b,c; 
      //Please input a,b:
	  /*********Begin*********/
	  scanf("%d,%d",&a,&b);
	  c=a+b;
	  printf("%d+%d=%d\n",a,b,c);
	  
	  /*********End**********/ 
       return 0;
	}

The first 2 Turn off   Don't use the second 3 A variable , Realize the exchange of two numbers

#include<stdio.h>
	int main(void)
	{  
	  int a,b;
	  //Enter a and b:
      scanf("%d%d",&a,&b); 
      printf("a=%d b=%d\n",a,b);
	  /*********Begin*********/
	  a=a^b;
	  b=a^b;
	  a=a^b;
	  /*********End**********/
	  printf("a=%d  b=%d\n",a,b);  
       return 0;
	}

  The first 3 Turn off   Define constants with macros

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	  const int N=30;// Define constants with const, That is, the price in the definition is 30.
      int x;
      scanf("%d",&x);
      printf("%d",x*N);
	  
	  /*********End**********/ 
       return 0;
	

The first 4 Turn off   Calculate the total score and average score

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	  int a,b,c,d,e;
      scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
      int s=a+b+c+d+e;//s Represents the total score 
      double p=s*1.0/5;//double  Used to define floating point numbers ( decimal ),p Represents the average score 
      printf("%d %.2f",s,p);//%.nf For output n Decimal place 
	  
	  /*********End**********/ 
       return 0;
	}

The first 5 Turn off   Solid geometry calculation problem

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	  const double PI=3.14;// Define constants pi
      double r, h, c, s, v;
      scanf("%lf,%lf",&r,&h);// Note that there are commas between the inputs 
      c=2*PI*r;
      s=4*PI*r*r;
      v=PI*r*r*h;
      printf("C1=%.2f\n",c);// Keep two decimal places 
      printf("Sb=%.2f\n",s);
      printf("Vb=%.2f\n",v);

	  /*********End**********/ 
       return 0;
	}

Character array and its string application

1 Target deletion

#include "stdio.h"
#include "string.h"

int main()
{ 
	char s[80];
	int i,j;
	
	scanf("%s",s);
	printf("The original string: \n");
	puts(s);
	
	/*****  There is an error in the following line  *****/
	for(i=j=0;s[i]!='\0';i++)
	{
		if(s[i]!= 'c') 
			
	/*****  There is an error in the following line  *****/
			s[j++]=s[i];


	}
	
	/*****  There is an error in the following line  *****/
	s[j]='\0';
	
	printf("The string after deleted: \n"); 
	puts(s);
	
	
	return 0;
}

  The first 2 Turn off : Remove whitespace

#include<stdio.h>
int main(){
    int i=0,j=0,k;
	char str[30];
	scanf("%[^\n]",str);
for(k=0;k<29;k++)// Loop maximum number of spaces  
{
	j=0;
for(i=0;i<30;i++)//  Maximum number of words  
{
	if((str[i]==32&&str[i+1]!=32)||(str[i]==32&&str[i+1]==32))// Judge the space 
	{ 
        j=1;
	}
	if(j==1)// Move the following characters forward 
	{
		str[i] = str[i+1];
	
	}
}
}

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

The first 3 Turn off : String manipulation

#include <stdio.h>
int main() {
	int i = 0, j = 0;
	char s[20], t[20];
	scanf("%s", s);
	for (i = 0; i <= 20; i++) {
		if (i % 2 != 0 || s[i] % 2 == 0)// Characters in odd positions or ASCII Characters with even code 
			t[j++] = s[i];
	}
	printf("%s", t);
	return 0;
}

The first 4 Turn off : Find the longest string

/********** Begin **********/


#include <stdio.h>
#include <string.h>
int main()
{
    int N;
    char s[100][100];
    int i;
    int max_i, max_len = 0;
    scanf("%d",&N);
		for(i = 0; i < N; i ++)
    {
    	scanf("%s",s[i]);// Input 
	}
    for(i = 0; i < N; i ++)
    {
        int l = strlen(s[i]);
        if(max_len <l) 
        {
            max_len = l;
            max_i = i;
        }
    }
	
	printf("%s\n", s[max_i]);// Output the longest string 
    return 0;
}

  The first 5 Turn off : Word order

#include <stdio.h>
#include <string.h>
int main()
{
//she its can ibm1 bbc NBA nhk2 BOY jxf 
//c python java c++ android caffe keras pytorch php 
    int N,i,k;
    char j,s[100][100],a[100][100];
    printf(" String before sorting :\n");
	for(i=0;i<10;i++)
    {
    	scanf("%s",s[i]);// Input 
		printf("%s",s[i]);
		if(i<9)
        printf("\n");
    	fflush(stdout);
	}

	for(i=0;i<9;i++)
	{
		k=i;
		for(j=i+1;j<10;j++)
		{
			if(strcmp(s[k],s[j])>0)
			k=j;
		}
		if(k!=i)
		{
			char temp[100];
            strcpy(temp, s[k]);
            strcpy(s[k], s[i]);
            strcpy(s[i], temp);
		}
	}
printf(" Sorted string :");
for(i=0;i<10;i++)
{
	printf("%s\n",s[i]);
} 	
    return 0;
}

  Choose structural programming

The first 1 Turn off   Sort

#include<stdio.h>
	int main(void)
	{ 
	int a,b,c,temp1,temp2,temp3;
	  /*********Begin*********/
	printf(" Please enter three integers :");
    scanf("%d%d%d",&a,&b,&c); 
	if(a>b)
	{
	temp1=a;
	a=b;
	b=temp1;
	}
	if (a>c)
	{
	temp2=a;
	a=c;
	c=temp2;
	}
	if (b>c)
	{
	temp3=b;
	b=c;
	c=temp3;
	}
	  printf(" From small to large, the order is :%d,%d,%d",a,b,c);
	  /*********End**********/ 
       return 0;
	}

The first 2 Turn off   Selection structure - Leap year judgment

#include<stdio.h>
	int main(void)
	{
	int a;  
	  /*********Begin*********/
	scanf("%d",&a);
	if (a%4!=0)
	printf("%d  It's not a leap year !",a);
	else if (a%100==0&&a%400!=0)
	printf("%d  It's not a leap year !",a);
	else 
	printf("%d  It's a leap year !",a);
	  /*********End**********/ 
       return 0;
	}

  The first 3 Turn off   Selection structure - Piecewise function problem

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	float x;
	float a;
	scanf("%f",&x);
	if (x<0&&x!=-3)
	{
	a=x*x+x-6;
	printf("%.3f",a);
	}
	else if (x>=0&&x<10&&x!=2&&x!=3)
	{
	a=x*x-5*x+6;
	printf("%.3f",a);
	}
	else
	{
	a=x*x-x-1;
	printf("%.3f",a);
	}
	
	  /*********End**********/ 
       return 0;
	}

  The first 4 Turn off   Student grade conversion

#include<stdio.h>
	int main(void)
	{  
	float x;
	  /*********Begin*********/
	scanf("%f",&x);
	if (x>=90&&x<=100)
	{
	printf("A");
	}
	if (x<90&&x>=80)
	{
		printf("B");
	} 
	if (x>=70&&x<79)
	{
		printf("C");
	}
	if (x>=60&&x<69)
	{
		printf("D");
	}
	if (x<60&&x>=0)
	{
		printf("E");

	}
	if (x<0||x>100)
	{
		printf("Score is error!");
	}
	  /*********End**********/ 
       return 0;
	}

  Cyclic structure programming 2

The first 1 Turn off  C loop - For the average score

#include<stdio.h>
int main()
{
    int s;
    float x,sum,average;
    sum=0;
    scanf("%d",&s);
    if(s>0)
    {
        for(int i=0;i<s;i++)
        {
            scanf("%f",&x);
            sum=sum+x;
        }
        average=sum/s;
        printf("the number of students:the scores:average=%.2f",average);
	}
        else
        printf("the number of students:the scores:average=0.00");
        return 0;
    
}

The first 2 Turn off  C loop - Find the product of your numbers

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	  int num=0,sum=1;
	  scanf("%d",&num);
	  while(num!=0)
	  {
		  sum*=num%10;
		  num/=10;
	  }
	  printf("%d",sum);
	  /*********End**********/ 
       return 0;
	}

The first 3 Turn off  C loop - Find the sum of factorials  

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	  int n=0,sum=0;
	  int m=1;
	  scanf("%d",&n);
	  for(int i=1;i<=n;i++)
	  {
		  for (int j=1;j<=i;j++)
		  {
			  m=m*j;
		  }
		  sum+=m;
		  m=1;
	  }
	  printf("%d",sum);
	  /*********End**********/ 
       return 0;
	

The first 4 Turn off  C loop - Narcissistic number

#include<stdio.h>
#include<math.h>
	int main(void)
	{  
	  /*********Begin*********/
	  int a=0,b=0,c=0,m=0,t=0;
	  scanf("%d",&t);
	  for(int i=100;i<=999;i++)
	  {
		  a=i/100;
		  b=(i-100*a)/10;
		  c=i-100*a-10*b;
		  m=pow(a,3)+pow(b,3)+pow(c,3);
		  if (m==i)
		  printf("%d ",i);
	  }

       return 0;
	}

The first 5 Turn off  C loop - Find the perfect number

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	  int n=0;
	  scanf("%d",&n);
	  int i,j,sum;
	  for (i=1;i<=n;i++)
	  {
		  sum=0;
		  for(j=1;j<i;j++)
		  {
			  if(i%j==0)
			  sum+=j;
		  }
		  if(sum==i)
		  printf("%d\n",i);
	  }
	  
	  /*********End**********/ 
       return 0;
	}

The first 6 Turn off   Sum fractions

#include<stdio.h>
#include<math.h>
	int main(void)
	{  
	  /*********Begin*********/
	  double a=1,b=-1;
	  double sum=0;
	  for(int i=1;i<=100;i++)
	  {
		  sum+=(a/i)*pow(b,i+1);
	  }
	  printf("%.3lf",sum);
	  
	  /*********End**********/ 
       return 0;
	}

C Introduction to basic grammar of language

The first 1 Turn off   Find the absolute value

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

int main()
{
    int a,b;
    scanf("%d",&a);
    if (a<0)
        b=-a;
    else b=a;
    printf("%d\n",b);
    return 0;
}

  The first 2 Turn off   seek m Sum of odd numbers within

#include <stdio.h>
int main()
{
    int n,m, sum;
    scanf("%d",&m);
    n=1;
    sum=0;
    while(n<=m)
    {
      sum=sum+n;
      n+=2;
    }
    printf("%d\n",sum);
    return 0;
}

The first 3 Turn off   Exclude this number or output this number

#include <stdio.h>
int main()
{
	// Get parameter method  scanf
    //int x  =0;
    //int y = 0;
    //scanf("%d", &x);

    // The result output uses prinf
    //printf("%d",x);

 	//  Please add your code here 
    /********** Begin *********/
    int x;
    scanf("%d",&x);
    if(x%3==0||x/100==3)
    {
    printf("%d",x);
    }
    else
    printf(" Exclude this number ");
    return 0;


	/********** End **********/

}

  The first 4 Turn off   Hospital charges .

#include <stdio.h>

int main()
{
	float a,b,c,d,e,f;
    scanf("%f %f %f %f",&a,&b,&c,&e);
    d = a + b*c;
    f = e - d;
    printf(" Please enter the drug fee 、 Bed fee / God 、 Length of stay : Accounts payable :%.2f\n Actual payment : Change is required :%.2f",d,f);
    return 0;

}

  The first 5 Turn off   Recombine in reverse order

// Be careful 
    //1: The time of each run of the program must be less than 200 millisecond , Otherwise it will time out , Program timeouts will not test the remaining test sets 
    //2: The memory used for each run of this program cannot exceed 1M, Otherwise, an error will be returned 
    //3: The maximum output of the program per run is 1000 Characters ( Superfluous does not show ), All spaces at the end of each line are marked with □ Express 
#include <stdio.h>
int main()
{
	int a,b,c,d,e,f,g,h;
    scanf("%4d",&a);
    b = a/1%10;
    c = a/10%10;
    d = a/100%10;
    e = a/1000%10;
    f = a/10000%10;
    g = a/100000%10;
    h = a/1000000%10;
    if(a>=1)
    {
        printf(" Please enter a 4 An integer : After regrouping :%d%d%d%d",b,c,d,e);
    }

    return 0;
}


C Language programming, editing and debugging environment

The first 1 Turn off   Printout Hello World

#include<stdio.h>
	int main()
	   {  
	/********* Begin *********/
		printf("Hello World");
	
	/********* End *********/
        return 0;
	   }

  The first 2 Turn off   Print out graphics

#include<stdio.h>
	int main(void)
	{  	
	/********* Begin *********/
	   printf("    *\n");
	   printf("   ***\n");
	   printf("  OK\n");
	   printf(" Hello!\n");
	   printf("*********\n");
       return 0;
	}

  The first 3 Turn off   seek 3 The maximum number of

#include<stdio.h>
	int main()
	{  
	/********* Begin *********/
	 int max(int x,int y,int z);
	 int a,b,c,f;
	 scanf("%d,%d,%d",&a,&b,&c);
	 f=max(a,b,c);
	 printf("max=%d",f);
	/********* End *********/
     return 0;
	}
	int max(int x,int y,int z)
	{
	 int e;
	 if(x>y)e=x;
	 else e=y;
	 if(e>z)e=e;
	 else e=z;
	 return e;
	}

  The first 4 Turn off   be familiar with C Language debugging process

#include<stdio.h>  
int main(void)  
{int  x;  
    int y=2,z=3;  
    scanf("%d",&x);   
    if(x==y+z)  
       printf("*****");  
    else    
       printf("#####" );  
   return 0;  
}  

The application of function

The first 1 Turn off : take a square root

#include <stdio.h>
#include <math.h>
 
/*****  There is an error in the following line  *****/
double fun(double a, double x0)
{
	double x1, y;
	x1=(x0+a/x0)/2.0;
	
/*****  There is an error in the following line  *****/
	if(fabs(x1-x0)>=0.00001)
		y=fun(a,x1);
	else 
		y=x1;
	return y;
}
 
int main()
{ 
	double x;
	scanf("%lf",&x);
	printf("The square root of %lf is %lf\n",x,fun(x,1.0));
	
	return 0;
}

The first 2 Turn off : Judgement primes

Find the greatest common divisor and common multiple  

  Function processing group

 

原网站

版权声明
本文为[TUSTer_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261619434182.html