.


:




:

































 

 

 

 


3




 

/*prog11.c*/

 

#include<stdio.h>

 

main()

{

char x = A, y = 97;

printf ( %d, %c \n %d, % \n, x, x, y, y);

return 0;

}

 

, ASCII- 65, ASCII- 97 .

, int char, , . .

 

sizeof (_)

 

, .

 

/*prog12.c*/

 

#include<stdio.h>

 

main()

{

int x = 97;

char y = 97;

printf ( int %d, %d char %d, %d , x, sizeof(x), y, sizeof(y));

return 0;

}

 

printf - x, sizeof(x), y, sizeof(y) .

 

, char. ( ), . - - %s. .

 

/*prog13.c*/

 

#include<stdio.h>

 

main()

{

char str[20] = I am a student;

str[6] = 0;

printf(%s \n, str);

return 0;

}

 

, str. char, str . : str[6] = 0 str 6 0. : str , char. ; 0, 19. I am a student 11 14 , , , 15 , . 6 0, , . prog5.c, , 6 : I am a.

 

. str , .. str str[0].

 

, *.

 

/*prog14.c*/

 

#include<stdio.h>

 

main()

{

char *str = I am a student;

str[7]=S;

printf(%s \n, str);

return 0;

}

 

I am a Student. ,

 

char str[20];

 

char *str;

 

. , , .

 

prog9.c prog10.c , . = , . :

scanf(%s, str);

 

/*prog15.c*/

 

/* ( ), */

 

#include <stdio.h>

#define N 1000

 

main()

{

char str[ N ];

scanf (%s, str);

printf(%s \n, str);

return 0;

}

 

str prog15 . , . , , define, .

 

/*prog16.c*/

 

/* ( ), */

 

#include<stdio.h>

#define N 1000

 

main()

{

unsigned int i = 0, count = 0; /* i (), count , , */

 

char c, str[ N ];

scanf (%s, str);

 

c = str[ 0 ];

while (c!= 0)

count++;

 

printf(%u \n, count);

return 0;

}

 

 

, :

 

<9.1> ( ) , , ;

 

<9.2> ( ) , , ;

 

 

 

, . , . :

 

/*prog17.c*/

 

#include<stdio.h>

main(int argc, char* argv[])

{

printf( %d \n, argc);

if (argc > 1)

printf( - %s \n, argv[ 1 ]);

else

printf ( %s \n, argv[0]);

return 0;

}

 

main , . argc argv. argc int, . argc , . (Tab), , . , , argc .

 

printf( %d \n, argc);

 

argc. :

 

prog6.c aa bb cc

 

argc 4.

 

argv char - * []:

 

har* argv[];

 

* char, argv . , , (, ), , . ([]) (*) . , argv, char* - . , argv , .. , . . argv : argv[0] - , argv [1] .., .

 

printf( - %s \n, argv[1]);

 

- argv[1].

 

, . if (). , , arg > 1. , , , else , . , .

 

printf( %s

\n, argv[0]);

 

.

 

.

 

, , , ( ). .

 

 

. ; 0, 1, 2,.. m * n, m , n . , i- j- a [ i ] [ j ]; m * n, i = 0, 1,.., m - 1, j = 0, 1,, n - 1. i j a [ i ] [ j ].

:

 

/* matrix1

1000*1000; a [ 0 ] [ 0 ], */

 

# include<stdio.h>

# define M 1000

# define N 1000

 

main()

{

int a [ M ] [ N ]; //

int i = 0, j = 0;

scanf (%d,&(a [ i ] [ j ])); // [ i ] [ j ], i = j = 0

printf ( %d\n, a [ i ] [ j ]); // [ i ] [ j ], i = j = 0

return 0;

}

 

# define; i, j (, ).

 

. matrix1.

 

, , . , /* */; , //, .

 

, , . . for.

 

/* matrix2

, ; (, )*/

 

//

 

# include<stdio.h>

# define M 1000

# define N 1000

 

main()

{

//

int a [ M ] [ N ];

int m, n, i, j;

//

scanf ( %d %d, &m, &n);

//

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

for (j = 0; j < n; j++)

scanf ( %d, &(a [ i ] [ j ]));

//

for (j = 0; i < n; i++)

for (i = 0; j < m; i++)

printf( %d , a [ i ] [ j ]);

return 0;

}

 

matrix1, , 1000*1000. , m n, , , 1000. , . , m*n.

for (i = 0; i < m; i++) i 0, i < m. , , for. i 1 ( i++, ), i < m .. i < m , , for, , .

for!!!

, for, . ; , for (i = 0; i < m; i++) . , . .

 

, , , , 2-4 .

 

. matrix2

 

, , . , , \n

 

for (j = 0; j < m; j++)

{

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

printf (%d , a [ i ] [ j ]);

printf ( \n );

}

 

( %d printf , ).

 

. .

 

, for . : . {}.

 

, , . , , . mydata.txt,

 

3 4

1 2 3 4

4 3 1 2

17 18 19 20

 

, . matrix2; matrix2.exe. ,

 

matrix2.exe < mydata.txt

 

< , matrix2.exe mydata.txt

 

. , .

 

 

 

. , - , , .

 

: m * n; .

 

matrix1, matrix2 . matrix3

 

/* matrix3

, ;

*/

 

//

# include<stdio.h>

# define M 1000

# define N 1000

 

main()

{

//

int a [ M ] [ N ];

int m, n, i, j;

 

int buf;

//

 

scanf(%d % d,&m, &n);

// ,

 

//

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

for (j = 0; j < n; j++)

scanf (%d,& (a [ i ] [ j ]));

 

//

for (j = 0; j < n; j++)

 

{ buf = a [ 0 ] [ j ];

// buf

 

// j

a [ 0 ] [ j ] = a [ m ] [ j ];

a [ m ] [ j ] = buf;

}

 

// -

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

for (j = 0; j < n; j++)

printf (%d , a [ i ] [ j ]);

return 0;

}

 

. matrix3, .

 

-. a [ i ] [ j ] a [ ] [ j ], i 0, n - 1, i 1, 1 , I .

 

 

for (j = 0; j < n; j++)

for (i = 0; = n - 1; i < ; i++, --)

{ buf = a [ i ] [ j ];

a [ i ] [ j ] = a [ ] [ j ];

a [ ] [ j ] = buf;

}

 

, , , ; , for i 0, - n - 1. i 1, 1 ( --, ).

 

 

 

. , , 1 . .

 

. , a b, , a b.

 

( ) perfnum: - - perfect number.

 

. , , 6 : 6 = 1 + 2+ 3. , :

 

 

a=2 b=10

 

perfnum.txt

 

6.

 

 

1

 

/*perfnum1.c*/

/* , */

 

#include <stdio.h>

 

main()

{

int a,b;

scanf (" a=%d b=%d", &a, &b);

printf (" a=%d b=%d\n");

return 0;

}

 

. :

 

perfnum.txt > perfnum.exe

 

2

 

/*perfnum2.c*/

/* a,b, a b*/

 

#include <stdio.h>

 

main()

{

int a,b;

int j;

 

scanf (" a=%d b=%d", &a, &b);

printf ("divisors of a: \n")

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

if (a % j == 0)

printf (" %d ", j);

printf (" /n divisors of b: \n")

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

if (b % j == 0)

printf ("%d ", j);

return 0;

}

 

a 1, 2,..., a-1. a j , j a, . b.

 

 

3

 

/*perfnum3.c*/

/* a,b, , a b*/

 

#include <stdio.h>

 

 

main()

{

int a, b;

int i, j, s;

 

scanf (" a=%d b=%d", &a, &b);

 

for (i = a; i <= b; i++)

{ // , a b

 

s = 0; // s

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

if (i % j == 0) s+=j;

/* s i */

if (i == s)

printf ("%d ", i);

}

return 0;

}

 

, a b. . , , - , , -, , .. - . . , a=1, b , n. , , n, 1+2+...+n-1 : 1 n k- k-1 , k;1+2+...+n-1 = n * (n 1) / 2, , n * n .

 

, . '' - , .

 

, 255 . 1 , s 1; 255 - , , , . 255 3, 255 / 3 = 85; s = 1+ 3 + 85 +... C - 5 51: s = 1 + 3 + 85 + 5 + 51 +... - 15 17. 17 , , s = 1 + 3 + 85 + 5 + 51 + 15 + 17 = 177 < 255, 255 .

 

, 16: , 2 4, 16 / 2 = 8, 16 / 4 = 4, , .

 

, , , p q, q q / p, q, . :

 

4

 

/*perfnum4.c*/

/* a,b, , a b*/

 

#include <math.h> /* math.h */

 

#include <stdio.h> /* -*/

 

main()

{

int a, b;

int i, j, k,s;

int p, q;

float m;

 

scanf (" a = %d b = %d", &a, &b);

 

if (b < 6)

{

printf ("NO PERFECT NUMBERS\n");

return 0;

}

 

if (a % 2 ==0) // a

{ p = a; q = a+1; }

else

{ q = a; p = a +1; }

 

 

for (i = p; i <= b; i = i+2)

{ // , a b

 

s = 1; // s

m = sqrt (i) + 1; // i, i

 

 

for (j = 2; j <= m; j++)

if (i % j == 0)

{

s+= j;

 

if ((k = i / j) < j) s+= k;

}

/* s i */

 

if (i == s)

printf ("%d ", i);

}

 

 

for (i = q; i <= b; i = i + 2)

{ // , a b

 

s = 1; // s

 

m = sqrt (i) + 1; // i, i

 

for (j = 3; j <= m; j = j + 2) /* */

 

if (i % j == 0)

{

s+= j;

if ((k = i / j) < j) s+=k;

}

/* s i */

if (i == s)

printf ("%d ",i);

}

return 0;

}

 

, - m sqm. , , , (int)sqrt (a) + 1 m * m. i sqm, sqm 2 * m + 1 m . , m , i.

 

5

 

/*perfnum5.c*/

/* a,b, , a b*/

 

#include <math.h> /* */

#include <stdio.h> /* -*/

 

 

main()

{

unsigned int a,b;

unsigned int i, j, k,s;

unsigned int p,q;

unsigned int m, sqm;

 

scanf (" a = %d b = %d", &a, &b);

 

if (b < 6)

{

printf ("NO PERFECT NUMBERS\n");

return 0;

}

if (a % 2 ==0) // a

{ p=a; q=a+1; }

else

{ q=a; p=a+1; }

 

m = (int) sqrt (p) + 1; sqm = m * m;

 

for (i = p; i <= b; i = i+2)

{ // , a b

s = 1; // s

 

for (j = 2; j <= m; j++)

if (i % j == 0)

{

s+= j;

if ((k = i / j) < j) s+= k;

}

/* s i */





:


: 2016-12-18; !; : 241 |


:

:

, .
==> ...

1376 - | 1152 -


© 2015-2024 lektsii.org - -

: 0.249 .