#include "cielib.h" #include <iostream> //-------------------------------------------------------------------- /* #ifndef CIELIB_C #define CIELIB_C #include <stdio.h> #include <stdlib.h> int d_v, k_v, r_v; void init(void); int podajD(void) { init(); return d_v; } int podajK(void) { init(); return k_v; } int podajR(void) { init(); return r_v; } int pos[500]; void init(void) { static int initialized = -1; if(initialized != -1) { return; } initialized = 1; printf("Podaj d: "); scanf("%d", &d_v); printf("Podaj k: "); scanf("%d", &k_v); printf("Podaj r: "); scanf("%d", &r_v); printf("Podaj pozycje Krotki:\n"); for(int i = 0; i < podajD(); ++i) { scanf("%d", &pos[i]); } } int call = 0; int czyCieplo(int answer[]) { init(); call += 1; if(call > podajK()) { printf("Przekroczono limit k = %d.", k_v); exit(1); } static int ldiff = -1; int diff = 0; for(int d, i = 0; i < podajD(); ++i) { d = pos[i] - answer[i]; d = (d<0) ? -d : d; diff = (d > diff) ? d : diff; } const int result = (diff < ldiff) ? 1 : 0; ldiff = diff; return result; } void znalazlem(int answer[]) { init(); printf("Podano:\n"); for(int i = 0; i < podajD(); ++i) { printf("%d%c", pos[i], (i + 1 == podajD()) ? '\n' : ' '); } int k = 1; for(int i = 0; i < podajD(); ++i) { if(answer[i] != pos[i]) { printf("NIE %d na %d!\n", answer[i], i); k = -1; } } if(k == 1) { printf("OK --- %d zapytan\n", call); } exit(0); } #endif */ //----------------------------------------------------------------- using namespace std; int d; int k; int r; int t2[500]; int t[500]; int bmax; int bmin; int med; int buf1; int buf2; long long buf3; int check; bool odp; void szukaj(int a){ bmax = r; bmin = 0; check = 0; odp = 0; med = 0; while(check != 2){ buf3 = bmin + bmax; buf1 = buf3 / 2 + buf3 % 2; t[a] = buf1; odp = czyCieplo(t); buf3 = bmax + buf1; med = buf3 / 2 + buf3 % 2; t[a] = med; odp = czyCieplo(t); if(odp == 1){ bmin = buf1; //cout << "odp1 bmin " << bmin << endl; } else{ buf3 = bmin + buf1; med = (buf3 / 2) + (buf3 % 2); /*cout << "odp0 bf3 " << buf3 << " buf1 " << buf1 << endl; cout << "odp0 bmin " << bmin << endl; cout << "odp0 bmax " << bmax << endl; cout << "odp0 med " << med << endl;*/ t[a] = med; odp = czyCieplo(t); if(odp == 0){ med++; t[a] = med; odp = czyCieplo(t); /*cout << "if med++ " << med; cout << " odp " << odp << endl;*/ if(odp == 1){ check = 2; med--; t[a] = med; } else{ t[a] = med; check = 2; } } else{ bmax = buf1; } } } czyCieplo(t); } int main() { d = podajD(); k = podajK(); r = podajR(); for(int i = 0; i < d; i++){ t[i] = 0; } czyCieplo(t); for(int j = 0; j < d; j++){ szukaj(j); } znalazlem(t); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | #include "cielib.h" #include <iostream> //-------------------------------------------------------------------- /* #ifndef CIELIB_C #define CIELIB_C #include <stdio.h> #include <stdlib.h> int d_v, k_v, r_v; void init(void); int podajD(void) { init(); return d_v; } int podajK(void) { init(); return k_v; } int podajR(void) { init(); return r_v; } int pos[500]; void init(void) { static int initialized = -1; if(initialized != -1) { return; } initialized = 1; printf("Podaj d: "); scanf("%d", &d_v); printf("Podaj k: "); scanf("%d", &k_v); printf("Podaj r: "); scanf("%d", &r_v); printf("Podaj pozycje Krotki:\n"); for(int i = 0; i < podajD(); ++i) { scanf("%d", &pos[i]); } } int call = 0; int czyCieplo(int answer[]) { init(); call += 1; if(call > podajK()) { printf("Przekroczono limit k = %d.", k_v); exit(1); } static int ldiff = -1; int diff = 0; for(int d, i = 0; i < podajD(); ++i) { d = pos[i] - answer[i]; d = (d<0) ? -d : d; diff = (d > diff) ? d : diff; } const int result = (diff < ldiff) ? 1 : 0; ldiff = diff; return result; } void znalazlem(int answer[]) { init(); printf("Podano:\n"); for(int i = 0; i < podajD(); ++i) { printf("%d%c", pos[i], (i + 1 == podajD()) ? '\n' : ' '); } int k = 1; for(int i = 0; i < podajD(); ++i) { if(answer[i] != pos[i]) { printf("NIE %d na %d!\n", answer[i], i); k = -1; } } if(k == 1) { printf("OK --- %d zapytan\n", call); } exit(0); } #endif */ //----------------------------------------------------------------- using namespace std; int d; int k; int r; int t2[500]; int t[500]; int bmax; int bmin; int med; int buf1; int buf2; long long buf3; int check; bool odp; void szukaj(int a){ bmax = r; bmin = 0; check = 0; odp = 0; med = 0; while(check != 2){ buf3 = bmin + bmax; buf1 = buf3 / 2 + buf3 % 2; t[a] = buf1; odp = czyCieplo(t); buf3 = bmax + buf1; med = buf3 / 2 + buf3 % 2; t[a] = med; odp = czyCieplo(t); if(odp == 1){ bmin = buf1; //cout << "odp1 bmin " << bmin << endl; } else{ buf3 = bmin + buf1; med = (buf3 / 2) + (buf3 % 2); /*cout << "odp0 bf3 " << buf3 << " buf1 " << buf1 << endl; cout << "odp0 bmin " << bmin << endl; cout << "odp0 bmax " << bmax << endl; cout << "odp0 med " << med << endl;*/ t[a] = med; odp = czyCieplo(t); if(odp == 0){ med++; t[a] = med; odp = czyCieplo(t); /*cout << "if med++ " << med; cout << " odp " << odp << endl;*/ if(odp == 1){ check = 2; med--; t[a] = med; } else{ t[a] = med; check = 2; } } else{ bmax = buf1; } } } czyCieplo(t); } int main() { d = podajD(); k = podajK(); r = podajR(); for(int i = 0; i < d; i++){ t[i] = 0; } czyCieplo(t); for(int j = 0; j < d; j++){ szukaj(j); } znalazlem(t); return 0; } |