C Language Programming exercises (i)

Source: Internet
Author: User
Tags ticket

Question one:

Problem Description: Enter n number, n<=100, find the smallest and largest number

Input Sample: 4

1 2 3 4

Output Sample: 14

1#include"stdio.h"2 intMain () {3     intI=0; 4     intmax=0, min=0; 5     intn=0; 6     inta[ -]; 7scanf"%d",&n);8      for(i=0; i<n;i++){9scanf"%d",&a[i]);Ten     } Onemax=min=a[0]; A      for(i=1; i<n;i++){ -         if(max<=A[i]) { -max=A[i]; the         } -         if(min>=A[i]) { -min=A[i]; -         } +     } -printf"%d%d", Min,max); +}

Question two:

Problem Description: The number of N and

Input Sample: 4

1 2 3 4

Output Sample: 10

1#include"stdio.h"2 intMain () {3     intI=0; 4     intb=0;5     intn=0; 6     inta[ -]; 7scanf"%d",&n);8      for(i=0; i<n;i++){9scanf"%d",&a[i]);Ten     } One      for(i=0; i<n;i++){ Ab+=A[i]; -     } -printf"%d", b); the     return 0; -}

Question three:

Problem Description: Given two floating-point numbers, please determine whether the two floating-point numbers are equal

Input Sample: 2.980000001 2.9800000000001

Output Example: Yes

1#include"stdio.h"2 intMain () {3     floatA=0; 4     floatb=0; 5scanf"%f",&a);6scanf"%f",&b);7     if(a==C) {8printf"Yes");9     }Ten     Else{ Oneprintf"No"); A     } -     return 0; -}

Question four:

Problem Description: Retain two decimal places to output a floating-point number.

Input Sample: 11

Output Sample: 11.00

1#include"stdio.h"2 intMain () {3     DoubleA=0; 4scanf"%LF",&a);5printf"%.2lf\n", a);6     return 0;7}

 

Question five:

Problem Description: Enter the original ticket price (positive integer 3 to 4 digits, Unit: yuan), and then enter the ticket discount rate (up to one digit after the decimal point). program calculates the actual price of the ticket after discount (unit: Yuan. The result of the calculation is to round the single digit to the 10-digit "element". The input is only a single row of two numbers (two spaces separated by a space), the first is an integer, the ticket price, the second integer or real number (as real, up to 1 digits after the decimal point) represents the discount rate.

Input Sample: 888 7

Output Sample: 620

1#include"stdio.h"2 intMain ()3 {4     intA=0;5     floatb=0;6     intC=0;7     intD=0;8     intE=0;9scanf"%d%f",&a,&b);TenC= (int) (a*b*0.1); OneD= (int) c%Ten; A     if(d>=5){ -E= (c/Ten+1)*Ten; -     } the     Else{ -E= (c/Ten)*Ten; -     } -printf"%d", E);  +     return 0; -  +}

  

Question six:

Description of the problem: give the string A and string B, and make sure B is a substring of a, and you output B where it first appears in a.

Input example: ABCD BC

Output Sample: 2

1#include <stdio.h>2#include <string.h>3 intMain ()4 {5     Charstr1[ -],str2[ -],*i,*J;6scanf"%s%s", STR1,STR2);7j=&str1[0];8I=strstr (STR1,STR2);9printf"%d", i-j+1);Ten     return 0; One}

Question seven:

Problem description:. Give an English sentence, I hope you turn over the word order in the sentence.

Input sample: I Love You

Output example: You love I

1#include <stdio.h>2#include <string.h>3 intMainvoid){4     Chara[ -];5     Charb[ -];6     intn,m,len=0;7 gets (a);8      for(n=0; N<=strlen (a)-1; n++){9len++;TenB[n]=a[strlen (a)-1-n]; One         if(A[strlen (a)-1-n]==' '){ A              for(m=n-1; m>n-len;m--)  -printf"%c", B[m]); -printf" ");  thelen=0;  -         } -     } -     if(len>0){ +          for(m=n-1; m>=n-len;m--){ -printf"%c", B[m]); +         } A     }  at     return 0; -}

(recently, when programming, logic and algorithms are the basic skills of a programmer.) We tend to value some of the tools of language, but ignore some of the underlying logic problems, resulting in the time to write code is unclear, affecting the quality of the code. So I decided to do some C language or Java programming exercises in my spare time, to exercise my logic of thinking. And synchronize the exercises in the blog, on the one hand, in order to encourage themselves, perseverance. Another convenience is to help the students who are about to get started or want to practice programming. )

C Language Programming exercises (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.