C language must read a few examples, understand after benefit

Source: Internet
Author: User

1. Find the sum of the first 5 items of SN=A+AA+AAA+AAAA+AAAAA, where a is a number, for example: 2+22+222+2222+22222


#include <stdio.h>

#include <stdlib.h>

int main ()

{

int i = 0;

intj = 0;

int a = 0;

int sum = 0;

printf ("Please enter a number (single digit):");

scanf_s ("%d", &i);

A = i;

for (j = 1; j < 6; J + +)

{

sum + = i;

i = i * ten + A;

}

printf ("%d", sum);

System ("pause");

return 0;

}


2.

Write a program that reads the C source code from the standard input (terminal) and verifies that all curly braces are correctly paired.

For example:

Enter: {} {}, match successfully.

Input: {}{{}}, successful match

Input: {}}{, match unsuccessful

Input:}}{{, match unsuccessful

Input: {}}, match unsuccessful

Input:}{}{{, match unsuccessful

#include <stdio.h>

#include <stdlib.h>

int main ()

{

int cou = 0;

Char ch;

while ((ch = getchar ())! = ' \ n ')

{

if (ch = = ' {')

cou++;

else if (ch = = '} ')

{

if (cou = = 0)

printf ("Match not successful!");

cou--;

}

}

if (cou = = 0)

printf ("Match success! ");

Else

printf ("The match is unsuccessful!") ");

System ("pause");

return 0;






3. Write the binary lookup function.

#include <stdio.h>

#include <stdlib.h>

int main ()

{

int a[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

intmin = 0, max = ten, Mid, N;

printf ("Please enter the number you are looking for: \ n");

scanf_s ("%d", &n);

while (min + 1! = max)

{

Mid = (min + max)/2;

if (N>a[mid]) min = mid;

else if (N<a[mid]) max = mid;

Else

{

printf ("Number of inputs in%d bits of the series \ n", mid); Break

}

}

if (n = = A[max]) printf ("Number of inputs in%d bits of the sequence \ n", max);

else if (n = = A[min]) printf ("Number of inputs in%d bits of the sequence \ n", min);

else if (n! = A[mid]) printf ("Number of inputs not in the sequence");

System ("pause");

return 0;

}


4. Write a program that reads several lines of input from the standard input. Each line of input is printed to the standard output, preceded by a line number.

There is no limit to the length of the input lines that the program can handle when writing this program.


#include <stdio.h>

#include <stdlib.h>

int main ()

{

int n = 1, at_beginning = 1;

Char ch;

while (1)

{

printf ("Please input the line:\n");

Do

{

scanf_s ("%c", &ch);

if (at_beginning = = 1)

{

at_beginning = 0;

printf ("%d", n);

}

printf ("%c", ch);


} while (ch! = ' \ n ');

at_beginning = 1;

n++;

}

System ("pause");

return 0;

}


5. Compile such a program, the user input 10 integers, the program to find the maximum and minimum value

#include <stdio.h>

#include <stdlib.h>

int main ()

{

int a[10], I, Max, Min;

for (i = 0; i<10; i++)

{

printf ("num%d=", i + 1);

scanf_s ("%d", &a[i]);

}

max = a[0]; min = a[0];

for (i = 0; i<10; i++)

{

if (a[i]>max) max = A[i];

if (a[i]<min) min = a[i];

}

printf ("max=%d\n", Max);

printf ("min=%d\n", min);

System ("pause");

return 0;

}



6. Write the program solution:

There are 1, 2, 3, 4 digits, how many different and no repetition of the number of three digits? Print it out?


#include <stdio.h>

#include <stdio.h>

Main ()

{

int I, j, K;

int m = 0;

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

for (j = 1; j<5; j + +)

for (k = 1; k<5; k++)

{

if (i! = j&&k! = j&&i! = k)

{

printf ("%d%d%d\n", I, J, K);

m++;

}

}

printf ("A total number of:");

printf ("%d\n", m);

System ("pause");

return 0;

}


This article is from "Sherry Wang" blog, please be sure to keep this source http://940814wang.blog.51cto.com/10910665/1722621

C language must read a few examples, understand after benefit

Related Article

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.