├── 100.c ├── 10004.cpp ├── 10008.cpp ├── 10009.cpp ├── 10010.cpp ├── 10013.cpp ├── 10018.cpp ├── 10019.cpp ├── 10034.cpp ├── 10035.cpp ├── 10038.cpp ├── 10041.cpp ├── 10048.cpp ├── 10050.cpp ├── 10055.cpp ├── 10062.cpp ├── 10066.cpp ├── 10070.cpp ├── 10071.cpp ├── 10079.CPP ├── 10082.cpp ├── 10098.cpp ├── 10106.cpp ├── 10107.cpp ├── 10110.cpp ├── 10115.cpp ├── 10116.cpp ├── 10141.cpp ├── 10162.cpp ├── 10176.cpp ├── 10189 best.cpp ├── 10189.cpp ├── 10192.cpp ├── 10193.cpp ├── 10195.cpp ├── 10197.cpp ├── 102.cpp ├── 10219.cpp ├── 10220.cpp ├── 10222.cpp ├── 10235.cpp ├── 10260.cpp ├── 10268.cpp ├── 10279.cpp ├── 10281.cpp ├── 10282.cpp ├── 10293.cpp ├── 10295.cpp ├── 10300.cpp ├── 10302.cpp ├── 10323.CPP ├── 10324.cpp ├── 10327.cpp ├── 10334.cpp ├── 10336.cpp ├── 10338.cpp ├── 10340.cpp ├── 10341.cpp ├── 10346.cpp ├── 10347.cpp ├── 10361.cpp ├── 10363.cpp ├── 10370.cpp ├── 10377.cpp ├── 10394.cpp ├── 10405.cpp ├── 10407.cpp ├── 10409.cpp ├── 10420.cpp ├── 10424.cpp ├── 10432.cpp ├── 10450.CPP ├── 10469.cpp ├── 10473.cpp ├── 10474.CPP ├── 10489.cpp ├── 10490.cpp ├── 10508.cpp ├── 10515.cpp ├── 10530.cpp ├── 10551.cpp ├── 10573.cpp ├── 10579.cpp ├── 10583.cpp ├── 10591.cpp ├── 10608.cpp ├── 10611.cpp ├── 10651.cpp ├── 10664.cpp ├── 10696.cpp ├── 10699.cpp ├── 10783.cpp ├── 10785.cpp ├── 10789.cpp ├── 10790.cpp ├── 10812.cpp ├── 10815.cpp ├── 10865.cpp ├── 10878.cpp ├── 10894.cpp ├── 10896.cpp ├── 10905.cpp ├── 10919.cpp ├── 10921.cpp ├── 10922.cpp ├── 10924.cpp ├── 10928.cpp ├── 10929.cpp ├── 10931.cpp ├── 10933.cpp ├── 10935.cpp ├── 10945.cpp ├── 10946.cpp ├── 10948.cpp ├── 10954.cpp ├── 11005.cpp ├── 11056.cpp ├── 11074.cpp ├── 11086.cpp ├── 11172.cpp ├── 11192.cpp ├── 113.cpp ├── 119.cpp ├── 133.cpp ├── 146.cpp ├── 147.cpp ├── 151.cpp ├── 160.cpp ├── 260.cpp ├── 263.cpp ├── 272.cpp ├── 278.cpp ├── 280.cpp ├── 294.cpp ├── 299.cpp ├── 324.cpp ├── 336.cpp ├── 338.cpp ├── 343.cpp ├── 344.cpp ├── 350.cpp ├── 352.cpp ├── 355.cpp ├── 369.cpp ├── 371.cpp ├── 374.cpp ├── 382.cpp ├── 383.cpp ├── 386.cpp ├── 389.cpp ├── 401.cpp ├── 408.cpp ├── 422.cpp ├── 423.cpp ├── 424.cpp ├── 429.cpp ├── 438.cpp ├── 439.cpp ├── 440.cpp ├── 441.cpp ├── 444.cpp ├── 445.cpp ├── 446.cpp ├── 455.cpp ├── 458.cpp ├── 459.cpp ├── 469.cpp ├── 483.cpp ├── 484.cpp ├── 486.cpp ├── 488.cpp ├── 489.cpp ├── 490.CPP ├── 492.cpp ├── 494.cpp ├── 495.cpp ├── 496.cpp ├── 498.cpp ├── 499.cpp ├── 524.cpp ├── 530.cpp ├── 532.cpp ├── 534.cpp ├── 541.cpp ├── 543.cpp ├── 544.cpp ├── 555.cpp ├── 558.cpp ├── 567.cpp ├── 572.cpp ├── 575.cpp ├── 576.cpp ├── 583.cpp ├── 591.cpp ├── 612.cpp ├── 621.cpp ├── 623.cpp ├── 673.cpp ├── 674.cpp ├── 686.cpp ├── 694.cpp ├── 713.cpp ├── 784.cpp ├── 785.cpp ├── 793.cpp ├── 834.cpp ├── 855.cpp ├── 895.cpp ├── LICENSE └── README.md /100.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int cycle(int m) 5 | { 6 | int i = 1; 7 | 8 | while (m != 1){ 9 | if (m % 2 == 0){ 10 | m = m/2; 11 | } 12 | else{ 13 | m = 3*m+1; 14 | } 15 | i++; 16 | } 17 | return i; 18 | } 19 | 20 | 21 | main() 22 | { 23 | int m,n,max,temp; 24 | int mOriginal,nOriginal; 25 | int i; 26 | 27 | while (scanf("%d %d\n",&m,&n)==2){ 28 | 29 | /* remember original order of entries for output */ 30 | mOriginal = m; 31 | nOriginal = n; 32 | 33 | /* swap if out of order */ 34 | if (m > n){ 35 | temp = m; 36 | m = n; 37 | n = temp; 38 | } 39 | 40 | max = cycle(m); 41 | for(i=m+1;i<=n;i++) { 42 | temp = cycle(i); 43 | if (temp > max) 44 | max = temp; 45 | } 46 | printf("%d %d %d\n",mOriginal,nOriginal,max); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /10008.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: What's Cryptanalysis? UVa 10008 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 26-11-04 5 | */ 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | namespace scope{ 12 | char alphabet[130][2]; 13 | } 14 | 15 | void init(); 16 | void checkCharacter(char *str); 17 | void display(); 18 | void myStrlwr(char *str); 19 | //================================================ 20 | int main(){ 21 | char str[900]; 22 | int input; 23 | int i; 24 | 25 | init(); 26 | 27 | cin >> input; 28 | 29 | for (i = 0; i <= input; i++){ 30 | cin.getline(str, sizeof(str)); 31 | myStrlwr(str); 32 | checkCharacter(str); 33 | } 34 | 35 | display(); 36 | return 0; 37 | } 38 | //=============================================== 39 | void init(){ 40 | for (int i = 96; i < 130; i++){ 41 | scope ::alphabet [i][0] = i; 42 | scope ::alphabet[i][1] = '0'; 43 | } 44 | } 45 | 46 | void checkCharacter(char *str){ 47 | int i; 48 | for (i = 0; str[i]; i++){ 49 | if (str[i] >= 'a' && str[i] <= 'z'){ 50 | scope ::alphabet[str[i]][1] += 1; 51 | } 52 | } 53 | } 54 | 55 | using namespace scope; 56 | void display(){ 57 | int i, j, max; 58 | char ch; 59 | 60 | max = 0; 61 | for (i = 96; i < 130; i++){ 62 | if ((alphabet[i][1] - 48) > max) 63 | max = alphabet[i][1] - 48; 64 | } 65 | 66 | 67 | for (i = max; i > 0; --i){ 68 | for (j = 96; j < 130; j++){ 69 | if ((alphabet[j][1] - 48) == i){ 70 | ch = alphabet[j][0]; 71 | cout << (char )toupper(ch) << " " << alphabet[j][1] << endl; 72 | } 73 | } 74 | } 75 | } 76 | 77 | void myStrlwr(char *str){ 78 | int i; 79 | for (i = 0; str[i]; i++){ 80 | str[i] = tolower(str[i]); 81 | } 82 | } -------------------------------------------------------------------------------- /10035.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Primary Arithmetic UVa 10035 PC 110501 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | 9 | int main(){ 10 | long int a, b, c , _a, _b, result, carry; 11 | 12 | 13 | while (cin >> a >> b){ 14 | if ( a == 0 && b == 0) break; 15 | carry = c = 0; 16 | while (a != 0 || b != 0){ 17 | _a = a % 10; 18 | a = a / 10; 19 | _b = b % 10; 20 | b = b / 10; 21 | 22 | result = _a + _b + c; 23 | if (result >= 10){ 24 | c=1; 25 | carry += 1; 26 | } 27 | else 28 | c=0; 29 | } 30 | 31 | if (carry == 0){ 32 | cout << "No carry operation." << endl; 33 | } 34 | else if (carry == 1){ 35 | cout << carry << " carry operation." << endl; 36 | } 37 | else if (carry > 1){ 38 | cout << carry << " carry operations." << endl; 39 | } 40 | } 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /10038.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Jolly Jumpers UVa 10038 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 29-09-04 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | int input; // for test case 14 | int i; 15 | int last, next, result; // temporary used for set the places 16 | bool flag; // check if jolly or not jolly 17 | 18 | while (cin >> input){ 19 | // each time place will automatically initialized to zero 20 | vector place(3005); 21 | flag = true; 22 | 23 | cin >> last; 24 | // i = 1 as first input take before it 25 | for (i = 1; i < input; i++){ 26 | cin >> next; 27 | result = abs(last - next); 28 | place[result] = true; 29 | last = next; 30 | } 31 | 32 | // main condition successive elements take place 33 | for (i = 1; i < input; i++) 34 | if (!place[i]){ 35 | flag = false; 36 | break; 37 | } 38 | 39 | if (flag) 40 | cout << "Jolly" << endl; 41 | else 42 | cout << "Not jolly" << endl; 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /10041.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Vito's family UVa 10041 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 07-10-04 5 | */ 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | //typedef type 12 | typedef long int lint; 13 | 14 | void insertion(lint array[], lint size); 15 | //================================================================================ 16 | int main(){ 17 | lint test; 18 | lint relative; 19 | lint dataSet[30005]; 20 | /* minSum = minimal sum of distances 21 | optimal = optimal here means middle of the street 22 | */ 23 | lint optimal, minSum, temp; 24 | int i, j; 25 | 26 | cin >> test; 27 | 28 | for (i = 0; i < test; i++){ 29 | minSum = 0; 30 | 31 | for (j = 0; j < 30005; j++) 32 | dataSet[j] = 0; 33 | 34 | cin >> relative; 35 | for (j = 0; j < relative; j++) 36 | cin >> dataSet[j]; 37 | 38 | // street numbers must be sorted 39 | insertion(dataSet, relative); 40 | optimal = relative / 2; 41 | 42 | for (j = 0; j < relative; j++){ 43 | // distance between two street number d[i][j] = abs(s[i] - s[j]) 44 | temp = abs (dataSet[optimal] - dataSet[j]); 45 | minSum += temp; 46 | } 47 | cout << minSum << endl; 48 | } 49 | return 0; 50 | } 51 | //=============================================================================== 52 | void insertion(lint array[], lint size){ 53 | lint i, j, key; 54 | 55 | for (j = 1; j < size; j++){ 56 | key = array[j]; 57 | i = j - 1; 58 | 59 | while (i >= 0 && array[i] > key){ 60 | array[i+1] = array[i]; 61 | i = i - 1; 62 | } 63 | array[i+1] = key; 64 | } 65 | } -------------------------------------------------------------------------------- /10048.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Audiophobia UVa 10048 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Graph + Floyd Warshall + Minimax 4 | Compiled: Visual C++ 7.0 5 | Date: 01-03-06 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | const int INF = 1 << 20; 12 | const int MX = 105; 13 | 14 | int graph[MX][MX], n; 15 | 16 | void init(){ 17 | for (int i = 1; i <= n; ++i){ 18 | for (int j = 1; j <= n; ++j) 19 | graph[i][j] = INF; 20 | graph[i][i] = 0; 21 | } 22 | } 23 | 24 | void floydWarshall(){ 25 | int i, j, k; 26 | for (k = 1; k <= n; ++k){ 27 | for(i = 1; i <= n; ++i){ 28 | for (j = 1; j <= n; ++j) 29 | graph[i][j] = min(graph[i][j], max(graph[i][k], graph[k][j])); 30 | } 31 | } 32 | } 33 | 34 | int main(){ 35 | //freopen("input.txt", "r", stdin); 36 | int s, q, i, j, cases = 0, temp, a, b; 37 | bool flag = false; 38 | 39 | while (cin >> n >> s >> q){ 40 | if (n == 0 && s == 0 && q == 0) break; 41 | 42 | init(); 43 | for (i = 1; i <= s; ++i){ 44 | cin >> a >> b >> temp; 45 | graph[a][b] = temp; 46 | graph[b][a] = temp; 47 | } 48 | floydWarshall(); 49 | 50 | if (flag)cout << endl; flag = true; 51 | cout << "Case #" << ++cases << endl; 52 | 53 | for (i = 1; i <= q; ++i){ 54 | cin >> a >> b; 55 | if (graph[a][b] == INF) 56 | cout << "no path" << endl; 57 | else 58 | cout << graph[a][b] << endl; 59 | } 60 | } 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /10050.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Hartals UVa 10050 PC 110203 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 04-09-04 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | vector days(3655); // store days 13 | int i, j, count; // count store the total hartals 14 | int t, n, parties; 15 | // n = number of days 16 | int averDay; // days of hartals by each party 17 | 18 | cin >> t; // test case 19 | 20 | for (int test = 0; test < t; test++){ 21 | 22 | count = 0; 23 | // initialize all to false 24 | for (i = 0; i < days.size(); i++) 25 | days[i] = false; 26 | 27 | cin >> n >> parties; 28 | 29 | for (j = 0; j < parties; j++){ 30 | // each parties hartals declared 31 | cin >> averDay; 32 | // i = averDay - 1 as array index starts with 0 33 | for (i = averDay - 1; i < n; i += averDay){ 34 | days[i] = true; 35 | } 36 | } 37 | 38 | // for friday and saturday 39 | days[5] = days[6] = false; 40 | for (i = 5; i < n; i += 7){ 41 | days[i] = false; 42 | days[i + 1] = false; 43 | } 44 | 45 | for (i = 0; i < n; i++){ 46 | if (days[i] != false) 47 | count++; 48 | } 49 | 50 | cout << count << endl; 51 | 52 | } 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /10055.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(){ 6 | long double a, b; 7 | 8 | while (cin >> a >> b){ 9 | printf("%.0lf\n", fabs(a-b)); 10 | } 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /10062.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Tell me the frequencies UVa 10062 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 03-10-04 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int getMax(int array[]); 12 | 13 | int main(){ 14 | char str[1005]; 15 | int freq[150]; 16 | int i, j; 17 | int size; 18 | int c; // use for counting frequencies 19 | int max; 20 | int flag = 0; 21 | 22 | while (cin.getline(str, sizeof(str)) && !cin.eof()){ 23 | size = strlen(str); 24 | 25 | for (i = 32; i <= 127; i++){ 26 | c = 0; 27 | for (j = 0; j < size; j++){ 28 | if (i == str[j]) 29 | ++c; 30 | } 31 | freq[i] = c; 32 | } 33 | max = getMax(freq); 34 | 35 | if (flag) 36 | cout << endl; 37 | flag = 1; 38 | for (i = 1; i <= max; i++){ 39 | for (j = 127; j >= 32; --j){ 40 | if (i == freq[j]) 41 | cout << j << " " << freq[j] << endl; 42 | } 43 | } 44 | } 45 | 46 | return 0; 47 | } 48 | 49 | int getMax(int array[]){ 50 | int max = 0; 51 | int i; 52 | for (i = 32; i <= 127; i++){ 53 | if (max < array[i]) 54 | max = array[i]; 55 | } 56 | return max; 57 | } -------------------------------------------------------------------------------- /10066.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: The Twin Towers UVa 10066 2 | Programmer: Md. Mahmud Ahsan 3 | Description: LCS 4 | Compiled: Visual C++ 7.0 5 | Date: 20-11-05 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | #define N 1005 12 | 13 | //Global Declaration 14 | int c[N+1][N+1]; 15 | //char b[N+1][N+1]; 16 | 17 | int LCSLength(int *x, int *y, int m, int n); 18 | //void printLCS(char *x, int i, int j); // i for x and j for y 19 | 20 | int main(){ 21 | int x[N], y[N]; 22 | int m, n, i, cases=0; 23 | 24 | while (cin >> m >> n){ 25 | if (m == 0 && n == 0) break; 26 | 27 | for (i = 1; i <= m; ++i) 28 | cin >> x[i]; 29 | for (i = 1; i <= n; ++i) 30 | cin >> y[i]; 31 | 32 | int len = LCSLength(x, y, m, n); 33 | cout << "Twin Towers #" << ++cases << endl; 34 | cout << "Number of Tiles : " << len << endl << endl; 35 | } 36 | 37 | return 0; 38 | } 39 | 40 | int LCSLength(int *x, int *y, int m , int n){ 41 | int i, j; 42 | 43 | for (i = 1; i <= m; ++i) 44 | c[i][0] = 0; 45 | for (j = 0; j <= n; ++j) 46 | c[0][j] = 0; 47 | 48 | for (i = 1; i <= m; ++i){ 49 | for (j = 1; j <= n; ++j){ 50 | if (x[i] == y[j]){ 51 | c[i][j] = c[i-1][j-1] + 1; 52 | //b[i][j] = '#'; //# for angle arrow 53 | } 54 | else if (c[i-1][j] >= c[i][j-1]){ 55 | c[i][j] = c[i-1][j]; 56 | //b[i][j] = '^'; //^ for up arrow 57 | } 58 | else{ 59 | c[i][j] = c[i][j-1]; 60 | //b[i][j] = '<'; //< for left arrow 61 | } 62 | } 63 | } 64 | return c[m][n]; 65 | } 66 | /* 67 | void printLCS(char *x, int i, int j){ 68 | if (i == 0 || j == 0) return; 69 | 70 | if (b[i][j] == '#'){ 71 | printLCS(x, i-1, j-1); 72 | cout << x[i]<< " "; 73 | } 74 | else if (b[i][j] == '^') 75 | printLCS(x, i-1, j); 76 | else 77 | printLCS(x, i, j-1); 78 | } 79 | */ -------------------------------------------------------------------------------- /10071.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Back to high school physics UVa 10071 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | */ 5 | 6 | #include 7 | 8 | int main(){ 9 | int v, t, result; 10 | 11 | while (cin >> v >> t){ 12 | result = v * t * 2; 13 | cout << result << endl; 14 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /10079.CPP: -------------------------------------------------------------------------------- 1 | /* Problem: Pizza Cutting UVa 10079 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Modifed version in Turbo C++ 4 | Date: 04-10-04 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | long long int input, result; 13 | 14 | while (cin >> input){ 15 | if (input < 0) break; 16 | result = ((input * (input + 1))/2) + 1; 17 | cout << result << endl; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /10082.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: WERTYU UVa 10082 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 07-08-04 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | /* store the code some rules [ \\ \' ] */ 12 | char string[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;\'ZXCVBNM,./"; 13 | char input[200]; 14 | int i, j; 15 | int flag; 16 | 17 | while (cin.getline(input, sizeof(input))){ 18 | for (i = 0; input[i] != '\0'; i++){ 19 | flag = 0; // chekc if other character that not in the code 20 | for (j = 0; string[j] != '\0'; j++){ 21 | if (input[i] == string[j]){ 22 | cout << string[j-1]; // print the previous character 23 | flag = 1; // if character found in the string code than say no 24 | break; 25 | } 26 | } 27 | if (flag == 0) cout << input[i]; 28 | } 29 | cout << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /10098.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Generating Fast, Sorted Permutation UVa 10098 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Permutation, Using STL, sort(), next_permutation(), snail_sort() 4 | Compiled: Visual C++ 7.0 5 | Date: 10-07-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | template 13 | void snail_sort(BidirectionalIterator first, BidirectionalIterator last){ 14 | while (next_permutation(first, last)){ 15 | copy(first, last, ostream_iterator(cout, "")); 16 | cout << endl; 17 | } 18 | } 19 | 20 | int main(){ 21 | int n, i, len; 22 | cin >> n; 23 | char str[15]; 24 | 25 | 26 | for (i = 0; i < n; ++i){ 27 | cin >> str; 28 | len = strlen(str); 29 | sort(str, str+len); 30 | 31 | 32 | cout << str << endl; 33 | 34 | snail_sort(str, str+len); 35 | 36 | cout << endl; 37 | 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /10107.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: What is Median? UVa 10107 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 11-12-04 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | typedef long lint; 13 | 14 | // prototypes 15 | void insertionSort(lint array[], lint size); 16 | 17 | //========================================== 18 | int main(){ 19 | //ifstream fin("input.in"); 20 | //cin = fin; 21 | lint i = 0; 22 | lint sum, med; 23 | lint array[10005]; 24 | 25 | while (cin >> array[i]&& !cin.eof()){ 26 | insertionSort(array, i+1); 27 | if (i % 2 == 1) { 28 | // if i is even as array begins 0 so here checking system differ 29 | med = i / 2; 30 | sum = array[med] + array[med + 1]; 31 | sum = sum / 2; 32 | cout << sum << endl; 33 | } 34 | else{ 35 | med = i / 2; 36 | cout << array[med] << endl; 37 | } 38 | ++i; 39 | } 40 | return 0; 41 | } 42 | //========================================= 43 | void insertionSort(lint array[], lint size){ 44 | int i, j, key; 45 | 46 | for (j = 1; j < size; j++){ 47 | key = array[j]; 48 | i = j - 1; 49 | 50 | while (i >= 0 && array[i] > key){ 51 | array[i + 1] = array[i]; 52 | i = i - 1; 53 | } 54 | array[i + 1] = key; 55 | } 56 | } -------------------------------------------------------------------------------- /10110.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Light More Light UVa 10110 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Linux GNU C++ Visual C++ 6.0 4 | Date: 04-09-04 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | typedef long long int lint; 13 | 14 | int main(){ 15 | lint input; 16 | lint _input; 17 | 18 | while (cin >> input){ 19 | if (input == 0) 20 | break; 21 | 22 | _input = sqrt(input); 23 | 24 | if (_input * _input == input) 25 | cout << "yes" << endl; 26 | else 27 | cout << "no" << endl; 28 | 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /10115.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Automatic Editing UVa 10115 2 | Programmer: Md. Mahmud Ahsan 3 | Description: String + replacing 4 | Compiled: Visual C++ 7.0 5 | Date: 30-08-05 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | struct dic{ 12 | char key[500]; 13 | char value[500]; 14 | }dics[100]; 15 | 16 | int main(){ 17 | char key[500], value[500], input[1000]; 18 | int n, i, j, count; 19 | 20 | while (cin >> n){ 21 | cin.getline(input, sizeof(input)) ;// eat blank line 22 | if (n == 0) break; 23 | 24 | j = 0; 25 | for (i = 0; i < n; ++i){ 26 | cin.getline(key, sizeof(key)); 27 | cin.getline(value, sizeof(value)); 28 | strcpy(dics[j].key, key); 29 | strcpy(dics[j].value, value); 30 | ++j; 31 | } 32 | 33 | cin.getline(input, sizeof(input)); 34 | string buffer(input); // converting char* to string object 35 | 36 | for (i = 0; i < n; ++i){//rules are applied 37 | do{ 38 | count = 0; 39 | int pos; 40 | while ( (pos = buffer.find(dics[i].key)) != string::npos){ 41 | buffer.replace(pos, strlen(dics[i].key), dics[i].value); 42 | ++count; 43 | } 44 | if (count == 0) break;//no new match 45 | }while (true); 46 | } 47 | cout << buffer << endl; 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /10116.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Robot Motion UVa 10116 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Graph 4 | Compiled: Visual C++ 7.0 5 | Date: 17-08-06 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | const int MX = 100; 12 | char grid[MX][MX]; 13 | int gridValue[MX][MX]; 14 | 15 | void init(int row, int col){ 16 | for (int i = 0; i < row; ++i) 17 | for (int j = 0; j < col; ++j) 18 | gridValue[i][j] = -1; 19 | } 20 | 21 | int main(){ 22 | //freopen("input.txt", "r", stdin); 23 | int row, col, pos; 24 | int a, b, path, exit, b4Loop; 25 | char ch; 26 | bool found; 27 | 28 | while (cin >> row >> col >> pos){ 29 | if (!row && !col && !pos) break; 30 | 31 | for (int i = 0; i < row; ++i) 32 | cin >> grid[i]; 33 | 34 | init(row, col); 35 | found = false; 36 | a = 0; 37 | b = --pos; 38 | path = 0; 39 | 40 | while (true){ 41 | ch = grid[a][b]; 42 | gridValue[a][b] = path++; 43 | 44 | switch (ch){ 45 | case 'N': --a; break; 46 | case 'S': ++a; break; 47 | case 'E': ++b; break; 48 | case 'W': --b; break; 49 | } 50 | if (a < 0 || a >= row || b < 0 || b >= col){ 51 | found = true; 52 | exit = path; 53 | break; 54 | } 55 | else if (gridValue[a][b] != -1){ 56 | found = false; 57 | exit = path - gridValue[a][b]; 58 | b4Loop = gridValue[a][b]; 59 | break; 60 | } 61 | } 62 | if (found) 63 | cout << exit << " step(s) to exit" << endl; 64 | else 65 | cout << b4Loop << " step(s) before a loop of " << exit << " step(s)" << endl; 66 | } 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /10141.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Request for Proposal UVa 10141 2 | Programmer: Md. Mahmud Ahsan 3 | Description: AD HOC 4 | Compiled: Visual C++ 7.0 5 | Date: 31-08-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int main(){ 14 | //freopen("output.txt", "w", stdout); 15 | int n, p, i, j, cases = 0, prio, prevPrio; 16 | double value, prevValue; 17 | char str[500], name[500]; 18 | char final[500]; 19 | bool flag = false; 20 | 21 | while (cin >> n >> p){ 22 | if (n == 0 && p == 0) break; 23 | 24 | cin.getline(str, sizeof(str));// eat blank char 25 | for (i = 0; i < n; ++i) 26 | cin.getline(str, sizeof(str)); // ignore this 27 | 28 | cin.getline(final, sizeof(final)); 29 | cin.getline(str, sizeof(str)); 30 | sscanf(str, "%lf%d", &prevValue,&prevPrio); 31 | 32 | for (j = 0; j < prevPrio; ++j) cin.getline(str, sizeof(str)); 33 | 34 | for (i = 1; i < p; ++i){ 35 | cin.getline(name, sizeof(name)); 36 | cin.getline(str, sizeof(str)); 37 | 38 | sscanf(str, "%lf%d", &value,&prio); 39 | 40 | for (j = 0; j < prio; ++j) cin.getline(str, sizeof(str)); 41 | 42 | if (prio > prevPrio){ 43 | prevPrio = prio; 44 | strcpy(final, name); 45 | } 46 | else if (prio == prevPrio){ 47 | if (value < prevValue){ 48 | prevValue = value; 49 | strcpy(final, name); 50 | } 51 | } 52 | } 53 | if (flag) cout << endl; 54 | flag = true; 55 | 56 | cout << "RFP #" << ++cases << endl; 57 | cout << final << endl; 58 | } 59 | return 0; 60 | } -------------------------------------------------------------------------------- /10162.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Last Digit UVa 10162 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 09-09-04 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | int ld[101]; 14 | long int r, x; 15 | 16 | int i, j; 17 | 18 | ld[0] = 0; 19 | 20 | for (i = 1; i <= 100; i++){ 21 | r = x = i % 10; 22 | for (j = 1; j < i; j++) 23 | r = (r * x) % 10; 24 | 25 | 26 | ld[i] = r + ld[i-1]; 27 | ld[i] %= 10; 28 | } 29 | 30 | char str[5000]; 31 | long int n; 32 | 33 | while (cin >> str){ 34 | if (str[0] == '0') break; 35 | 36 | i = 0; 37 | j = strlen(str); 38 | 39 | if (j > 2) 40 | i = j - 2; 41 | sscanf(&str[i], "%d", &n); 42 | 43 | cout << ld[n] << endl; 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /10176.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Ocean Deep! Make it shallow!! UVa 10176 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Binary to decimal, Module 4 | Compiled: Visual C++ 7.0 5 | Date: 10-06-05 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | char str[20000]; 13 | int i, len, number; 14 | char ch; 15 | 16 | while (cin >> ch){ 17 | number = 0; 18 | number += ch - 48; 19 | while (cin >> ch){ 20 | if (ch == '#') break; 21 | number = number << 1;//left shift multiplying by 2 each time 22 | number += ch - 48; 23 | number = number % 131071; 24 | } 25 | 26 | if (number == 0) 27 | cout << "YES" << endl; 28 | else 29 | cout << "NO" << endl; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /10189.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Minesweeper UVa 10189 PC 110102 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Linux GNU C++ Visual C++ 6.0 4 | Date: 23-07-04 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | // typedef 11 | typedef struct{ 12 | int m; 13 | int n; 14 | char grid[105][105]; 15 | }field; 16 | 17 | // prototypes 18 | void show(field, int); 19 | 20 | //=============================================================== 21 | int main(){ 22 | int m, n, fieldcount = 0; 23 | int i, j; // for loop counter 24 | field board; // declare a board 25 | 26 | // enter the board 27 | while (cin >> m >> n){ 28 | if (m == 0 && n == 0) break; // terminate the program 29 | 30 | board.m = m; 31 | board.n = n; 32 | // initialize the board 33 | for (i = 0; i < m; i++){ 34 | for (j = 0; j < n; j++){ 35 | cin >> board.grid[i][j]; 36 | if (board.grid[i][j] == '.') board.grid[i][j] = '0'; 37 | } 38 | } 39 | 40 | // parse and generate the hints 41 | for (i = 0; i < m; i++){ 42 | for (j = 0; j < n; j++){ 43 | if (board.grid[i][j] == '*'){ 44 | // generate the range 45 | for (int p = i - 1; p <= i + 1; p++){ 46 | for (int q = j - 1; q <= j + 1; q++){ 47 | if ((p >= 0) && (p <= m) && (q >= 0) && (q <= n)){ 48 | if (board.grid[p][q] != '*'){ 49 | board.grid[p][q] += 1; 50 | } 51 | } 52 | } 53 | } 54 | } 55 | } 56 | } 57 | 58 | // increase the field 59 | ++fieldcount; 60 | 61 | // show the board 62 | show (board, fieldcount); 63 | } // end the while loop 64 | return 0; 65 | } 66 | //==================================================================== 67 | 68 | void show(field board, int count){ 69 | // show the m * n matrix of the board 70 | int i, j; 71 | 72 | cout << endl << "Field #" << count << ":" << endl; 73 | for (i = 0; i < board.m; i++){ 74 | for (j = 0; j < board.n; j++){ 75 | cout << board.grid[i][j]; 76 | } 77 | cout << endl; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /10192.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Vacation UVa 10192 2 | Programmer: Md. Mahmud Ahsan 3 | Description: LCS 4 | Compiled: Visual C++ 7.0 5 | Date: 21-11-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | #define N 500 13 | 14 | //Global Declaration 15 | int c[N+1][N+1]; 16 | char x[N], y[N], temp[N]; 17 | 18 | int LCSLength(int m, int n); 19 | 20 | int main(){ 21 | int i, j, m, n, cases = 0; 22 | 23 | while( gets(x)&&gets(y)!=NULL) { 24 | if(strcmp(x,"#")==0) 25 | break; 26 | 27 | m=strlen(x); 28 | n=strlen(y); 29 | 30 | int len = LCSLength(m, n); 31 | printf("Case #%d: you can visit at most %d cities.\n",++cases,c[m][n]); 32 | 33 | //cout << "Case #" << ++cases << ": you can visit at most " << len << " cities." << endl; 34 | 35 | } 36 | return 0; 37 | } 38 | 39 | int LCSLength(int m , int n){ 40 | int i, j; 41 | 42 | for (i = 0; i <= m; ++i) 43 | c[i][0] = 0; 44 | 45 | for (j = 0; j <= n; ++j) 46 | c[0][j] = 0; 47 | 48 | for (i = 1; i <= m; ++i){ 49 | for (j = 1; j <= n; ++j){ 50 | if (x[i-1] == y[j-1]){ 51 | c[i][j] = c[i-1][j-1] + 1; 52 | } 53 | else if (c[i-1][j] >= c[i][j-1]){ 54 | c[i][j] = c[i-1][j]; 55 | } 56 | else{ 57 | c[i][j] = c[i][j-1]; 58 | } 59 | } 60 | } 61 | return c[m][n]; 62 | } 63 | -------------------------------------------------------------------------------- /10193.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: All You Need Is Love UVa 10193 2 | Programmer: Md. Mahmud Ahsan 3 | Description: AD HOC, GCD and Binary to decimal 4 | Compiled: Visual C++ 7.0 5 | Date: 03-06-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | typedef unsigned long ulint; 13 | 14 | ulint gcd(ulint x, ulint y){ 15 | ulint r; 16 | while (y != 0){ 17 | r = x % y; 18 | x = y; 19 | y = r; 20 | } 21 | return x; 22 | } 23 | 24 | int main(){ 25 | char strA[40], strB[40]; 26 | ulint lenA, lenB, len, result; 27 | int n, i, j, cases = 0; 28 | cin >> n; 29 | for (i = 0; i < n; ++i){ 30 | lenA = lenB = 0; 31 | cin >> strA >> strB; 32 | 33 | len = strlen(strA); 34 | for (j = 0; strA[j]; ++j) 35 | lenA += (strA[j] - 48) * pow (2, --len); 36 | len = strlen(strB); 37 | for (j = 0; j < strB[j]; ++j) 38 | lenB += (strB[j] - 48) * pow (2, --len); 39 | 40 | result = gcd(lenA, lenB); 41 | cout << "Pair #" << ++cases << ": "; 42 | 43 | if (result == 1) 44 | cout << "Love is not all you need!" << endl; 45 | else 46 | cout << "All you need is love!" << endl; 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /10195.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: The Knights Of The Round Table UVa 10195 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 19-08-04 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | double a, b, c; 14 | double s, r; 15 | 16 | while (cin >> a >> b >> c){ 17 | // special case 18 | if (a == 0.0 || b == 0.0 || c == 0.0){ 19 | // special case 20 | cout << "The radius of the round table is: 0.000" << endl; 21 | continue; 22 | } 23 | // law in geometry 24 | s = (a + b + c) / 2; 25 | r = ( sqrt(s * (s - a) * (s - b) * (s - c) ) ) / s; 26 | 27 | cout << setiosflags(ios :: fixed) << setprecision(3) << "The radius of the round table is: " << r << endl; 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /10197.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahmudahsan/203-ACM-Problems-Code/6e8f9698809c649a4c6bbba907d973e20360bc21/10197.cpp -------------------------------------------------------------------------------- /102.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Ecological Bin Packing UVa 102 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 25-08-04 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | // struct type 12 | struct Bin{ 13 | char style[4]; 14 | int amount; 15 | }; 16 | 17 | //prototype 18 | void printOutput(Bin obj[]); 19 | 20 | //================================================================= 21 | int main(){ 22 | int a, b, c, d, e, f, g, h, i; 23 | 24 | while (cin >> a >> b >> c >> d >> e >> f >> g >> h >> i){ 25 | Bin obj[6]; 26 | 27 | strcpy(obj[0].style, "BCG"); 28 | obj[0].amount = d + g + c + i + b + e; 29 | 30 | strcpy(obj[1].style, "BGC"); 31 | obj[1].amount = d + g + b + h + c + f; 32 | 33 | strcpy(obj[2].style, "CBG"); 34 | obj[2].amount = f + i + a + g + b + e; 35 | 36 | strcpy(obj[3].style, "CGB"); 37 | obj[3].amount = f + i + b + h + a + d; 38 | 39 | strcpy(obj[4].style, "GBC"); 40 | obj[4].amount = e + h + a + g + c + f; 41 | 42 | strcpy(obj[5].style, "GCB"); 43 | obj[5].amount = e + h + c + i + a + d; 44 | 45 | printOutput(obj); 46 | } 47 | 48 | return 0; 49 | } 50 | //====================================================================== 51 | 52 | void printOutput(Bin obj[]){ 53 | int temp = obj[0].amount; 54 | int flag = 0; 55 | 56 | for (int i = 1; i < 6; i++){ 57 | if (temp > obj[i].amount){ 58 | temp = obj[i].amount; 59 | flag = i; 60 | } 61 | } 62 | cout << obj[flag].style << " " << obj[flag].amount << endl; 63 | } -------------------------------------------------------------------------------- /10219.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Find the ways! UVa 10219 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 24-12-04 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | typedef double dbl; 12 | 13 | dbl nCr(long n, long r); 14 | 15 | int main(){ 16 | dbl n, r; 17 | double result; 18 | cout.precision(0); 19 | 20 | while (cin >> n >> r && !cin.eof()){ 21 | if (!n && !r) break; 22 | 23 | result = nCr(n, r); 24 | cout << result << endl; 25 | } 26 | return 0; 27 | } 28 | 29 | dbl nCr(long n, long r){ 30 | dbl result = 0; 31 | 32 | if (n - r < r) 33 | r = n - r; 34 | 35 | for (long i = 1; i <= r; i++) 36 | result = result + (log10(n--) - log10(i)); 37 | 38 | return floor(result) + 1; 39 | } -------------------------------------------------------------------------------- /10222.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Decode the Mad man UVa 10222 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 07-08-04 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | /* store the code special rules [ \\ \' ] */ 12 | char string[] = "`1234567890-=qwertyuiop[]\\asdfghjkl;\'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?"; 13 | char input[10000]; 14 | int i, j; 15 | int flag; 16 | 17 | while (cin.getline(input, sizeof(input))){ 18 | for (i = 0; input[i] != '\0'; i++){ 19 | flag = 0; //check input character is found int the string code 20 | for (j = 0; string[j] != '\0'; j++){ 21 | if (input[i] == string[j]){ 22 | cout << string[j-2]; // print the previous - 1 character 23 | flag = 1; // ensure that input character is found in the code 24 | break; 25 | } 26 | } 27 | if (flag == 0) cout << input[i]; 28 | } 29 | cout << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /10235.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Simply Emirp UVa 10235 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 29-03-05 5 | */ 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | const long length = 1000005; 11 | 12 | long seive[length]; 13 | void genSeive(); 14 | long reverse(long n); 15 | 16 | int main(){ 17 | genSeive(); 18 | long input, rev; 19 | bool prime, emirp; 20 | 21 | while (cin >> input){ 22 | prime = emirp = false; 23 | if (seive[input] == 0) 24 | prime = true; 25 | rev = reverse(input); 26 | if (seive[rev] == 0) 27 | emirp = true; 28 | 29 | cout << input; 30 | if (!prime) 31 | cout << " is not prime." << endl; 32 | else if (prime && (input == rev)) 33 | cout << " is prime." << endl; 34 | else if (prime && !emirp) 35 | cout << " is prime." << endl; 36 | else if (prime && emirp) 37 | cout << " is emirp." << endl; 38 | } 39 | 40 | return 0; 41 | } 42 | 43 | long reverse(long n){ 44 | int digit = floor(log10(n))+1; 45 | long m; 46 | long result = 0; 47 | 48 | while (n != 0){ 49 | m = n % 10; 50 | n = n / 10; 51 | 52 | if (digit > 1){ 53 | result = result + (m * pow(10, --digit)); 54 | } 55 | else 56 | result = result + m; 57 | } 58 | return result; 59 | } 60 | 61 | void genSeive(){ 62 | long i, j; 63 | 64 | seive[0]=seive[1] = 1; 65 | for(i = 2; i < length; ++i){ 66 | for (j = i + i; j < length; j += i) 67 | seive[j] = 1; 68 | } 69 | } -------------------------------------------------------------------------------- /10260.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Soundex UVa 10260 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 06-08-04 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | char code[92][2]; // first column contains character second contains code 12 | int i, j; 13 | 14 | for (i = 65; i <= 91; i++){ 15 | code[i][0] = i; 16 | } 17 | 18 | // initialize the seond column 19 | code['A'][1] = code['E'][1] = code['I'][1] = code['O'][1] = code['U'][1] = code['H'][1] = code['W'][1] = code['Y'][1] = '0'; 20 | code['B'][1] = code['F'][1] = code['P'][1] = code['V'][1] = '1'; 21 | code['C'][1] = code['G'][1] = code['J'][1] = code['K'][1] = code['Q'][1] = code['S'][1] = code['X'][1] = code['Z'][1] = '2'; 22 | code['D'][1] = code['T'][1] = '3'; 23 | code['L'][1] = '4'; 24 | code['M'][1] = code['N'][1] = '5'; 25 | code['R'][1] = '6'; 26 | 27 | char input[100]; 28 | 29 | while (cin >> input){ 30 | 31 | for (i = 0; input[i] != '\0'; i++){ 32 | for (j = 65; j <= 91; j++){ 33 | // search if input[i] equal to code[j][0] 34 | if ((input[i] == code[j][0]) && (code[j][1] != '0')){ 35 | // Words with the same Soundex coding are considered equivalent so 36 | if (code[input[i]][1] != code[input[i-1]][1]) 37 | cout << code[j][1]; 38 | } 39 | } 40 | } 41 | cout << endl; 42 | } 43 | return 0; 44 | } -------------------------------------------------------------------------------- /10268.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: F: 498 UVa 10268 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Horner's Rule + Derivative of the equation 4 | Compiled: Visual Studio .Net 5 | Date: 26-12-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | const long MX = 1000001; 15 | double array[MX], arrayD[MX], result, temp; 16 | char str1[MX], str2[MX], *ptr; 17 | 18 | double horner(int n, double array[], double x){ 19 | //horner compute the value of a polynomial of order n 20 | double p; 21 | p = array[n]; 22 | for (int i = n-1; i >= 0; --i){ 23 | p = p * x + array[i]; 24 | } 25 | return p; 26 | } 27 | 28 | int main(){ 29 | //freopen("input.txt", "r", stdin); 30 | //freopen("output.txt", "w", stdout); 31 | int x, i, order; 32 | 33 | while (cin.getline(str1, sizeof(str1))){ 34 | cin.getline(str2, sizeof(str2)); 35 | 36 | sscanf(str1, "%d", &x); 37 | 38 | //separate the coefficients and store in the array 39 | i = -1; 40 | ptr = strtok(str2, " \t\n"); 41 | while (ptr){ 42 | ++i; 43 | sscanf(ptr, "%lf", &array[i]); 44 | 45 | ptr = strtok(NULL, " \t\n"); 46 | } 47 | temp = i; 48 | order = --i; //after derivative order decrease by 1 49 | for (int j = 0; j <= order; ++j){ 50 | arrayD[j] = temp-- * array[j]; 51 | } 52 | 53 | reverse(arrayD, arrayD+order+1); 54 | 55 | result = horner(order, arrayD, x); 56 | //cout << result << endl; 57 | printf("%.0lf\n", result); 58 | } 59 | return 0; 60 | } -------------------------------------------------------------------------------- /10279.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Minesweeper UVa 10279 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Grid Searching 4 | Compiled: Visual C++ 7.0 5 | Date: 17-02-06 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | char data[20][20]; 12 | char str[20][20]; 13 | char output[20][20]; 14 | 15 | int dr[] = {-1, -1, -1, 0, 1, 1, 1, 0}; 16 | int dc[] = {-1, 0, 1, 1, 1, 0, -1, -1}; 17 | 18 | void init(int n){ 19 | for (int i = 0; i < n; ++i){ 20 | for (int j = 0; j < n; ++j) 21 | output[i][j] = '.'; 22 | } 23 | } 24 | 25 | char check(int n, int R, int C){ 26 | int i, r, c, num = 0; 27 | 28 | for (i = 0; i < 8; ++i){ 29 | r = R + dr[i]; 30 | c = C + dc[i]; 31 | 32 | if ( r >= 0 && r < n && c >= 0 && c < n){ 33 | if (data[r][c] == '*') 34 | ++num; 35 | } 36 | } 37 | return num+48; 38 | } 39 | 40 | void checkAll(int n){ 41 | for (int i = 0; i < n; ++i){ 42 | for (int j = 0; j < n; ++j){ 43 | if (data[i][j] == '*') 44 | output[i][j] = '*'; 45 | } 46 | } 47 | } 48 | 49 | int main(){ 50 | //freopen("input.txt", "r", stdin); 51 | //freopen("output.txt", "w", stdout); 52 | bool flag = false; 53 | 54 | int test, i, j, t, n; 55 | 56 | cin >> test; 57 | while (test--){ 58 | cin >> n; 59 | 60 | if (flag) cout << endl; flag = true; 61 | 62 | init(n); 63 | for (t = 0; t < n; ++t) 64 | cin >> data[t]; 65 | 66 | for (t = 0; t < n; ++t) 67 | cin >> str[t]; 68 | 69 | for (i = 0; i < n; ++i){ 70 | for (j = 0; j < n; ++j){ 71 | if (data[i][j] == '.' && str[i][j] == 'x'){ 72 | output[i][j] = check(n, i, j); 73 | } 74 | if (data[i][j] == '*' && str[i][j] == 'x'){ 75 | checkAll(n); 76 | } 77 | } 78 | } 79 | for (i = 0; i < n; ++i){ 80 | for (j = 0; j < n; ++j) 81 | cout << output[i][j]; 82 | cout << endl; 83 | } 84 | } 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /10281.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Average Speed UVa 10281 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 15-04-05 5 | */ 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | char buffer[1000]; 12 | int n, time = 0, now; // now = current time, time = elapsed time 13 | int hour, min, sec, speed=0, newSpeed; 14 | double distance=0; 15 | 16 | while (cin.getline(buffer, sizeof(buffer))){ 17 | n = sscanf(buffer, "%d:%d:%d %d", &hour, &min, &sec, &newSpeed); 18 | now = hour * 3600 + min * 60 + sec; 19 | 20 | distance += (now - time) * speed; 21 | time = now; 22 | 23 | if (n == 3) 24 | printf("%02d:%02d:%02d %.2lf km\n", hour, min, sec, distance/3600); 25 | else if (n == 4) 26 | speed = newSpeed; 27 | } 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /10282.cpp: -------------------------------------------------------------------------------- 1 | /* UVA: Babelfish prob: 10282 2 | Programmer: Md. Mahmud Ahsan 3 | Visual C++ 6 4 | IN Lab 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | struct dic{ 14 | char foreign[12]; 15 | char english[12]; 16 | }dict[100005]; 17 | 18 | int bSearch(char *str, int max){ 19 | int mid, low, high, cmp; 20 | low = 0; 21 | high = max; 22 | 23 | while (low <= high){ 24 | mid = (low + high) / 2; 25 | 26 | cmp = strcmp(dict[mid].foreign, str); 27 | if (cmp == 0) 28 | return mid; 29 | else if (cmp > 0) 30 | high = mid - 1; 31 | else 32 | low = mid + 1; 33 | } 34 | return -1; 35 | } 36 | 37 | int cmp(const void *a, const void *b){ 38 | dic *x, *y; 39 | x = (dic*) a; 40 | y = (dic*) b; 41 | 42 | return strcmp(x->foreign, y->foreign); 43 | } 44 | 45 | 46 | int main(){ 47 | freopen("input.txt", "r", stdin); 48 | 49 | int i = 0, j; 50 | char str[100], a[100], b[100]; 51 | 52 | while(gets(str)){ 53 | if (strlen(str) == 0) break; 54 | 55 | sscanf(str, "%s %s", a, b); 56 | 57 | strcpy(dict[i].english, a); 58 | strcpy(dict[i].foreign, b); 59 | ++i; 60 | } 61 | qsort(dict, i, sizeof(dict[0]), cmp); 62 | 63 | 64 | while (cin >> str){ 65 | j = bSearch(str, i); 66 | 67 | if (j >= 0) 68 | cout << dict[j].english << endl; 69 | else 70 | cout << "eh" << endl; 71 | } 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /10293.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Word Length and Frequency UVa 10293 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Ad-hoc 4 | Compiled: Visual C++ 7.0 5 | Date: 17-08-06 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int data[50]; 12 | void init(){ 13 | for (int i = 0; i < 50; ++i) 14 | data[i] = 0; 15 | } 16 | 17 | int main(){ 18 | //freopen("input.txt", "r", stdin); 19 | char ch, prev; 20 | int i, j, len; 21 | bool flag = false; 22 | 23 | while (!cin.eof()){ 24 | init(); 25 | do{ 26 | len = 0; 27 | ch = cin.get(); 28 | prev = ch; 29 | if (cin.eof()) { 30 | flag = true; 31 | break; 32 | } 33 | 34 | while (ch!=' '&&ch!='?'&&ch!='!'&&ch!=','&&ch!='.'){ 35 | if (ch == '#') break; 36 | if (ch == '\'' || ch == '-' || ch == '\n'){ 37 | if (ch == '\n' && prev != '-'){ 38 | break; 39 | } 40 | prev = ch; 41 | ch = cin.get(); 42 | continue; 43 | } 44 | ++len; 45 | prev = ch; 46 | ch = cin.get(); 47 | } 48 | ++data[len]; 49 | }while (ch != '#'); 50 | 51 | if (flag) break; 52 | for (i = 1; i < 50; ++i){ 53 | if (data[i] != 0) 54 | cout << i << " " << data[i]<< endl; 55 | } 56 | cout << endl; 57 | } 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /10295.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Hay Points UVa 10295 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Sorting + Searching 4 | Compiled: Visual C++ 7.0 5 | Date: 18-09-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | struct word{ 14 | string str; 15 | int dollar; 16 | }dics[1200]; 17 | 18 | inline bool comp(word a, word b){ 19 | if (a.str > b.str) return false; 20 | return true; 21 | } 22 | 23 | int binarySearch(string a, int h){ 24 | int low, mid, high = h; 25 | low = 0; 26 | 27 | while (low <= high){ 28 | mid = (high + low) / 2; 29 | 30 | if (dics[mid].str == a) 31 | return mid; 32 | else if (dics[mid].str < a) 33 | low = mid + 1; 34 | else 35 | high = mid - 1; 36 | } 37 | return -1; 38 | } 39 | 40 | int main(){ 41 | int m, n, k, i, dollar; 42 | string a; 43 | 44 | while (cin >> m >> n){ 45 | for (i = 0; i < m; ++i) 46 | cin >> dics[i].str >> dics[i].dollar; 47 | 48 | sort(dics, dics+m, comp); 49 | 50 | for (i = 0; i < n; ++i){ 51 | dollar = 0; 52 | while (cin >> a){ 53 | if (a == ".") break; 54 | 55 | k = binarySearch(a, m); 56 | if (k != -1) 57 | dollar += dics[k].dollar; 58 | } 59 | cout << dollar << endl; 60 | } 61 | } 62 | 63 | return 0; 64 | } -------------------------------------------------------------------------------- /10300.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Ecological Premium UVa 10300 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 20-10-04 5 | */ 6 | 7 | /* let a = area, b = animals, c = envirounment friendliness 8 | the given condition 9 | ((a / b) * c ) * c, it can be expressed 10 | = a * c 11 | */ 12 | 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main(){ 18 | int test, farmer; 19 | double a, b, c, result; 20 | int i, j; 21 | 22 | cin >> test; 23 | 24 | for (i = 0; i < test; i++){ 25 | result = 0; 26 | cin >> farmer; 27 | 28 | for (j = 0; j < farmer; j++){ 29 | cin >> a >> b >> c; 30 | result += a * c; 31 | } 32 | printf("%.0lf\n", result); 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /10302.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Summation of Polynomials UVa 10302 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 23-03-05 5 | */ 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | long double array[50001]; 12 | 13 | int main(){ 14 | long double result; 15 | long double input; 16 | 17 | int i, k = 1; 18 | for (i = 0; i < 50001; i++){ 19 | array[i] = pow(k, 3); 20 | ++k; 21 | } 22 | 23 | 24 | while (cin >> input){ 25 | result = 0; 26 | for (i = 0; i < input; ++i) 27 | result = result + array[i]; 28 | printf("%.0LF\n", result); 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /10324.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Zeros And Ones UVa 10324 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 06-04-05 5 | */ 6 | #include 7 | using namespace std; 8 | 9 | char str[1000005], tt[10]; 10 | long data[1000005]; 11 | 12 | 13 | int main(){ 14 | //freopen("input.txt", "r", stdin); 15 | //freopen("output.txt", "w", stdout); 16 | long cases, i, j, k,l, counter; 17 | char c; 18 | cases = 0; 19 | 20 | while ( cin.getline(str, sizeof(str))){ 21 | k = strlen(str); 22 | 23 | if (k == 0) break; 24 | 25 | counter = 0; 26 | l = -1; 27 | c = str[0]; 28 | 29 | for(i = 0; i < k; ++i){ 30 | if (c != str[i]){ 31 | c = str[i]; 32 | ++counter; 33 | } 34 | data[++l] = counter; 35 | } 36 | 37 | cin >> k; 38 | cout << "Case " << ++cases << ":"<< endl; 39 | for(l = 0; l < k; ++l){ 40 | cin >> i >> j; 41 | if (data[i] == data[j]) 42 | cout << "Yes" << endl; 43 | else 44 | cout << "No" << endl; 45 | } 46 | cin.getline(tt, sizeof(tt)); // eat blankline 47 | } 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /10327.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Flip Short UVa 10327 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | */ 5 | 6 | #include 7 | 8 | int bubble(int array[], int size){ 9 | int i, j, exchange, temp; 10 | 11 | exchange = 0; 12 | 13 | for (i = 0; i < size; i++){ 14 | for (j = 0; j < size - 1; j++){ 15 | if (array[j] > array[j+1]){ 16 | temp = array[j]; 17 | array[j] = array[j+1]; 18 | array[j+1] = temp; 19 | exchange += 1; 20 | } 21 | } 22 | } 23 | return exchange; 24 | } 25 | 26 | 27 | int main(){ 28 | int *p; 29 | int n, i, exchange; 30 | 31 | while (cin >> n){ 32 | p = new int[n]; 33 | for (i = 0; i < n; i++){ 34 | cin >> p[i]; 35 | } 36 | 37 | exchange = bubble(p, n); 38 | cout << "Minimum exchange operations : " << exchange << endl; 39 | } 40 | delete [] p; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /10338.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Mischievous Children UVa 10338 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 18-03-05 5 | */ 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | typedef long double ld; 12 | 13 | ld factorial(int n){ 14 | ld total = 1; 15 | for (int i = n; i > 0; --i) 16 | total = total * i; 17 | 18 | return total; 19 | } 20 | 21 | void init(int array[][2]){ 22 | int i; 23 | for (i = 65; i < 91; i++){ 24 | array[i][0] = i; 25 | array[i][1] = 0; 26 | } 27 | } 28 | 29 | void search(char *str, int array[][2]){ 30 | int len, i, j; 31 | len = strlen(str); 32 | 33 | for (i = 0; i < len; i++){ 34 | for (j = 65; j < 91; j++){ 35 | if (str[i] == array[j][0]) 36 | array[j][1] = array[j][1] + 1; 37 | } 38 | } 39 | } 40 | 41 | int main(){ 42 | char str[100]; 43 | int array[92][2]; 44 | 45 | int limit, len, j; 46 | ld result; 47 | 48 | cin >> limit; 49 | for (int i = 0; i < limit; ++i){ 50 | cin >> str; 51 | 52 | init(array); 53 | search(str, array); 54 | len = strlen(str); 55 | result = factorial(len); 56 | 57 | for (j = 65; j < 91; ++j){ 58 | if (array[j][1] != 0) 59 | result = result / factorial(array[j][1]); 60 | } 61 | 62 | 63 | cout << "Data set " << i+1 << ": "; 64 | printf("%.0llf\n", result); 65 | 66 | } 67 | return 0; 68 | } -------------------------------------------------------------------------------- /10340.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: All in All UVa 10340 2 | Programmer: Md. Mahmud Ahsan 3 | Description: SET 4 | Compiled: Visual C++ 7.0 5 | Date: 13-02-06 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | const int MX = 5000000; 13 | char s[MX], t[MX]; 14 | 15 | bool chkSub(){ 16 | int lenS = strlen(s); 17 | int lenT = strlen(t); 18 | int i, m = 0; 19 | 20 | for (i = 0; i < lenT; ++i){ 21 | if (s[m] == t[i]){ 22 | ++m; 23 | if (m >= lenS) 24 | return true; 25 | } 26 | } 27 | return false; 28 | } 29 | 30 | int main(){ 31 | while (cin >> s >> t){ 32 | if (chkSub()) 33 | cout << "Yes" << endl; 34 | else 35 | cout << "No" << endl; 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /10341.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Solve It UVa 10341 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Bisection Method 4 | Compiled: Visual Studio .Net 5 | Date: 24-12-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | const long double ERROR = 1e-14; 13 | 14 | //global 15 | long double root, x0, x1, x2,f0, f1, f2; 16 | long double p, q, r, s, t, u; 17 | 18 | long double func(long double x){ 19 | // p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 20 | double temp; 21 | temp = (p * exp(-x) + (q * sin(x)) + (r * cos(x)) + (s * tan(x)) + (t * x * x) + u); 22 | return temp; 23 | } 24 | 25 | long double bisection(){ 26 | long double x1 = 1.0; 27 | long double x2 = 0.0; 28 | 29 | f1 = func(x1); 30 | f2 = func(x2); 31 | 32 | if (fabs(f1) < ERROR) 33 | return x1; 34 | if (fabs(f2) < ERROR) 35 | return x2; 36 | 37 | if (f1 * f2 > 0.0) 38 | return -1; 39 | 40 | while (true){ 41 | x0 = (x1 + x2) / 2.0; 42 | f0 = func(x0); 43 | 44 | if (f1 * f0 < 0.0) 45 | x2 = x0; 46 | else{ 47 | x1 = x0; 48 | f1 = f0; 49 | } 50 | if (fabs( (x2 - x1) / x2) < ERROR){ 51 | root = ( x1 + x2) / 2.0; 52 | return root; 53 | } 54 | } 55 | return -1; 56 | } 57 | 58 | int main(){ 59 | long double temp; 60 | while (cin >> p >> q >> r >> s >> t >> u){ 61 | temp = bisection(); 62 | if (temp < 0) 63 | cout << "No solution" << endl; 64 | else 65 | printf("%.4Lf\n", temp); 66 | } 67 | return 0; 68 | } -------------------------------------------------------------------------------- /10346.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahmudahsan/203-ACM-Problems-Code/6e8f9698809c649a4c6bbba907d973e20360bc21/10346.cpp -------------------------------------------------------------------------------- /10347.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Medians UVa 10347 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Geometry 4 | Compiled: Visual C++ 7.0 5 | Date: 26-10-05 6 | */ 7 | /* 8 | if m1,m2,m3 are medians 9 | s = 0.5 * (m1+m2+m3) 10 | area = (4/3) * sqrt(s * (s-m1) * (s-m2) * (s-m3)) 11 | */ 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main(){ 18 | double m1, m2, m3, s, area; 19 | 20 | while (cin >> m1 >> m2 >> m3){ 21 | s = 0.5 * (m1+m2+m3); 22 | area = (4.0 / 3.0) * sqrt ( s * (s-m1) * (s-m2) * (s-m3)); 23 | 24 | if (area > 0.0) 25 | printf("%.3lf\n", area); 26 | else 27 | printf("-1.000\n"); 28 | } 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /10361.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Automatic Poetry UVa 10361 2 | Programmer: Md. Mahmud Ahsan 3 | Description: String Processing 4 | Compiled: Visual C++ 7.0 5 | Date: 20-08-05 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | char strA[200], strB[200]; 14 | int n, i, j,k, brak; 15 | bool flag; 16 | 17 | cin >> n; 18 | cin.getline(strA, sizeof(strA)); // eat the newline 19 | for (i = 0; i < n; ++i){ 20 | cin.getline(strA, sizeof(strA)); 21 | cin.getline(strB, sizeof(strB)); 22 | 23 | //first output 24 | for (j = 0; strA[j]; ++j) 25 | if (strA[j] != '>' && strA[j] != '<') 26 | cout << strA[j]; 27 | cout << endl; 28 | 29 | //second output 30 | for (j = 0; strB[j]; ++j){ 31 | if (strB[j] == '.') 32 | break; 33 | cout << strB[j]; 34 | } 35 | // cout << s4 << s3 << s2 << s5 << endl; 36 | brak = 0; 37 | for (j = 0; strA[j]; ++j){ // for s4 38 | if (strA[j] == '<') 39 | ++brak; 40 | if (brak == 2){ 41 | for (k = j+1; strA[k] != '>'; ++k) 42 | cout << strA[k]; 43 | break; 44 | } 45 | } 46 | for (j = 0; strA[j]; ++j){ // for s3 47 | if (strA[j] == '>'){ 48 | for (k = j+1; strA[k] != '<'; ++k) 49 | cout << strA[k]; 50 | break; 51 | } 52 | } 53 | for (j = 0; strA[j]; ++j){ // for s2 54 | if (strA[j] == '<'){ 55 | for (k = j+1; strA[k] != '>'; ++k) 56 | cout << strA[k]; 57 | break; 58 | } 59 | } 60 | brak = 0; 61 | for (j = 0; strA[j]; ++j){ // for s5 62 | if (strA[j] == '>') 63 | ++brak; 64 | if (brak == 2){ 65 | for (k = j+1; strA[k]; ++k) 66 | cout << strA[k]; 67 | break; 68 | } 69 | } 70 | cout << endl; 71 | } 72 | 73 | return 0; 74 | } -------------------------------------------------------------------------------- /10363.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Tic Tac Toe UVa 10363 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Ad-hoc 4 | Compiled: Visual C++ 7.0 5 | Date: 21-08-06 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | char grid[5][5]; 12 | 13 | bool chkWin(char c){ 14 | int i, r1, r2, r3; 15 | r1 = r2 = r3 = 0; 16 | for (i = 0; i < 3; ++i){ 17 | if (grid[0][i] == c) ++r1; 18 | if (grid[1][i] == c) ++r2; 19 | if (grid[2][i] == c) ++r3; 20 | } 21 | if (r1 == 3 || r2 == 3 || r3 == 3) return true; 22 | 23 | //now r stands for col 24 | r1 = r2 = r3 = 0; 25 | for (i = 0; i < 3; ++i){ 26 | if (grid[i][0] == c) ++r1; 27 | if (grid[i][1] == c) ++r2; 28 | if (grid[i][2] == c) ++r3; 29 | } 30 | if (r1 == 3 || r2 == 3 || r3 == 3) return true; 31 | 32 | if (grid[0][0] == c && grid[1][1] == c && grid[2][2] == c) return true; 33 | if (grid[0][2] == c && grid[1][1] == c && grid[2][0] == c) return true; 34 | 35 | return false; 36 | } 37 | 38 | int count(char c){ 39 | int temp = 0; 40 | for (int i = 0; i < 3; ++i){ 41 | for (int j = 0; j < 3; ++j) 42 | if (grid[i][j] == c) 43 | ++temp; 44 | } 45 | return temp; 46 | } 47 | 48 | int main(){ 49 | //freopen("input.txt", "r", stdin); 50 | int n, x, o; 51 | bool oWin, xWin, possible; 52 | 53 | cin >> n; 54 | 55 | while (n--){ 56 | possible = true; 57 | 58 | cin >> grid[0]; 59 | cin >> grid[1]; 60 | cin >> grid[2]; 61 | 62 | xWin = chkWin('X'); 63 | oWin = chkWin('O'); 64 | 65 | x = count('X'); 66 | o = count('O'); 67 | 68 | while(true){ 69 | if (xWin && oWin) { possible = false; break;} 70 | if (x < o || x > o+1) { possible = false; break;} 71 | if (xWin && x != o+1) { possible = false; break;} 72 | if (oWin && o != x) { possible = false; break;} 73 | break; 74 | } 75 | 76 | if (possible) cout << "yes" << endl; 77 | else cout << "no" << endl; 78 | } 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /10370.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Above Average UVa 10370 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 06-08-04 5 | */ 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | int input; // number of test cases 12 | int *mark; // dynamic memory allocation 13 | int i, j; 14 | double sum, average, result; 15 | int student; 16 | double belowAverage; 17 | 18 | cin >> input; 19 | 20 | for (i = 0; i < input; i++){ 21 | cin >> student; 22 | 23 | sum = 0; 24 | belowAverage = 0; 25 | mark = new int[student]; 26 | for (j = 0; j < student; j++){ 27 | cin >> mark[j]; 28 | sum += mark[j]; 29 | } 30 | 31 | average = sum / student; 32 | for (j = 0; j < student; j++){ 33 | if (mark[j] > average) 34 | belowAverage += 1; 35 | } 36 | 37 | result = (belowAverage / student) * 100; 38 | printf("%.3f%c\n", result, '%'); 39 | 40 | delete [] mark; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /10377.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Maze Traversal UVa 10377 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Simulation 4 | Compiled: Visual C++ 7.0 5 | Date: 18-09-05 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | char dir = 'N'; 12 | string coridor[100]; 13 | int test, row, col, i, _row, _col; 14 | 15 | void forward(){ 16 | if (dir == 'E'){ 17 | if (coridor[_row][_col+1] != '*') 18 | ++_col; 19 | } 20 | else if (dir == 'W'){ 21 | if (coridor[_row][_col-1] != '*') 22 | --_col; 23 | } 24 | else if (dir == 'S'){ 25 | if (coridor[_row+1][_col] != '*') 26 | ++_row; 27 | } 28 | else if (dir == 'N'){ 29 | if (coridor[_row-1][_col] != '*') 30 | --_row; 31 | } 32 | } 33 | 34 | void right(){ 35 | switch(dir){ 36 | case 'N': dir = 'E'; break; 37 | case 'E': dir = 'S'; break; 38 | case 'S': dir = 'W'; break; 39 | case 'W': dir = 'N'; break; 40 | } 41 | } 42 | 43 | void left(){ 44 | switch(dir){ 45 | case 'N': dir = 'W'; break; 46 | case 'W': dir = 'S'; break; 47 | case 'S': dir = 'E'; break; 48 | case 'E': dir = 'N'; break; 49 | } 50 | } 51 | 52 | int main(){ 53 | char c, str[200]; 54 | cin >> test; 55 | bool flag = false; 56 | 57 | while (test--){ 58 | cin >> row >> col; 59 | cin.getline(str, sizeof(str)); // eat blank line 60 | for (i = 0; i < row; ++i){ 61 | cin.getline(str, sizeof(str)); 62 | coridor[i] = str; 63 | } 64 | 65 | cin >> _row >> _col; 66 | dir = 'N'; 67 | --_row; --_col; 68 | 69 | while ( (c = cin.get()) != 'Q'){ 70 | switch(c){ 71 | case 'R': right(); break; 72 | case 'L': left(); break; 73 | case 'F': forward(); break; 74 | } 75 | } 76 | if (flag) cout << endl; flag = true; 77 | cout << _row+1 << " " << _col+1 << " " << dir << endl; 78 | } 79 | 80 | return 0; 81 | } -------------------------------------------------------------------------------- /10394.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Twin Primes UVa 10394 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Prime Number 4 | Compiled: Visual C++ 7.0 5 | Date: 13-08-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | #define max 20000000 13 | char seive[max]; 14 | int prime[100002]; 15 | 16 | //modified seive of eratosthenes 17 | void genSeive(){ 18 | int i, j, sq; 19 | seive[0] =seive[1] = 1; 20 | sq = sqrt( (double) max); 21 | for (i = 2; i <= sq; ++i){ 22 | if (!seive[i]){ 23 | for (j = i * i; j < max; j+=i) 24 | seive[j] = 1; 25 | } 26 | } 27 | //for twin prime modification 28 | j = 0; 29 | for (i = 2; i < max; ++i){ 30 | if (!seive[i] && !seive[i + 2]) 31 | prime[++j] = i; 32 | if (j == 100001) break; 33 | } 34 | } 35 | 36 | 37 | 38 | int main(){ 39 | genSeive(); 40 | int n; 41 | while (cin >> n){ 42 | cout << "(" << prime[n] << ", " << prime[n] + 2 << ")" << endl; 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /10405.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Longest Common Subsequence UVa 10405 2 | Programmer: Md. Mahmud Ahsan 3 | Description: LCS 4 | Compiled: Visual C++ 7.0 5 | Date: 20-11-05 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | #define N 1005 12 | 13 | //Global Declaration 14 | int c[N+1][N+1]; 15 | //char b[N+1][N+1]; 16 | 17 | int LCSLength(char *x, char *y, int m, int n); 18 | //void printLCS(char *x, int i, int j); // i for x and j for y 19 | 20 | int main(){ 21 | char x[N], y[N]; 22 | 23 | while (cin.getline(&x[1], sizeof(x))){ 24 | cin.getline(&y[1], sizeof(y)); 25 | 26 | int m = strlen(&x[1]); // as string index begins from 1 27 | int n = strlen(&y[1]); // as string index begins from 1 28 | 29 | int len = LCSLength(x, y, m, n); 30 | cout << len << endl; 31 | //printLCS(x, m, n); 32 | //cout << endl; 33 | } 34 | 35 | return 0; 36 | } 37 | 38 | int LCSLength(char *x, char *y, int m , int n){ 39 | int i, j; 40 | 41 | /* initialize all to zero */ 42 | for (i = 0; i <= m; ++i) 43 | for (j = 0; j <= n; ++j) 44 | c[i][j] = 0; 45 | 46 | for (i = 1; i <= m; ++i){ 47 | for (j = 1; j <= n; ++j){ 48 | if (x[i] == y[j]){ 49 | c[i][j] = c[i-1][j-1] + 1; 50 | //b[i][j] = '#'; //# for angle arrow 51 | } 52 | else if (c[i-1][j] >= c[i][j-1]){ 53 | c[i][j] = c[i-1][j]; 54 | //b[i][j] = '^'; //^ for up arrow 55 | } 56 | else{ 57 | c[i][j] = c[i][j-1]; 58 | //b[i][j] = '<'; //< for left arrow 59 | } 60 | } 61 | } 62 | return c[m][n]; 63 | } 64 | /* 65 | void printLCS(char *x, int i, int j){ 66 | if (i == 0 || j == 0) return; 67 | 68 | if (b[i][j] == '#'){ 69 | printLCS(x, i-1, j-1); 70 | cout << x[i]<< " "; 71 | } 72 | else if (b[i][j] == '^') 73 | printLCS(x, i-1, j); 74 | else 75 | printLCS(x, i, j-1); 76 | } 77 | */ 78 | -------------------------------------------------------------------------------- /10407.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Simple Division UVa 10407 2 | Programmer: Md. Mahmud Ahsan 3 | Description: GCD, for divisor, subtract smallest number from all other then gcd 4 | Compiled: Visual C++ 7.0 5 | Date: 03-06-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | long gcd(long x, long y){ 13 | long r; 14 | while (y != 0){ 15 | r = x % y; 16 | x = y; 17 | y = r; 18 | } 19 | return x; 20 | } 21 | 22 | int main(){ 23 | long small, next, temp; 24 | 25 | while (cin >> small){ 26 | if (small == 0) break; 27 | cin >> next; 28 | temp = abs(next - small); 29 | while (cin >> next){ 30 | if (next == 0) break; 31 | temp = gcd(temp, abs(next-small)); 32 | } 33 | cout << temp << endl; 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /10420.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: List of Conquests UVa 10420 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 18-10-04 5 | */ 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | // cmp function for quick sort 12 | int cmp(const void *a, const void *b){ 13 | return strcmp( (char*) a, (char*) b); 14 | } 15 | 16 | int main(){ 17 | char str[2200][100]; // list of countries 18 | char temp[100]; 19 | int input; 20 | int i, j; 21 | 22 | cin >> input; 23 | 24 | for (i = 0; i < input; i++){ 25 | cin >> str[i]; 26 | cin.getline(temp, sizeof(temp)); // to ignore woman's name 27 | } 28 | 29 | qsort(str, input, sizeof(str[0]), cmp); // alphabetical order 30 | 31 | int count; 32 | for (i = 0; i < input; i+= count){ 33 | count = 0; 34 | cout << str[i] << " "; 35 | for (j = i; j < input; j++){ 36 | if (strcmp(str[i], str[j]) == 0) 37 | ++count; 38 | } 39 | cout << count << endl; 40 | } 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /10432.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Polygon Inside A Circle UVa 10432 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 19-08-04 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | 13 | int main(){ 14 | 15 | int n; 16 | double area, dn, dr; 17 | double al; 18 | 19 | while (scanf("%lf %d",&dr,&n) == 2){ 20 | dn = (double) n; 21 | al = sin(acos(-1.0)*2.0/n); 22 | area = (al*dr*dr*dn)*0.5; 23 | printf("%.3lf\n",area); 24 | } 25 | return 0; 26 | } 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /10450.CPP: -------------------------------------------------------------------------------- 1 | /* Prblem: World Cup Noise Uva: 10450 2 | Programmer: Md. Mahmud Ahsan 3 | Date: 09-01-05 4 | */ 5 | 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | typedef long long lint; 11 | 12 | int main(){ 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | 16 | lint input, testCase; 17 | lint array[52]; 18 | lint i; 19 | int cases = 0; 20 | int flag = 0; 21 | array[1] = 2; 22 | array[2] = 3; 23 | 24 | for (i = 3; i < 52; i++) 25 | array[i] = array[i-1] + array[i-2]; 26 | cin >> testCase; 27 | for (int t = 0; t < testCase; ++t){ 28 | cin >> input; 29 | 30 | cout << "Scenario #" << ++cases << ":" << endl; 31 | cout << array[input] << endl << endl; 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /10469.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: To Carry or not to Carry UVa 10469 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 09-08-04 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | /* Let p and q be propostions. The exclusive or XOR of p and q is the propostion 11 | That is true when exactly one of p or q is true and false otherwise */ 12 | 13 | int main(){ 14 | unsigned long int input1, input2, result; 15 | 16 | while (cin >> input1 >> input2){ 17 | // See the logic diagram and found there showed to do XOR 18 | result = input1 ^ input2; 19 | cout << result << endl; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /10473.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Simple Base Conversion UVa 10473 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 14-05-04 5 | */ 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | char str[100]; 13 | int result, exit; 14 | while (cin >> str){ 15 | exit = atoi(str); 16 | if (exit < 0) break; 17 | 18 | if ( (strlen(str) > 2) && (str[1] == 'x' || str[1] == 'X')){ 19 | sscanf(str, "%X", &result); 20 | cout << result << endl; 21 | } 22 | else{ 23 | sscanf(str, "%d", &result); 24 | printf("0x%X\n", result); 25 | } 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /10474.CPP: -------------------------------------------------------------------------------- 1 | /* Problem: Where is the Marble? UVa 10474 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 14-05-04 5 | */ 6 | #include 7 | #include 8 | //using namespace std; 9 | 10 | // use for built in quick sort 11 | int cmp_func(const void *a, const void * b){ 12 | int *_a, *_b; 13 | _a = (int *) a; 14 | _b = (int *) b; 15 | if ( *_a < *_b) return -1; 16 | else if (*_a == *_b) return 0; 17 | else return 1; 18 | } 19 | 20 | int linearSearch(int array[], int n, int item); 21 | //=============================================== 22 | int main(){ 23 | int *data; 24 | int n, q, result, query; 25 | int i, cases = 0; 26 | 27 | while (cin >> n >> q){ 28 | if (n == 0 && q == 0) break; 29 | 30 | data = new int[n]; 31 | for (i = 0; i < n; ++i) 32 | cin >> data[i]; 33 | 34 | qsort(data, n, sizeof(data), cmp_func); 35 | 36 | cout << "CASE# " << ++cases << ":" << endl; 37 | for (i = 0; i < q; ++i){ 38 | cin >> query; 39 | result = linearSearch(data, n, query); 40 | 41 | if (result != -1){ 42 | cout << query << " found at " << ++result << endl; 43 | } 44 | else 45 | cout << query << " not found" << endl; 46 | } 47 | delete[] data; 48 | } 49 | return 0; 50 | } 51 | //================================================ 52 | int linearSearch(int array[], int n, int item){ 53 | int i; 54 | for (i = 0; i < n; ++i){ 55 | if (array[i] == item) 56 | return i; 57 | } 58 | return -1; 59 | } -------------------------------------------------------------------------------- /10489.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Boxes of Chocolates UVa 10489 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Modular Arithmetic 4 | Compiled: Visual C++ 7.0 5 | Date: 21-08-06 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | //freopen("input.txt", "r", stdin); 13 | long long test, friends, boxes, result, i, j; 14 | long long temp, temp2, finalResult; 15 | 16 | cin >> test; 17 | 18 | while(test--){ 19 | cin >> friends >> boxes; 20 | finalResult = 0; 21 | 22 | for (i = 0; i < boxes; ++i){ 23 | cin >> temp; 24 | result = 1; 25 | 26 | for (j = 0; j < temp; ++j){ 27 | cin >> temp2; 28 | result = (temp2 * result) % friends; 29 | } 30 | finalResult += result % friends; 31 | finalResult = finalResult % friends; 32 | } 33 | cout << finalResult << endl; 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /10490.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Mr. Azad and His Son!!!!! UVa 10490 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 14-04-05 5 | */ 6 | 7 | //** One Concept found: except '2' even numbers aren't perfect not prime provided the law 8 | //** 11, 23, 29 are prime but not perfect 9 | //** Others primes <=31 are perfect 10 | //** and help from steven Halim 11 | 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | /* Doing Precalculation 17 | bool chkPrime(long long n); 18 | bool chkPerfect(long long n); 19 | */ 20 | 21 | int main(){ 22 | long long data; 23 | int input; 24 | //bool prime, perfect; 25 | bool flag; 26 | int prime[] = {2, 3, 5, 7, 13, 17, 19, 31}; 27 | int notPerfect[] = {11, 23, 29}; 28 | 29 | while (cin >> input){ 30 | if (input == 0) break; 31 | data = ( pow (2 ,(input - 1)) ) * (( pow(2, input))-1); 32 | flag = false; 33 | /* 34 | cout << "Data "<< data << endl; 35 | prime = chkPrime(input); // check giver number for prime 36 | perfect = chkPerfect(data); 37 | */ 38 | for (int i = 0; i < 8; ++i){ 39 | if (prime[i] == input){ 40 | cout << "Perfect: " << data << "!" << endl; 41 | flag = true; 42 | } 43 | } 44 | if (flag) continue; 45 | 46 | if (notPerfect[0] == input || notPerfect[1] == input || notPerfect[2] == input){ 47 | cout << "Given number is prime. But, NO perfect number is available." << endl; 48 | } 49 | else 50 | cout << "Given number is NOT prime! NO perfect number is available." << endl; 51 | } 52 | return 0; 53 | } 54 | 55 | /* 56 | bool chkPrime(long long n){ 57 | if (n == 1) return false; 58 | long long k = sqrt( (double) n); 59 | long long i; 60 | for(i = 2; i <= k; ++i){ 61 | if (n % i == 0) 62 | return false; 63 | } 64 | return true; 65 | } 66 | bool chkPerfect(long long n){ 67 | long long i, total, limit = n / 2; 68 | 69 | total = 0; 70 | for (i = 1; i <= limit; ++i){ 71 | if (n % i == 0) 72 | total += i; 73 | } 74 | if (total == n) 75 | return true; 76 | return false; 77 | } 78 | */ -------------------------------------------------------------------------------- /10508.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Word Morphing UVa 10508 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Ad-hoc 4 | Compiled: Visual C++ 7.0 5 | Date: 13-09-06 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | //freopen("input.txt", "r", stdin); 14 | vector str; 15 | string org, temp; 16 | int word, letter, cnt; 17 | 18 | while (cin >> word >> letter){ 19 | str.resize(word); 20 | 21 | cin >> org; 22 | str[0] = org; 23 | 24 | for (int j = 1; j < word; ++j){ 25 | cin >> temp; 26 | cnt = 0; 27 | for (int i = 0; i < letter; ++i){ 28 | if (org[i] != temp[i]) 29 | ++cnt; 30 | } 31 | str[cnt] = temp; 32 | } 33 | for (int i = 0; i < str.size(); ++i) 34 | cout << str[i] << endl; 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /10515.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Power et. All UVa 10515 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Number Theory 4 | Compiled: Visual C++ 7.0 5 | Date: 21-02-06 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int myPow(int m, int n){ 13 | int total; 14 | if (m == 0) return 0; //base 0 15 | if (n == 0) //powe 0 16 | total = 1; 17 | else 18 | total = m; 19 | 20 | for (int i = 1; i < n; ++i){ 21 | total = total * m; 22 | total = total % 10; 23 | } 24 | return total; 25 | } 26 | 27 | int main(){ 28 | //freopen("input.txt", "r", stdin); 29 | string m, n; 30 | int _m, _n, temp; 31 | 32 | while (cin >> m >> n){ 33 | if (m == "0" && n == "0") break; 34 | 35 | _m = m[m.size()-1] - 48; 36 | if (n.size() >= 2){ // we need n's last two digit 37 | temp = n[n.size()-1] - 48; 38 | _n = (n[n.size()-2]-48) * 10 + temp; 39 | } 40 | else 41 | _n = n[n.size()-1]-48; 42 | 43 | if (_n == 0){ //last two digit zero then 44 | temp = myPow(_m, 99); //_m ^ 100 = _m ^ 99 * _m 45 | temp = (temp * _m) % 10; 46 | } 47 | else 48 | temp = myPow(_m, _n) % 10; 49 | cout << temp << endl; 50 | 51 | } 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /10530.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Guessing Game UVa 10530 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 09-10-04 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | int array[11]; 13 | int guess; 14 | char strA[30]; 15 | char strB[30]; 16 | int i; 17 | bool flag = true; 18 | 19 | // when need a integer input and a string input line by line 20 | // then it's better to use two getline() 21 | while(cin.getline(strA, sizeof(strA))){ 22 | 23 | if (strlen(strA) == 2) 24 | guess = 10; 25 | else 26 | guess = strA[0] - 48; 27 | 28 | if (guess == 0) 29 | break; 30 | cin.getline(strB, sizeof(strB)); // input second line 31 | 32 | if (flag){ 33 | for (i = 1; i <= 10; i++) 34 | array[i] = 1; 35 | flag = false; 36 | } 37 | 38 | 39 | if (strcmp(strB, "too high") == 0){ 40 | for (i = guess; i <= 10; ++i) 41 | array[i] = 0; 42 | } 43 | else if (strcmp(strB, "too low") == 0){ 44 | for (i = guess; i >= 1; --i) 45 | array[i] = 0; 46 | } 47 | else if (strcmp(strB, "right on") == 0){ 48 | flag = true; 49 | if (array[guess] == 1) 50 | cout << "Stan may be honest" << endl; 51 | else 52 | cout << "Stan is dishonest" << endl; 53 | } 54 | } 55 | return 0; 56 | } -------------------------------------------------------------------------------- /10573.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Geometry Paradox UVa 10573 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Geometry, if only t is given then r1 = r2, and there is no "impossible" 4 | Compiled: Visual C++ 7.0 5 | Date: 10-06-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | #define pi 2 * acos(0.0) 13 | 14 | int main(){ 15 | char item[100], temp[100]; 16 | int a, b; 17 | double outerArea, cirBig, cir1, cir2; 18 | int test, t; 19 | cin >> test; 20 | // eat newline 21 | cin.getline(temp, sizeof(temp)); 22 | for (t = 0; t < test; ++t){ 23 | cin.getline(item, sizeof(item)); 24 | a = b = 0; 25 | sscanf(item, "%d %d", &a, &b); 26 | if (a != 0 && b == 0){ 27 | // if only t is given 28 | cirBig = pi * a/2 * a/2; 29 | cir1 = cir2 = pi * a/4 * a/4; 30 | outerArea = cirBig - (cir1+cir2); 31 | } 32 | else if (a == 0 && b != 0){ 33 | // if only t is given 34 | a = b; 35 | cirBig = pi * a/2 * a/2; 36 | cir1 = cir2 = pi * a/4 * a/4; 37 | outerArea = cirBig - (cir1+cir2); 38 | } 39 | else{ 40 | cir1 = pi * a * a; 41 | cir2 = pi * b * b; 42 | cirBig = pi * (a+b) * (a+b); 43 | outerArea = cirBig - (cir1+ cir2); 44 | } 45 | printf("%0.4lf\n", outerArea); 46 | } 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /10583.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Ubiquitous Religions UVa 10583 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Set Data Structure 4 | Compiled: Visual C++ 7.0 5 | Date: 18-02-06 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | const int MX = 50005; 13 | 14 | int p[MX], rank[MX], tree[MX]; // tree for counting each set's elements 15 | int n, m; 16 | 17 | void makeSet(int x){ 18 | p[x] = x; 19 | rank[x] = 0; 20 | } 21 | 22 | void link(int x, int y){ 23 | if (rank[x] > rank[y]){ 24 | p[y] = x; 25 | tree[x] += tree[y]; 26 | tree[y] = 0; 27 | } 28 | else{ 29 | p[x] = y; 30 | if (rank[x] == rank[y]) 31 | rank[y] = rank[y] + 1; 32 | tree[y] += tree[x]; 33 | tree[x] = 0; 34 | } 35 | } 36 | int findSet(int x){ 37 | if (x != p[x]) 38 | p[x] = findSet(p[x]); 39 | return p[x]; 40 | } 41 | 42 | void unions(int x, int y){ 43 | //link(findSet(x), findSet(y)); 44 | link(x, y); // because findSet() is called in the main function 45 | } 46 | 47 | int main(){ 48 | //freopen("input.txt", "r", stdin); 49 | int i, j, a, b, maximum, fA, fB, cases = 0; 50 | 51 | while (cin >> n >> m){ 52 | if (n == 0 && m == 0) break; 53 | for (i = 1; i <= n; ++i) 54 | makeSet(i); 55 | 56 | fill(tree, tree+n+1, 1); // as <= n 57 | for (i = 0; i < m; ++i){ 58 | cin >> a >> b; 59 | 60 | fA = findSet(a); 61 | fB = findSet(b); 62 | if (fA != fB) 63 | unions(fA, fB); 64 | } 65 | 66 | maximum = 0; 67 | for (i = 1; i <= n; ++i) 68 | if (tree[i] == 0) // zero means no religion 69 | ++maximum; 70 | maximum = n - maximum; //total religion - how many zero religion 71 | cout << "Case " << ++cases << ": " << maximum << endl; 72 | } 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /10591.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Happy Number UVa 10591 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Ad hoc, if any repetion of the new number then unhappy 4 | Compiled: Visual C++ 7.0 5 | Date: 03-06-05 6 | */ 7 | #include 8 | using namespace std; 9 | 10 | long array[10000]; 11 | int main(){ 12 | long item, itemOrg, j, store; 13 | int cases = 0, test, t, k, temp, flag; 14 | cin >> test; 15 | 16 | for (t = 0; t < test; ++t){ 17 | cin >> item; 18 | itemOrg = item; 19 | flag = 100; // flag but 1,0 both is needed 20 | k = 0; 21 | 22 | while (flag != 1 && flag != 0){ 23 | store = 0; 24 | while (item != 0){ 25 | temp = item % 10; 26 | item = item / 10; 27 | store += temp * temp; 28 | } 29 | item = store; 30 | if (store == 1) 31 | flag = 1; 32 | else{ 33 | array[++k] = store; 34 | for (j = k-1; j > 0; --j){ 35 | if (store == array[j]){ 36 | flag = 0; 37 | break; 38 | } 39 | } 40 | } 41 | } 42 | cout << "Case #" << ++cases << ": "; 43 | if (flag) 44 | cout << itemOrg << " is a Happy number." << endl; 45 | else 46 | cout << itemOrg << " is an Unhappy number." << endl; 47 | } 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /10608.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Friends UVa 10608 2 | Programmer: Md. Mahmud Ahsan 3 | Description: SET 4 | Compiled: Visual C++ 7.0 5 | Date: 19-02-06 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | const int MX = 500005; 12 | int rank[MX], p[MX], tree[MX];//tree counts the number of elements 13 | int twoD[MX][2], maximum; 14 | 15 | 16 | void makeSet(int x){ 17 | p[x] = x; 18 | rank[x] = 0; 19 | } 20 | 21 | void link(int x, int y){ 22 | if (rank[x] > rank[y]){ 23 | p[y] = x; 24 | tree[x] += tree[y]; 25 | tree[y] = 0; 26 | } 27 | else{ 28 | p[x] = y; 29 | if (rank[x] == rank[y]){ 30 | rank[y] = rank[y]+1; 31 | } 32 | tree[y] += tree[x]; 33 | tree[x] = 0; 34 | } 35 | } 36 | 37 | int findSet(int x){ 38 | if (x != p[x]) 39 | p[x] = findSet(p[x]); 40 | return p[x]; 41 | } 42 | 43 | void unions(int x, int y){ 44 | link(findSet(x), findSet(y)); 45 | } 46 | 47 | int main(){ 48 | //freopen("input.txt", "r", stdin); 49 | int n, m, test; 50 | int i, j, a, b; 51 | 52 | cin >> test; 53 | while (test--){ 54 | cin >> m >> n; 55 | 56 | for (j = 1; j <= n; ++j){ 57 | cin >> a >> b; 58 | makeSet(a); makeSet(b); 59 | twoD[j][0] = a; 60 | twoD[j][1] = b; 61 | } 62 | fill(tree, tree+m+1, 1); //as <=n 63 | 64 | for (i = 1; i <= n; ++i){ 65 | if (findSet(twoD[i][0]) != findSet(twoD[i][1])) 66 | unions(twoD[i][0], twoD[i][1]); 67 | } 68 | maximum = 1; 69 | for (i = 1; i <= n; ++i){ 70 | if (tree[i] > maximum) 71 | maximum = tree[i]; 72 | } 73 | cout << maximum << endl; 74 | } 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /10611.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: The Playboy Chimp UVa 10611 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Binary Search 4 | Compiled: Visual C++ 7.0 5 | Date: 18-08-05 6 | */ 7 | 8 | #include 9 | #define max 50002 10 | 11 | int arr[max]; 12 | 13 | int binarySearch(int item, int size, int *l, int *h){ 14 | int low = 0, high = size - 1; 15 | int mid; 16 | 17 | while (low <= high){ 18 | mid = (low + high) / 2; 19 | if (arr[mid] == item) 20 | return mid; 21 | else if (arr[mid]< item) 22 | low = mid + 1; 23 | else 24 | high = mid - 1; 25 | } 26 | //if no item match, low become higher and high become lower 27 | *l = low; 28 | *h = high; 29 | return -1; 30 | } 31 | 32 | int main(){ 33 | int item, l, size, h, N, i, temp, prev; 34 | int result; 35 | 36 | i = -1; 37 | scanf("%d", &size); 38 | 39 | scanf("%d", &arr[++i]);//first element is inputed 40 | for (h = 1; h < size; ++h){ 41 | scanf("%d", &temp); 42 | if (arr[i] == temp) 43 | continue; // as we ignore same input data element 44 | arr[++i] = temp; 45 | } 46 | size = i + 1; // modified size rejecting same elements 47 | 48 | scanf("%d", &N); 49 | for(i = 0; i < N; ++i){ 50 | scanf("%d", &item); 51 | 52 | // special cases 53 | if (item < arr[0]){ 54 | printf("X %d\n", arr[0]); 55 | continue; 56 | } 57 | else if (item == arr[0]){ 58 | printf("X %d\n", arr[1]); 59 | continue; 60 | } 61 | else if (item > arr[size-1]){ 62 | printf("%d X\n", arr[size-1]); 63 | continue; 64 | } 65 | else if (item == arr[size-1]){ 66 | printf("%d X\n", arr[size-2]); 67 | continue; 68 | } 69 | 70 | result = binarySearch(item, size, &l, &h); 71 | 72 | if (result != -1) 73 | printf("%d %d\n", arr[result-1], arr[result+1]); 74 | else 75 | printf("%d %d\n", arr[h], arr[l]); 76 | } 77 | return 0; 78 | } -------------------------------------------------------------------------------- /10651.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Pebble Solitaire UVa 10651 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Brute Force 4 | Compiled: Visual C++ 7.0 5 | Date: 27-03-06 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | char data[20]; 13 | int min, k; 14 | 15 | int pebbleCount(){ 16 | int temp = 0; 17 | for (int i = 0; i < 12; ++i) 18 | if (data[i] == 'o') 19 | ++temp; 20 | return temp; 21 | } 22 | 23 | void pebble(){ 24 | if (pebbleCount() < min) 25 | min = pebbleCount(); 26 | for (int i = 0; i < 10; ++i){ 27 | if (data[i] == '-' && data[i+1] == 'o' && data[i+2] == 'o'){ 28 | data[i] = 'o'; 29 | data[i+1] = '-'; 30 | data[i+2] = '-'; 31 | 32 | if (pebbleCount() < min) 33 | min = pebbleCount(); 34 | 35 | pebble(); 36 | data[i] = '-'; 37 | data[i+1] = 'o'; 38 | data[i+2] = 'o'; 39 | 40 | } 41 | else if (data[i] == 'o' && data[i+1] == 'o' && data[i+2] == '-'){ 42 | data[i] = '-'; 43 | data[i+1] = '-'; 44 | data[i+2] = 'o'; 45 | if (pebbleCount() < min) 46 | min = pebbleCount(); 47 | 48 | pebble(); 49 | data[i] = 'o'; 50 | data[i+1] = 'o'; 51 | data[i+2] = '-'; 52 | } 53 | } 54 | } 55 | 56 | int main(){ 57 | //freopen("input.txt", "r", stdin); 58 | int n; 59 | 60 | 61 | cin >> n; 62 | cin.getline(data, sizeof(data));//eat newline 63 | while (n--){ 64 | 65 | cin.getline(data, sizeof(data)); 66 | 67 | min = 12; 68 | pebble(); 69 | cout << min << endl; 70 | 71 | } 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /10664.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Luggage UVa 10664 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Backtracking + sorting 4 | Compiled: Visual C++ 7.0 5 | Date: 28-07-06 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | int data[100], sum, size, tempSum, n; 15 | bool visited[100]; 16 | bool found; 17 | 18 | inline bool comp(const int a, const int b){ 19 | return a > b; 20 | } 21 | 22 | void backtrack(int level){ 23 | if (tempSum == sum){ 24 | cout << "YES" << endl; 25 | found = true; 26 | return; 27 | } 28 | else if (tempSum > sum) 29 | return; 30 | if (level > (n/2)) return; 31 | 32 | for (int i = 0; i < n; ++i){ 33 | if (visited[i])continue; 34 | 35 | tempSum += data[i]; 36 | visited[i] = true; 37 | backtrack(i+1); 38 | 39 | if (found) return; 40 | 41 | tempSum -= data[i]; 42 | visited[i] = false; 43 | } 44 | 45 | } 46 | 47 | int main(){ 48 | //freopen("input.txt", "r", stdin); 49 | int test, i; 50 | char str[100], *ptr; 51 | 52 | cin >> test; 53 | cin.getline(str, sizeof(str)); 54 | 55 | while(test--){ 56 | cin.getline(str, sizeof(str)); 57 | 58 | size = sum = 0; 59 | ptr =strtok(str, " \n"); 60 | while (ptr){ 61 | sscanf(ptr, "%d", &data[size]); 62 | sum += data[size]; 63 | ++size; 64 | ptr = strtok(NULL, " \n"); 65 | } 66 | 67 | if (size == 1){ 68 | cout << "NO" << endl; 69 | continue; 70 | } 71 | 72 | if (sum % 2 != 0) { 73 | cout << "NO" << endl; 74 | continue; 75 | } 76 | 77 | fill(visited, visited + size, false); 78 | sum = sum / 2; //if sum even 79 | sort(data, data + size, comp); 80 | 81 | n = size; 82 | 83 | tempSum = 0; 84 | found = false; 85 | backtrack(0); 86 | 87 | if (!found) cout << "NO" << endl; 88 | } 89 | 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /10696.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: f91 UVa 10696 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 15-09-04 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | typedef long int lint; 12 | 13 | // prototypes 14 | lint f91(lint N); 15 | 16 | //=========================================================== 17 | int main(){ 18 | lint input; 19 | lint result; 20 | 21 | while (cin >> input){ 22 | if (input == 0) break; 23 | result = f91(input); 24 | cout << "f91(" << input << ") = " << result << endl; 25 | } 26 | 27 | return 0; 28 | } 29 | //============================================================ 30 | 31 | lint f91(lint N){ 32 | if (N <= 100) 33 | return f91(f91(N+11)); 34 | else 35 | return N-10; 36 | } -------------------------------------------------------------------------------- /10699.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Problem D - Count the factors UVa 10699 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 15-04-05 5 | */ 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | #define sqRoot sqrt( (double) 1000000) 11 | 12 | long seive[1000000]; 13 | long prmCol[1000000]; 14 | 15 | void genSeive(); 16 | //====================================== 17 | int main(){ 18 | genSeive(); 19 | long orginal, input, i; 20 | int prmFactor; 21 | 22 | while (cin >> input && input != 0){ 23 | orginal = input; 24 | prmFactor = 0; 25 | for (i = 0; i <= 1000000 && prmCol[i] != 0; ++i){ 26 | if ( input % prmCol[i] == 0){ 27 | input = input / prmCol[i]; 28 | ++prmFactor; 29 | } 30 | } 31 | cout << orginal << " : " << prmFactor << endl; 32 | } 33 | return 0; 34 | } 35 | //========================================================== 36 | 37 | void genSeive(){ 38 | long i, j; 39 | 40 | seive[0]=seive[1] = 1; 41 | for (i = 2; i <= sqRoot; ++i){ 42 | for (j = i + i; j < 1000000; j += i) 43 | seive[j] = 1; 44 | } 45 | 46 | j = -1; 47 | for (i = 2; i < 1000000; ++i){ 48 | if (seive[i] == 0) 49 | prmCol[++j] = i; 50 | } 51 | } -------------------------------------------------------------------------------- /10783.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Odd Sum UVa 10783 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 28-11-04 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | int test, a, b; 12 | int count, k, testCase = 1; 13 | cin >> test; 14 | 15 | for (int i = 0; i < test; i++){ 16 | cin >> a >> b; 17 | count = 0; 18 | 19 | if (a % 2 == 0){ 20 | for (k = a + 1; k <= b; k += 2){ 21 | count += k; 22 | } 23 | } 24 | else{ 25 | for (k = a; k <= b; k += 2){ 26 | count += k; 27 | } 28 | } 29 | cout << "Case " << testCase++ << ": " << count << endl; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /10790.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: How Many Points of Intersection? UVa 10790 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 07-01-05 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | typedef long long lint; 11 | 12 | int main(){ 13 | int cases = 0; 14 | lint m, n, result; 15 | 16 | while (cin >> m >> n){ 17 | if (!m && !n) break; 18 | 19 | result = (( (m*(m-1))/2) * (n*(n-1)/2)); 20 | cout << "Case " << ++cases << ": " << result << endl; 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /10812.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Beat the Spread UVa 10812 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 09-05-05 5 | */ 6 | #include 7 | using namespace std; 8 | 9 | int main(){ 10 | long n, sum, diff, x, y, temp; 11 | cin >> n; 12 | long i; 13 | for (i = 0; i < n; ++i){ 14 | cin >> sum >> diff; 15 | 16 | 17 | 18 | temp = sum + diff; 19 | x = temp /2; 20 | y = sum - x; 21 | 22 | if ((diff > sum) || (temp % 2 != 0)){ 23 | cout << "impossible" << endl; 24 | continue; 25 | } 26 | 27 | if (x < y){ 28 | temp = x; 29 | x = y; 30 | y = temp; 31 | } 32 | cout << x << " " << y << endl; 33 | } 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /10815.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Andy's First Dictionary UVa 10815 2 | Programmer: Md. Mahmud Ahsan 3 | Description: String + Sorting 4 | Compiled: Visual C++ 7.0 5 | Date: 18-02-06 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | void strLwr(char *s){ 17 | char str[1000]; 18 | int i; 19 | for (i = 0; s[i]; ++i){ 20 | str[i] = tolower(s[i]); 21 | } 22 | str[i] = '\0'; 23 | strcpy(s, str); 24 | } 25 | 26 | int main(){ 27 | //freopen("input.txt", "r", stdin); 28 | //freopen("output.txt", "w", stdout); 29 | 30 | char input[1000], taken[1000]; 31 | vector v; 32 | int i, j, len; 33 | 34 | while (cin.getline(input, sizeof(input))){ 35 | strLwr(input); 36 | 37 | j = 0; 38 | len = strlen(input); 39 | 40 | for (i = 0; i <= len; ++i){ 41 | if (input[i] >= 'a' && input[i] <= 'z'){ 42 | taken[j++] = input[i]; 43 | } 44 | else{ 45 | taken[j] = '\0'; 46 | j = 0; 47 | if (strlen(taken) == 0) 48 | continue; 49 | 50 | string sss(taken); 51 | v.push_back(sss); 52 | } 53 | } 54 | } 55 | 56 | sort (v.begin(), v.end()); 57 | 58 | len = v.size(); 59 | cout << v[0] << endl; 60 | for (int i = 1; i < len; ++i){ 61 | if (v[i] == v[i-1]) continue; 62 | cout << v[i] << endl; 63 | } 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /10865.cpp: -------------------------------------------------------------------------------- 1 | /* Prob: Brownie Points UVA: 10865 2 | Programmer: Md. Mahmud Ahsan 3 | Desc: Using x,y coordinate 4 | Visual C++ 5 | Online contest 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | struct k{ 12 | long x, y; 13 | }A[ 200000]; 14 | 15 | int main(){ 16 | long i, n, mid; 17 | long stan, ollie; 18 | while (cin >> n){ 19 | stan = ollie = 0; 20 | if (n == 0) break; 21 | mid = n / 2; 22 | 23 | for (i = 0; i < n; ++i){ 24 | cin >> A[i].x >> A[i].y; 25 | } 26 | 27 | for (i = 0; i < n; ++i){ 28 | if (A[i].x < A[mid].x && A[i].y > A[mid].y) 29 | ++stan; 30 | else if (A[i].x > A[mid].x && A[i].y < A[mid].y) 31 | ++stan; 32 | else if (A[i].x > A[mid].x && A[i].y > A[mid].y) 33 | ++ollie; 34 | else if (A[i].x < A[mid].x && A[i].y < A[mid].y) 35 | ++ollie; 36 | } 37 | cout << ollie << " " << stan < 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | long convertBase10(char *str, int base){ 14 | int len = strlen(str) - 1; 15 | int i, n; 16 | long number = 0; 17 | for (i = 0; str[i]; ++i){ 18 | switch(str[i]){ 19 | case 'a': 20 | case 'A': n = 10; break; 21 | case 'b': 22 | case 'B': n = 11; break; 23 | case 'c': 24 | case 'C': n = 12; break; 25 | case 'd': 26 | case 'D': n = 13; break; 27 | case 'e': 28 | case 'E': n = 14; break; 29 | case 'f': 30 | case 'F': n = 15; break; 31 | default: n = str[i]-48; 32 | } 33 | number = number + (n * pow((double)base, (double)len)); 34 | --len; 35 | } 36 | return number; 37 | } 38 | 39 | void removes(char *str){ 40 | char n[100], m[100]; 41 | int i, j; 42 | j = 0; 43 | for (i = 0; str[i]; ++i){ 44 | if (str[i] == ' '){ 45 | n[j] = '0'; 46 | ++j; 47 | } 48 | else if (str[i] == 'o'){ 49 | n[j] = '1'; 50 | ++j; 51 | } 52 | } 53 | n[j] = '\0'; 54 | strcpy(str, n); 55 | } 56 | 57 | int main(){ 58 | //freopen("input.txt", "r", stdin); 59 | char str[100], bin[100]; 60 | long dec; 61 | 62 | while (cin.getline(str, sizeof(str))){ 63 | if (str[0] == '|'){ 64 | removes(str); 65 | dec = convertBase10(str, 2); 66 | cout << (char) dec; 67 | } 68 | } 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /10896.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Known Plaintext Attack UVa 10896 2 | Programmer: Md. Mahmud Ahsan 3 | Description: String 4 | Compiled: Visual C++ 7.0 5 | Date: 15-09-06 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | char seq[] = "abcdefghijklmnopqrstuvwxyz"; 15 | 16 | int getPos(char a, char b){ 17 | int posA, posB, total; 18 | posA = a-97; 19 | posB = b-97; 20 | 21 | if (posA <= posB) 22 | return posB - posA; 23 | 24 | total = 26 - posA; 25 | return total + posB; 26 | } 27 | 28 | int main(){ 29 | //freopen("input.txt", "r", stdin); 30 | int n; 31 | char line[100], word[100], *ptr; 32 | int lenLine, lenWord, i, j, temp, total, prev; 33 | bool ok; 34 | 35 | cin >> n; 36 | cin.getline(line, sizeof(line)); 37 | 38 | while (n--){ 39 | vector ch; 40 | 41 | cin.getline(line, sizeof(line)); 42 | cin.getline(word, sizeof(word)); 43 | 44 | lenWord = strlen(word); 45 | 46 | ptr = strtok(line, " \n"); 47 | 48 | while (ptr){ 49 | lenLine = strlen(ptr); 50 | 51 | if (lenLine == lenWord){ 52 | prev = getPos(word[0], ptr[0]); 53 | temp = prev; 54 | ok = true; 55 | for (i = 1; word[i]; ++i){ 56 | temp = getPos(word[i], ptr[i]); 57 | if (temp != prev){ 58 | ok = false; 59 | break; 60 | } 61 | 62 | } 63 | if (ok){ 64 | int character = temp; 65 | ch.push_back(seq[character % 26]); 66 | 67 | } 68 | } 69 | 70 | ptr = strtok(NULL, " \n"); 71 | } 72 | sort(ch.begin(), ch.end()); 73 | for (i = 0; i < ch.size(); ++i) 74 | cout << ch[i]; 75 | cout << endl; 76 | 77 | } 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /10905.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Children's Game UVa 10905 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Radix Sort 4 | Compiled: Visual C++ 7.0 5 | Date: 26-09-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int comp(const void *_a, const void *_b){ 13 | char *a = (char*) _a; 14 | char *b = (char*) _b; 15 | 16 | char temp1[100], temp2[100]; 17 | if (strlen(a) == strlen(b)) 18 | return (strcmp(a, b) < 0); 19 | else if(strlen(a) != strlen(b)){ 20 | strcpy(temp1, a); 21 | strcat(temp1, b); 22 | strcpy(temp2, b); 23 | strcat(temp2, a); 24 | 25 | return (strcmp(temp1, temp2) < 0); 26 | } 27 | return 1; 28 | } 29 | 30 | int main(){ 31 | char str[55][100]; 32 | 33 | int i, n; 34 | while (cin >> n){ 35 | if (n == 0) break; 36 | 37 | for (i = 0; i < n; ++i) 38 | cin >> str[i]; 39 | 40 | qsort(str, n, sizeof(str[0]), comp); 41 | for (i = 0; i < n; ++i) 42 | cout << str[i]; 43 | cout << endl; 44 | } 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /10919.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Prerequisite UVa 10919 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Ad hoc 4 | Compiled: Visual C++ 7.0 5 | Date: 20-08-06 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | //freopen("input.txt", "r", stdin); 14 | int arr[200], courseTaken, category, selection, minimum; 15 | int i, size, counter, temp; 16 | bool flag; 17 | 18 | while (cin >> courseTaken){ 19 | if (courseTaken == 0) break; 20 | 21 | cin >> category; 22 | for (i = 0; i < courseTaken; ++i){ 23 | cin >> arr[i]; 24 | } 25 | size = i; 26 | 27 | sort(arr, arr+size); 28 | flag = true; 29 | for (int j = 0; j < category; ++j){ 30 | cin >> selection >> minimum; 31 | 32 | counter = 0; 33 | for (int k = 0; k < selection; ++k){ 34 | cin >> temp; 35 | if (minimum != 0 && binary_search(arr, arr+size, temp)){ 36 | if (counter >= minimum) continue; 37 | 38 | ++counter; 39 | } 40 | } 41 | if (minimum == 0) continue; 42 | 43 | if (counter != minimum){ 44 | flag = false; 45 | } 46 | } 47 | if (flag) 48 | cout << "yes" << endl; 49 | else 50 | cout << "no" << endl; 51 | } 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /10921.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Find the Telephone UVa 10921 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Simulation 4 | Compiled: Visual C++ 7.0 5 | Date: 17-02-06 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | //freopen("input.txt", "r", stdin); 14 | int a[100], i, len; 15 | string str; 16 | 17 | a['1'] = 1; a['0'] = 0; 18 | a['A'] = a['B'] = a['C'] = 2; 19 | a['D'] = a['E'] = a['F'] = 3; 20 | a['G'] = a['H'] = a['I'] = 4; 21 | a['J'] = a['K'] = a['L'] = 5; 22 | a['M'] = a['N'] = a['O'] = 6; 23 | a['P'] = a['Q'] = a['R'] = a['S'] = 7; 24 | a['T'] = a['U'] = a['V'] = 8; 25 | a['W'] = a['X'] = a['Y'] = a['Z'] = 9; 26 | 27 | while (cin >> str){ 28 | len = str.size(); 29 | for (i = 0; i < len; ++i){ 30 | if (str[i] == '-') 31 | cout << "-"; 32 | else 33 | cout << a[str[i]]; 34 | } 35 | cout << endl; 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /10922.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Problem C - 2 the 9s UVa 10922 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Number Theory 4 | Compiled: Visual C++ 7.0 5 | Date: 19-08-06 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | const int MX = 1050; 12 | char str[MX]; 13 | int n; 14 | 15 | int getDegree(int number){ 16 | int temp = 0; 17 | int digit; 18 | 19 | while (number != 0){ 20 | digit = number % 10; 21 | number = number / 10; 22 | temp += digit; 23 | } 24 | return temp; 25 | } 26 | 27 | 28 | bool isMultiple(){ 29 | int temp=0; 30 | int i; 31 | n = 1; 32 | 33 | for (i = 0; str[i]; ++i){ 34 | temp += str[i]-48; 35 | } 36 | if (temp % 9 != 0) return false; 37 | 38 | while (temp != 9){ 39 | ++n; 40 | temp = getDegree(temp); 41 | } 42 | 43 | return true; 44 | } 45 | 46 | int main(){ 47 | //freopen("input.txt", "r", stdin); 48 | while (cin >> str){ 49 | if (str[0] == '0') break; 50 | 51 | if (isMultiple()) 52 | cout << str << " is a multiple of 9 and has 9-degree " << n << "."< 9 | #include 10 | using namespace std; 11 | 12 | bool isPrime(int n){ 13 | if (n == 1) return true; 14 | if (n < 1) return false; 15 | if (n == 2) return true; 16 | if (n%2 == 0) return false; 17 | 18 | for (int i = 3; i * i <= n; i+=2) 19 | if (n % i == 0) return false; 20 | return true; 21 | } 22 | 23 | int main(){ 24 | //freopen("input.txt", "r", stdin); 25 | 26 | string str; 27 | int i, j, temp; 28 | int value[150]; 29 | j = 1; 30 | for (i = 97; i <= 'z'; ++i) 31 | value[i] = j++; 32 | 33 | for (i = 65; i <= 'Z'; ++i) 34 | value[i] = j++; 35 | 36 | while (cin >> str){ 37 | temp = 0; 38 | j = str.size(); 39 | for (i = 0; i < j; ++i) 40 | temp += value[str[i]]; 41 | if (isPrime(temp)) 42 | cout << "It is a prime word." << endl; 43 | else 44 | cout << "It is not a prime word." << endl; 45 | } 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /10928.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: My Dear Neighbours UVa 10928 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Number Theory 4 | Compiled: Visual C++ 7.0 5 | Date: 19-08-06 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | struct Data{ 13 | int index; 14 | int count; 15 | }datas[20000]; 16 | 17 | inline bool comp(const Data &a, const Data &b){ 18 | if (a.count != b.count) 19 | return a.count < b.count; 20 | return a.index < b.index; 21 | } 22 | 23 | int main(){ 24 | //freopen("input.txt" , "r", stdin); 25 | 26 | int test, p, i, temp, counter; 27 | char str[20000],*ptr; 28 | 29 | cin >> test; 30 | while(test--){ 31 | counter = 0; 32 | cin >> p; 33 | cin.getline(str, sizeof(str)); // eat new line character 34 | 35 | for (i = 0; i < p; ++i){ 36 | cin.getline(str, sizeof(str)); 37 | 38 | temp = 0; 39 | ptr = strtok(str, " \n"); 40 | while(ptr){ 41 | ++temp; 42 | ptr = strtok(NULL, " \n"); 43 | } 44 | datas[counter].index = counter + 1; 45 | datas[counter].count = temp; 46 | ++counter; 47 | } 48 | 49 | sort(datas, datas + counter, comp); 50 | 51 | cout << datas[0].index; 52 | 53 | for (i = 1; i < counter; ++i){ 54 | if (datas[i].count == datas[0].count) 55 | cout << " " << datas[i].index; 56 | else 57 | break; 58 | } 59 | cout << endl; 60 | } 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /10929.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: You can say 11 UVa 10929 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Number Theory 4 | Compiled: Visual C++ 7.0 5 | Date: 17-02-06 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | //freopen("input.txt", "r", stdin); 14 | string str; 15 | int i, result; 16 | bool alt; 17 | 18 | while (cin >> str){ 19 | if (str == "0") break; 20 | result = 0; 21 | alt = false; 22 | for (i = 0; i < str.size(); ++i){ 23 | if (!alt) 24 | result += str[i]-48; 25 | else 26 | result -= str[i]-48; 27 | alt = !alt; 28 | } 29 | if (result % 11 == 0) 30 | cout << str << " is a multiple of 11." << endl; 31 | else 32 | cout << str << " is not a multiple of 11." << endl; 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /10931.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Parity UVa 10931 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Ad hoc 4 | Compiled: Visual C++ 7.0 5 | Date: 17-02-06 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int main(){ 14 | //freopen("input.txt", "r", stdin); 15 | //freopen("output.txt", "w", stdout); 16 | long long n, p, r; 17 | int j; 18 | char bin[100]; 19 | 20 | while (cin >> n){ 21 | if (n == 0) break; 22 | 23 | p = 0; 24 | j = 0; 25 | while (n != 0){ 26 | r = n % 2; 27 | n = n / 2; 28 | p += r; 29 | bin[j++] = r + 48; 30 | } 31 | bin[j] = '\0'; 32 | reverse(bin, bin+j); 33 | cout << "The parity of " << bin << " is " << p << " (mod 2)." << endl; 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /10935.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Throwing cards away I UVa 10935 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Simulation + Data Structure 4 | Compiled: Visual C++ 7.0 5 | Date: 18-02-06 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int r, dis; 14 | deque v; 15 | 16 | void odd(int n){ 17 | int i; 18 | 19 | cout << "Discarded cards:"; 20 | cout << " 1"; 21 | 22 | for (i = 3; i <= n; i += 2){ 23 | cout << ", " << i; 24 | v.push_back(i-1); 25 | } 26 | 27 | while (!v.empty()){ 28 | r = v[0]; 29 | v.pop_front(); 30 | 31 | if (v.empty()) break; 32 | dis = v[0]; 33 | v.pop_front(); 34 | cout << ", " << dis; 35 | if (!v.empty()) 36 | v.push_back(r); 37 | } 38 | 39 | cout << "\nRemaining card: " << r << endl; 40 | } 41 | 42 | void even(int n){ 43 | int i; 44 | 45 | cout << "Discarded cards:"; 46 | cout << " 1"; 47 | for (i = 3; i <= n; i += 2){ 48 | cout << ", " << i; 49 | v.push_back(i-1); 50 | } 51 | v.push_back(n); 52 | 53 | while (!v.empty()){ 54 | dis = v[0]; 55 | v.pop_front(); 56 | 57 | cout << ", " << dis; 58 | if (!v.empty()){ 59 | r = v[0]; 60 | v.pop_front(); 61 | 62 | if (v.empty()) break; 63 | v.push_back(r); 64 | } 65 | } 66 | cout << "\nRemaining card: " << r << endl; 67 | } 68 | 69 | int main(){ 70 | //freopen("input.txt", "r", stdin); 71 | //freopen("output.txt", "w", stdout); 72 | int n, i; 73 | 74 | while (cin >> n){ 75 | if (n == 0) break; 76 | 77 | if (n == 1){ 78 | cout << "Discarded cards:" << endl; 79 | cout << "Remaining card: 1" << endl; 80 | continue; 81 | } 82 | if (n == 2){ 83 | cout << "Discarded cards: 1" << endl; 84 | cout << "Remaining card: 2" << endl; 85 | continue; 86 | } 87 | if (n % 2 == 0) 88 | even(n); 89 | else 90 | odd(n); 91 | v.clear(); 92 | } 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /10945.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Mother Bear UVa 10945 2 | Programmer: Md. Mahmud Ahsan 3 | Description: String 4 | Compiled: Visual C++ 7.0 5 | Date: 18-02-06 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | char str[100000], modified[100000]; 14 | 15 | bool isPalindrome(){ 16 | int i, len = strlen(modified)-1; 17 | 18 | for (i = 0; i <= len/2; ++i){ 19 | if (tolower(modified[i]) != tolower(modified[len-i])) return false; 20 | } 21 | return true; 22 | } 23 | 24 | int main(){ 25 | //freopen("input.txt", "r", stdin); 26 | int i, j; 27 | 28 | while (cin.getline(str, sizeof(str))){ 29 | if (strcmp(str, "DONE") == 0) break; 30 | 31 | i = 0; 32 | for (j = 0; j < str[j]; ++j){ 33 | if ((str[j] >= 'a' && str[j] <= 'z') || (str[j] >= 'A' && str[j] <= 'Z')) 34 | modified[i++] = str[j]; 35 | } 36 | modified[i] = '\0'; 37 | 38 | if (isPalindrome()) 39 | cout << "You won't be eaten!" << endl; 40 | else 41 | cout << "Uh oh.." << endl; 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /10946.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: You want what filled? UVa 10946 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Graph + DFS 4 | Compiled: Visual C++ 7.0 5 | Date: 19-08-06 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int dr[] = {-1, 0, 1, 0}; 13 | int dc[] = { 0, 1, 0, -1}; 14 | 15 | const int MX = 100; 16 | 17 | struct Data{ 18 | char ch; 19 | int count; 20 | }datas[5000]; 21 | 22 | int x, y, counter, temp; 23 | char grid[MX][MX]; 24 | char ch; 25 | 26 | void dfsVisit(int a, int b){ 27 | int r, c; 28 | grid[a][b] = '.'; 29 | for (int i = 0; i < 4; ++i){ 30 | r = a + dr[i]; 31 | c = b + dc[i]; 32 | if (grid[r][c] == ch){ 33 | ++temp; 34 | dfsVisit(r, c); 35 | } 36 | } 37 | } 38 | void dfs(){ 39 | for (int i = 0; i < x; ++i){ 40 | for (int j = 0; j < y; ++j){ 41 | temp = 0; // temp for how many holes of a character 42 | if (grid[i][j] != '.'){ 43 | ++temp; 44 | ch = grid[i][j]; 45 | dfsVisit(i, j); 46 | datas[counter].ch = ch; 47 | datas[counter++].count = temp; 48 | } 49 | } 50 | } 51 | } 52 | 53 | inline bool comp(const Data &a, const Data &b){ 54 | if (a.count != b.count) 55 | return a.count > b.count; 56 | return a.ch < b.ch; 57 | } 58 | 59 | int main(){ 60 | //freopen("input.txt", "r", stdin); 61 | int i, cases = 0; 62 | 63 | while (cin >> x >> y){ 64 | if (!x && !y) break; 65 | 66 | for (i = 0; i < x; ++i) 67 | cin >> grid[i]; 68 | 69 | counter = 0; 70 | dfs(); 71 | sort(datas, datas+counter, comp); 72 | 73 | cout << "Problem " << ++cases << ":" << endl; 74 | for (i = 0; i < counter; ++i) 75 | cout << datas[i].ch << " " << datas[i].count << endl; 76 | } 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /10948.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: The primary problem. UVa 10948 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Sorting + Output 4 | Compiled: Visual C++ 6.0 5 | Date: 18-02-06 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | const int MX = 1200000; 12 | 13 | char seive[MX]; 14 | 15 | void genSeive(){ 16 | seive[0] = seive[1] = 1; 17 | int i, j; 18 | 19 | for (i = 2; i * i <= MX; ++i){ 20 | if (!seive[i]) 21 | for (j = i * i; j < MX; j += i) 22 | seive[j] = 1; 23 | } 24 | } 25 | 26 | int main(){ 27 | //freopen("input.txt", "r", stdin); 28 | 29 | genSeive(); 30 | int n, divN, total, i; 31 | bool found; 32 | 33 | while (cin >> n){ 34 | if (n == 0) break; 35 | 36 | found = false; 37 | cout << n << ":" << endl; 38 | divN = n / 2; 39 | for (i = 1; i <= divN; ++i){ 40 | if (seive[i] == 0 && seive[n-i] == 0){ 41 | total = i + (n-i); 42 | if (total == n) { 43 | cout << i << "+" << (n-i) << endl; 44 | found = true; 45 | break; 46 | } 47 | } 48 | } 49 | if (!found) 50 | cout << "NO WAY!" << endl; 51 | } 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /10954.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Add All UVa 10954 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Ad-hoc + Data structure 4 | Compiled: Visual C++ 7.0 5 | Date: 19-08-06 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | struct X{ 13 | int n; 14 | }; 15 | 16 | bool operator < (const X &a, const X &b){ 17 | return a.n > b.n; 18 | } 19 | 20 | int main(){ 21 | //freopen("input.txt", "r", stdin); 22 | int n, i, temp, final; 23 | priority_queue q; 24 | X x, y; 25 | 26 | while (cin >> n){ 27 | if (!n) break; 28 | final = temp = 0; 29 | 30 | for (i = 0; i < n; ++i){ 31 | cin >> temp; 32 | x.n = temp; 33 | q.push(x); 34 | } 35 | 36 | while (!(q.size() == 1)){ 37 | x = q.top(); q.pop(); 38 | y = q.top(); q.pop(); 39 | temp = x.n + y.n; 40 | final += temp; 41 | x.n = temp; 42 | q.push(x); 43 | } 44 | q.pop(); 45 | cout << final << endl; 46 | } 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /11005.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Cheapest Base UVa 11005 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Base 4 | Compiled: Visual C++ 7.0 5 | Date: 07-03-06 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | typedef long long lint; 14 | 15 | lint weight[40]; 16 | vector index; 17 | 18 | lint convert(lint number, int base){ 19 | lint temp, mod; 20 | 21 | temp = 0; 22 | while (number != 0){ 23 | mod = number % base; 24 | number = number / base; 25 | temp += weight[mod]; 26 | } 27 | return temp; 28 | } 29 | 30 | int main(){ 31 | //freopen("input.txt", "r", stdin); 32 | //freopen("output.txt", "w", stdout); 33 | int i, j, k; 34 | int test, query, cases = 0; 35 | lint temp, data; 36 | lint mn; 37 | bool flag =false; 38 | 39 | cin >> test; 40 | while (test--){ 41 | for (i = 0; i < 36; ++i){ 42 | cin >> weight[i]; 43 | } 44 | cin >> query; 45 | 46 | if (flag) cout << endl; flag = true; 47 | cout << "Case " << ++cases << ":" << endl; 48 | 49 | for (i = 0; i < query; ++i){ 50 | cin >> data; 51 | 52 | index.clear(); 53 | index.push_back(2); 54 | mn = convert(data, 2); 55 | for (j = 3; j <= 36; ++j){ 56 | temp = convert(data, j); 57 | if (temp < mn){ 58 | mn = temp; 59 | index.clear(); 60 | index.push_back(j); 61 | } 62 | else if (temp == mn){ 63 | index.push_back(j); 64 | } 65 | } 66 | 67 | cout << "Cheapest base(s) for number " << data << ":"; 68 | for (k = 0; k < index.size(); ++k) 69 | cout << " " << index[k]; 70 | cout << endl; 71 | } 72 | 73 | } 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /11056.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahmudahsan/203-ACM-Problems-Code/6e8f9698809c649a4c6bbba907d973e20360bc21/11056.cpp -------------------------------------------------------------------------------- /11074.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Draw Grid UVa 11074 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Simulation + output formated 4 | Compiled: Visual C++ 7.0 5 | Date: 12-09-06 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | //freopen("input.txt", "r", stdin); 13 | //freopen("output.txt", "w", stdout); 14 | 15 | int s, t, n, temp1, i, j, k, counter, cases = 0; 16 | char str[50]; 17 | 18 | while (cin >> s >> t >> n){ 19 | if (!s && !t && !n) break; 20 | 21 | cout << "Case " << ++cases << ":" << endl; 22 | counter = 0; 23 | temp1 = s * n + t * n + t; 24 | 25 | // initialize the string t + s 26 | for (i = 0; i < t; ++i) 27 | str[i] = '*'; 28 | for (j = i; j < t+s; ++j) 29 | str[j] = '.'; 30 | str[j] = '\0'; 31 | 32 | 33 | for (i = 0; i < t; ++i){ 34 | for (j = 0; j < temp1; ++j) 35 | cout << "*"; 36 | cout << endl; 37 | } 38 | 39 | for (i = t; i < temp1 - t; ){ 40 | ++counter; 41 | for (j = 0; j < n; ++j) 42 | cout << str; 43 | 44 | for (j = 0; j < t; ++j) 45 | cout << "*"; 46 | cout << endl; 47 | 48 | if (counter == s){ 49 | counter = 0; 50 | 51 | for (int m = 0; m < t; ++m){ 52 | for (int n = 0; n < temp1; ++n) 53 | cout << "*"; 54 | cout << endl; 55 | } 56 | i += t; 57 | } 58 | ++i; 59 | } 60 | cout << endl; 61 | } 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /11086.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Composite Prime UVa 11086 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Prime 4 | Compiled: Visual C++ 7.0 5 | Date: 10-09-06 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | const int MX = 1548578; 12 | 13 | char seive[MX]; 14 | 15 | void genSeive(){ 16 | seive[0] = seive[1] = 0; 17 | 18 | for (int i = 2; i * i < MX; ++i){ 19 | if (!seive[i]) 20 | for (int j = i * i; j < MX; j+=i) 21 | seive[j] = 1; 22 | } 23 | 24 | for (int i = 4; i < MX; ++i){ 25 | if (seive[i]) 26 | for (int j = i * 2; j < MX; j+=i) 27 | seive[j] = 0; 28 | } 29 | } 30 | 31 | int main(){ 32 | //freopen("input.txt", "r", stdin); 33 | genSeive(); 34 | int n, i, temp, counter; 35 | 36 | while (cin >> n){ 37 | counter = 0; 38 | for (i = 0; i < n; ++i){ 39 | cin >> temp; 40 | if (seive[temp]) 41 | ++counter; 42 | } 43 | cout << counter << endl; 44 | } 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /11172.cpp: -------------------------------------------------------------------------------- 1 | //11172 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | long long a, b; 8 | int n; 9 | 10 | cin >> n; 11 | 12 | while (n--){ 13 | cin >> a >> b; 14 | if (a > b) cout << ">" << endl; 15 | else if (a < b) cout << "<" << endl; 16 | else cout << "=" << endl; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /11192.cpp: -------------------------------------------------------------------------------- 1 | //11192 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int MX = 1000; 7 | 8 | int main(){ 9 | int n, i, k; 10 | string s; 11 | char buff[MX]; 12 | 13 | while (cin >> n){ 14 | if (n == 0) break; 15 | cin >> s; 16 | 17 | n = s.size() / n; 18 | k = 0; 19 | for (i = 0; i < s.size(); ++i){ 20 | if (k == n){ 21 | for (int j = k-1; j >= 0; --j) 22 | cout << buff[j]; 23 | k = 0; 24 | } 25 | buff[k++] = s[i]; 26 | } 27 | for (int j = k-1; j >= 0; --j) 28 | cout << buff[j]; 29 | cout << endl; 30 | } 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /113.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main (){ 5 | long double result; 6 | long double i,j,k,l; 7 | 8 | for( ;scanf("%Lf%Lf",&i,&j)==2; ) 9 | { 10 | k=log(j); 11 | l=k/i; 12 | result = exp(l); 13 | printf("%.0Lf\n",result); 14 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /119.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Greedy Gift Givers UVa 119 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 7.0 4 | Date: 30-05-05 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | struct person{ 12 | string name; 13 | int dollar; 14 | }; 15 | 16 | int main(){ 17 | int i, j, k, n, flag = 0; 18 | int dollar, people, temp, differ; 19 | 20 | person obj[15]; 21 | string name, name1; 22 | while (cin >> n){ 23 | for (i = 0; i < n; ++i){ 24 | cin >> obj[i].name; 25 | obj[i].dollar = 0; 26 | } 27 | 28 | for (i = 0; i < n; ++i){ 29 | cin >> name >> dollar >> people; 30 | 31 | //the money holder who will give 32 | for (k = 0; k < n; ++k){ 33 | if (name == obj[k].name){ 34 | obj[k].dollar += -dollar; 35 | if (people != 0) 36 | temp = dollar / people; 37 | differ = dollar - (temp * people); 38 | obj[k].dollar += differ; 39 | break; 40 | } 41 | } 42 | 43 | for (j = 0; j < people; ++j){ 44 | cin >> name1; 45 | //checking each person whom gift money to give 46 | for (k = 0; k < n; ++k){ 47 | if (name1 == obj[k].name){ 48 | if (people != 0) 49 | temp = dollar / people; 50 | obj[k].dollar += temp; 51 | break; 52 | } 53 | } 54 | } 55 | } 56 | 57 | if (flag == 1) cout << endl; 58 | flag = 1; 59 | for(i = 0; i < n; ++i) 60 | cout << obj[i].name << " " << obj[i].dollar << endl; 61 | 62 | } 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /133.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: The Dole Queue UVa 133 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Simulation, Circular Queue 4 | Compiled: Visual C++ 7.0 5 | Date: 17-08-05 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | char cq[22]; 13 | 14 | void init(){ 15 | for(int i = 0; i < 22; ++i) 16 | cq[i] = 0; 17 | } 18 | // is empty or not 19 | bool check(int n){ 20 | for (int i = 1; i <= n; ++i) 21 | if (cq[i] != 1) 22 | return false; 23 | return true; 24 | } 25 | 26 | int main(){ 27 | //freopen("output.txt", "w", stdout); 28 | int n , m, k, a, b, i, j, clock, anticlock; 29 | bool foundA, foundB, comma; 30 | 31 | while (cin >> n >> k >> m){ 32 | if (!m && !n && !k) break; 33 | 34 | init(); 35 | comma = false; 36 | 37 | a = 1; b = n; 38 | clock = anticlock = 0; 39 | foundA = foundB = false; 40 | 41 | while (true){ 42 | if (check(n)) 43 | break; 44 | //for anti clockwise 45 | if (cq[a] == 0){ 46 | ++anticlock; 47 | if (anticlock == k){ 48 | foundA = true; 49 | i = a; // i for fixed position 50 | } 51 | } 52 | if (!foundA) 53 | a = a % n + 1; // next move 54 | 55 | //for clock wise 56 | if (cq[b] == 0){ 57 | ++clock; 58 | if (clock == m){ 59 | foundB = true; 60 | j = b; 61 | } 62 | } 63 | 64 | if (!foundB){ // next move 65 | b = b - 1; 66 | if (b < 1) 67 | b = n; 68 | } 69 | 70 | // for both clockwise and anticlockwise man found 71 | if (foundA && foundB){ 72 | cq[i] = cq[j] = 1; 73 | foundA = foundB = false; 74 | clock = anticlock = 0; 75 | 76 | if (comma) 77 | cout << ","; 78 | comma = true; 79 | 80 | if (i != j){ 81 | printf("%3d%3d", i, j); 82 | } 83 | else 84 | printf("%3d", i); 85 | } 86 | } 87 | cout << endl; 88 | } 89 | return 0; 90 | } -------------------------------------------------------------------------------- /146.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: ID Codes UVa 146 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Permutation, STL USED here 4 | Compiled: Visual C++ 7.0 5 | Date: 17-08-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | char str[200]; 14 | int n; 15 | while (cin >> str){ 16 | if (str[0] == '#') break; 17 | 18 | n = strlen(str); 19 | if (next_permutation(str, str+n)) 20 | copy(str, str+n, ostream_iterator(cout)); 21 | else 22 | cout << "No Successor"; 23 | cout << endl; 24 | } 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /147.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Dollar UVa 147 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Counting Change 4 | Compiled: Visual C++ 7.0 5 | Date: 25-08-05 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | #define max 7000 12 | 13 | //multiplying by 100 to convert cent to dollar 14 | //then divide by 5,coz every input is multiple of 5 15 | int coin[11] = {2000, 1000, 400, 200, 100, 40, 20, 10, 4, 2, 1}; 16 | long long nway[max]; 17 | 18 | void init(){ 19 | for (int i = 0; i < max; ++i) 20 | nway[i] = 0; 21 | } 22 | 23 | int main(){ 24 | int i, j, c, v = 11, intAmount; 25 | double n; 26 | 27 | while (cin >> n){ 28 | if (n == 0.00) break; 29 | 30 | init(); 31 | nway[0] = 1; 32 | intAmount = (int) (n * 100 + .5);//to make larger than .5 makes a 1 33 | intAmount = intAmount / 5; // multiple of 5 said in problem 34 | 35 | for (i = 0; i < v; ++i){ 36 | c = coin[i]; 37 | for (j = c; j <= intAmount; ++j) 38 | nway[j] += nway[j-c]; 39 | } 40 | 41 | printf("%6.2lf", n); 42 | cout.width(17); 43 | cout << nway[intAmount] << endl; 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /151.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Power Crisis UVa 151 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 12-04-05 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int array[101], data[101]; 11 | 12 | int main(){ 13 | int i, j, k, l, counter, counts; 14 | bool flag; 15 | int dFinal = 12; // to store variable 13<=n<100 16 | for (i = 13; i < 101; ++i){ 17 | for (j = 1; j <= 1000; ++j){ 18 | 19 | for (l = 1; l <= i; ++l) 20 | array[l] = 0; 21 | 22 | flag = true; 23 | // counter used for counting how many array[zeros] exists 24 | counter = counts = 0; 25 | array[1] = 1; 26 | k = 1; 27 | // main calculation 28 | while (counter != (i-2)){ 29 | k = (k % i) + 1; 30 | if (array[k] == 0) 31 | counts++; 32 | if (counts == j){ 33 | array[k] = 1; 34 | if (array[13] == 1){ 35 | flag = false; 36 | break; 37 | } 38 | counts = 0; 39 | counter++; 40 | } 41 | } 42 | if (flag){ 43 | data[++dFinal] = j; 44 | break; 45 | } 46 | } 47 | } 48 | 49 | int input; 50 | while (cin >> input){ 51 | if (input == 0) break; 52 | cout << data[input] << endl; 53 | } 54 | return 0; 55 | } -------------------------------------------------------------------------------- /160.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Factors and Factorials UVa 160 2 | Programmer: Md. Mahmud Ahsan 3 | Description: !50 = 50.49.48...., seive, prime factor 4 | Compiled: Visual C++ 7.0 5 | Date: 10-06-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | char seive[104]; 13 | int prm[40]; 14 | int k; 15 | void genSeive(){ 16 | seive[0] = seive[1] = 1; 17 | int i, j; 18 | for (i = 2; i < sqrt( (double) 104); ++i){ 19 | if (!seive[i]) 20 | for (j = i + i; j < 104; j += i) 21 | seive[j] = 1; 22 | } 23 | int k = -1; 24 | for (i = 0; i < 104; ++i){ 25 | if (!seive[i]) 26 | prm[++k] = i; 27 | } 28 | ::k = k; 29 | } 30 | 31 | int main(){ 32 | genSeive(); 33 | int i, j, temp; 34 | int array[101]; 35 | 36 | int input; 37 | while (cin >> input){ 38 | if (!input) break; 39 | 40 | for (i = 0; i < 101; ++i) 41 | array[i] = 0; 42 | 43 | int flag; 44 | for (i = input; i > 1; --i){ 45 | temp = i; 46 | if (!seive[temp]){ 47 | array[temp] += 1; 48 | continue; 49 | } 50 | for (j = 0; j <= k; ){ 51 | if (temp % prm[j]==0 ){ 52 | ++array[prm[j]]; 53 | temp = temp / prm[j]; 54 | continue; 55 | } 56 | ++j; 57 | } 58 | } 59 | int count = 1; 60 | printf("%3d! =", input); 61 | for (i = 0; i < 101; ++i){ 62 | if (array[i] != 0 && count != 16){ 63 | printf("%3d", array[i]); 64 | ++count; 65 | } 66 | else if (array[i] != 0 && count == 16){ 67 | printf("\n%9d", array[i]); 68 | count = 1; 69 | } 70 | } 71 | cout << endl; 72 | } 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /260.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Il Gioco dell'X UVa 260 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Graph + DFS 4 | Compiled: Visual C++ 7.0 5 | Date: 14-02-06 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | const int MX = 500; 13 | 14 | int n; 15 | string graph[MX]; 16 | 17 | int dr[] = {-1, -1, 0, 0, 1, 1}; 18 | int dc[] = {-1, 0, -1, 1, 0, 1}; 19 | 20 | void dfsVisit(int u, int v, char ch, char replac){ 21 | int i, j, r, c; 22 | graph[u][v] = replac; 23 | 24 | for (i = 0; i < 6; ++i){ 25 | r = u + dr[i]; 26 | c = v + dc[i]; 27 | if (r >= 0 && r < n && c >= 0 && c < n){ 28 | if (graph[r][c] == ch){ 29 | dfsVisit(r, c, ch, replac); 30 | } 31 | } 32 | } 33 | } 34 | 35 | void dfs(){ 36 | int i, j, k; 37 | for (i = 0; i < n;++i){ 38 | if (graph[0][i] == 'b') 39 | dfsVisit(0, i, 'b', 'B'); 40 | if (graph[i][0] == 'w') 41 | dfsVisit(i, 0, 'w', 'W'); 42 | } 43 | } 44 | 45 | 46 | int main(){ 47 | int cases = 0; 48 | //freopen("input.txt", "r", stdin); 49 | int i; 50 | while (cin >> n){ 51 | if (n == 0) break; 52 | ++cases; 53 | for (i = 0; i < n; ++i) 54 | cin >> graph[i]; 55 | 56 | dfs(); 57 | for (i = 0; i < n; ++i){ 58 | if (graph[i][n-1] == 'W'){ 59 | cout << cases << " " << "W" << endl; 60 | break; 61 | } 62 | else if (graph[n-1][i] == 'B'){ 63 | cout << cases << " " << "B" << endl; 64 | break; 65 | } 66 | } 67 | } 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /263.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Number Chains UVa 263 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Simulation 4 | Compiled: Visual C++ 7.0 5 | Date: 17-08-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | // compare function for decresing order 16 | inline bool comp(char a, char b){ 17 | return a > b; 18 | } 19 | 20 | vector v; // collect all the new numbers 21 | 22 | bool found(int n){ 23 | for (int i = 0; i < v.size(); ++i) 24 | if (v[i] == n) 25 | return true; 26 | return false; 27 | } 28 | 29 | int main(){ 30 | char str[30]; 31 | char strIncre[30], strDecre[30]; 32 | int incre, decre, number; 33 | bool flag = false; 34 | 35 | while (cin >> str){ 36 | if (str[0] == '0') break; 37 | 38 | if (flag) cout << endl; flag = true; 39 | cout << "Original number was " << str << endl; 40 | do{ 41 | strcpy(strIncre, str); 42 | strcpy(strDecre, str); 43 | sort(strIncre, strIncre+strlen(strIncre)); 44 | sort(strDecre, strDecre+strlen(strDecre), comp); 45 | 46 | sscanf(strIncre, "%d", &incre); 47 | sscanf(strDecre, "%d", &decre); 48 | 49 | number = abs(decre - incre); 50 | cout << decre << " - " << incre << " = " << number << endl; 51 | 52 | if (found(number)) break; 53 | 54 | sprintf(str, "%d", number); 55 | v.push_back(number); 56 | }while (true); 57 | 58 | cout << "Chain length " << v.size()+1 << endl; 59 | v.clear(); // clear all the elements 60 | } 61 | cout << endl; 62 | return 0; 63 | } -------------------------------------------------------------------------------- /272.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: TeX Quotes UVa 272 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 08-08-04 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | //====================================================== 12 | int main(){ 13 | //freopen("input.txt", "r", stdin); 14 | //freopen("output.txt", "w", stdout); 15 | 16 | char string[1000000]; // buffer where the lines stored 17 | char c; 18 | int i = 0; 19 | 20 | do{ 21 | c = cin.get(); // it also takes '\n' character 22 | string[i] = c; 23 | i++; 24 | }while (c != EOF); // until ctrl + z is pressed 25 | 26 | string[i] = '\0'; 27 | 28 | // root work 29 | int check = 1; // if 1 then first target, if 2 then second target 30 | 31 | for (i = 0; string[i]; i++){ 32 | if (string[i] == '\"'){ 33 | if (check % 2 != 0){ 34 | // for first target 35 | cout << "``"; 36 | check++; 37 | continue; // leave the rest process 38 | } 39 | else if (check % 2 == 0){ 40 | // for second target 41 | cout << "''"; 42 | check--; 43 | continue; 44 | } 45 | } 46 | // general print 47 | if (string[i] == EOF); 48 | else 49 | cout << string[i]; 50 | } 51 | //complete root work 52 | return 0; 53 | } 54 | //======================================================= 55 | -------------------------------------------------------------------------------- /278.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Chess UVa 278 2 | Programmer: Md. Mahmud Ahsan 3 | Description: AD HOC 4 | Compiled: Visual C++ 7.0 5 | Date: 26-10-05 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | char board[20][20]; 12 | int white, black; 13 | 14 | int main(){ 15 | //freopen("input.txt", "r", stdin); 16 | int test, m, n, i, j, result; 17 | char c; 18 | 19 | cin >> test; 20 | while (test--){ 21 | cin >> c >> m >> n; 22 | 23 | if (c == 'k'){//knight 24 | bool flag = false; 25 | white = black = 0; 26 | 27 | //generating the board to obtain the number of black and white room 28 | for (i = 0; i < m; ++i){ 29 | if (!flag) flag = true; 30 | else flag = false; 31 | 32 | for (j = 0; j < n; j+=2){ 33 | if (flag) ++white;// board[i][j] = 'w'; 34 | else ++black; //board[i][j] = 'b'; 35 | } 36 | 37 | for (j = 1; j < n; j+=2){ 38 | if (!flag) ++white; //board[i][j] = 'w'; 39 | else ++black; //board[i][j] = 'b'; 40 | } 41 | } 42 | result = max(black, white); 43 | cout << result << endl; 44 | } 45 | else if (c == 'r'){//rook 46 | result = min(m,n); 47 | cout << result << endl; 48 | } 49 | else if (c == 'Q'){//queen 50 | result = min(m,n); 51 | cout << result << endl; 52 | } 53 | else if (c == 'K'){//king 54 | result = ( ((m+1) / 2) * ((n+1)/2)); 55 | cout << result << endl; 56 | } 57 | } 58 | 59 | return 0; 60 | } -------------------------------------------------------------------------------- /280.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Vertex UVa 280 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Graph + DFS 4 | Compiled: Visual C++ 7.0 5 | Date: 24-03-06 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | const int MX = 105; 12 | 13 | int graph[MX][MX], n; 14 | char color[MX]; 15 | 16 | void init(){ 17 | for (int i = 1; i <= n; ++i){ 18 | for (int j = 1; j <= n; ++j) 19 | graph[i][j] = 0; 20 | } 21 | } 22 | 23 | void dfsVisit(int i){ 24 | color[i] = 'g'; 25 | for (int j = 1; j <= n; ++j){ 26 | if (graph[i][j] == 1 && color[j] == 'w'){ 27 | dfsVisit(j); 28 | } 29 | } 30 | color[i] = 'b'; 31 | } 32 | 33 | void dfs(int start){ 34 | for (int i = 1; i <= n; ++i) 35 | color[i] = 'w'; 36 | 37 | for (int i = 1; i <= n; ++i){ 38 | if (graph[start][i] == 1 && color[i] == 'w') 39 | dfsVisit(i); 40 | } 41 | } 42 | 43 | int main(){ 44 | //freopen("input.txt", "r", stdin); 45 | int start, next, test, total = 0, j, arr[MX]; 46 | 47 | while (cin >> n){ 48 | if (n == 0) break; 49 | 50 | init(); 51 | while (cin >> start){ 52 | if (start == 0) break; 53 | 54 | while (cin >> next){ 55 | if (next == 0) break; 56 | graph[start][next] = 1; 57 | } 58 | } 59 | cin >> test; 60 | while (test--){ 61 | cin >> start; 62 | dfs(start); 63 | total = j = 0; 64 | for (int i = 1; i <= n;++i){ 65 | if (color[i] == 'w'){ 66 | ++total; 67 | arr[j++] = i; 68 | } 69 | } 70 | cout << total; 71 | for (int i = 0; i < j; ++i) 72 | cout << " " << arr[i]; 73 | cout << endl; 74 | } 75 | } 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /294.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* Problem: Divisors UVa 294 3 | Programmer: Md. Mahmud Ahsan 4 | Compiled: Visual C++ 6.0 5 | Date: 15-03-05 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | int n, i; // number of inputlines 14 | long a, b, tempA, tempB; 15 | long maxDivisor, maxNumber; // final maximum divisor and number counts 16 | long sqRoot; 17 | long mD, quotient; // temporary maximum Divisor count 18 | 19 | long num, j; // use in loop 20 | cin >> n; 21 | for (i = 0; i < n; i++){ 22 | maxDivisor = maxNumber = 0; 23 | cin >> a >> b; 24 | tempA = a; 25 | tempB = b; 26 | 27 | // special case 28 | if (a == 0 && b == 1){ 29 | cout << "Between " << tempA << " and " << tempB << ", " << 1 30 | << " has a maximum of " << 1 << " divisors." << endl; 31 | continue; 32 | } 33 | else if (a == 0 && b != 0 && b != 1){ 34 | a = 2; 35 | } 36 | 37 | for (num = a; num <= b; ++num){ 38 | mD = 0; 39 | quotient = 0; 40 | sqRoot = sqrt( (double) num); 41 | for (j = 1; j <= sqRoot; ++j){ 42 | if (num % j == 0){ 43 | ++mD; 44 | quotient = num / j; 45 | if (quotient > sqRoot) 46 | ++mD; 47 | } 48 | } 49 | 50 | if (mD > maxDivisor){ 51 | maxDivisor = mD; 52 | maxNumber = num; 53 | } 54 | } 55 | cout << "Between " << tempA << " and " << tempB << ", " << maxNumber 56 | << " has a maximum of " << maxDivisor << " divisors." << endl; 57 | 58 | } 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /299.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Train Swaping UVa 299 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | 9 | int bubbleSort(long int array[], long int size){ 10 | long int i, j, temp, result = 0; 11 | 12 | for (i = 0; i < size; i++){ 13 | for (j = 0; j < size-1; j++){ 14 | if (array[j] > array[j+1]){ 15 | temp = array[j]; 16 | array[j] = array[j+1]; 17 | array[j+1] = temp; 18 | result += 1; 19 | } 20 | } 21 | } 22 | return result; 23 | } 24 | 25 | 26 | int main(){ 27 | long int array[1000]; 28 | long int n, i, j, inFirst, inSecond, result; 29 | 30 | cin >> n; 31 | for (i = 0; i < n; i++){ 32 | cin >> inFirst; 33 | for (j = 0; j < inFirst; j++){ 34 | cin >> array[j]; 35 | } 36 | result = bubbleSort(array, inFirst); 37 | cout << "Optimal train swapping takes " << result << " swaps." << endl; 38 | } 39 | cout << endl; 40 | return 0; 41 | 42 | } -------------------------------------------------------------------------------- /336.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: A Node Too Far UVa 336 2 | Programmer: Md. Mahmud Ahsan 3 | Description: DFS 4 | Compiled: Visual C++ 7.0 5 | Date: 10-12-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | const int MX = 100; 13 | const int INF = 1<<28; 14 | 15 | //global declaration 16 | char color[MX]; 17 | int dis[MX], graph[100][100]; 18 | int nc, id[MX]; 19 | 20 | //============================================= 21 | int getId(int x){ 22 | int i; 23 | for (i = 0; i < nc; ++i){ 24 | if (id[i] == x) return i; 25 | } 26 | id[nc] = x; 27 | return nc++; 28 | } 29 | 30 | void init(){ 31 | for (int i = 0; i < MX; ++i){ 32 | for (int j = 0; j < MX; ++j) 33 | graph[i][j] = 0; 34 | } 35 | } 36 | 37 | void bfs(int start){ 38 | int i, j, u; 39 | for (i = 0; i < nc; ++i){ 40 | dis[i] = INF; 41 | color[i] = 'w'; 42 | } 43 | queue q; 44 | 45 | color[start] = 'g'; 46 | dis[start] = 0; 47 | q.push(start); 48 | 49 | while (!q.empty()){ 50 | u = q.front(); 51 | q.pop(); 52 | for (i = 0; i < nc; ++i){ 53 | if ((graph[u][i] == 1) && (color[i] == 'w')){ 54 | color[i] = 'g'; 55 | dis[i] = dis[u]+1; 56 | q.push(i); 57 | } 58 | } 59 | color[u] = 'b'; 60 | } 61 | } 62 | 63 | int main(){ 64 | freopen("input.txt", "r", stdin); 65 | int N, i, cases = 0, a, count, b; 66 | while (cin >> N){ 67 | if (N == 0) break; 68 | nc = 0; 69 | init(); 70 | 71 | for (i = 0; i < N; ++i){ 72 | cin >> a >> b; 73 | a = getId(a); 74 | b = getId(b); 75 | graph[a][b] = 1; 76 | graph[b][a] = 1; 77 | } 78 | 79 | while (cin >> a >> b){ 80 | if (a == 0 && b == 0) break; 81 | 82 | i = getId(a); 83 | bfs(i); 84 | 85 | count = 0; 86 | for (i = 0; i < nc; ++i) 87 | if (dis[i] > b) ++count; 88 | 89 | cout << "Case " << ++cases << ": " << count << " nodes not reachable from node " << a << " with TTL = " << b << "." << endl; 90 | 91 | } 92 | 93 | } 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /344.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Roman Digititis UVa 344 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 29-10-04 5 | */ 6 | #include 7 | using namespace std; 8 | 9 | //total number of roman digits 10 | namespace global{ 11 | int nI, nV, nX, nL, nC; 12 | } 13 | 14 | using namespace global; 15 | 16 | // prototypes 17 | void unit(int n); 18 | void tenUnit(int n); 19 | 20 | //============================================================== 21 | int main(){ 22 | int i; 23 | int input; 24 | 25 | while (cin >> input){ 26 | if (input == 0) 27 | break; 28 | 29 | nI = nV = nX = nL = nC = 0; 30 | 31 | // the total digits of preface 32 | for (i = 1; i <= input; i++){ 33 | unit(i % 10); 34 | tenUnit(i / 10); 35 | } 36 | 37 | cout << input << ": " << nI << " i, " << nV << " v, " << nX << " x, " 38 | << nL << " l, " << nC << " c" << endl; 39 | 40 | } 41 | 42 | return 0; 43 | } 44 | //============================================================= 45 | void unit(int n){ 46 | switch(n){ 47 | case 1: ++nI; 48 | break; 49 | case 2: nI += 2; 50 | break; 51 | case 3: nI += 3; 52 | break; 53 | case 4: ++nI; ++nV; 54 | break; 55 | case 5: ++nV; 56 | break; 57 | case 6: ++nI; ++nV; 58 | break; 59 | case 7: nI += 2; ++nV; 60 | break; 61 | case 8: nI += 3; ++nV; 62 | break; 63 | case 9: ++nI; ++nX; 64 | break; 65 | } 66 | } 67 | 68 | void tenUnit(int n){ 69 | switch(n){ 70 | case 1: ++nX; 71 | break; 72 | case 2: nX += 2; 73 | break; 74 | case 3: nX += 3; 75 | break; 76 | case 4: ++nX; ++nL; 77 | break; 78 | case 5: ++nL; 79 | break; 80 | case 6: ++nX; ++nL; 81 | break; 82 | case 7: nX += 2; ++nL; 83 | break; 84 | case 8: nX += 3; ++nL; 85 | break; 86 | case 9: ++nX; ++nC; 87 | break; 88 | case 10:++nC; 89 | break; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /350.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Pseudo-Random Numbers UVa 350 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 07-10-04 5 | */ 6 | #include 7 | using namespace std; 8 | 9 | typedef long int lint; 10 | 11 | int main(){ 12 | lint z, i, m, l; 13 | lint count; 14 | lint caseCount = 0; 15 | lint dataSet[10000]; 16 | lint x, y, temp; 17 | bool flag; 18 | 19 | while (cin >> z >> i >> m >> l){ 20 | if (!z && !i && !m && !l) break; 21 | 22 | for (x = 0; x < 10000; ++x) 23 | dataSet[x] = 0; 24 | dataSet[0] = l; 25 | 26 | flag = true; 27 | y = 1; 28 | temp = l; 29 | while (flag){ 30 | temp = (z * temp + i) % m; 31 | dataSet[y] = temp; 32 | 33 | for (x = y - 1; x > 0; --x){ 34 | if (dataSet[y] == dataSet[x]){ 35 | count = y; 36 | flag = false; 37 | break; 38 | } 39 | } 40 | ++y; 41 | } 42 | 43 | //for output case 44 | ++caseCount; 45 | cout << "Case " << caseCount << ": " << --count << endl; 46 | } 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /352.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: The Seasonal War UVa 352 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 05-09-04 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int dr[] = {-1, -1, -1, 0, 1, 1, 1, 0}; 12 | int dc[] = {-1, 0, 1, 1, 1, 0, -1, -1}; 13 | // as it contains 8 so in DFSvisit we must check 8 14 | 15 | int n; 16 | char array[100][100]; 17 | 18 | // prototlype 19 | int valid(int, int); 20 | int DFS(); 21 | void DFSvisit(int r, int c); 22 | 23 | //================================================================================ 24 | int main(){ 25 | char string[100]; 26 | int i, j; 27 | int len; 28 | static int title = 1; 29 | 30 | while (cin >> n){ 31 | len = 0; 32 | for (i = 0; i < 100; i++){ 33 | for (j = 0; j < 100; j++) 34 | array[i][j] = 0; 35 | } 36 | 37 | for (i = 0; i < n; i++){ 38 | cin >> string; 39 | len = strlen(string); 40 | 41 | for (j = 0; j < len; j++){ 42 | array[i][j] = string[j] ; 43 | } 44 | } 45 | 46 | int result = DFS(); 47 | cout << "Image number "<< title << " contains " << result << " war eagles." << endl; 48 | ++title; 49 | } 50 | 51 | return 0; 52 | } 53 | //================================================================================== 54 | 55 | int DFS(){ 56 | int r, c, count = 0; 57 | for (r = 0; r < n; r++){ 58 | for (c = 0; c < n; c++){ 59 | if ('1' == array[r][c]){ 60 | count++; 61 | DFSvisit(r, c); 62 | } 63 | } 64 | } 65 | return count; 66 | } 67 | 68 | void DFSvisit(int r, int c){ 69 | int nr, nc, i; 70 | array[r][c] = '0'; 71 | 72 | for (i = 0; i < 8; i++){ 73 | nr = r + dr[i]; 74 | nc = c + dc[i]; 75 | if (valid(nr, nc) && '1' == array[nr][nc]) 76 | DFSvisit(nr, nc); 77 | } 78 | 79 | } 80 | 81 | int valid(int r, int c){ 82 | if (r < 0 || c < 0 || r >= n || c >= n) 83 | return 0; 84 | else 85 | return 1; 86 | } 87 | -------------------------------------------------------------------------------- /369.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Combination UVa 369 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 15-12-04 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | typedef long long lint; 11 | 12 | lint nCr(lint n, lint r); 13 | //=============================================== 14 | int main(){ 15 | lint n, r, nMr; 16 | lint result; 17 | 18 | while (cin >> n >> r){ 19 | if (!n && !r) break; 20 | 21 | nMr = r; 22 | if (n-r < r){ 23 | nMr = n-r; 24 | } 25 | 26 | result = nCr(n, nMr); 27 | cout << n << " things taken " << r << " at a time is " << result << " exactly." << endl; 28 | } 29 | return 0; 30 | } 31 | //=============================================== 32 | lint nCr(lint n, lint r){ 33 | lint i; 34 | lint k, prod; 35 | 36 | prod = 1; 37 | k = n; 38 | 39 | for (i = 1; i <= r; ++i) 40 | prod = (prod * k--) / i; 41 | return prod; 42 | } -------------------------------------------------------------------------------- /371.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Ackermann Functions UVa 371 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Linux GNU C++ 4 | Date: 05-08-04 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | // typedef 11 | typedef long long int llint; 12 | 13 | // prototypes 14 | llint cycleLength(llint); 15 | void show(llint l, llint h, llint value, llint max); 16 | 17 | //=================================================================== 18 | int main(){ 19 | llint l, h, temp, value, max; // l means lower, h means higher 20 | // value that generates max length 21 | int i; // for counter loop 22 | 23 | while (cin >> l >> h){ 24 | if (l == 0 && h == 0) break; 25 | 26 | // swapping the lower and higher 27 | if (l > h){ 28 | temp = l; 29 | l = h; 30 | h = temp; 31 | } 32 | 33 | value = 0; 34 | max = 0; 35 | 36 | for (i = l ; i <= h; i++){ 37 | temp = cycleLength(i); 38 | if (temp > max) { 39 | max = temp; 40 | value = i; 41 | } 42 | } 43 | show(l, h, value, max); 44 | } 45 | 46 | return 0; 47 | } 48 | //================================================================= 49 | llint cycleLength(llint n){ 50 | int i = 0; 51 | do{ 52 | if ((n % 2) == 0) 53 | n = n / 2; 54 | else 55 | n = 3 * n + 1; 56 | 57 | i++; 58 | } while (n != 1); 59 | return i; 60 | } 61 | 62 | void show(llint l, llint h, llint value, llint max){ 63 | cout << "Between " << l << " and " << h << ", " << value << " generates the longest sequence of " << max << " values." << endl; 64 | } -------------------------------------------------------------------------------- /374.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Big Mod UVa 374 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Linux GNU C++ 4 | Date: 05-08-04 5 | */ 6 | 7 | 8 | #include 9 | using namespace std; 10 | 11 | typedef long long int lint; 12 | 13 | // prototypes 14 | lint square(lint n); 15 | lint bigMod(lint b, lint p, lint m); 16 | 17 | //================================================ 18 | int main(){ 19 | lint b, p, m; 20 | lint result; 21 | 22 | while (cin >> b >> p >> m){ 23 | result = bigMod(b, p, m); 24 | cout << result << endl; 25 | } 26 | return 0; 27 | } 28 | //================================================= 29 | 30 | lint square(lint n){ 31 | return n * n; 32 | } 33 | 34 | // The main part is shown below 35 | // This is called dynamic programming 36 | lint bigMod(lint b, lint p, lint m){ 37 | if (p == 0) return 1; 38 | else if (p % 2 == 0) 39 | return square(bigMod(b, p / 2, m) %m) %m; 40 | else 41 | return ((b % m) * bigMod(b, p - 1, m) %m) %m; 42 | } -------------------------------------------------------------------------------- /382.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Perfection UVa 382 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 24-10-04 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | typedef long int lint; 11 | 12 | int main(){ 13 | lint input; 14 | lint i, sum; 15 | 16 | cout << "PERFECTION OUTPUT" << endl; 17 | while (cin >> input){ 18 | if (input == 0){ 19 | cout << "END OF OUTPUT" << endl; 20 | break; 21 | } 22 | sum = 0; 23 | for (i = 1; i <= input / 2; i++){ 24 | if (input % i == 0) 25 | sum += i; 26 | } 27 | 28 | printf("%5ld ", input); 29 | 30 | if (sum == input) 31 | cout << "PERFECT" << endl; 32 | else if (sum < input) 33 | cout << "DEFICIENT" << endl; 34 | else 35 | cout << "ABUNDANT" << endl; 36 | 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /386.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Perfect Cubes UVa 386 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 23-03-05 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | long a, b, c, d; 12 | 13 | for (a = 6; a <= 200; ++a){ 14 | for (b = 2; b < a; ++b){ 15 | for (c = b; c < a; ++c){ 16 | for (d = c; d < a; ++d){ 17 | if (a*a*a == (b*b*b) + (c*c*c) + (d*d*d)) 18 | cout << "Cube = " << a << ", Triple = (" << b << "," << c << "," << d << ")" << endl; 19 | } 20 | } 21 | } 22 | } 23 | 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /408.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Uniform Generator UVa 408 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 17-03-05 5 | */ 6 | 7 | /*description: By observing sample input/output we found that 8 | when (gcd(x,y) == 1) => good choice, otherwise bad choice */ 9 | 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | long gcd(long x, long y){ 15 | long r; 16 | 17 | while (y != 0){ 18 | r = x % y; 19 | x = y; 20 | y = r; 21 | } 22 | return x; 23 | } 24 | 25 | int main(){ 26 | long a, b, result; 27 | 28 | while (cin >> a >> b){ 29 | result = gcd(a, b); 30 | 31 | if (result == 1) 32 | printf("%10ld%10ld Good Choice\n", a, b); 33 | else 34 | printf("%10ld%10ld Bad Choice\n", a, b); 35 | 36 | } 37 | 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /423.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: MPI Maelstrom UVa 423 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Graph + Floyd Warshall 4 | Compiled: Visual C++ 7.0 5 | Date: 28-02-06 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | const int MX = 105; 12 | const int INF = 1 << 20; 13 | 14 | int graph[MX][MX], n; 15 | 16 | void init(){ 17 | for (int i = 1; i <= n; ++i){ 18 | for (int j = 1; j <= n; ++j){ 19 | graph[i][j] = INF; 20 | } 21 | graph[i][i] = 0; 22 | } 23 | } 24 | 25 | void floydWarshall(){ 26 | int i, j, k; 27 | for (k = 1; k <= n; ++k){ 28 | for (i = 1; i <= n; ++i){ 29 | for (j = 1; j <= n; ++j){ 30 | if (graph[i][j] > graph[i][k] + graph[k][j]){ 31 | graph[i][j] = graph[i][k] + graph[k][j]; 32 | } 33 | } 34 | } 35 | } 36 | } 37 | 38 | int main(){ 39 | //freopen("input.txt", "r", stdin); 40 | int i, j, k, temp; 41 | char str[100]; 42 | 43 | while (cin >> n){ 44 | init(); 45 | for (i = 1; i <= n; ++i){ 46 | for (j = 1; j < i; ++j){ 47 | cin >> str; 48 | if (str[0] == 'x') continue; 49 | 50 | temp = atoi(str); 51 | graph[i][j] = temp; 52 | graph[j][i] = temp; 53 | } 54 | } 55 | floydWarshall(); 56 | 57 | int maxi = 0; 58 | for (i = 2; i <= n; ++i){ 59 | if (graph[1][i]>maxi) 60 | maxi = graph[1][i]; 61 | } 62 | cout << maxi << endl; 63 | } 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /424.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Integer Inquiry UVa 424 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 15-09-04 5 | */ 6 | // Summation Big Integer using string 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | const int length = 200; 13 | //prototypes 14 | void reverseStr(char *one, char *two); 15 | 16 | //======================================================================= 17 | int main(){ 18 | char sFirst[length], sSecond[length], sSum[length]; 19 | int firstLen, secondLen; 20 | int iFirst, iSecond, iSum; 21 | int carry; 22 | int t; 23 | 24 | // all other lines added to the first line sFirst 25 | cin >> sFirst; 26 | while (cin >> sSecond && sSecond[0] != '0'){ 27 | carry = 0; 28 | t = 0; 29 | sSum[0] = '\0'; 30 | firstLen = strlen(sFirst) - 1; 31 | secondLen = strlen(sSecond) - 1; 32 | 33 | // Individual digit used for sum 34 | while (firstLen >= 0 || secondLen >= 0){ 35 | iFirst = sFirst[firstLen] - 48; 36 | iSecond= sSecond[secondLen] - 48; 37 | 38 | // if first or second's number length less than zero 39 | if (firstLen < 0) iFirst = 0; 40 | if (secondLen < 0) iSecond = 0; 41 | 42 | iSum = iFirst + iSecond + carry; 43 | 44 | if (iSum > 9) 45 | carry = 1; 46 | else 47 | carry = 0; 48 | 49 | sSum[t] = (iSum % 10) + 48; 50 | ++t; 51 | --firstLen; 52 | --secondLen; 53 | } 54 | // at last carry should be inserted to string 55 | if (carry == 1){ 56 | sSum[t] = carry + 48; 57 | sSum[++t] = '\0'; 58 | } 59 | else 60 | sSum[t] = '\0'; 61 | 62 | // must be reversed 63 | reverseStr(sSum, sFirst); 64 | } 65 | cout << sFirst << endl; 66 | return 0; 67 | } 68 | //========================================================================= 69 | void reverseStr(char *one, char *two){ 70 | int h = strlen(one) - 1; 71 | int i = 0; 72 | 73 | for (int j = h; j >= 0; j--){ 74 | two[i] = one[j]; 75 | ++i; 76 | } 77 | two[i] = '\0'; // must set to null at last for "two" second string 78 | } -------------------------------------------------------------------------------- /438.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: The Circumference of the Circle UVa 438 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 29-03-05 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | #define pi 3.141592653589793 13 | 14 | int main(){ 15 | double x1, x2, x3, y1, y2, y3; 16 | double a, b, c; 17 | double s, k, r, circum; 18 | 19 | while (cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3){ 20 | a = sqrt( pow((x1-x2), 2) + pow((y1-y2), 2) ); 21 | b = sqrt( pow((x2-x3), 2) + pow((y2-y3), 2) ); 22 | c = sqrt( pow((x1-x3), 2) + pow((y1-y3), 2) ); 23 | 24 | s = (a+b+c)/2; 25 | k = sqrt( s * (s-a) * (s-b) * (s-c)); 26 | 27 | r = (a*b*c)/(4 * k); 28 | 29 | circum = 2 * pi * r; 30 | printf("%.2lf\n", (double) circum); 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /440.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Eeny Meeny Moo UVa 440 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 12-04-05 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int array[151], data[151]; 11 | 12 | int main(){ 13 | int i, j, k, l, counter, counts; 14 | bool flag; 15 | int dFinal = 2; // to store variable 3<=n<150 16 | for (i = 3; i < 151; ++i){ 17 | for (j = 1; j <= 1000; ++j){ 18 | 19 | for (l = 1; l <= i; ++l) 20 | array[l] = 0; 21 | 22 | flag = true; 23 | // counter used for counting how many array[zeros] exists 24 | counter = counts = 0; 25 | array[1] = 1; 26 | k = 1; 27 | // main calculation 28 | while (counter != (i-2)){ 29 | k = (k % i) + 1; 30 | if (array[k] == 0) 31 | counts++; 32 | if (counts == j){ 33 | array[k] = 1; 34 | if (array[2] == 1){ 35 | flag = false; 36 | break; 37 | } 38 | counts = 0; 39 | counter++; 40 | } 41 | } 42 | if (flag){ 43 | data[++dFinal] = j; 44 | break; 45 | } 46 | } 47 | } 48 | 49 | int input; 50 | while (cin >> input){ 51 | if (input == 0) break; 52 | cout << data[input] << endl; 53 | } 54 | return 0; 55 | } -------------------------------------------------------------------------------- /441.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Lotto UVa 441 2 | Programmer: Md. Mahmud Ahsan 3 | Description: AD HOC, identify loop 4 | Compiled: Visual C++ 7.0 5 | Date: 26-07-05 6 | */ 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | //freopen("input.txt", "r", stdin); 12 | //freopen("output.txt", "w", stdout); 13 | 14 | int n, array[30]; 15 | int a, b, c, d, e, f, i; 16 | 17 | bool flag = false; 18 | 19 | while (cin >> n){ 20 | if (!n) break; 21 | for (i = 0; i < n; ++i) 22 | cin >> array[i]; 23 | 24 | if (flag) cout << endl; flag = true; 25 | 26 | for (a = 0; a < n-5; ++a) 27 | for (b = a + 1; b < n - 4; ++b) 28 | for (c = b + 1; c < n - 3; ++c) 29 | for (d = c + 1; d < n - 2; ++d) 30 | for (e = d + 1; e < n - 1; ++e) 31 | for (f = e + 1; f < n; ++f){ 32 | cout << array[a] << " " << array[b] << " " << array[c] << 33 | " " << array[d] << " " << array[e] << " " << array[f] << endl; 34 | } 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /444.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Encoder and Decoder UVa 444 2 | Programmer: Md. Mahmud Ahsan 3 | Description: String processing, reversing, given ascii code only 4 | Compiled: Visual C++ 7.0 5 | Date: 10-07-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | char str[1005]; 14 | int i, n, len; 15 | while (cin.getline(str, sizeof(str))!= NULL){ 16 | len = strlen(str); 17 | reverse(str, str+len); 18 | 19 | n = 0; 20 | if ( (str[0] -48) >= 0 && str[0]-48 <= 9){ 21 | for (i = 0; str[i]; ++i){ 22 | n = n * 10 + str[i]-48; 23 | if ( (n >= 65 && n <= 90) || (n >= 97 && n <= 122) || n == 32 || n == 33 24 | ||n == 44 || n == 46 || n == 58 || n == 59 || n == 63){ 25 | cout << (char) n; 26 | n = 0; 27 | } 28 | } 29 | } 30 | else{ 31 | for (i = 0; str[i]; ++i){ 32 | n = str[i]; 33 | while (n != 0){ 34 | cout << n % 10; 35 | n = n / 10; 36 | } 37 | } 38 | } 39 | cout << endl; 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /445.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Marvelous Mazes UVa 445 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 11-09-04 5 | */ 6 | #include 7 | using namespace std; 8 | 9 | int main(){ 10 | // c for select one character 11 | char c, str[200]; 12 | // in contains integer converted from character 13 | // temp counts the total summation for multiple digit 14 | int in, temp; 15 | int i, j; 16 | 17 | while (cin.getline(str, sizeof(str))){ 18 | temp = 0; 19 | for (i = 0; str[i]; i++){ 20 | c = str[i]; 21 | if (c >= '1' && c <= '9'){ 22 | in = c - 48; // conver character to integer 23 | temp += in; 24 | continue; 25 | } 26 | 27 | if(c == 'b'){ 28 | for (j = 0; j < temp; j++) 29 | cout << " "; 30 | temp = 0; 31 | continue; 32 | } 33 | else if(c == '!' || c == '\0'){ 34 | cout << endl; 35 | continue; 36 | } 37 | // if no special character found then 38 | for (j = 0; j < temp; j++) 39 | cout << c; 40 | temp = 0; 41 | } 42 | cout << endl; 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /446.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Kibbles `n' Bits `n' Bits `n' Bits UVa 446 2 | Programmer: Md. Mahmud Ahsan 3 | Description: hex, binary to decimal, number conversion 4 | Compiled: Visual C++ 7.0 5 | Date: 10-06-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | void revStr(char *str){ 14 | int i, len; 15 | len = strlen(str)-1; 16 | char st[100]; 17 | for (i = 0; i <= len; ++i){ 18 | st[i] = str[len-i]; 19 | } 20 | st[i] = '\0'; 21 | strcpy(str, st); 22 | } 23 | 24 | int main(){ 25 | int a, b, sum, i, j, n; 26 | long binA, binB, temp; 27 | char c, strA[100], strB[100]; 28 | cin >> n; 29 | for (i = 0; i < n; ++i){ 30 | cin >> hex >> a; 31 | cin >> c; 32 | cin >> hex >> b; 33 | 34 | if (c == '+')sum = a + b; 35 | else if (c == '-') sum = a - b; 36 | 37 | binA = binB = 0; 38 | j = -1; 39 | do{ 40 | temp = a % 2; 41 | a = a / 2; 42 | strA[++j] = temp+48; 43 | }while (a != 0); 44 | strA[++j] = '\0'; 45 | revStr(strA); 46 | j = -1; 47 | do{ 48 | temp = b % 2; 49 | b = b / 2; 50 | strB[++j] = temp+48; 51 | }while (b != 0); 52 | strB[++j] = '\0'; 53 | revStr(strB); 54 | 55 | //printf("%013s\n", strA); 56 | cout << setfill('0') << setw(13) << strA << " " << c << " "; 57 | cout << setfill('0') << setw(13) << strB << " = " << sum << endl; 58 | } 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /455.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Periodic Strings UVa 455 2 | Programmer: Md. Mahmud Ahsan 3 | Description: AD HOC 4 | Compiled: Visual C++ 7.0 5 | Date: 26-10-05 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | char strMain[100]; 12 | char strSub[100]; 13 | int lenMain, lenSub; 14 | 15 | 16 | int comp(){ 17 | lenSub = strlen(strSub); 18 | int i, j; 19 | for (i = 0; strMain[i]; i += lenSub){ 20 | for (j = 0; strSub[j]; ++j){ 21 | if (strSub[j] != strMain[i+j]) 22 | return 0; 23 | } 24 | } 25 | return 1; 26 | } 27 | 28 | int main(){ 29 | //freopen("input.txt", "r", stdin); 30 | int n, i, j, k; 31 | cin >> n; 32 | 33 | int flag = 1; 34 | while (n--){ 35 | cin >> strMain; 36 | lenMain = strlen(strMain); 37 | 38 | if (!flag) cout << endl; flag = 0; 39 | 40 | for (i = 0; strMain[i]; ++i){ 41 | for (j = 0; j <= i; ++j) 42 | strSub[j] = strMain[j]; 43 | strSub[j] = '\0'; 44 | k = comp(); 45 | if (k == 1){ 46 | cout << lenSub << endl; 47 | break; 48 | } 49 | } 50 | } 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /458.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: The Decoder UVa 458 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 07-08-04 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | int i; 12 | char input[200]; 13 | char c; 14 | 15 | while (cin >> input){ 16 | for (i = 0; input[i] != '\0'; i++){ 17 | c = input[i] - 7; 18 | cout << c; 19 | } 20 | cout << endl; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /469.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Wetlands of Florida UVa 469 2 | Programmer: Md. Mahmud Ahsan 3 | Description: DFS 4 | Compiled: Visual C++ 7.0 5 | Date: 08-12-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | //global declaration 13 | int dr[] = {-1, -1, -1, 0, 1, 1, 1, 0}; 14 | int dc[] = {-1, 0, 1, 1, 1, 0, -1, -1}; 15 | int N, row, col; 16 | char grid[105][105]; 17 | char tempGrid[105][105]; 18 | 19 | int dfs(int x, int y){ 20 | int dx, dy, i, total = 1; 21 | 22 | grid[x][y] = '2'; // 2 for gray 23 | for (i = 0; i < 8; ++i){ 24 | dx = dr[i] + x; 25 | dy = dc[i] + y; 26 | if ((dx >=0 && dx < row) && (dy >= 0 && dy < col)){ 27 | if (grid[dx][dy] == 'W'){ 28 | total += dfs(dx, dy); 29 | } 30 | } 31 | } 32 | grid[x][y] = '3'; // 3 for black 33 | return total; 34 | } 35 | 36 | int main(){ 37 | //freopen("input.txt", "r", stdin); 38 | int i, x, y, j,flag = 1, total; 39 | char temp[120]; 40 | cin >> N; 41 | cin.getline(temp, sizeof(temp)); // eat new line 42 | cin.getline(temp, sizeof(temp)); // eat blank line 43 | 44 | while (N--){ 45 | i = 0; 46 | if (flag == 0) cout << endl; flag = 0; 47 | 48 | while(cin.getline(temp, sizeof(temp))){ 49 | if (strlen(temp) == 0) break; 50 | if (temp[0] >= 'A' && temp[0] <= 'Z'){ 51 | strcpy(grid[i], temp); 52 | strcpy(tempGrid[i], temp); 53 | ++i; 54 | continue; 55 | } 56 | col = strlen(grid[0]); 57 | row = i; 58 | sscanf(temp, "%d%d", &x, &y); 59 | --x; 60 | --y; 61 | if (grid[x][y] == 'W'){ 62 | total = dfs(x, y); 63 | } 64 | else{ 65 | cout << "0" << endl; 66 | continue; 67 | } 68 | cout << total << endl; 69 | //rebuild the grid again 70 | for (i = 0; i < row; ++i) 71 | strcpy(grid[i], tempGrid[i]); 72 | }; 73 | } 74 | return 0; 75 | } -------------------------------------------------------------------------------- /483.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Word Scramble UVa 483 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 25-08-04 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | //freopen("input.txt", "r", stdin); 13 | //freopen("output.txt", "w", stdout); 14 | 15 | char str[1000000]; 16 | int len; // count the lenght of the string 17 | int begin, end; 18 | 19 | while (cin.getline(str, sizeof(str))){ 20 | begin = end = 0; 21 | len = strlen(str); 22 | str[len] = '\0'; // end of the string is set to null 23 | 24 | for (int i = 0; i <= len ; i++){ 25 | // as '\0' must be counted so i <= len 26 | if (str[i] == ' ' || str[i] == '\0'){ 27 | end = i; // set end flag 28 | } 29 | else 30 | continue; 31 | 32 | for (int j = end - 1; j >= begin; j--){ 33 | cout << str[j]; 34 | } 35 | if (str[i] == ' ') cout << str[i]; 36 | begin = end + 1; // set begin after blank 37 | } 38 | cout << endl; 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /484.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: The Department of Redundancy Department UVa 484 2 | Programmer: Md. Mahmud Ahsan 3 | Description: AD HOC 4 | Compiled: Visual C++ 7.0 5 | Date: 20-08-05 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int count[50000][2]; 12 | vector v; 13 | 14 | int main(){ 15 | int i, j, k, n ,total; 16 | 17 | while (cin >> n){ 18 | v.push_back(n); 19 | } 20 | 21 | j = 0; 22 | int siz = v.size(); 23 | for (i = 0; i < siz; ++i){ 24 | if (v[i] == -777979) 25 | continue; 26 | total = 1; 27 | for (k = i+1; k < siz; ++k){ 28 | if (v[i] == v[k]){ 29 | ++total; 30 | v[k] = -777979; // marking for this item searched prev 31 | } 32 | } 33 | count[j][0] = v[i]; 34 | count[j][1] = total; 35 | ++j; 36 | } 37 | for (i = 0; i < j; ++i){ 38 | cout << count[i][0] << " " << count[i][1] << endl; 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /488.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Triangle Wave UVa 488 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 24-10-04 5 | */ 6 | #include 7 | 8 | void print(int n){ 9 | int i, j; 10 | for (i = 1; i <= n; i++){ 11 | for (j = 0; j < i; j++) 12 | cout << i; 13 | cout << endl; 14 | } 15 | for (i = n-1; i >= 1; --i){ 16 | for (j = 0; j < i; j++) 17 | cout << i; 18 | cout << endl; 19 | } 20 | } 21 | 22 | int main(){ 23 | int test; 24 | int amp, freq; 25 | int i, j, flag; 26 | 27 | flag = 0; 28 | cin >> test; 29 | 30 | for (i = 0; i < test; i++){ 31 | cin >> amp >> freq; 32 | 33 | for (j = 0; j < freq; j++){ 34 | if (flag) cout << endl; 35 | print(amp); 36 | flag = 1; 37 | } 38 | 39 | 40 | } 41 | 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /489.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Hangman UVa 489 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 25-03-05 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | char strSol[50]; 13 | char strGuess[50]; 14 | int round; 15 | 16 | int sol[50][2]; 17 | int guess[50][2]; 18 | int i, j, lenSol, lenGuess; 19 | 20 | while (true){ 21 | cin >> round; 22 | if (round == -1) break; 23 | 24 | cin >> strSol >> strGuess; 25 | 26 | lenSol = strlen(strSol); 27 | for (i = 0; i < lenSol; ++i){ 28 | sol[i][0] = strSol[i]; 29 | sol[i][1] = 1; 30 | } 31 | 32 | lenGuess = strlen(strGuess); 33 | for (i = 0; i < lenGuess; ++i){ 34 | guess[i][0] = strGuess[i]; 35 | guess[i][1] = 1; 36 | } 37 | 38 | for (j = 0; j < lenGuess; ++j){ 39 | for (i = 0; i < lenSol; ++i){ 40 | if (guess[j][0] == sol[i][0]){ 41 | guess[j][1] = sol[i][1] = 0; 42 | } 43 | } 44 | } 45 | 46 | int resSol = 0; 47 | for (i = 0; i < lenSol; ++i){ 48 | if (sol[i][1] == 1) 49 | ++resSol; 50 | } 51 | 52 | int resGuess = 0; 53 | for (i = 0; i < lenGuess; ++i){ 54 | if (guess[i][1] == 1) 55 | ++resGuess; 56 | } 57 | 58 | cout << "Round " << round << endl; 59 | 60 | if (resSol == 0 && resGuess < 7) 61 | cout << "You win." << endl; 62 | else if (resSol != 0 && (resGuess < 7 || resGuess > 7)) 63 | cout << "You chickened out." << endl; 64 | else{ 65 | cout << "You lose." << endl; 66 | } 67 | } 68 | 69 | return 0; 70 | } -------------------------------------------------------------------------------- /490.CPP: -------------------------------------------------------------------------------- 1 | /* Problem: Rotating Sentences UVa 490 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 14-04-05 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | int main(){ 12 | char str[105][105]; 13 | int array[105]; 14 | int n, i, j, k, max; 15 | 16 | i = 0; 17 | max = 0; 18 | 19 | while (cin.getline(str[i], sizeof(str)) != NULL){ 20 | n = strlen (str[i]); 21 | if (n > max) max = n; 22 | array[i] = n; 23 | ++i; 24 | } 25 | 26 | i = i - 1; 27 | 28 | for(k = 0; k < max; ++k){ 29 | for(j = i; j >= 0; --j){ 30 | if (k >= array[j]) 31 | cout << " "; 32 | else 33 | cout << str[j][k]; 34 | } 35 | cout << endl; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /492.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Pig-Latin UVa 492 2 | Programmer: Md. Mahmud Ahsan 3 | Description: AD HOC 4 | Compiled: Visual C++ 7.0 5 | Date: 20-08-05 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | int state, save; 14 | char c, temp, saveChar; 15 | 16 | state = 0; 17 | while ( (c=cin.get()) && !cin.eof()){ 18 | //state is used to detect for the first letter 19 | if (state == 0 && ( (c >= 'a' && c <='z') || (c >= 'A' && c <= 'Z'))){ 20 | state = 1; 21 | temp = tolower(c); // converting temporarily lower case 22 | if (temp=='a'||temp=='e'||temp=='i'||temp=='o'||temp=='u'){ 23 | cout << c;//but always print the orginal case 24 | save = 0;// no consonent at the first letter of the word 25 | } 26 | else{ 27 | saveChar = c; 28 | save = 1; 29 | } 30 | } 31 | else if( (c>='a' && c<='z') || (c>='A' && c<='Z')){ 32 | //letter other than the first letter of a word 33 | cout << c; 34 | } 35 | else{ 36 | //when a word finish 37 | if (state == 1 && save == 1){ 38 | //for consonent 39 | cout << saveChar << "ay"; 40 | state = save = 0; 41 | } 42 | else if (state == 1){// for vowel 43 | cout << "ay"; 44 | state = 0; 45 | } 46 | 47 | //for other sumbol, space, newline, digit etc 48 | cout << c; 49 | 50 | } 51 | } 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /494.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Kindergarten Counting Game UVa 494 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 25-08-04 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | char c; 12 | bool flag = false; // remember the first character of a word if letter 13 | int words = 0; 14 | 15 | c = cin.get(); 16 | while (!cin.eof()){ // untile the end of the file is reached 17 | 18 | while (c != '\n'){ // for per line 19 | if (( c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){ 20 | if (!flag){ 21 | // if false 22 | flag = true; 23 | words++; 24 | } 25 | } 26 | else 27 | flag = false; 28 | 29 | c = cin.get(); 30 | } 31 | 32 | cout << words << endl; 33 | words = 0; 34 | flag = false; 35 | c = cin.get(); 36 | } 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /496.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Simply Subsets UVa 496 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 14-05-04 5 | */ 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | char strA[5000], *ptr; 12 | char strB[5000]; 13 | 14 | int arrayA[1000], arrayB[1000]; 15 | int i, j, lengthA, lengthB, count; 16 | bool emptyA, emptyB; 17 | 18 | while (cin.getline(strA, sizeof(strA))){ 19 | cin.getline(strB, sizeof(strB)); 20 | 21 | emptyA = emptyB = true; 22 | count = 0; 23 | // for string A 24 | ptr = strtok(strA, " \t\n"); 25 | i = 0; 26 | while (ptr){ 27 | arrayA[i++] = atoi(ptr); 28 | ptr = strtok(NULL, " \t\n"); 29 | } 30 | lengthA = i; 31 | // for string B 32 | ptr = strtok(strB, " \t\n"); 33 | i = 0; 34 | while (ptr){ 35 | arrayB[i++] = atoi(ptr); 36 | ptr = strtok(NULL, " \t\n"); 37 | } 38 | lengthB = i; 39 | 40 | for (i = 0; i < lengthA; ++i){ 41 | for (j = 0; j < lengthB; ++j){ 42 | if (arrayA[i] == arrayB[j]){ 43 | ++count; 44 | arrayA[i] = 0; 45 | arrayB[j] = 0; 46 | } 47 | } 48 | } 49 | 50 | for (i = 0; i < lengthA; ++i){ 51 | if (arrayA[i] != 0) 52 | emptyA = false; 53 | } 54 | 55 | for (i = 0; i < lengthB; ++i){ 56 | if (arrayB[i] != 0) 57 | emptyB = false; 58 | } 59 | 60 | if (emptyA && emptyB) 61 | cout << "A equals B" << endl; 62 | else if (!emptyA && !emptyB && count) 63 | cout << "I'm confused!" << endl; 64 | else if (!emptyA && !emptyB) 65 | cout << "A and B are disjoint" << endl; 66 | else if (emptyA && !emptyB) 67 | cout << "A is a proper subset of B" << endl; 68 | else if (!emptyA && emptyB) 69 | cout << "B is a proper subset of A" << endl; 70 | } 71 | return 0; 72 | } -------------------------------------------------------------------------------- /498.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Polly the Polynomial UVa 498 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Horner's Rule 4 | Compiled: Visual Studio .Net 5 | Date: 26-12-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | const long MX = 1000000; 15 | 16 | double horner(int n, double array[], double x){ 17 | //horner compute the value of a polynomial of order n 18 | double p; 19 | p = array[n]; 20 | for (int i = n-1; i >= 0; --i){ 21 | p = p * x + array[i]; 22 | } 23 | return p; 24 | } 25 | 26 | double array[MX], result; 27 | char str1[MX], str2[MX], *ptr; 28 | 29 | int main(){ 30 | #ifndef ONLINE_JUDGE 31 | freopen("498.in", "r", stdin); 32 | freopen("498.out", "w", stdout); 33 | #endif 34 | int n, i; 35 | 36 | bool space; 37 | 38 | while (cin.getline(str1, sizeof(str1))){ 39 | cin.getline(str2, sizeof(str2)); 40 | 41 | space = false; 42 | i = -1; 43 | ptr = strtok(str1, " \t\n"); 44 | while (ptr){ 45 | ++i; 46 | sscanf(ptr, "%lf", &array[i]); 47 | ptr = strtok(NULL, " \t\n"); 48 | } 49 | 50 | // reverse the order of the coefficients 51 | reverse(array, array + i+1); 52 | 53 | ptr = strtok(str2, " \t\n"); 54 | while (ptr){ 55 | sscanf(ptr, "%d", &n); 56 | result = horner(i, array, n); 57 | if (space)cout << " "; space = true; 58 | //cout << result; 59 | printf ("%.0f", result); 60 | ptr = strtok(NULL, " \t\n"); 61 | } 62 | cout << endl; 63 | } 64 | return 0; 65 | } -------------------------------------------------------------------------------- /499.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: What's The Frequency, Kenneth? UVa 499 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 10-10-04 5 | */ 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | // prototypes 11 | void initialize(char code[][2], int &a); 12 | int getMax(char array[][2]); 13 | 14 | //=========================================================================== 15 | int main(){ 16 | char code[52][2]; 17 | char str[100]; 18 | int max; 19 | int i, j; 20 | int temp; 21 | 22 | while (cin.getline(str, sizeof(str)) && !cin.eof()){ 23 | // if blank line is inputed 24 | if (strlen(str) == 0) continue; 25 | 26 | // initialization 27 | initialize(code, max); 28 | 29 | //search letters frequency 30 | for (i = 0; str[i]; i++){ 31 | for (j = 0; j < 52; j++){ 32 | if (str[i] == code[j][0]){ 33 | code[j][1] += 1; 34 | break; 35 | } 36 | } 37 | } 38 | 39 | max = getMax(code); 40 | // output the letter that occurs maximum 41 | for (i = 0; i < 52; i++){ 42 | temp = code[i][1] - 48; 43 | if (max == temp) 44 | cout << code[i][0]; 45 | } 46 | // output the max 47 | cout << " " << max << endl; 48 | } 49 | 50 | return 0; 51 | } 52 | //=========================================================================== 53 | 54 | void initialize(char code[][2], int &a){ 55 | int i; 56 | a = 0; 57 | 58 | for (i = 0; i < 26; i++){ 59 | code[i][0] = 'A' + i; 60 | code[i][1] = '0'; 61 | } 62 | 63 | for (i = 0; i < 26; i++){ 64 | code[i + 26][0] = 'a' + i; 65 | code[i + 26][1] = '0'; 66 | } 67 | } 68 | 69 | int getMax(char array[][2]){ 70 | int max, temp; 71 | int i; 72 | 73 | max = 0; 74 | for (i = 0; i < 52; i++){ 75 | temp = array[i][1] - 48; 76 | if (max < temp) 77 | max = temp; 78 | } 79 | return max; 80 | } -------------------------------------------------------------------------------- /524.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Prime Reing Problems UVa 524 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Backtracking 4 | Compiled: Visual C++ 7.0 5 | Date: 14-03-06 6 | */ 7 | #include 8 | using namespace std; 9 | 10 | bool visited[20]; 11 | int data[20], output[20], n, maxLevel; 12 | 13 | void init(){ 14 | for (int i = 0; i < n; ++i){ 15 | visited[i] = false; 16 | data[i] = i + 1; 17 | } 18 | } 19 | 20 | bool isPrime(int x){ 21 | if (x <= 1) return false; 22 | if (x == 2) return true; 23 | if (x % 2 == 0) return false; 24 | 25 | for (int i = 3; i * i <= x; i += 2) 26 | if (x % i == 0) return false; 27 | return true; 28 | } 29 | 30 | void backtrack(int level){ 31 | int temp; 32 | if (level >= maxLevel){ 33 | if (!isPrime(output[0] + output[maxLevel-1])) 34 | return; 35 | 36 | cout << output[0]; 37 | for (int i = 1; i < maxLevel; ++i) 38 | cout << " " << output[i]; 39 | cout << endl; 40 | return; 41 | } 42 | 43 | for (int i = 1; i < n; ++i){ 44 | if (visited[i]) continue; 45 | 46 | temp = output[level-1] + data[i]; 47 | if (isPrime(temp)){ 48 | visited[i] = true; 49 | output[level] = data[i]; 50 | backtrack(level + 1); 51 | visited[i] = false; 52 | } 53 | } 54 | } 55 | 56 | 57 | int main(){ 58 | //freopen("input.txt", "r", stdin); 59 | //freopen("output.txt", "w", stdout); 60 | int cases = 0; 61 | bool flag = false; 62 | 63 | while (cin >> n){ 64 | init(); 65 | if (flag) cout << endl; flag = true; 66 | cout << "Case " << ++cases << ":" << endl; 67 | visited[0] = true; 68 | output[0] = 1; 69 | maxLevel = n; 70 | backtrack(1); 71 | } 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /530.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Binomial Showdown UVa 530 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 15-12-04 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | typedef long long lint; 11 | 12 | lint nCr(lint n, lint r); 13 | //=============================================== 14 | int main(){ 15 | lint n, r, nMr; 16 | lint result; 17 | 18 | while (cin >> n >> r){ 19 | if (!n && !r) break; 20 | 21 | nMr = r; 22 | if (n-r < r){ 23 | nMr = n-r; 24 | } 25 | 26 | result = nCr(n, nMr); 27 | cout << result << endl; 28 | } 29 | return 0; 30 | } 31 | //=============================================== 32 | lint nCr(lint n, lint r){ 33 | lint i; 34 | lint k, prod; 35 | 36 | prod = 1; 37 | k = n; 38 | 39 | for (i = 1; i <= r; ++i) 40 | prod = (prod * k--) / i; 41 | return prod; 42 | } -------------------------------------------------------------------------------- /534.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Frogger UVa 534 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Graph + Floyd Warshall + Minimax 4 | Compiled: Visual C++ 7.0 5 | Date: 01-03-06 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | const int MX = 205; 15 | const int INF = 1 << 20; 16 | 17 | struct Node{ 18 | int x, y; 19 | }nodes[MX]; 20 | 21 | double graph[MX][MX]; 22 | int n; 23 | 24 | double getWeight(Node a, Node b){ 25 | return sqrt ((double) ((a.x - b.x) * (a.x - b.x) + (a.y - b.y)*(a.y - b.y))); 26 | } 27 | 28 | void init(){ 29 | for (int i = 1; i <= n; ++i){ 30 | for (int j = 1; j <= n; ++j) 31 | graph[i][j] = INF; 32 | } 33 | } 34 | 35 | void floydWarshall(){ 36 | int i, j, k; 37 | for (k = 0; k < n; ++k){ 38 | for (i = 0; i < n; ++i){ 39 | for (j = 0; j < n; ++j){ 40 | graph[i][j] = min(graph[i][j], max(graph[i][k], graph[k][j])); 41 | } 42 | } 43 | } 44 | } 45 | 46 | int main(){ 47 | //freopen("input.txt", "r", stdin); 48 | int i, j, cases = 0, a, b; 49 | double temp; 50 | 51 | while (cin >> n){ 52 | if (n == 0) break; 53 | 54 | init(); 55 | for (i = 0; i < n; ++i){ 56 | cin >> a >> b; 57 | nodes[i].x = a; 58 | nodes[i].y = b; 59 | } 60 | 61 | //compute edges 62 | for (i = 0; i < n; ++i){ 63 | for (j = i + 1; j < n; ++j){ 64 | temp = getWeight(nodes[i], nodes[j]); 65 | graph[i][j] = temp; 66 | graph[j][i] = temp; 67 | } 68 | } 69 | floydWarshall(); 70 | cout << "Scenario #" << ++cases << endl; 71 | printf("Frog Distance = %.3lf\n", graph[0][1]); //graph[0][1] is fiona's stone 72 | cout << endl; 73 | } 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /543.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Goldbach's Conjecture UVa 543 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 01-12-04 5 | */ 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | //-------------------------- 11 | const int limit = 1000002; 12 | long primes[1000002]; // contains prime numbers 13 | //-------------------------- 14 | 15 | void initPrimes(); // using sieve medhod 16 | //========================== 17 | int main(){ 18 | long input; 19 | int i; 20 | 21 | initPrimes(); 22 | while (cin >> input){ 23 | if (input == 0) break; 24 | 25 | // applying goldbach's conjecture 26 | for (i = 2; i <= input; i++){ 27 | if (primes[i] && primes[input - i]){ 28 | cout << input << " = " << i << " + " << input - i << endl; 29 | break; 30 | } 31 | if (i == input) 32 | cout << "Goldbach's conjecture is wrong." << endl; 33 | } 34 | 35 | } 36 | return 0; 37 | } 38 | 39 | void initPrimes(){ 40 | // prime factors using sieve method 41 | int i, j; 42 | 43 | for (i = 0; i <= limit; i++) 44 | primes[i] = 1; 45 | 46 | primes[0] = primes[1] = 0; 47 | 48 | for (i = 2; i < limit; i++){ 49 | for (j = i + i; j < limit; j += i){ 50 | primes[j] = 0; 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /544.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Heave Cargo UVa 544 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Graph + Floyd Warshall + Maximini 4 | Compiled: Visual C++ 7.0 5 | Date: 01-03-06 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | const int MX = 205; 13 | 14 | int graph[MX][MX], n; 15 | string id[MX]; 16 | 17 | int getId(string x){ 18 | for (int i = 0; i < n; ++i) 19 | if (id[i] == x) return i; 20 | id[n] = x; 21 | return n++; 22 | } 23 | 24 | void init(){ 25 | for (int i = 0; i < n; ++i){ 26 | for (int j = 0; j < n; ++j) 27 | graph[i][j] = 0; 28 | } 29 | } 30 | 31 | void floydWarshall(){ 32 | int i, j, k; 33 | for (k = 0; k < n; ++k){ 34 | for(i = 0; i < n; ++i){ 35 | for (j = 0; j < n; ++j) 36 | graph[i][j] = max(graph[i][j], min(graph[i][k], graph[k][j])); 37 | } 38 | } 39 | } 40 | 41 | int main(){ 42 | //freopen("input.txt", "r", stdin); 43 | //freopen("output.txt", "w", stdout); 44 | int r, i, j, cases = 0, temp; 45 | int id1, id2; 46 | string a, b; 47 | 48 | while (cin >> n >> r){ 49 | if (n == 0 && r == 0) break; 50 | 51 | init(); 52 | n = 0; 53 | for (i = 0; i < r; ++i){ 54 | cin >> a >> b >> temp; 55 | id1 = getId(a); 56 | id2 = getId(b); 57 | graph[id1][id2] = temp; 58 | graph[id2][id1] = temp; 59 | } 60 | 61 | floydWarshall(); 62 | 63 | cout << "Scenario #" << ++cases << endl; 64 | cin >> a >> b; 65 | id1 = getId(a); id2 = getId(b); 66 | cout << graph[id1][id2] << " tons" << endl; 67 | cout << endl; 68 | } 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /558.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Wormholes UVa 558 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Graph + Bellman Ford 4 | Compiled: Visual C++ 7.0 5 | Date: 14-02-06 6 | */ 7 | #include 8 | using namespace std; 9 | 10 | const int INF = 10000000; 11 | const int MX = 2050; 12 | 13 | struct Node{ 14 | int u, v, weight; 15 | }nodes[MX]; 16 | 17 | int d[MX]; 18 | int m, n, t; 19 | 20 | void initializeSingleSource(int s){ 21 | for (int i = 0; i < n; ++i) 22 | d[i]= INF; 23 | d[s] = 0; 24 | } 25 | 26 | void relax(int u, int v, int w){ 27 | if (d[v] > d[u] + w) 28 | d[v] = d[u] + w; 29 | } 30 | 31 | bool bellmanFord(int s){ 32 | //initializeSingleSource(s); 33 | int i, j; 34 | for (i = 0; i < n-1; ++i){ 35 | for (j = 0; j < m; ++j){ 36 | relax(nodes[j].u, nodes[j].v, nodes[j].weight); 37 | } 38 | } 39 | for (j = 0; j < m; ++j){ 40 | if (d[nodes[j].v] > d[nodes[j].u] + nodes[j].weight) 41 | return false; 42 | } 43 | return true; 44 | } 45 | 46 | int main(){ 47 | //freopen("input.txt", "r", stdin); 48 | int c, x, y, z, i; 49 | cin >> c; 50 | 51 | while (c--){ 52 | cin >> n >> m; 53 | for (i = 0; i < m; ++i){ 54 | cin >> x >> y >> z; 55 | nodes[i].u = x; 56 | nodes[i].v = y; 57 | nodes[i].weight = z; 58 | } 59 | if (!bellmanFord(0)) 60 | cout << "possible" << endl; 61 | else 62 | cout << "not possible" << endl; 63 | 64 | } 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /567.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Risk UVa 567 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Graph + Floyd Warshall 4 | Compiled: Visual C++ 7.0 5 | Date: 28-02-06 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | const int MX = 30; 13 | const int INF = 1 << 20; 14 | 15 | int graph[MX][MX]; 16 | int parent[MX][MX], counter; 17 | 18 | void init(){ 19 | for (int i = 1; i < MX; ++i){ 20 | for (int j = 1; j < MX; ++j){ 21 | graph[i][j] = INF; 22 | parent[i][j] = i; 23 | } 24 | graph[i][i] = 0; 25 | } 26 | } 27 | 28 | void floydWarshall(int n){ 29 | int i,j ,k; 30 | for (k = 1; k <= n; ++k){ 31 | for (i = 1; i <= n; ++i){ 32 | for (j = 1; j <= n; ++j){ 33 | if (graph[i][j] > graph[i][k] + graph[k][j]){ 34 | graph[i][j] = graph[i][k] + graph[k][j]; 35 | parent[i][j] = parent[k][j]; 36 | } 37 | } 38 | } 39 | } 40 | } 41 | 42 | void getCountries(int i, int j){ 43 | if (i == j) 44 | return; 45 | else{ 46 | getCountries(i, parent[i][j]); 47 | ++counter; 48 | } 49 | } 50 | 51 | int main(){ 52 | //freopen("input.txt", "r", stdin); 53 | int i, j, k, temp, m, cases = 0; 54 | bool exit = false; 55 | 56 | while (!cin.eof()){ 57 | init(); 58 | for (i = 1; i <= 19; ++i){ 59 | cin >> m; 60 | if (cin.eof()){ 61 | exit = true; 62 | break; 63 | } 64 | for (j = 1; j <= m; ++j){ 65 | cin >> temp; 66 | graph[i][temp] = 1; 67 | graph[temp][i] = 1; 68 | 69 | parent[i][temp] = i; 70 | parent[temp][i] = temp; 71 | } 72 | } 73 | if (exit) break; 74 | floydWarshall(20); 75 | 76 | cout << "Test Set #" << ++cases << endl; 77 | cin >> m; 78 | for (i = 1; i <= m; ++i){ 79 | counter = 0; 80 | cin >> j >> k; 81 | getCountries(j, k); 82 | printf("%2d", j); 83 | cout << " to "; 84 | printf("%2d", k); 85 | cout << ": "; 86 | printf("%-2d\n", counter); 87 | } 88 | cout << endl; 89 | } 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /572.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Oil Deposit UVa 572 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 05-09-04 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int dr[] = {-1, -1, -1, 0, 1, 1, 1, 0}; 12 | int dc[] = {-1, 0, 1, 1, 1, 0, -1, -1}; 13 | // as it contains 8 so in DFSvisit we must check 8 14 | 15 | int m, n; 16 | char array[100][100]; 17 | 18 | // prototlype 19 | int valid(int, int); 20 | int DFS(); 21 | void DFSvisit(int r, int c); 22 | 23 | //================================================================================ 24 | int main(){ 25 | char string[100]; 26 | int i, j; 27 | int len; 28 | static int title = 1; 29 | 30 | while (cin >> m >> n){ 31 | if (m == 0 && n == 0) break; 32 | len = 0; 33 | for (i = 0; i < 100; i++){ 34 | for (j = 0; j < 100; j++) 35 | array[i][j] = 0; 36 | } 37 | 38 | for (i = 0; i < m; i++){ 39 | cin >> string; 40 | 41 | for (j = 0; j < n; j++){ 42 | array[i][j] = string[j] ; 43 | } 44 | } 45 | 46 | int result = DFS(); 47 | cout << result << endl; 48 | } 49 | 50 | return 0; 51 | } 52 | //================================================================================== 53 | 54 | int DFS(){ 55 | int r, c, count = 0; 56 | for (r = 0; r < m; r++){ 57 | for (c = 0; c < n; c++){ 58 | if ('@' == array[r][c]){ 59 | count++; 60 | DFSvisit(r, c); 61 | } 62 | } 63 | } 64 | return count; 65 | } 66 | 67 | void DFSvisit(int r, int c){ 68 | int nr, nc, i; 69 | array[r][c] = '*'; 70 | 71 | for (i = 0; i < 8; i++){ 72 | nr = r + dr[i]; 73 | nc = c + dc[i]; 74 | if (valid(nr, nc) && '@' == array[nr][nc]) 75 | DFSvisit(nr, nc); 76 | } 77 | 78 | } 79 | 80 | int valid(int r, int c){ 81 | if (r < 0 || c < 0 || r >= m || c >= n) 82 | return 0; 83 | else 84 | return 1; 85 | } 86 | -------------------------------------------------------------------------------- /575.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Skew Binary UVa 575 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 05-08-04 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | //prototype 13 | long int skewBinary(char string[]); 14 | 15 | //===================================================== 16 | int main(){ 17 | char string[500]; 18 | long int result; 19 | 20 | while (cin >> string){ 21 | if (string[0] == '0') break; 22 | 23 | result = skewBinary(string); 24 | cout << result << endl; 25 | } 26 | return 0; 27 | } 28 | //===================================================== 29 | 30 | long int skewBinary(char string[]){ 31 | long int result, temp; 32 | long int digit; 33 | int count; 34 | int i; 35 | 36 | result = 0; 37 | count = strlen(string); 38 | for (i = 0; string[i] != '\0'; i++){ 39 | //convert a charcter of string to decimal form 40 | digit = string[i] - 48; 41 | //as the problem has given the equation 42 | temp = digit * (pow(2, count) - 1); 43 | result += temp; 44 | --count; 45 | } 46 | 47 | if (result > 2147483647 || result < 0) return 2147483647; 48 | return result; 49 | } -------------------------------------------------------------------------------- /576.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Haiku Review UVa 576 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 14-02-05 5 | */ 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | char str[200]; 12 | int i; 13 | int iFirst, iSecond, iThird, iCount; 14 | bool fFirst, fSecond, fThird; 15 | 16 | while (cin.getline(str, sizeof(str))){ 17 | if (strcmp(str, "e/o/i") == 0) 18 | break; 19 | 20 | i = iFirst = iSecond = iThird = 0; 21 | iCount = 1; 22 | fFirst = true; 23 | 24 | while (str[i]){ 25 | if (str[i] == '/') 26 | iCount++; 27 | if (iCount == 2){ 28 | fFirst =false; fSecond = true; fThird = false; 29 | } 30 | else if (iCount == 3){ 31 | fSecond = false; fThird = true; fFirst = false; 32 | } 33 | 34 | int k = 1; 35 | 36 | if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' || str[i] == 'y'){ 37 | if (fFirst) 38 | iFirst++; 39 | 40 | else if (fSecond) 41 | iSecond++; 42 | 43 | else if (fThird) 44 | iThird++; 45 | while (str[i] && str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' || str[i] == 'y'){ 46 | i++; 47 | k = 0; 48 | } 49 | } 50 | if (k != 0) 51 | ++i; 52 | } 53 | if (iFirst!= 5) 54 | cout << 1 << endl; 55 | else if (iSecond != 7) 56 | cout << 2 << endl; 57 | else if (iThird != 5) 58 | cout << 3 << endl; 59 | else 60 | cout << "Y" << endl; 61 | 62 | } 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /591.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Box of Bricks UVa 591 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 06-08-04 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | //prototypes 12 | 13 | int move(int array[], int size); 14 | 15 | //========================================================== 16 | int main(){ 17 | //freopen("input.txt", "r", stdin); 18 | //freopen("output.txt", "w", stdout); 19 | 20 | static int set = 0; 21 | int *p; // dynamic memory allocation 22 | int input, result; 23 | int i; 24 | 25 | while (cin >> input){ 26 | if (input == 0) break; 27 | p = new int[input]; 28 | for (i = 0; i < input; i++) 29 | cin >> p[i]; 30 | 31 | result = move(p, input); 32 | cout << "Set #" << ++set << endl; 33 | cout << "The minimum number of moves is " << result << "." << endl; 34 | cout << endl; 35 | delete [] p; 36 | } 37 | return 0; 38 | } 39 | //========================================================== 40 | 41 | int move(int array[], int size){ 42 | int i, total, average; 43 | int temp; 44 | 45 | total = 0; 46 | temp = 0; 47 | for (i = 0; i < size; i++) 48 | total += array[i]; 49 | 50 | average = total / size; // for average height 51 | 52 | for (i = 0; i < size; i++) 53 | // check which pillar is larger or smaller than the average height 54 | temp += abs(array[i] - average); 55 | 56 | // because move can be happen only one time 57 | return temp / 2; 58 | } 59 | -------------------------------------------------------------------------------- /612.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: DNA Sorting UVa 612 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Sorting, Bubble + Stable 4 | Compiled: Visual C++ 7.0 5 | Date: 20-08-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | struct word{ 14 | char str[200]; 15 | int order; 16 | }words[200]; 17 | 18 | inline bool comp(word a, word b){ 19 | return a.order < b.order; 20 | } 21 | 22 | int bubble(char *); 23 | 24 | int main(){ 25 | //freopen("output.txt", "w", stdout); 26 | int test, t, m, n, i; 27 | char st[200]; 28 | 29 | bool flag = false; 30 | cin >> test; 31 | for (t = 0; t < test; ++t){ 32 | cin >> n >> m; 33 | for (i = 0; i < m; ++i){ 34 | cin >> words[i].str; 35 | strcpy(st, words[i].str); // temporarily sort by bubble 36 | words[i].order = bubble(st); 37 | } 38 | //for output 39 | if (flag) cout << endl; flag = true; 40 | 41 | stable_sort(words, words+m, comp); 42 | for (i = 0; i < m; ++i) 43 | cout << words[i].str << endl; 44 | 45 | } 46 | 47 | return 0; 48 | } 49 | int bubble(char *str){ 50 | int count=0; 51 | char temp; 52 | int i, j, len = strlen(str);; 53 | for (i = 1; i < len; ++i){ 54 | for (j = 0; j < len-i; ++j){ 55 | if (str[j] > str[j+1]){ 56 | temp = str[j]; 57 | str[j] = str[j+1]; 58 | str[j+1] = temp; 59 | ++count; 60 | } 61 | } 62 | } 63 | return count; 64 | } -------------------------------------------------------------------------------- /621.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Secret Research UVa 621 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Linux GNU C++ 4 | Date: 28-12-04 5 | */ 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | char str[1000]; 13 | int len, test; 14 | 15 | cin >> test; 16 | for (int i = 0; i < test; i++){ 17 | cin >> str; 18 | len = strlen(str); 19 | 20 | if (len == 1){ 21 | if (str[0] == '1' || str[0] == '4') 22 | cout << '+' << endl; 23 | } 24 | else if (len == 2){ 25 | if (str[0] == '7' && str[1] == '8') 26 | cout << '+' << endl; 27 | } 28 | if (len >= 3){ 29 | if (str[len-1] == '5' && str[len-2] == '3') 30 | cout << '-' << endl; 31 | else if (str[len-1] == '4' && str[0] == '9') 32 | cout << '*' << endl; 33 | else if (str[0] == '1' && str[1] == '9' && str[2] == '0') 34 | cout << '?' << endl; 35 | } 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /673.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Parentheses Balance UVa 673 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 14-09-04 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | 13 | //============================================================================= 14 | int main(){ 15 | bool flag; 16 | int test; 17 | // vector used here as like as stack 18 | vector v; 19 | char c[30], str[200]; 20 | int i; 21 | 22 | // Here a critical style input is taken. As I got 3 times wrong answer. 23 | cin.getline(c, sizeof(c)); 24 | sscanf(c, "%d", &test); 25 | 26 | for (int t = 0; t < test; t++){ 27 | i = 0; 28 | flag =false; 29 | 30 | cin.getline(str, sizeof(str)); 31 | while (str[i]){ 32 | flag = true; // it's reason described below 33 | 34 | if (str[i] == ')' && v[v.size()-1] == '(' && !v.empty()){ 35 | v.pop_back(); 36 | } 37 | else if (str[i] == ']' && v[v.size() - 1] == '[' && !v.empty()){ 38 | v.pop_back(); 39 | } 40 | else if (str[i] == ')' && v[v.size() - 1] == '['&& !v.empty()) 41 | break; 42 | else if (str[i] == ']' && v[v.size() - 1] == '('&& !v.empty()) 43 | break; 44 | else 45 | v.push_back(str[i]); 46 | 47 | ++i; 48 | } 49 | 50 | // if I not use it, then this function calls automatically before whileloop 51 | if (flag) { 52 | if (v.empty()) 53 | cout << "Yes" << endl; 54 | else 55 | cout << "No" << endl; 56 | v.clear(); 57 | } 58 | else 59 | cout << "Yes" << endl; 60 | } 61 | return 0; 62 | } 63 | //=========================================================================== 64 | -------------------------------------------------------------------------------- /674.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Coin Change UVa 674 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Counting Change + dp 4 | Compiled: Visual C++ 7.0 5 | Date: 20-08-05 6 | */ 7 | #include 8 | using namespace std; 9 | 10 | #define max 8000 11 | 12 | 13 | int coin[5] = {50, 25, 10, 5, 1}; 14 | long long nway[max]; 15 | long long dp[max]; 16 | 17 | void init(){ 18 | for(int i = 0; i < max; ++i) 19 | nway[i] = 0; 20 | } 21 | 22 | int main(){ 23 | int i, j, n, v = 5, c; 24 | int k = -1; 25 | for (n = 0; n < max; ++n){ 26 | init(); 27 | nway[0] = 1; 28 | 29 | for (i = 0; i < v; ++i){ 30 | c = coin[i]; 31 | for (j = c; j <= n; ++j) 32 | nway[j] += nway[j-c]; 33 | } 34 | dp[++k] = nway[n]; 35 | } 36 | while (cin >> n){ 37 | cout << nway[n] << endl; 38 | } 39 | 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /686.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Goldbach's Conjecture (II) UVa 686 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 01-12-04 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | void initPrimes(long array[], long limit); 11 | //=========================================== 12 | int main(){ 13 | long input; 14 | long primes[32770]; 15 | int i; 16 | int count; 17 | 18 | while (cin >> input){ 19 | count = 0; 20 | if (input == 0) break; 21 | 22 | initPrimes(primes, input); 23 | 24 | for (i = 2; i <= input; i++){ 25 | if (primes[i] && primes[input - i]){ 26 | ++count; 27 | primes[input - i] = 0; 28 | } 29 | if (i == input) break; 30 | } 31 | cout << count << endl; 32 | } 33 | 34 | return 0; 35 | } 36 | 37 | void initPrimes(long array[], long limit){ 38 | int i, j; 39 | 40 | for (i = 0; i < limit; i++) 41 | array[i] = 1; 42 | 43 | array[0] = array[1] = 0; 44 | 45 | for (i = 2; i <= limit; i++){ 46 | for (j = i + i; j <= limit; j += i) 47 | array[j] = 0; 48 | } 49 | } -------------------------------------------------------------------------------- /694.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: The Collatz Sequence UVa 694 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Linux GNU C++ 4 | Date: 05-08-04 5 | */ 6 | 7 | /********************************************************************* 8 | We have to follow the 3n + 1 law for the initial value a's input. if a == 1 break 9 | By computing Step 4, if a > l (limit value) then break without counting length of sequence 10 | We have to use do - while loop 11 | ***********************************************************************/ 12 | #include 13 | using namespace std; 14 | 15 | // typedef 16 | typedef long long int llint; 17 | 18 | // prototypes 19 | llint cycle(llint, llint); 20 | void show(int, llint, llint, llint); 21 | 22 | //==================================================================== 23 | int main(){ 24 | llint a, l, temp, max; // a means first input and l means last input 25 | static int count = 0; 26 | while (cin >> a >> l){ 27 | if (a < 0 && l < 0) break; 28 | 29 | // always keep a to lower value 30 | if (a > l){ 31 | temp = a; 32 | a = l; 33 | l = temp; 34 | } 35 | 36 | max = cycle(a, l); 37 | 38 | ++count; 39 | // print the desired output 40 | show(count, a, l, max); 41 | } 42 | return 0; 43 | } 44 | //==================================================================== 45 | 46 | llint cycle(llint a, llint l){ // a means first input, l means last input 47 | int i = 1; 48 | 49 | do{ 50 | // applying 3n + 1 law with modification 51 | if (a % 2 == 0){ 52 | a = a / 2; 53 | ++i; 54 | } 55 | else{ 56 | a = 3 * a + 1; 57 | if (a > l) break; // if a > l not count the sequence 58 | ++i; 59 | } 60 | }while (a != 1); 61 | 62 | return i; 63 | } 64 | 65 | void show(int i, llint a, llint l, llint max){ 66 | cout << "Case " << i << ": A = " << a << ", limit = " << l << ", number of terms = " << max << endl; 67 | } -------------------------------------------------------------------------------- /784.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Maze Exploration UVa 784 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Graph + DFS 4 | Compiled: Visual C++ 7.0 5 | Date: 08-12-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int dr[] = {-1, -1, -1, 0, 1, 1, 1, 0}; 13 | int dc[] = {-1, 0, 1, 1, 1, 0, -1, -1}; 14 | 15 | char grid[100][100]; 16 | int rowLimit, colLimit; 17 | 18 | void dfs(int x, int y){ 19 | int dx, dy, i; 20 | for (i = 0; i < 8; ++i){ 21 | dx = x + dr[i]; 22 | dy = y + dc[i]; 23 | 24 | colLimit = strlen(grid[x]); 25 | if ((dx >= 0 && dx < rowLimit) && (dy >= 0 && dy < colLimit)){ 26 | if (grid[dx][dy] == ' '){ 27 | grid[dx][dy] = '#'; 28 | dfs(dx, dy); 29 | } 30 | } 31 | } 32 | } 33 | 34 | int main(){ 35 | //freopen("input.txt", "r", stdin); 36 | char temp[100]; 37 | int N, i, j, row, col; 38 | cin >> N; 39 | cin.getline(temp, sizeof(temp)); // eat new line 40 | 41 | while (N--){ 42 | i = -1; 43 | while (cin.getline(temp, sizeof(temp))){ 44 | if (temp[0] == '_') break; 45 | 46 | strcpy(grid[++i], temp); 47 | for (j = 0; temp[j]; ++j){ //search for '*' 's location 48 | if (temp[j] == '*'){ 49 | row = i; 50 | col = j; 51 | grid[row][col] = '#'; 52 | } 53 | } 54 | } 55 | rowLimit = i+1; 56 | dfs(row, col); 57 | 58 | //output 59 | for (i = 0; i < rowLimit; ++i){ 60 | col = strlen(grid[i]); 61 | for (j = 0; j < col; ++j) 62 | cout << grid[i][j]; 63 | cout << endl; 64 | } 65 | cout << temp << endl; 66 | } 67 | return 0; 68 | } -------------------------------------------------------------------------------- /785.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Grid Colouring UVa 785 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Graph + Flood Fill 4 | Compiled: Visual C++ 7.0 5 | Date: 20-02-06 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | char str[2000], grid[2000][2000]; 12 | int rowLen, colLen; 13 | 14 | int dr[] = {-1, 0, 1, 0}; 15 | int dc[] = {0, 1, 0, -1}; 16 | 17 | void dfsVisit(int x, int y, char mark, char print){ 18 | int r, c, i; 19 | 20 | for (i = 0; i < 4; ++i){ 21 | r = x + dr[i]; 22 | c = y + dc[i]; 23 | if (r >= 0 && r < rowLen && c >= 0 && c < colLen && grid[r][c] == ' ' && grid[r][c] != mark ){ 24 | grid[r][c] = print; 25 | dfsVisit(r, c, mark, print); 26 | } 27 | } 28 | } 29 | 30 | void dfs(){ 31 | char mark, print; 32 | bool markFound; 33 | int i, j; 34 | 35 | markFound = false; 36 | for (i = 0; i < rowLen; ++i){ 37 | colLen = strlen(grid[i]); 38 | for (j = 0; j < colLen; ++j){ 39 | if (grid[i][j] != '_' && grid[i][j] != ' ' && !markFound){ 40 | mark = grid[i][j]; 41 | markFound = true; 42 | } 43 | if (markFound && grid[i][j] != mark && grid[i][j] != ' ' && grid[i][j] != '_'){ 44 | print = grid[i][j]; 45 | dfsVisit(i, j, mark, print); 46 | } 47 | } 48 | } 49 | } 50 | 51 | int main(){ 52 | //freopen("input.txt", "r", stdin); 53 | //freopen("output.txt", "w", stdout); 54 | int i, j, k, len; 55 | 56 | i = colLen = 0; 57 | while (cin.getline(str, sizeof(str))){ 58 | if (str[0] == '_'){ 59 | //output 60 | rowLen = i; 61 | dfs(); 62 | for (j = 0; j < rowLen; ++j){ 63 | colLen = strlen(grid[j]); 64 | for (k = 0; k < colLen; ++k) 65 | cout << grid[j][k]; 66 | cout << endl; 67 | } 68 | cout << str << endl; 69 | i = colLen = 0; 70 | continue; 71 | } 72 | strcpy(grid[i], str); 73 | len = strlen(grid[i]); 74 | if (len > colLen) 75 | colLen = len; 76 | ++i; 77 | } 78 | 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /793.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Network Connection UVa 793 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Data Structure + Set 4 | Compiled: Visual C++ 7.0 5 | Date: 15-02-06 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | const int MX = 1000000; 14 | int p[MX], rank[MX]; 15 | 16 | void makeSet(int x){ 17 | p[x] = x; 18 | rank[x] = 0; 19 | } 20 | 21 | void link(int x, int y){ 22 | if (rank[x] > rank[y]) 23 | p[y] = x; 24 | else{ 25 | p[x] = y; 26 | if (rank[x] == rank[y]) 27 | rank[y] = rank[y] + 1; 28 | } 29 | } 30 | 31 | int findSet(int x){ 32 | if (x != p[x]) 33 | p[x] = findSet(p[x]); 34 | return p[x]; 35 | } 36 | 37 | void unions(int x, int y){ 38 | link(findSet(x), findSet(y)); 39 | } 40 | int main(){ 41 | //freopen("input.txt", "r", stdin); 42 | int test, i, computers, a, b, success, unsuccess; 43 | char c, temp[100]; 44 | bool newLine = false; 45 | 46 | cin >> test; 47 | while (test--){ 48 | cin >> computers; 49 | cin.getline(temp, sizeof(temp)); // eat blank line 50 | 51 | success = unsuccess = 0; 52 | for (i = 1; i <= computers; ++i) 53 | makeSet(i); 54 | 55 | while (cin.getline(temp, sizeof(temp))){ 56 | if (strlen(temp) == 0) break; 57 | sscanf(temp, "%c%d%d", &c, &a, &b); 58 | if (c == 'c') 59 | unions(a,b); 60 | else if (c == 'q'){ 61 | if (findSet(a) == findSet(b)) 62 | ++success; 63 | else 64 | ++unsuccess; 65 | } 66 | } 67 | if (newLine) cout << endl; newLine = true; 68 | cout << success << "," << unsuccess << endl; 69 | } 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /834.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Continued Fraction UVa 834 2 | Programmer: Md. Mahmud Ahsan 3 | Compiled: Visual C++ 6.0 4 | Date: 11-09-04 5 | */ 6 | 7 | 8 | #include 9 | using namespace std; 10 | 11 | typedef long int lint; 12 | 13 | int main(){ 14 | lint first, second, result, remain; 15 | 16 | while (cin >> first >> second){ 17 | if (first == 0 && second == 0) break; 18 | 19 | result = first / second; 20 | remain = first % second; 21 | cout << "[" << result << ";"; 22 | 23 | while (remain != 0){ 24 | first = second; 25 | second = remain; 26 | 27 | result = first / second; 28 | remain = first % second; 29 | 30 | cout << result; 31 | if (remain > 0) cout << ","; 32 | } 33 | 34 | cout << "]" << endl; 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /855.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Lunch in Grid City UVa 855 2 | Programmer: Md. Mahmud Ahsan 3 | Description: Sorting + Median 4 | Compiled: Visual C++ 7.0 5 | Date: 09-12-05 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | inline bool comp(int a, int b){ 14 | return a < b; 15 | } 16 | 17 | int street[50000], avenue[50000]; 18 | 19 | int main(){ 20 | int T, median, s, a, f, i; 21 | 22 | cin >> T; 23 | while (T--){ 24 | cin >> s >> a >> f; 25 | for (i = 1; i <= f; ++i){ 26 | cin >> street[i] >> avenue[i]; 27 | } 28 | sort(street, street+f, comp); 29 | sort(avenue, avenue+f, comp); 30 | 31 | median = ceil(f / 2.0); 32 | 33 | cout << "(Street: " << street[median] << ", Avenue: " << avenue[median] <<")" << endl; 34 | 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /895.cpp: -------------------------------------------------------------------------------- 1 | /* Problem: Word Problem UVa 895 2 | Programmer: Md. Mahmud Ahsan 3 | Description: String 4 | Compiled: Visual C++ 7.0 5 | Date: 15-09-06 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int arr[1005][26]; 13 | 14 | int main(){ 15 | //freopen("input.txt", "r", stdin); 16 | char str[1000], ch; 17 | int i, j, counter, count; 18 | int letters[26]; 19 | bool found; 20 | 21 | counter = 0; 22 | while (cin >> str){ 23 | if (str[0] == '#') break; 24 | 25 | for (i = 0; str[i]; ++i){ 26 | ch = str[i]; 27 | if (ch >= 'a' && ch <= 'z'){ 28 | arr[counter][ch-97]++; 29 | } 30 | } 31 | ++counter; 32 | } 33 | 34 | cin.getline(str, sizeof(str)); 35 | while (cin.getline(str, sizeof(str))){ 36 | if (str[0] == '#') break; 37 | count = 0; 38 | 39 | for (i = 0; i < 26; ++i) 40 | letters[i] = 0; 41 | 42 | for (i = 0; str[i]; ++i){ 43 | ch = str[i]; 44 | if (ch >= 'a' && ch <= 'z'){ 45 | ++letters[ch-97]; 46 | } 47 | } 48 | for (i = 0; i < counter; ++i){ 49 | found = true; 50 | for (j = 0; j < 26; ++j){ 51 | if (arr[i][j] > letters[j]){ 52 | found = false; 53 | break; 54 | } 55 | } 56 | if (found) ++count; 57 | } 58 | cout << count << endl; 59 | } 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Mahmud Ahsan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 203 ACM Problems Source Code in C++ 2 | 3 | During 2003~2007 I was an undergraduate computer science student. In this time for our course material and for programming passion I used to try and solve ACM problems from the well known University of Valladolid site. I solved arround 203+ ACM problems from this site. Today I found in my laptop still the accepted codes are saved. So I think I should share these code. May be these could be useful for someone. 4 | 5 | One thing I want to mention these codes are accepted but not the best optimized code. These codes were written my during my student life so you can't expect that also. 6 | 7 | In the code repository file names are the problem number from the University of Valladolid site. 8 | --------------------------------------------------------------------------------