Compare commits
	
		
			6 commits
		
	
	
		
			dc6b745dd6
			...
			f0027409e9
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | f0027409e9 | ||
|  | 40cf51271f | ||
|  | ab668d12de | ||
|  | b52d5291fd | ||
|  | 92c4a7411a | ||
|  | 9922fad481 | 
					 9 changed files with 332 additions and 4 deletions
				
			
		|  | @ -1,5 +1,4 @@ | ||||||
| # MIPT C | # MIPT C | ||||||
| 
 | 
 | ||||||
| Код из лекций Бабичева по алгоритмам и структурам данных. | Решения контестов с Ejudge по архитектуре компьютера. | ||||||
| Несколько решенных задач для контестов с Ejudge и горка всякой мелочевки. | Публикуется, как правило, после окончания контеста. | ||||||
| 
 |  | ||||||
|  |  | ||||||
|  | @ -1,5 +1,5 @@ | ||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| #define SIZE 10000000 | #define SIZE 100000000 | ||||||
| //#define _DEBUG
 | //#define _DEBUG
 | ||||||
| 
 | 
 | ||||||
| const char base64[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; | const char base64[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; | ||||||
|  |  | ||||||
							
								
								
									
										83
									
								
								data/float.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								data/float.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,83 @@ | ||||||
|  | #include <stdio.h> | ||||||
|  | #include <stdlib.h> | ||||||
|  | #include <string.h> | ||||||
|  | #define SIZE 1000000 | ||||||
|  | 
 | ||||||
|  | int euclid(int a, int b); | ||||||
|  | 
 | ||||||
|  | int main() { | ||||||
|  | 	float f; | ||||||
|  | 	char p = 0; | ||||||
|  | 	static char s[SIZE] = ""; | ||||||
|  | 	static char x[SIZE] = "", y[SIZE] = "", z[SIZE] = ""; | ||||||
|  | 	int i, j; | ||||||
|  | 	int xi = 0, yi = 0, zi = 0, gcd = 1; | ||||||
|  | 	scanf("%f", &f); | ||||||
|  | 	sprintf(s, "%.6f", f); | ||||||
|  | 	 | ||||||
|  | 	for (i = 0; (i < (int)strlen(s)) && (s[i] != '.'); ++i); | ||||||
|  | 
 | ||||||
|  | 	for (j = 0; j < i; ++j) | ||||||
|  | 		x[j] = s[j]; | ||||||
|  | 
 | ||||||
|  | 	for (j = 0; j < (int)strlen(s); ++j) { | ||||||
|  | 		y[j] = s[i+1+j]; | ||||||
|  | 		if ((y[j] != '0') && (y[j] != '\0')) | ||||||
|  | 			p = 1; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	if (p) | ||||||
|  | 		for (j = 0; j < (int)strlen(s) - i; ++j) | ||||||
|  | 			z[j] = '0'; | ||||||
|  | 	else { | ||||||
|  | 		z[0] = '1'; | ||||||
|  | 		z[1] = '\0'; | ||||||
|  | 	} | ||||||
|  | 		 | ||||||
|  | 	if (j) { | ||||||
|  | 		z[0] = '1'; | ||||||
|  | 		z[j] = '\0'; | ||||||
|  | 	} | ||||||
|  | 	else { | ||||||
|  | 		y[0] = '0'; | ||||||
|  | 		y[1] = '\0'; | ||||||
|  | 		z[0] = '1'; | ||||||
|  | 		z[1] = '\0'; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	xi = atoi(x); | ||||||
|  | 	yi = atoi(y); | ||||||
|  | 	zi = atoi(z); | ||||||
|  | 	if (zi != 0) | ||||||
|  | 		gcd = euclid(zi, yi); | ||||||
|  | 
 | ||||||
|  | 	if (gcd) { | ||||||
|  | 		zi = (int) zi / gcd; | ||||||
|  | 		yi = (int) yi / gcd; | ||||||
|  | 	} | ||||||
|  | 	if (zi == 0) | ||||||
|  | 		zi = 1; | ||||||
|  | 	printf("%d (%d/%d)", xi, yi ,zi); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int euclid(int a, int b) { | ||||||
|  | 	if ((a == 0) || (b == 0)) | ||||||
|  | 		return 0; | ||||||
|  | 	int x = 1; | ||||||
|  | 	if (a < b) { | ||||||
|  | 		int c = a; | ||||||
|  | 		a = b; | ||||||
|  | 		b = c; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	while (x != 0) { | ||||||
|  | 		x = a % b; | ||||||
|  | 		a = x;	 | ||||||
|  | 		if (a < b) { | ||||||
|  | 			int c = a; | ||||||
|  | 			a = b; | ||||||
|  | 			b = c; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return a; | ||||||
|  | } | ||||||
							
								
								
									
										10
									
								
								data/meteorit.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								data/meteorit.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,10 @@ | ||||||
|  | union data convert(union data x) { | ||||||
|  | 	int i; | ||||||
|  | 	for (i = 0; i < 4; ++i) { | ||||||
|  | 		uint8_t z = x.arr[i]; | ||||||
|  | 		x.arr[i] = x.arr[7-i]; | ||||||
|  | 		x.arr[7-i] = z; | ||||||
|  | 	} | ||||||
|  | 	return x; | ||||||
|  | } | ||||||
|  | 
 | ||||||
							
								
								
									
										16
									
								
								data/patch.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								data/patch.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,16 @@ | ||||||
|  | #ifdef _DEBUG | ||||||
|  | #include <stdio.h> | ||||||
|  | unsigned int patch(unsigned int x); | ||||||
|  | 
 | ||||||
|  | int main() { | ||||||
|  | 	unsigned int x = 1000 * 100; | ||||||
|  | 	unsigned int y = 2310 * 100 + 72; | ||||||
|  | 	unsigned int z = patch(y); | ||||||
|  | 	return 0; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  | unsigned int patch(unsigned int x) { | ||||||
|  | 	return x & ~(1 << 17); | ||||||
|  | } | ||||||
							
								
								
									
										42
									
								
								data/showdouble.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								data/showdouble.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,42 @@ | ||||||
|  | #include <stdio.h> | ||||||
|  | 
 | ||||||
|  | union u { | ||||||
|  |     double d; | ||||||
|  |     float f; | ||||||
|  |     unsigned u; | ||||||
|  |     unsigned long long l; | ||||||
|  | } uu; | ||||||
|  | 
 | ||||||
|  | int main() { | ||||||
|  |     char s[10] = ""; | ||||||
|  |     scanf("%s", s); | ||||||
|  |     if (s[0] == 'f') { | ||||||
|  |         float a; | ||||||
|  |         scanf("%f", &a); | ||||||
|  |         uu.f = a; | ||||||
|  |         int t = 0; | ||||||
|  |         for (int i = sizeof(float) * 8 - 1; i >= 0; i--) { | ||||||
|  |             printf("%d", (uu.u >> i) & 1); | ||||||
|  |             t += 1; | ||||||
|  |             if (t==4) { | ||||||
|  |                 t=0; | ||||||
|  |                 putchar(' '); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } else if (s[0] == 'd') { | ||||||
|  |         double a; | ||||||
|  |         scanf("%lf", &a); | ||||||
|  |         uu.d = a; | ||||||
|  |         int t = 0; | ||||||
|  |         for (int i = sizeof(double) * 8 - 1; i >= 0; i--) { | ||||||
|  |             printf("%lld", (uu.l >> i) & 1); | ||||||
|  |             t += 1; | ||||||
|  |             if (t==4) { | ||||||
|  |                 t=0; | ||||||
|  |                 putchar(' '); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } else { | ||||||
|  |         printf("0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1011 1111 1111 1111 1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000"); | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										37
									
								
								data/sum.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								data/sum.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,37 @@ | ||||||
|  | #include <stdio.h> | ||||||
|  | #include <stdlib.h> | ||||||
|  | #define SIZE 10000000 | ||||||
|  | 
 | ||||||
|  | int f(const void *a, const void *b); | ||||||
|  | 
 | ||||||
|  | int main() { | ||||||
|  | 	static long double nums[SIZE]; | ||||||
|  | 	int len, i; | ||||||
|  | 	long double x; | ||||||
|  | 	long double sum = 0; | ||||||
|  | 
 | ||||||
|  | 	for (len = 0; scanf("%Lf", &x) != EOF; ++len) { | ||||||
|  | 		nums[len] = x; | ||||||
|  | 	} | ||||||
|  | 	qsort(nums, len, sizeof(long double), f); | ||||||
|  | 
 | ||||||
|  | #ifdef _DEBUG | ||||||
|  | 	for (int z = 0; z < len; ++z) | ||||||
|  | 		printf("%.20Lf ", nums[z]); | ||||||
|  | 	putchar('\n'); | ||||||
|  | 	printf("len:%d\n", len); | ||||||
|  | #endif | ||||||
|  | 	for (i = 0; i < len; ++i) | ||||||
|  | 		sum += nums[i]; | ||||||
|  | 	printf("%.20Lf", sum); | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int f(const void *a, const void *b) { | ||||||
|  | #ifdef _DEBUG | ||||||
|  | 	printf("a = %f, b = %f\n", *(float *)a, *(float *)b); | ||||||
|  | #endif | ||||||
|  | 	if (*(const long double *)a > *(const long double *)b)	 | ||||||
|  | 		return 1; | ||||||
|  | 	return -1; | ||||||
|  | } | ||||||
							
								
								
									
										70
									
								
								data/virus.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								data/virus.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,70 @@ | ||||||
|  | #include <stdio.h> | ||||||
|  | #include <stdlib.h> | ||||||
|  | #define SIZE 12500000 | ||||||
|  | 
 | ||||||
|  | int check_bit(char x, int pos); | ||||||
|  | int count_bits(char x, int max_offset); | ||||||
|  | 
 | ||||||
|  | int main() { | ||||||
|  | 	char *robots = (char*)calloc(SIZE, sizeof(char)); | ||||||
|  | 	int v, N, i, j, x, s, byte_n, byte, offset; | ||||||
|  | 	int res = 0; | ||||||
|  | 
 | ||||||
|  | 	scanf("%d %d", &v, &N); | ||||||
|  | 	if ((N + 1) % 8) { | ||||||
|  | 		byte_n = (int)((N + 1) / 8) + 1; | ||||||
|  | 	} | ||||||
|  | 	else { | ||||||
|  | 		byte_n = (int)((N + 1) / 8); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | #ifdef _DEBUG | ||||||
|  | 	printf("byte_n:%d, N;%d\n", byte_n, N); | ||||||
|  | #endif | ||||||
|  | 	for (i = 0; i < v; ++i) { | ||||||
|  | 		scanf("%d", &x); | ||||||
|  | 		byte = (int)(x / 8); | ||||||
|  | 		offset = x % 8; | ||||||
|  | #ifdef _DEBUG | ||||||
|  | 		printf("Setting first robot at byte %d[%d]\n!=!=!\n", byte, offset); | ||||||
|  | #endif | ||||||
|  | 		robots[byte] = robots[byte] | (128 >> offset); | ||||||
|  | 		for (j = 1, s = 0; x * (j + 1) + s  <= N; s += j, ++j) { | ||||||
|  | #ifdef _DEBUG | ||||||
|  | 			printf("x*%d+%d = %d\n", j+1, s, x * (j+1) + s); | ||||||
|  | #endif | ||||||
|  | 			byte = (int)((x * (j + 1) + s) / 8); | ||||||
|  | 			offset = (x * (j + 1) + s) % 8; | ||||||
|  | #ifdef _DEBUG | ||||||
|  | 			printf("Setting byte %d[%d]\n", byte, offset); | ||||||
|  | #endif | ||||||
|  | 			robots[byte] = robots[byte] | (128 >> offset); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	for (i = 0; i < byte_n - 1; ++i) { | ||||||
|  | #ifdef _DEBUG | ||||||
|  | 		printf("Counting bits at byte %d, found %d bits\n", i, count_bits(robots[i], 7)); | ||||||
|  | #endif | ||||||
|  | 		res += count_bits(robots[i], 7); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	offset = (N - 1) % 8; | ||||||
|  | 
 | ||||||
|  | #ifdef _DEBUG | ||||||
|  | 	printf("Counting bits at byte %d, found %d bits[final offset: %d]\n", i, count_bits(robots[i], offset), offset); | ||||||
|  | #endif | ||||||
|  | 	res += count_bits(robots[i], offset); | ||||||
|  | 	printf("%d", res); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int check_bit(char x, int pos) { | ||||||
|  | 	return (x >> (7 - pos)) & 1; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int count_bits(char x, int max_offset) { | ||||||
|  | 	int res = 0; | ||||||
|  | 	for (int i = 0; i <= max_offset; ++i) { | ||||||
|  | 		res += check_bit(x, i); | ||||||
|  | 	} | ||||||
|  | 	return res; | ||||||
|  | } | ||||||
							
								
								
									
										71
									
								
								numbers/tolongn.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								numbers/tolongn.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,71 @@ | ||||||
|  | #include <stdio.h> | ||||||
|  | #include <stdlib.h> | ||||||
|  | #include <string.h> | ||||||
|  | 
 | ||||||
|  | typedef struct{ | ||||||
|  | 	char *dig; | ||||||
|  | 	int n; | ||||||
|  | 	char sign; | ||||||
|  | } LongN; | ||||||
|  | 
 | ||||||
|  | LongN getLongN(char * s); | ||||||
|  | void printLong(LongN x);  | ||||||
|  | int main() { | ||||||
|  | 	LongN a; | ||||||
|  | 	a.dig = (char*)calloc(101, sizeof(char)); | ||||||
|  | 	a.dig[0] = 12; | ||||||
|  | 	a.dig[1] = 34; | ||||||
|  | 	a.dig[2] = 5; | ||||||
|  | 	a.n = 5; | ||||||
|  | 	a.sign = 1; | ||||||
|  | 	//printLong(a);
 | ||||||
|  | 	char s[50]; //= "-1234";
 | ||||||
|  | 	scanf("%s", s); | ||||||
|  | 	LongN x = getLongN(s); | ||||||
|  | 	printLong(x); | ||||||
|  | 	free(a.dig); | ||||||
|  | 	free(x.dig); | ||||||
|  | 	return 0; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void printLong(LongN x) { | ||||||
|  | 	if (x.sign) { | ||||||
|  | 		printf("-"); | ||||||
|  | 	} | ||||||
|  | 	if (x.n % 2) { | ||||||
|  | 		x.n++; | ||||||
|  | 	} | ||||||
|  | 	for (int i = 0; i < (int)(x.n/2); ++i) { | ||||||
|  | 		printf("%d", x.dig[i]); | ||||||
|  | 	} | ||||||
|  | 	printf("\n"); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | LongN getLongN(char* s) { | ||||||
|  | 	int k = 0; | ||||||
|  | 	LongN r; | ||||||
|  | 
 | ||||||
|  | 	r.sign = 0; | ||||||
|  | 	if (s[0] == '-') { | ||||||
|  | 		k = 1; | ||||||
|  | 		r.sign = 1; | ||||||
|  | 	} | ||||||
|  | 	if (s[0] == '+') | ||||||
|  | 		k = 1; | ||||||
|  | 
 | ||||||
|  | 	int l = k; | ||||||
|  | 
 | ||||||
|  | 	for (; s[l] >= '0' && s[l] <= '9'; ++l); | ||||||
|  | 	r.n = (l - k - 1) / 2 + 1; | ||||||
|  | 	r.dig = (char*)calloc(101, sizeof(char)); | ||||||
|  | 
 | ||||||
|  | 	int t = 0; | ||||||
|  | 	for (int i = l - 1; i >= k; i -= 2, t++) { | ||||||
|  | 		if (i == k) { | ||||||
|  | 			r.dig[t] = s[i] - '0'; | ||||||
|  | 		} | ||||||
|  | 		else  | ||||||
|  | 			r.dig[t] = 10 * (s[i - 1] - '0') + s[i] - '0'; | ||||||
|  | 	} | ||||||
|  | 	return r; | ||||||
|  | } | ||||||
		Reference in a new issue