buggy_code
stringlengths
11
625k
fixed_code
stringlengths
17
625k
bug_type
stringlengths
2
4.45k
language
int64
0
8
token_count
int64
5
200k
change_count
int64
0
100
#include <cmath> #include <iostream> using namespace std; int main(void) { int n; cin >> n; bool f1 = false; for (int j = n - 1; j > 1; j--) { for (int i = 2; i <= sqrt(j); i++) { if ((j % i) == 0) { f1 = true; break; } } if (!f1) { cout << j << " "; break; } f1 = false; } bool f2 = false; for (int j = n + 1; true; j++) { for (int i = 2; i <= sqrt(j); i++) { if ((j % i) == 0) { f2 = true; break; } } if (!f2) { cout << j << endl; break; } f2 = false; } }
#include <cmath> #include <iostream> using namespace std; int main(void) { int n; while (cin >> n) { bool f1 = false; for (int j = n - 1; j > 1; j--) { for (int i = 2; i <= sqrt(j); i++) { if ((j % i) == 0) { f1 = true; break; } } if (!f1) { cout << j << " "; break; } f1 = false; } bool f2 = false; for (int j = n + 1; true; j++) { for (int i = 2; i <= sqrt(j); i++) { if ((j % i) == 0) { f2 = true; break; } } if (!f2) { cout << j << endl; break; } f2 = false; } } }
[["+", 0, 30, 0, 14, 8, 9, 0, 52, 0, 89], ["+", 0, 14, 8, 9, 0, 52, 15, 339, 0, 24], ["-", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35], ["+", 0, 14, 8, 9, 0, 52, 15, 339, 0, 25], ["+", 0, 14, 8, 9, 0, 52, 8, 9, 0, 45], ["+", 0, 52, 8, 9, 0, 7, 8, 9, 0, 46]]
1
180
6
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <string> #include <vector> #define loop(i, b, n) for (int i = b; i < n; i++) #define rep(i, n) loop(i, 0, n) #define INF 10000 #define MAX 1e6 using namespace std; int main() { vector<bool> check(MAX + 1); rep(i, MAX + 1) check[i] = true; vector<int> Primenumber(MAX + 1, 0); // Primearray int counter = 0; // Primearray counter; for (int i = 2; i < MAX + 1; i++) { if (check[i]) { for (int j = 2; i * j < MAX; j++) check[i * j] = false; //?´???°??????Primearray???????´??????????????????°?????¨???false??? Primenumber[counter] = i; counter++; } } int n; while (cin >> n) { int bigans = 0, sans = 0; for (int i = n + 1; n < INF; i++) if (check[i]) { bigans = i; break; } for (int i = n - 1; i > 1; i--) if (check[i]) { sans = i; break; } cout << sans << " " << bigans << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <string> #include <vector> #define loop(i, b, n) for (int i = b; i < n; i++) #define rep(i, n) loop(i, 0, n) #define INF 10000 #define MAX 1e7 using namespace std; int main() { vector<bool> check(MAX + 1); rep(i, MAX + 1) check[i] = true; vector<int> Primenumber(MAX + 1, 0); // Primearray int counter = 0; // Primearray counter; for (int i = 2; i < MAX + 1; i++) { if (check[i]) { for (int j = 2; i * j < MAX; j++) check[i * j] = false; //?´???°??????Primearray???????´??????????????????°?????¨???false??? Primenumber[counter] = i; counter++; } } int n; while (cin >> n) { int bigans = 0, sans = 0; for (int i = n + 1; n < MAX; i++) if (check[i]) { bigans = i; break; } for (int i = n - 1; i > 1; i--) if (check[i]) { sans = i; break; } cout << sans << " " << bigans << endl; } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22]]
1
252
4
#include <algorithm> #include <cctype> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; bool is_prime(int x) { for (int i = 2; i <= (int)sqrt(x); i++) { if (x % i == 0) return false; } return true; } int main() { int N; while (cin >> N) { int min = N; int max = N; while (min--) if (is_prime(min)) break; while (max--) if (is_prime(max)) break; cout << min << " " << max << endl; } // while(1); return 0; }
#include <algorithm> #include <cctype> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; bool is_prime(int x) { for (int i = 2; i <= (int)sqrt(x); i++) { if (x % i == 0) return false; } return true; } int main() { int N; while (cin >> N) { int min = N; int max = N; while (min--) if (is_prime(min)) break; while (max++) if (is_prime(max)) break; cout << min << " " << max << endl; } // while(1); return 0; }
[["-", 8, 9, 0, 52, 15, 339, 51, 27, 17, 68], ["+", 8, 9, 0, 52, 15, 339, 51, 27, 17, 29]]
1
147
2
#include <bits/stdc++.h> using namespace std; using db = double; using ll = long long; using vi = vector<int>; #define op operator #define pb push_back const int N = 6e4L + 11; int p[N]; int main() { iota(p, p + N, 0); for (int i = 2; i < N; i++) if (p[i] == i) for (int j = i; j < N; j += i) p[j] = i; ios ::sync_with_stdio(0); for (int x; cin >> x;) { for (int i = x - 1;; i--) if (p[i] == i) { cout << i << ' '; break; } for (int i = x + 1;; i++) if (p[i] == i) { cout << i << ' '; break; } } return 0; }
#include <bits/stdc++.h> using namespace std; using db = double; using ll = long long; using vi = vector<int>; #define op operator #define pb push_back const int N = 6e4L + 11; int p[N]; int main() { iota(p, p + N, 0); for (int i = 2; i < N; i++) if (p[i] == i) for (int j = i; j < N; j += i) p[j] = i; ios ::sync_with_stdio(0); for (int x; cin >> x;) { for (int i = x - 1;; i--) if (p[i] == i) { cout << i << ' '; break; } for (int i = x + 1;; i++) if (p[i] == i) { cout << i << '\n'; break; } } return 0; }
[["-", 64, 9, 0, 1, 0, 16, 12, 103, 0, 125], ["+", 64, 9, 0, 1, 0, 16, 12, 103, 0, 44]]
1
197
2
#include <iostream> using namespace std; bool is_sosu(int a) { for (int i = 2; i * i < a; i++) { if (a % i == 0) return false; } return true; } int main() { int n; while (cin >> n) { for (int i = n - 1; i >= 2; i--) { if (is_sosu(i)) { cout << i << " "; break; } } for (int j = n + 1;; j++) { if (is_sosu(j)) { cout << j << endl; break; } } } return 0; }
#include <iostream> using namespace std; bool is_sosu(int a) { for (int i = 2; i * i <= a; i++) { if (a % i == 0) return false; } return true; } int main() { int n; while (cin >> n) { for (int i = n - 1; i >= 2; i--) { if (is_sosu(i)) { cout << i << " "; break; } } for (int j = n + 1;; j++) { if (is_sosu(j)) { cout << j << endl; break; } } } return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19]]
1
135
2
#include <iostream> #include <vector> using namespace std; bool IsPrime[60000]; vector<int> Prime; void PrimeCalc() { for (int i = 0; i < 60000; ++i) { IsPrime[i] = true; } IsPrime[0] = IsPrime[1] = false; for (int i = 2; i < 60000; ++i) { if (IsPrime[i]) { Prime.push_back(i); for (int j = 2 * i; j < 60000; j += i) { IsPrime[j] = false; } } } } void solve() { PrimeCalc(); int n; while (~scanf("%d", &n)) { int count = 0; while (n > Prime[count]) { ++count; } cout << Prime[count - 1] << " "; if (IsPrime[Prime[count]]) { ++count; } cout << Prime[count] << endl; } } int main() { solve(); return (0); }
#include <iostream> #include <vector> using namespace std; bool IsPrime[60000]; vector<int> Prime; void PrimeCalc() { for (int i = 0; i < 60000; ++i) { IsPrime[i] = true; } IsPrime[0] = IsPrime[1] = false; for (int i = 2; i < 60000; ++i) { if (IsPrime[i]) { Prime.push_back(i); for (int j = 2 * i; j < 60000; j += i) { IsPrime[j] = false; } } } } void solve() { PrimeCalc(); int n; while (~scanf("%d", &n)) { int count = 0; while (n > Prime[count]) { ++count; } cout << Prime[count - 1] << " "; if (IsPrime[n]) { ++count; } cout << Prime[count] << endl; } } int main() { solve(); return (0); }
[["-", 15, 339, 51, 69, 341, 342, 0, 69, 28, 22], ["-", 51, 69, 341, 342, 0, 69, 341, 342, 0, 70], ["-", 51, 69, 341, 342, 0, 69, 341, 342, 0, 22], ["-", 51, 69, 341, 342, 0, 69, 341, 342, 0, 73], ["+", 0, 57, 15, 339, 51, 69, 341, 342, 0, 22]]
1
218
5
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n; while (cin >> n) { bool prime[500000]; fill(prime, prime + 500000, 0); vector<int> nums; for (int i = 2; i < 500000; ++i) { if (prime[i]) continue; nums.push_back(i); for (int j = i * 2; j < 500000; j += i) { prime[j] = true; } } vector<int>::iterator it = lower_bound(nums.begin(), nums.end(), n); if (*(it - 1) == n) cout << *(it - 1); else cout << *(it - 2); it = upper_bound(nums.begin(), nums.end(), n); cout << " " << *it << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n; while (cin >> n) { bool prime[500000]; fill(prime, prime + 500000, 0); vector<int> nums; for (int i = 2; i < 500000; ++i) { if (prime[i]) continue; nums.push_back(i); for (int j = i * 2; j < 500000; j += i) { prime[j] = true; } } vector<int>::iterator it = lower_bound(nums.begin(), nums.end(), n); if (*(it - 1) != n) cout << *(it - 1); else cout << *(it - 2); it = upper_bound(nums.begin(), nums.end(), n); cout << " " << *it << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 79]]
1
196
2
#include <iostream> #include <string> using namespace std; int main() { bool prime[60000]; for (int i = 0; i < 60000; i++) { prime[i] = 1; } prime[0] = prime[1] = 0; for (int i = 0; i * i < 60000; i++) { if (prime[i] == 1) { for (int j = i * 2; j < 60000; j += i) { prime[j] = 0; } } } int n; while (cin >> n) { int i; for (i = 1; prime[n - i] == 0; i--) ; cout << n - i << " "; for (i = 1; prime[n + i] == 0; i++) ; cout << n + i << endl; } }
#include <iostream> #include <string> using namespace std; int main() { bool prime[60000]; for (int i = 0; i < 60000; i++) { prime[i] = 1; } prime[0] = prime[1] = 0; for (int i = 0; i * i < 60000; i++) { if (prime[i] == 1) { for (int j = i * 2; j < 60000; j += i) { prime[j] = 0; } } } int n; while (cin >> n) { int i; for (i = 1; prime[n - i] == 0; i++) ; cout << n - i << " "; for (i = 1; prime[n + i] == 0; i++) ; cout << n + i << endl; } }
[["-", 0, 52, 8, 9, 0, 7, 26, 27, 17, 68], ["+", 0, 52, 8, 9, 0, 7, 26, 27, 17, 29]]
1
180
2
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <vector> using namespace std; int main() { const int END = -1; const int NUM = 50000; int num; int i, index, index2, index3; cin >> num; vector<int> eratos(NUM); for (i = 0; i < NUM - 1; i++) eratos[i] = i + 2; eratos[i] = END; while (!cin.eof()) { i = 0; index3 = 0; while (eratos[i] != END) { if (eratos[i] != 0) { index = i + 1; index3 = i + 1; while (eratos[index] != END) { if (eratos[index] % eratos[i] != 0) { eratos[index3] = eratos[index]; index3++; } index++; } eratos[index3] = END; } i++; } for (i = 0; i < index3; i++) { if (eratos[i] == num) { cout << eratos[i - 1] << " " << eratos[i + 1] << endl; break; } else if (eratos[i] > num) { cout << eratos[i - 2] << " " << eratos[i] << endl; break; } } cin >> num; } return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <vector> using namespace std; int main() { const int END = -1; const int NUM = 50022; int num; int i, index, index2, index3; cin >> num; vector<int> eratos(NUM); for (i = 0; i < NUM - 1; i++) eratos[i] = i + 2; eratos[i] = END; while (!cin.eof()) { i = 0; index3 = 0; while (eratos[i] != END) { if (eratos[i] != 0) { index = i + 1; index3 = i + 1; while (eratos[index] != END) { if (eratos[index] % eratos[i] != 0) { eratos[index3] = eratos[index]; index3++; } index++; } eratos[index3] = END; } i++; } for (i = 0; i < index3; i++) { if (eratos[i] == num) { cout << eratos[i - 1] << " " << eratos[i + 1] << endl; break; } else if (eratos[i] > num) { cout << eratos[i - 1] << ' ' << eratos[i] << endl; break; } } cin >> num; } return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["-", 31, 16, 12, 69, 341, 342, 0, 16, 12, 13], ["+", 31, 16, 12, 69, 341, 342, 0, 16, 12, 13], ["-", 0, 16, 31, 16, 31, 16, 12, 5, 0, 62], ["+", 0, 16, 31, 16, 31, 16, 12, 103, 0, 104]]
1
285
8
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int main(void) { int prime[50000]; int prime_f[51001]; int k; int n; int i, j; int n_size; while (cin >> n) { k = 0; memset(prime_f, 0, sizeof(prime_f)); prime_f[0] = prime_f[1] = 1; for (i = 2; i < 51000; i++) { if (!prime_f[i]) { k++; prime[k] = i; for (j = 2 * i; j < 50000; j += i) { prime_f[j] = 1; } } } for (i = 0; i < k; i++) { if (prime[i] >= n) { if (prime[i] = n) { cout << prime[i - 1] << " " << prime[i + 1] << endl; } else { cout << prime[i] << " " << prime[i + 1] << endl; } break; } } } return (0); }
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int main(void) { int prime[50000]; int prime_f[51001]; int k; int n; int i, j; int n_size; while (cin >> n) { k = 0; memset(prime_f, 0, sizeof(prime_f)); prime_f[0] = prime_f[1] = 1; for (i = 2; i < 51000; i++) { if (!prime_f[i]) { k++; prime[k] = i; for (j = 2 * i; j < 51000; j += i) { prime_f[j] = 1; } } } for (i = 0; i < k; i++) { if (prime[i] >= n) { if (prime[i] == n) { cout << prime[i - 1] << " " << prime[i + 1] << endl; } else { cout << prime[i - 1] << " " << prime[i] << endl; } break; } } } return (0); }
[["-", 0, 57, 64, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 57, 64, 9, 0, 7, 15, 16, 12, 13], ["-", 64, 9, 0, 57, 15, 339, 51, 11, 17, 32], ["+", 64, 9, 0, 57, 15, 339, 51, 16, 17, 60], ["+", 31, 16, 12, 69, 341, 342, 0, 16, 17, 33], ["+", 31, 16, 12, 69, 341, 342, 0, 16, 12, 13], ["-", 31, 16, 12, 69, 341, 342, 0, 16, 17, 72], ["-", 31, 16, 12, 69, 341, 342, 0, 16, 12, 13]]
1
228
8
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; const int SIZE = 50100; bool isP[SIZE]; int main() { fill(isP, isP + SIZE + 1, true); for (int i = 2; i <= SIZE; i++) { if (isP[i]) { for (int j = i * 2; j <= SIZE; j += i) { isP[j] = false; } } } int n; while (cin >> n) { for (int i = n - 1; i >= 2; i--) { if (isP[i]) { cout << i << endl; break; } } for (int i = n + 1; i <= SIZE; i++) { if (isP[i]) { cout << i << endl; break; } } } return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; const int SIZE = 50100; bool isP[SIZE]; int main() { fill(isP, isP + SIZE + 1, true); for (int i = 2; i <= SIZE; i++) { if (isP[i]) { for (int j = i * 2; j <= SIZE; j += i) { isP[j] = false; } } } int n; while (cin >> n) { for (int i = n - 1; i >= 2; i--) { if (isP[i]) { cout << i << " "; break; } } for (int i = n + 1; i <= SIZE; i++) { if (isP[i]) { cout << i << endl; break; } } } return 0; }
[["-", 0, 57, 64, 9, 0, 1, 0, 16, 12, 22], ["+", 64, 9, 0, 1, 0, 16, 12, 5, 0, 62], ["+", 64, 9, 0, 1, 0, 16, 12, 5, 0, 6]]
1
184
4
#include <iostream> using namespace std; int main() { bool num[10001] = {false}; for (int i = 2; i < 100; i++) if (!num[i]) for (int j = i * i; j < 10001; j += i) num[j] = true; for (int n; cin >> n;) { for (int i = n - 1;; i--) if (!num[i]) { cout << i << " "; break; } for (int i = n + 1;; i++) if (!num[i]) { cout << i << endl; break; } } return 0; }
#include <iostream> using namespace std; int main() { bool num[1000001] = {false}; for (int i = 2; i < 1000; i++) if (!num[i]) for (int j = i * i; j < 1000001; j += i) num[j] = true; for (int n; cin >> n;) { for (int i = n - 1;; i--) if (!num[i]) { cout << i << " "; break; } for (int i = n + 1;; i++) if (!num[i]) { cout << i << endl; break; } } return 0; }
[["-", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 7, 8, 57, 64, 7, 15, 16, 12, 13], ["+", 0, 7, 8, 57, 64, 7, 15, 16, 12, 13]]
1
147
19
#include <iostream> using namespace std; bool prime(int x) { if (x == 0 || x == 1) return false; for (int i = 2; i * i < x; i++) { if (x % i == 0) return false; } return true; } int main() { int n, nl, nh; while (cin >> n) { nl = n; nh = n; while (1) { nl--; if (prime(nl)) break; } while (1) { nh++; if (prime(nh)) break; } cout << nl << " " << nh << endl; } return 0; }
#include <iostream> using namespace std; bool prime(int x) { if (x == 0 || x == 1) return false; for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true; } int main() { int n, nl, nh; while (cin >> n) { nl = n; nh = n; while (1) { nl--; if (prime(nl)) break; } while (1) { nh++; if (prime(nh)) break; } cout << nl << " " << nh << endl; } return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19]]
1
139
2
#include <iostream> using namespace std; int main() { int n; cin >> n; int math[100000] = {0}; math[1] = 1; for (int i = 2; i < 100000; i++) { int bell = i; while (bell + i <= 100000) { bell += i; if (math[bell] == 0) { math[bell] = 1; } } } int hess = n; while (1) { hess--; if (math[hess] == 0) { cout << hess << " "; break; } } hess = n; while (1) { hess++; if (math[hess] == 0) { cout << hess << endl; break; } } return (0); }
#include <iostream> using namespace std; int main() { int n; while (cin >> n) { int math[100000] = {0}; math[1] = 1; for (int i = 2; i < 100000; i++) { int bell = i; while (bell + i <= 100000) { bell += i; if (math[bell] == 0) { math[bell] = 1; } } } int hess = n; while (1) { hess--; if (math[hess] == 0) { cout << hess << " "; break; } } hess = n; while (1) { hess++; if (math[hess] == 0) { cout << hess << endl; break; } } } return (0); }
[["+", 0, 30, 0, 14, 8, 9, 0, 52, 0, 89], ["+", 0, 14, 8, 9, 0, 52, 15, 339, 0, 24], ["-", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35], ["+", 0, 14, 8, 9, 0, 52, 15, 339, 0, 25], ["+", 0, 14, 8, 9, 0, 52, 8, 9, 0, 45], ["+", 0, 52, 8, 9, 0, 57, 64, 9, 0, 46]]
1
161
6
n = 50021 a = Array.new(n){|i| i+1} a[0] = 0 (2..n**(0.50)).each do |i| (2..(n/i)).each{|j| a[i*j-1] = 0} if a[i-1]>0 end a.delete(0) while gets q = $_.to_i i = 1 while a[i] < q do i+=1 end puts "#{a[i-1]}, #{a[i]==q ? a[i+1]:a[i]}" end
n = 50021 a = Array.new(n){|i| i+1} a[0] = 0 (2..n**(0.50)).each do |i| (2..(n/i)).each{|j| a[i*j-1] = 0} if a[i-1]>0 end a.delete(0) while gets q = $_.to_i i = 1 while a[i] < q do i+=1 end puts "#{a[i-1]} #{a[i]==q ? a[i+1]:a[i]}" end
[["-", 8, 170, 0, 652, 3, 4, 0, 557, 0, 6], ["+", 8, 170, 0, 652, 3, 4, 0, 557, 0, 6]]
4
137
2
var input = require('fs').readFileSync('/dev/stdin', 'utf8'); var Arr = (input.trim()).split("\n"); var cnt = 1; while (true) { var W = Arr.shift() - 0; if (W == 0) break; var N = Arr.shift() - 0; var dp = []; for (var y = 0; y < N + 1; y++) { dp[y] = []; for (var x = 0; x <= W; x++) { dp[y][x] = 0; } } var v = []; var w = []; for (var i = 0; i < N; i++) { var arr = (Arr.shift()).split(",").map(Number); v.push(arr[0]); w.push(arr[1]); } for (var y = 0; y < N; y++) { for (var x = 0; x <= W; x++) { if (x - w[y] >= 0) dp[y + 1][x] = Math.max(dp[y][x], dp[y][x - w[y]] + v[y]); else dp[y + 1][x] = dp[y][x]; } } console.log("Case " + cnt + ":"); cnt++; console.log(dp[n][W]); var maxValue = dp[n][W]; while (maxValue == dp[n][W - 1]) W--; console.log(W); }
var input = require('fs').readFileSync('/dev/stdin', 'utf8'); var Arr = (input.trim()).split("\n"); var cnt = 1; while (true) { var W = Arr.shift() - 0; if (W == 0) break; var N = Arr.shift() - 0; var dp = []; for (var y = 0; y < N + 1; y++) { dp[y] = []; for (var x = 0; x <= W; x++) { dp[y][x] = 0; } } var v = []; var w = []; for (var i = 0; i < N; i++) { var arr = (Arr.shift()).split(",").map(Number); v.push(arr[0]); w.push(arr[1]); } for (var y = 0; y < N; y++) { for (var x = 0; x <= W; x++) { if (x - w[y] >= 0) dp[y + 1][x] = Math.max(dp[y][x], dp[y][x - w[y]] + v[y]); else dp[y + 1][x] = dp[y][x]; } } console.log("Case " + cnt + ":"); cnt++; console.log(dp[N][W]); var maxValue = dp[N][W]; while (maxValue == dp[N][W - 1]) W--; console.log(W); }
[["-", 0, 2, 3, 3, 0, 69, 500, 69, 71, 22], ["+", 0, 2, 3, 3, 0, 69, 500, 69, 71, 22], ["-", 0, 198, 0, 200, 51, 69, 500, 69, 71, 22], ["+", 0, 198, 0, 200, 51, 69, 500, 69, 71, 22], ["-", 15, 23, 0, 16, 12, 69, 500, 69, 71, 22], ["+", 15, 23, 0, 16, 12, 69, 500, 69, 71, 22]]
2
378
6
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int cnt = 1; while (sc.hasNext()) { int W = sc.nextInt(); if (W == 0) break; int N = sc.nextInt(); int[] dp = new int[W + 1]; int[] v = new int[N]; int[] w = new int[N]; Arrays.fill(dp, -1); dp[0] = 0; for (int i = 0; i < N; i++) { String[] s = sc.next().split(","); v[i] = Integer.parseInt(s[0]); w[i] = Integer.parseInt(s[1]); } for (int i = 0; i < N; i++) { for (int j = W; j >= 0; j--) { if (dp[j] != -1 && j + w[i] <= W) { int t = w[i] + j; dp[t] = Math.max(dp[t], dp[j] + v[i]); } } } int wei = 0; int val = 0; for (int i = 0; i <= W; i++) { if (val < dp[i]) { wei = i; val = dp[i]; } } System.out.printf("Case %d:\n", cnt); System.out.println(wei); System.out.println(val); cnt++; } sc.close(); } }
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int cnt = 1; while (sc.hasNext()) { int W = sc.nextInt(); if (W == 0) break; int N = sc.nextInt(); int[] dp = new int[W + 1]; int[] v = new int[N]; int[] w = new int[N]; Arrays.fill(dp, -1); dp[0] = 0; for (int i = 0; i < N; i++) { String[] s = sc.next().split(","); v[i] = Integer.parseInt(s[0]); w[i] = Integer.parseInt(s[1]); } for (int i = 0; i < N; i++) { for (int j = W; j >= 0; j--) { if (dp[j] != -1 && j + w[i] <= W) { int t = w[i] + j; dp[t] = Math.max(dp[t], dp[j] + v[i]); } } } int wei = 0; int val = 0; for (int i = 0; i <= W; i++) { if (val < dp[i]) { wei = i; val = dp[i]; } } System.out.printf("Case %d:\n", cnt); System.out.println(val); System.out.println(wei); cnt++; } sc.close(); } }
[["-", 8, 196, 0, 1, 0, 492, 3, 4, 0, 22], ["+", 8, 196, 0, 1, 0, 492, 3, 4, 0, 22]]
3
376
4
import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { try (Scanner sc = new Scanner(System.in)) { int count = 0; while (true) { int W = sc.nextInt(); if (W == 0) break; count++; int N = sc.nextInt(); // dp[重さ] = max 価値 long[] dp = new long[W + 1]; int[] v = new int[N]; int[] w = new int[N]; for (int i = 0; i < N; i++) { String str = sc.next(); String[] strs = str.split(","); v[i] = Integer.parseInt(strs[0]); w[i] = Integer.parseInt(strs[1]); } for (int i = 0; i < W + 1; i++) { dp[i] = 0; } for (int i = 0; i < N; i++) { for (int j = W; j > 0; j--) { if (j - w[i] >= 0) dp[j] = Math.max(dp[j], dp[j - w[i]] + v[i]); } } for (int i = 0; i < W + 1; i++) { if (dp[i] == dp[W]) { System.out.println("Case " + count); System.out.println(dp[i]); System.out.println(i); break; } } } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { try (Scanner sc = new Scanner(System.in)) { int count = 0; while (true) { int W = sc.nextInt(); if (W == 0) break; count++; int N = sc.nextInt(); // dp[重さ] = max 価値 long[] dp = new long[W + 1]; int[] v = new int[N]; int[] w = new int[N]; for (int i = 0; i < N; i++) { String str = sc.next(); String[] strs = str.split(","); v[i] = Integer.parseInt(strs[0]); w[i] = Integer.parseInt(strs[1]); } for (int i = 0; i < W + 1; i++) { dp[i] = 0; } for (int i = 0; i < N; i++) { for (int j = W; j > 0; j--) { if (j - w[i] >= 0) dp[j] = Math.max(dp[j], dp[j - w[i]] + v[i]); } } for (int i = 0; i < W + 1; i++) { if (dp[i] == dp[W]) { System.out.println("Case " + count + ":"); System.out.println(dp[i]); System.out.println(i); break; } } } } } }
[["+", 0, 1, 0, 492, 3, 4, 0, 16, 17, 72], ["+", 0, 492, 3, 4, 0, 16, 12, 5, 0, 62], ["+", 0, 492, 3, 4, 0, 16, 12, 5, 0, 491]]
3
352
4
import java.util.*; public class Main { Scanner sc = new Scanner(System.in); public static void main(String[] args) { new Main(); } public Main() { new aoj0042().doIt(); } class aoj0042 { void DP(int W, int TE[], int TW[], int n, int sum[]) { for (int i = 0; i < n; i++) { for (int j = W; j > 0; j--) { if (sum[j] > 0) sum[j + TW[i]] = Math.max(sum[j + TW[i]], sum[j] + TE[i]); } sum[TW[i]] = Math.max(sum[TW[i]], TE[i]); } } void doIt() { int cnt = 1; while (true) { int W = sc.nextInt(); if (W == 0) break; int n = sc.nextInt(); int sum[] = new int[2 * W]; int TE[] = new int[n]; int TW[] = new int[n]; for (int i = 0; i < n; i++) { String[] node = sc.next().split(","); TE[i] = Integer.valueOf(node[0]); TW[i] = Integer.valueOf(node[1]); } System.out.println("Case " + cnt + ":"); DP(W, TE, TW, n, sum); int Eans = 0; int Wans = 0; for (int i = 0; i <= W; i++) { if (Eans < sum[i]) { Eans = sum[i]; Wans = i; } } System.out.println(Eans); System.out.println(Wans); cnt++; } } } }
import java.util.*; public class Main { Scanner sc = new Scanner(System.in); public static void main(String[] args) { new Main(); } public Main() { new aoj0042().doIt(); } class aoj0042 { void DP(int W, int TE[], int TW[], int n, int sum[]) { for (int i = 0; i < n; i++) { for (int j = W; j > 0; j--) { if (sum[j] > 0) sum[j + TW[i]] = Math.max(sum[j + TW[i]], sum[j] + TE[i]); } sum[TW[i]] = Math.max(sum[TW[i]], TE[i]); } } void doIt() { int cnt = 1; while (true) { int W = sc.nextInt(); if (W == 0) break; int n = sc.nextInt(); int sum[] = new int[3 * W]; int TE[] = new int[n]; int TW[] = new int[n]; for (int i = 0; i < n; i++) { String[] node = sc.next().split(","); TE[i] = Integer.valueOf(node[0]); TW[i] = Integer.valueOf(node[1]); } System.out.println("Case " + cnt + ":"); DP(W, TE, TW, n, sum); int Eans = 0; int Wans = 0; for (int i = 0; i <= W; i++) { if (Eans < sum[i]) { Eans = sum[i]; Wans = i; } } System.out.println(Eans); System.out.println(Wans); cnt++; } } } }
[["-", 49, 200, 51, 227, 497, 505, 0, 16, 31, 499], ["+", 49, 200, 51, 227, 497, 505, 0, 16, 31, 499]]
3
428
2
import java.io.*; class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = ""; int count = 1; while (!(line = br.readLine()).equals("0")) { int weight = 0; int W = Integer.parseInt(line); int n = Integer.parseInt(br.readLine()); int[] v = new int[n]; int[] w = new int[n]; for (int i = 0; i < n; i++) { String[] str = br.readLine().split(","); v[i] = Integer.parseInt(str[0]); w[i] = Integer.parseInt(str[1]); } solve(W, n, v, w, count); count++; } } private static void solve(final int W, final int n, final int[] v, final int[] w, int count) { int[][] dp = new int[n + 1][W + 1]; int minWeight = 0; for (int i = 0; i <= W; i++) { dp[n][i] = 0; } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j <= W; j++) { if (j < w[i]) { dp[i][j] = dp[i + 1][j]; } else { dp[i][j] = Math.max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]); } } } for (int i = W - 1; i > 0; i--) { if (dp[0][W] == dp[0][i]) { minWeight = i; } } System.out.println("Case " + count + ":"); System.out.println(dp[0][W]); System.out.println(minWeight); } }
import java.io.*; class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = ""; int count = 1; while (!(line = br.readLine()).equals("0")) { int weight = 0; int W = Integer.parseInt(line); int n = Integer.parseInt(br.readLine()); int[] v = new int[n]; int[] w = new int[n]; for (int i = 0; i < n; i++) { String[] str = br.readLine().split(","); v[i] = Integer.parseInt(str[0]); w[i] = Integer.parseInt(str[1]); } solve(W, n, v, w, count); count++; } } private static void solve(final int W, final int n, final int[] v, final int[] w, int count) { int[][] dp = new int[n + 1][W + 1]; int minWeight = 0; for (int i = 0; i <= W; i++) { dp[n][i] = 0; } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j <= W; j++) { if (j < w[i]) { dp[i][j] = dp[i + 1][j]; } else { dp[i][j] = Math.max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]); } } } for (int i = W; i > 0; i--) { if (dp[0][W] == dp[0][i]) { minWeight = i; } } System.out.println("Case " + count + ":"); System.out.println(dp[0][W]); System.out.println(minWeight); } }
[["-", 0, 7, 502, 503, 49, 200, 51, 16, 17, 33], ["-", 0, 7, 502, 503, 49, 200, 51, 16, 12, 499]]
3
477
2
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int No = 1; while (true) { int W = in.nextInt(); if (W == 0) break; int N = in.nextInt(); List<Integer> va = new ArrayList<>(), wa = new ArrayList<>(); for (int i = 0; i < N; i++) { String str = in.next(); String[] datas = str.split(","); va.add(Integer.parseInt(datas[0])); wa.add(Integer.parseInt(datas[1])); } int dp[][] = new int[N + 1][W + 1]; for (int i = 1; i <= N; i++) { int v = va.get(i - 1), w = wa.get(i - 1); for (int j = 0; j <= W; j++) { dp[i][j] = dp[i - 1][j]; if (j - w >= 0) { dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - w] + v); } } } int max = 0, p = W + 1; for (int i = W; i >= 0; i--) { if (dp[N][i] >= max) { p = i; if (dp[N][i] > max) max = dp[N][i]; } } System.out.printf("Case %d\n%d\n%d\n", No, max, p); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int No = 1; while (true) { int W = in.nextInt(); if (W == 0) break; int N = in.nextInt(); List<Integer> va = new ArrayList<>(), wa = new ArrayList<>(); for (int i = 0; i < N; i++) { String str = in.next(); String[] datas = str.split(","); va.add(Integer.parseInt(datas[0])); wa.add(Integer.parseInt(datas[1])); } int dp[][] = new int[N + 1][W + 1]; for (int i = 1; i <= N; i++) { int v = va.get(i - 1), w = wa.get(i - 1); for (int j = 0; j <= W; j++) { dp[i][j] = dp[i - 1][j]; if (j - w >= 0) { dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - w] + v); } } } int max = 0, p = W + 1; for (int i = W; i >= 0; i--) { if (dp[N][i] >= max) { p = i; if (dp[N][i] > max) max = dp[N][i]; } } System.out.printf("Case %d:\n%d\n%d\n", No, max, p); No++; } } }
[["-", 0, 1, 0, 492, 3, 4, 0, 5, 0, 491], ["+", 0, 1, 0, 492, 3, 4, 0, 5, 0, 491], ["+", 8, 196, 0, 52, 8, 196, 0, 1, 0, 35], ["+", 0, 52, 8, 196, 0, 1, 0, 27, 0, 22], ["+", 0, 52, 8, 196, 0, 1, 0, 27, 0, 29]]
3
392
5
import java.util.*; class Main { public static void main(String[] z) { Scanner s = new Scanner(System.in); for (int t = 1, W, v, w, i, n; (W = s.nextInt()) > 0; System.out.printf("Case %d:\n%d\n%d\n", t++, v, w)) { int[] d = new int[W + 1]; for (n = s.nextInt(); n-- > 0; d[w] = d[w] > v ? d[w] : v) for (z = s.next().split(","), v = new Integer(z[0]), w = new Integer(z[1]), i = W - w; i-- > 0;) if (d[i] > 0) d[i + w] = Math.max(d[i + w], d[i] + v); for (v = w = 0, W++; W-- > 0;) if (v <= d[W]) v = d[w = W]; } } }
import java.util.*; class Main { public static void main(String[] z) { Scanner s = new Scanner(System.in); for (int t = 1, W, v, w, i, n; (W = s.nextInt()) > 0; System.out.printf("Case %d:\n%d\n%d\n", t++, v, w)) { int[] d = new int[W + 1]; for (n = s.nextInt(); n-- > 0; d[w] = d[w] > v ? d[w] : v) for (z = s.next().split(","), v = new Integer(z[0]), w = new Integer(z[1]), i = W - w; i > 0; --i) if (d[i] > 0) d[i + w] = Math.max(d[i + w], d[i] + v); for (v = w = 0, W++; W-- > 0;) if (v <= d[W]) v = d[w = W]; } } }
[["-", 0, 7, 8, 7, 15, 16, 31, 27, 0, 68], ["+", 8, 196, 0, 7, 8, 7, 26, 27, 0, 68], ["+", 8, 196, 0, 7, 8, 7, 26, 27, 0, 22]]
3
256
3
import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; import java.io.*; import java.util.*; public class Main { int INF = 1 << 28; // long INF = 1L << 62; double EPS = 1e-10; void run() { Scanner sc = new Scanner(System.in); for (int c = 1;; c++) { int W = sc.nextInt(); if (W == 0) break; int n = sc.nextInt(); int[] v = new int[n], w = new int[n]; for (int i = 0; i < n; i++) { String[] ws = sc.next().split(","); v[i] = Integer.valueOf(ws[0]); w[i] = Integer.valueOf(ws[1]); } int[] dp = new int[W + 1]; fill(dp, -1); dp[0] = 0; for (int i = 0; i < n; i++) for (int j = W; j >= w[i]; j--) if (dp[j - w[i]] >= 0) { dp[j] = max(dp[j], dp[j - w[i]] + v[i]); } int aw = 0, av = 0; for (int i = 0; i <= W; i++) if (av < dp[i]) { av = dp[i]; aw = i; } System.out.println("case " + c + ":"); System.out.println(av); System.out.println(aw); } } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } public static void main(String[] args) { new Main().run(); } }
import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; import java.io.*; import java.util.*; public class Main { int INF = 1 << 28; // long INF = 1L << 62; double EPS = 1e-10; void run() { Scanner sc = new Scanner(System.in); for (int c = 1;; c++) { int W = sc.nextInt(); if (W == 0) break; int n = sc.nextInt(); int[] v = new int[n], w = new int[n]; for (int i = 0; i < n; i++) { String[] ws = sc.next().split(","); v[i] = Integer.valueOf(ws[0]); w[i] = Integer.valueOf(ws[1]); } int[] dp = new int[W + 1]; fill(dp, -1); dp[0] = 0; for (int i = 0; i < n; i++) for (int j = W; j >= w[i]; j--) if (dp[j - w[i]] >= 0) { dp[j] = max(dp[j], dp[j - w[i]] + v[i]); } int aw = 0, av = 0; for (int i = 0; i <= W; i++) if (av < dp[i]) { av = dp[i]; aw = i; } System.out.println("Case " + c + ":"); System.out.println(av); System.out.println(aw); } } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } public static void main(String[] args) { new Main().run(); } }
[["-", 3, 4, 0, 16, 31, 16, 31, 5, 0, 491], ["+", 3, 4, 0, 16, 31, 16, 31, 5, 0, 491]]
3
428
2
import java.util.*; public class Main { public void doIt() { Scanner sc = new Scanner(System.in); int count = 1; while (true) { int w = sc.nextInt(); if (w == 0) break; int n = sc.nextInt(); int[][] dp = new int[n + 1][w + 1]; int[][] data = new int[n + 1][2]; for (int i = 1; i < n + 1; i++) { String[] input = sc.next().split(","); data[i][0] = Integer.parseInt(input[0]); data[i][1] = Integer.parseInt(input[1]); } for (int i = 1; i < n + 1; i++) { for (int j = 1; j <= w; j++) { int index = j - data[i][1]; if (index < 0) { dp[i][j] = dp[i - 1][j]; } else { dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][index] + data[i][0]); } } } // debug // for(int i=0; i < w; i++){ // System.out.printf("%3d ",i); // } // System.out.println(""); // for(int j=0; j < n+1; j++){ // for(int i=0; i < w; i++){ // System.out.printf("%3d ", //dp[j][i]); // } // System.out.println(""); // // } int max = 0; int maxIdx = 0; for (int i = 0; i < w; i++) { if (dp[n][i] > max) { max = dp[n][i]; maxIdx = i; } } System.out.println("Case " + count++ + ":"); System.out.println(max); System.out.println(maxIdx); } } public static void main(String[] args) { Main obj = new Main(); obj.doIt(); } }
import java.util.*; public class Main { public void doIt() { Scanner sc = new Scanner(System.in); int count = 1; while (true) { int w = sc.nextInt(); if (w == 0) break; int n = sc.nextInt(); int[][] dp = new int[n + 1][w + 1]; int[][] data = new int[n + 1][2]; for (int i = 1; i < n + 1; i++) { String[] input = sc.next().split(","); data[i][0] = Integer.parseInt(input[0]); data[i][1] = Integer.parseInt(input[1]); } for (int i = 1; i < n + 1; i++) { for (int j = 1; j <= w; j++) { int index = j - data[i][1]; if (index < 0) { dp[i][j] = dp[i - 1][j]; } else { dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][index] + data[i][0]); } } } // debug // for(int i=0; i < w; i++){ // System.out.printf("%3d ",i); // } // System.out.println(""); // for(int j=0; j < n+1; j++){ // for(int i=0; i < w; i++){ // System.out.printf("%3d ", //dp[j][i]); // } // System.out.println(""); // // } int max = 0; int maxIdx = 0; for (int i = 0; i <= w; i++) { if (dp[n][i] > max) { max = dp[n][i]; maxIdx = i; } } System.out.println("Case " + count++ + ":"); System.out.println(max); System.out.println(maxIdx); } } public static void main(String[] args) { Main obj = new Main(); obj.doIt(); } }
[["-", 0, 52, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 196, 0, 7, 15, 16, 17, 19]]
3
423
2
import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.util.regex.*; // AOJ public class Main { Scanner sc = new Scanner(System.in); public static void main(String[] args) { new Main().AOJ0042(); } void AOJ0004() { while (sc.hasNext()) { int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(), d = sc.nextInt(), e = sc.nextInt(), f = sc.nextInt(); // ax + by = c // dx + ey = f } } void AOJ0042() { int num = 1; while (sc.hasNext()) { int w = sc.nextInt(); if (w == 0) break; int n = sc.nextInt(); int[][] dp = new int[n + 1][w + 1]; int[] ww = new int[n + 1], pp = new int[n + 1]; for (int i = 1; i <= n; i++) { Scanner sc2 = new Scanner(sc.next()).useDelimiter(","); pp[i] = sc2.nextInt(); ww[i] = sc2.nextInt(); } int ans = 0, wei = Integer.MAX_VALUE; for (int i = 1; i <= n; i++) { for (int j = 1; j < w; j++) { dp[i][j] = max(dp[i - 1][j], j >= ww[i] ? dp[i - 1][j - ww[i]] + pp[i] : dp[i - 1][j]); if (dp[i][j] > ans) { wei = j; ans = dp[i][j]; } else if (dp[i][j] == ans) { wei = min(wei, j); } } } // debug /* for(int i=1; i<=w; i++){ for(int j=1; j<=n; j++) out.printf("%4d",dp[j][i]); out.println(); } */ out.println("Case " + (num++) + ":"); out.println(ans); out.println(wei); } } void AOJ0003() { int n = sc.nextInt(); while (n-- > 0) { int[] a = new int[3]; for (int i = 0; i < 3; i++) a[i] = sc.nextInt(); Arrays.sort(a); if (a[2] * a[2] == (a[1] * a[1]) + (a[0] * a[0])) out.println("YES"); else out.println("NO"); } } void AOJ0002() { while (sc.hasNext()) out.println(new String("" + (sc.nextInt() + sc.nextInt())).length()); } void AOJ0516() { while (sc.hasNext()) { int n = sc.nextInt(), k = sc.nextInt(), ans = 0; if (n == 0) break; int[] sum = new int[n + 1]; for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + sc.nextInt(); for (int i = k; i <= n; i++) ans = max(ans, sum[i] - sum[i - k]); out.println(ans); } } c547[][] r; final int MOD547 = 100000; void AOJ0547() { // Commute routes while (sc.hasNext()) { int w = sc.nextInt(), h = sc.nextInt(); if (w < 2) break; r = new c547[w + 1][h + 1]; r[2][1] = new c547(0, 0, 0, 1); r[1][2] = new c547(0, 1, 0, 0); for (int x = 1; x <= w; x++) { for (int y = 1; y <= h; y++) r[x][y] = r[x][y] == null ? solve547(x, y) : r[x][y]; } // r[w][h].list(); out.println(r[w][h].sum()); } } c547 solve547(int x, int y) { int s1 = y - 1 > 0 ? r[x][y - 1].w2 : 0; int s2 = y - 1 > 0 ? (r[x][y - 1].s1 + r[x][y - 1].s2) % MOD547 : 0; int w1 = x - 1 > 0 ? r[x - 1][y].s2 : 0; int w2 = x - 1 > 0 ? (r[x - 1][y].w1 + r[x - 1][y].w2) % MOD547 : 0; return new c547(s1, s2, w1, w2); } class c547 { int s1, s2, w1, w2; c547(int s1, int s2, int w1, int w2) { this.s1 = s1; this.s2 = s2; this.w1 = w1; this.w2 = w2; } int sum() { return (this.s1 + this.s2 + this.w1 + this.w2) % MOD547; } void list() { out.println("(" + this.s1 + " " + this.s2 + " " + this.w1 + " " + this.w2 + ")"); } } int[][] a1209; void AOJ1209() { while (sc.hasNext()) { int n = sc.nextInt(); if (n <= 0) break; a1209 = new int[18][n + 1]; for (int i = 0; i <= n; i++) a1209[1][i] = 1; for (int x = 2; x <= 17; x++) { for (int y = 1; y <= n; y++) a1209[x][y] = solve1209(x, y); } int ans = 0; for (int i = 1; i <= 17; i++) ans += a1209[i][n]; out.println(ans); // debug /* for(int i=0; i<=n; i++){ for(int j=0; j<=17; j++) out.print(a1209[j][i]+" "); out.println(); } */ } } int solve1209(int x, int y) { int ans = 0; for (int i = x; i > 0; i--) ans += y - (x * x) >= 0 ? a1209[i][y - (x * x)] : 0; return ans; } int[][] r515; boolean[][] b515; void AOJ0515() { // School Road while (sc.hasNext()) { int xn = sc.nextInt(), yn = sc.nextInt(); if (xn <= 0) break; int n = sc.nextInt(); b515 = new boolean[xn + 1][yn + 1]; for (int i = 0; i < n; i++) b515[sc.nextInt()][sc.nextInt()] = true; r515 = new int[xn + 1][yn + 1]; r515[1][1] = 1; for (int x = 1; x <= xn; x++) { for (int y = 1; y <= yn; y++) { if (x == 1 && y == 1) continue; r515[x][y] = solve515(x, y); } } out.println(r515[xn][yn]); } } int solve515(int x, int y) { int ans = 0; if (!b515[x][y]) { ans += y - 1 > 0 ? r515[x][y - 1] : 0; ans += x - 1 > 0 ? r515[x - 1][y] : 0; } return ans; } int[] a168; void AOJ0168() { // Kannondou while (sc.hasNext()) { int n = sc.nextInt(); if (n <= 0) break; a168 = new int[n + 1]; a168[0] = 1; for (int i = 1; i <= n; i++) a168[i] = solve168(i); int day = (a168[n] / 10) + (a168[n] % 10 == 0 ? 0 : 1); // out.println("n"+a168[n]+" day"+day); out.println(((day / 365) + (day % 365 == 0 ? 0 : 1))); } } int solve168(int a) { int ans = 0; for (int i = a - 1; i >= a - 3; i--) ans += i >= 0 ? a168[i] : 0; return ans; } void AOJ0176() { // What Color? int[] R = {0, 0, 0, 0, 255, 255, 255, 255}; int[] G = {0, 0, 255, 255, 0, 0, 255, 255}; int[] B = {0, 255, 0, 255, 0, 255, 0, 255}; HashMap<Integer, String> dic = new HashMap<Integer, String>(); dic.put(0, "black"); dic.put(1, "blue"); dic.put(2, "lime"); dic.put(3, "aqua"); dic.put(4, "red"); dic.put(5, "fuchsia"); dic.put(6, "yellow"); dic.put(7, "white"); while (sc.hasNext()) { String s = sc.next(); if (s.equals("0")) break; int r = Integer.parseInt("" + s.charAt(1) + s.charAt(2), 16), g = Integer.parseInt("" + s.charAt(3) + s.charAt(4), 16), b = Integer.parseInt("" + s.charAt(5) + s.charAt(6), 16); int index = 0; double last = Double.MAX_VALUE; for (int i = 0; i < 8; i++) { double d = sqrt((abs(r - R[i]) << 1) + (abs(g - G[i]) << 1) + (abs(b - B[i]) << 1)); index = last > d ? i : index; last = last > d ? d : last; } out.println(dic.get(index)); } } void AOJ0175() { // A King in Hawaii while (sc.hasNext()) { int n = sc.nextInt(); if (n < 0) break; StringBuilder sb = new StringBuilder(); while (n != 0) { // out.println(n&3); sb.append(n & 3); n = n >> 2; } out.println(sb.length() > 0 ? sb.reverse() : 0); } } void AOJ0031() { // Weight while (sc.hasNext()) { int n = sc.nextInt(); int temp = Integer.lowestOneBit(n); out.print(temp); n = (n ^ temp); while (n != 0) { temp = Integer.lowestOneBit(n); out.print(" " + temp); n = (n ^ temp); } out.println(); } } void AOJ0051() { // Differential II int n = sc.nextInt(); for (int i = 0; i < n; i++) { char[] c = sc.next().toCharArray(); int[] a = new int[8]; for (int j = 0; j < 8; j++) a[j] = Character.digit(c[j], 10); Arrays.sort(a); StringBuilder sb = new StringBuilder(); for (int j = 0; j < 8; j++) sb.append(a[j]); int min = Integer.parseInt(sb.toString()); sb = new StringBuilder(); for (int j = 7; j >= 0; j--) sb.append(a[j]); int max = Integer.parseInt(sb.toString()); out.println(max - min); } } void AOJ0196() { // Baseball Championship while (sc.hasNext()) { int n = sc.nextInt(); if (n == 0) break; Team196[] teams = new Team196[n]; for (int i = 0; i < n; i++) { String s = sc.next(); int win = 0, lose = 0; for (int j = 0; j < n - 1; j++) { int temp = sc.nextInt(); if (temp == 0) win++; if (temp == 1) lose++; } teams[i] = new Team196(s, win, lose, i); } Arrays.sort(teams); for (int i = 0; i < n; i++) out.println(teams[i].name); } } class Team196 implements Comparable<Team196> { String name; int win, lose, index; Team196(String name, int win, int lose, int index) { this.name = name; this.win = win; this.lose = lose; this.index = index; } @Override public int compareTo(Team196 o) { if (this.win < o.win) return 1; if (this.win > o.win) return -1; if (this.lose < o.lose) return -1; if (this.lose > o.lose) return 1; if (this.index < o.index) return -1; if (this.index > o.index) return 1; return 0; } } void AOJ0065() { // Trading final int MAX = 1000000; int[] current = new int[MAX]; int[] last = new int[MAX]; boolean flag = false; while (sc.hasNext()) { String s = sc.nextLine(); if (s.equals("")) { flag = true; continue; } Scanner sc2 = new Scanner(s).useDelimiter(","); if (flag) last[sc2.nextInt()]++; else current[sc2.nextInt()]++; } for (int i = 0; i < MAX; i++) if (current[i] > 0 && last[i] > 0) out.println(i + " " + (current[i] + last[i])); } void AOJ0161() { // Sport Meet while (sc.hasNext()) { int n = sc.nextInt(); if (n == 0) break; Time161[] times = new Time161[n]; for (int i = 0; i < n; i++) times[i] = new Time161(sc.nextInt(), (sc.nextInt() * 60 + sc.nextInt() + sc.nextInt() * 60 + sc.nextInt() + sc.nextInt() * 60 + sc.nextInt() + sc.nextInt() * 60 + sc.nextInt())); Arrays.sort(times); out.println(times[0].num); out.println(times[1].num); out.println(times[times.length - 2].num); } } class Time161 implements Comparable<Time161> { int num, time; Time161(int num, int time) { this.num = num; this.time = time; } @Override public int compareTo(Time161 o) { if (this.time < o.time) return -1; if (this.time > o.time) return 1; return 0; } } void AOJ0138() { // Track and Field Competition Time138[] times1st = new Time138[8]; Time138[] times2nd = new Time138[8]; Time138[] times3rd = new Time138[8]; for (int i = 0; i < 8; i++) times1st[i] = new Time138(sc.nextInt(), sc.nextDouble()); for (int i = 0; i < 8; i++) times2nd[i] = new Time138(sc.nextInt(), sc.nextDouble()); for (int i = 0; i < 8; i++) times3rd[i] = new Time138(sc.nextInt(), sc.nextDouble()); Arrays.sort(times1st); Arrays.sort(times2nd); Arrays.sort(times3rd); for (int i = 0; i < 2; i++) out.println(times1st[i].num + " " + times1st[i].time); for (int i = 0; i < 2; i++) out.println(times2nd[i].num + " " + times2nd[i].time); for (int i = 0; i < 2; i++) out.println(times3rd[i].num + " " + times3rd[i].time); Time138[] times = new Time138[6]; times[0] = times1st[2]; times[1] = times1st[3]; times[2] = times2nd[2]; times[3] = times2nd[3]; times[4] = times3rd[2]; times[5] = times3rd[3]; Arrays.sort(times); for (int i = 0; i < 2; i++) out.println(times[i].num + " " + times[i].time); } class Time138 implements Comparable<Time138> { int num; double time; Time138(int num, double time) { this.num = num; this.time = time; } @Override public int compareTo(Time138 o) { if (this.time < o.time) return -1; if (this.time > o.time) return 1; return 0; } } void AOJ0005() { // GCD and LCM while (sc.hasNext()) { int a = sc.nextInt(), b = sc.nextInt(); int temp = gcd(max(a, b), min(a, b)); out.println(temp + " " + max(a, b) / temp * min(a, b)); } } int gcd(int x, int y) { if (y == 0) return x; else return gcd(y, x % y); } int c197 = 0; void AOJ0197() { // Greatest Common Divisor: Euclidean Algorithm while (sc.hasNext()) { int x = sc.nextInt(), y = sc.nextInt(); if (x < 2) break; c197 = 0; out.println(gcd197(max(x, y), min(x, y)) + " " + c197); } } int gcd197(int x, int y) { if (y == 0) return x; c197++; return gcd197(y, x % y); } void AOJ0001() { // List of Top 3 Hills int[] m = new int[10]; for (int i = 0; i < 10; i++) m[i] = sc.nextInt(); Arrays.sort(m); for (int i = 9; i >= 7; i--) out.println(m[i]); } void AOJ10029() { // Sort II int n = sc.nextInt(); int[] ans = new int[n]; for (int i = 0; i < n; i++) ans[i] = sc.nextInt(); Arrays.sort(ans); out.print(ans[0]); for (int i = 1; i < n; i++) out.print(" " + ans[i]); out.println(); } int[][] p26 = new int[10][10]; int c26 = 100; void AOJ0026() { // Dropping Ink int ans = 0; while (sc.hasNext()) { Scanner sc2 = new Scanner(sc.nextLine()).useDelimiter(","); int x = sc2.nextInt(), y = sc2.nextInt(), s = sc2.nextInt(); // out.println("x"+x+" y"+y+" s"+s); ans = max(solve26(x, y), ans); ans = max(solve26(x - 1, y), ans); ans = max(solve26(x + 1, y), ans); ans = max(solve26(x, y - 1), ans); ans = max(solve26(x, y + 1), ans); if (s >= 2) { ans = max(solve26(x - 1, y - 1), ans); ans = max(solve26(x - 1, y + 1), ans); ans = max(solve26(x + 1, y - 1), ans); ans = max(solve26(x + 1, y + 1), ans); } if (s >= 3) { ans = max(solve26(x - 2, y), ans); ans = max(solve26(x + 2, y), ans); ans = max(solve26(x, y - 2), ans); ans = max(solve26(x, y + 2), ans); } } out.println(c26); out.println(ans); } int solve26(int x, int y) { int r = -1; if (0 <= x && x <= 9 && 0 <= y && y <= 9) { c26 -= p26[x][y] == 0 ? 1 : 0; p26[x][y]++; r = p26[x][y]; } return r; // RA } void AOJ0008() { // Sum of 4 Integers while (sc.hasNext()) { int ans = 0, n = sc.nextInt(); for (int a = 0; a < 10; a++) { for (int b = 0; b < 10; b++) { for (int c = 0; c < 10; c++) { for (int d = 0; d < 10; d++) if ((a + b + c + d) == n) ans++; } } } out.println(ans); } } void AOJ0055() { // Sequence while (sc.hasNext()) { double last = sc.nextDouble(), ans = last; for (int i = 2; i <= 10; i++) { ans += i % 2 == 0 ? last * 2 : last / 3; last = i % 2 == 0 ? last * 2 : last / 3; // out.println(last); } out.println(ans); } } void AOJ0084() { // Search Engine StringBuilder sb = new StringBuilder(); while (sc.hasNext()) { String s = sc.next().replaceAll(",", "").replaceAll("\\.", "").replaceAll( " ", ""); // out.println(s); if (s.length() <= 2 || s.length() >= 7) continue; sb.append(s + " "); } out.println(sb.deleteCharAt(sb.length() - 1)); } void AOJ0077() { // Run Length while (sc.hasNext()) { char[] c = sc.nextLine().toCharArray(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < c.length; i++) { if (c[i] == '@') { for (int j = 0; j < Character.digit(c[i + 1], 10); j++) sb.append(c[i + 2]); i += 2; } else sb.append(c[i]); } out.println(sb.toString()); } } void AOJ0064() { // Secret Number int ans = 0; while (sc.hasNext()) { Matcher m = Pattern.compile("\\d+").matcher(sc.nextLine()); while (m.find()) ans += Integer.parseInt(m.group()); } out.println(ans); } void AOJ0050() { // Apple and Peach while (sc.hasNext()) out.println(sc.nextLine() .replaceAll("apple", "PEACH") .replaceAll("peach", "apple") .replaceAll("PEACH", "peach")); } void AOJ0039() { // Roman Figure HashMap<Character, Integer> dic = new HashMap<Character, Integer>(); dic.put('I', 1); dic.put('V', 5); dic.put('X', 10); dic.put('L', 50); dic.put('C', 100); dic.put('D', 500); dic.put('M', 1000); while (sc.hasNext()) { char[] c = sc.next().toCharArray(); int ans = 0, last = dic.get(c[0]); for (int i = 0; i < c.length; i++) { if (last >= dic.get(c[i])) { ans += dic.get(c[i]); last = dic.get(c[i]); } else { ans += dic.get(c[i]) - last - last; last = dic.get(c[i]); } } out.println(ans); } } void AOJ0020() { // Capitalize while (sc.hasNext()) out.println(sc.nextLine().toUpperCase()); } }
import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.util.regex.*; // AOJ public class Main { Scanner sc = new Scanner(System.in); public static void main(String[] args) { new Main().AOJ0042(); } void AOJ0004() { while (sc.hasNext()) { int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(), d = sc.nextInt(), e = sc.nextInt(), f = sc.nextInt(); // ax + by = c // dx + ey = f } } void AOJ0042() { int num = 1; while (sc.hasNext()) { int w = sc.nextInt(); if (w == 0) break; int n = sc.nextInt(); int[][] dp = new int[n + 1][w + 1]; int[] ww = new int[n + 1], pp = new int[n + 1]; for (int i = 1; i <= n; i++) { Scanner sc2 = new Scanner(sc.next()).useDelimiter(","); pp[i] = sc2.nextInt(); ww[i] = sc2.nextInt(); } int ans = 0, wei = Integer.MAX_VALUE; for (int i = 1; i <= n; i++) { for (int j = 1; j <= w; j++) { dp[i][j] = max(dp[i - 1][j], j >= ww[i] ? dp[i - 1][j - ww[i]] + pp[i] : dp[i - 1][j]); if (dp[i][j] > ans) { wei = j; ans = dp[i][j]; } else if (dp[i][j] == ans) { wei = min(wei, j); } } } // debug /* for(int i=0; i<=w; i++){ for(int j=0; j<=n; j++) out.printf("%4d",dp[j][i]); out.println(); } */ out.println("Case " + (num++) + ":"); out.println(ans); out.println(wei); } } void AOJ0003() { int n = sc.nextInt(); while (n-- > 0) { int[] a = new int[3]; for (int i = 0; i < 3; i++) a[i] = sc.nextInt(); Arrays.sort(a); if (a[2] * a[2] == (a[1] * a[1]) + (a[0] * a[0])) out.println("YES"); else out.println("NO"); } } void AOJ0002() { while (sc.hasNext()) out.println(new String("" + (sc.nextInt() + sc.nextInt())).length()); } void AOJ0516() { while (sc.hasNext()) { int n = sc.nextInt(), k = sc.nextInt(), ans = 0; if (n == 0) break; int[] sum = new int[n + 1]; for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + sc.nextInt(); for (int i = k; i <= n; i++) ans = max(ans, sum[i] - sum[i - k]); out.println(ans); } } c547[][] r; final int MOD547 = 100000; void AOJ0547() { // Commute routes while (sc.hasNext()) { int w = sc.nextInt(), h = sc.nextInt(); if (w < 2) break; r = new c547[w + 1][h + 1]; r[2][1] = new c547(0, 0, 0, 1); r[1][2] = new c547(0, 1, 0, 0); for (int x = 1; x <= w; x++) { for (int y = 1; y <= h; y++) r[x][y] = r[x][y] == null ? solve547(x, y) : r[x][y]; } // r[w][h].list(); out.println(r[w][h].sum()); } } c547 solve547(int x, int y) { int s1 = y - 1 > 0 ? r[x][y - 1].w2 : 0; int s2 = y - 1 > 0 ? (r[x][y - 1].s1 + r[x][y - 1].s2) % MOD547 : 0; int w1 = x - 1 > 0 ? r[x - 1][y].s2 : 0; int w2 = x - 1 > 0 ? (r[x - 1][y].w1 + r[x - 1][y].w2) % MOD547 : 0; return new c547(s1, s2, w1, w2); } class c547 { int s1, s2, w1, w2; c547(int s1, int s2, int w1, int w2) { this.s1 = s1; this.s2 = s2; this.w1 = w1; this.w2 = w2; } int sum() { return (this.s1 + this.s2 + this.w1 + this.w2) % MOD547; } void list() { out.println("(" + this.s1 + " " + this.s2 + " " + this.w1 + " " + this.w2 + ")"); } } int[][] a1209; void AOJ1209() { while (sc.hasNext()) { int n = sc.nextInt(); if (n <= 0) break; a1209 = new int[18][n + 1]; for (int i = 0; i <= n; i++) a1209[1][i] = 1; for (int x = 2; x <= 17; x++) { for (int y = 1; y <= n; y++) a1209[x][y] = solve1209(x, y); } int ans = 0; for (int i = 1; i <= 17; i++) ans += a1209[i][n]; out.println(ans); // debug /* for(int i=0; i<=n; i++){ for(int j=0; j<=17; j++) out.print(a1209[j][i]+" "); out.println(); } */ } } int solve1209(int x, int y) { int ans = 0; for (int i = x; i > 0; i--) ans += y - (x * x) >= 0 ? a1209[i][y - (x * x)] : 0; return ans; } int[][] r515; boolean[][] b515; void AOJ0515() { // School Road while (sc.hasNext()) { int xn = sc.nextInt(), yn = sc.nextInt(); if (xn <= 0) break; int n = sc.nextInt(); b515 = new boolean[xn + 1][yn + 1]; for (int i = 0; i < n; i++) b515[sc.nextInt()][sc.nextInt()] = true; r515 = new int[xn + 1][yn + 1]; r515[1][1] = 1; for (int x = 1; x <= xn; x++) { for (int y = 1; y <= yn; y++) { if (x == 1 && y == 1) continue; r515[x][y] = solve515(x, y); } } out.println(r515[xn][yn]); } } int solve515(int x, int y) { int ans = 0; if (!b515[x][y]) { ans += y - 1 > 0 ? r515[x][y - 1] : 0; ans += x - 1 > 0 ? r515[x - 1][y] : 0; } return ans; } int[] a168; void AOJ0168() { // Kannondou while (sc.hasNext()) { int n = sc.nextInt(); if (n <= 0) break; a168 = new int[n + 1]; a168[0] = 1; for (int i = 1; i <= n; i++) a168[i] = solve168(i); int day = (a168[n] / 10) + (a168[n] % 10 == 0 ? 0 : 1); // out.println("n"+a168[n]+" day"+day); out.println(((day / 365) + (day % 365 == 0 ? 0 : 1))); } } int solve168(int a) { int ans = 0; for (int i = a - 1; i >= a - 3; i--) ans += i >= 0 ? a168[i] : 0; return ans; } void AOJ0176() { // What Color? int[] R = {0, 0, 0, 0, 255, 255, 255, 255}; int[] G = {0, 0, 255, 255, 0, 0, 255, 255}; int[] B = {0, 255, 0, 255, 0, 255, 0, 255}; HashMap<Integer, String> dic = new HashMap<Integer, String>(); dic.put(0, "black"); dic.put(1, "blue"); dic.put(2, "lime"); dic.put(3, "aqua"); dic.put(4, "red"); dic.put(5, "fuchsia"); dic.put(6, "yellow"); dic.put(7, "white"); while (sc.hasNext()) { String s = sc.next(); if (s.equals("0")) break; int r = Integer.parseInt("" + s.charAt(1) + s.charAt(2), 16), g = Integer.parseInt("" + s.charAt(3) + s.charAt(4), 16), b = Integer.parseInt("" + s.charAt(5) + s.charAt(6), 16); int index = 0; double last = Double.MAX_VALUE; for (int i = 0; i < 8; i++) { double d = sqrt((abs(r - R[i]) << 1) + (abs(g - G[i]) << 1) + (abs(b - B[i]) << 1)); index = last > d ? i : index; last = last > d ? d : last; } out.println(dic.get(index)); } } void AOJ0175() { // A King in Hawaii while (sc.hasNext()) { int n = sc.nextInt(); if (n < 0) break; StringBuilder sb = new StringBuilder(); while (n != 0) { // out.println(n&3); sb.append(n & 3); n = n >> 2; } out.println(sb.length() > 0 ? sb.reverse() : 0); } } void AOJ0031() { // Weight while (sc.hasNext()) { int n = sc.nextInt(); int temp = Integer.lowestOneBit(n); out.print(temp); n = (n ^ temp); while (n != 0) { temp = Integer.lowestOneBit(n); out.print(" " + temp); n = (n ^ temp); } out.println(); } } void AOJ0051() { // Differential II int n = sc.nextInt(); for (int i = 0; i < n; i++) { char[] c = sc.next().toCharArray(); int[] a = new int[8]; for (int j = 0; j < 8; j++) a[j] = Character.digit(c[j], 10); Arrays.sort(a); StringBuilder sb = new StringBuilder(); for (int j = 0; j < 8; j++) sb.append(a[j]); int min = Integer.parseInt(sb.toString()); sb = new StringBuilder(); for (int j = 7; j >= 0; j--) sb.append(a[j]); int max = Integer.parseInt(sb.toString()); out.println(max - min); } } void AOJ0196() { // Baseball Championship while (sc.hasNext()) { int n = sc.nextInt(); if (n == 0) break; Team196[] teams = new Team196[n]; for (int i = 0; i < n; i++) { String s = sc.next(); int win = 0, lose = 0; for (int j = 0; j < n - 1; j++) { int temp = sc.nextInt(); if (temp == 0) win++; if (temp == 1) lose++; } teams[i] = new Team196(s, win, lose, i); } Arrays.sort(teams); for (int i = 0; i < n; i++) out.println(teams[i].name); } } class Team196 implements Comparable<Team196> { String name; int win, lose, index; Team196(String name, int win, int lose, int index) { this.name = name; this.win = win; this.lose = lose; this.index = index; } @Override public int compareTo(Team196 o) { if (this.win < o.win) return 1; if (this.win > o.win) return -1; if (this.lose < o.lose) return -1; if (this.lose > o.lose) return 1; if (this.index < o.index) return -1; if (this.index > o.index) return 1; return 0; } } void AOJ0065() { // Trading final int MAX = 1000000; int[] current = new int[MAX]; int[] last = new int[MAX]; boolean flag = false; while (sc.hasNext()) { String s = sc.nextLine(); if (s.equals("")) { flag = true; continue; } Scanner sc2 = new Scanner(s).useDelimiter(","); if (flag) last[sc2.nextInt()]++; else current[sc2.nextInt()]++; } for (int i = 0; i < MAX; i++) if (current[i] > 0 && last[i] > 0) out.println(i + " " + (current[i] + last[i])); } void AOJ0161() { // Sport Meet while (sc.hasNext()) { int n = sc.nextInt(); if (n == 0) break; Time161[] times = new Time161[n]; for (int i = 0; i < n; i++) times[i] = new Time161(sc.nextInt(), (sc.nextInt() * 60 + sc.nextInt() + sc.nextInt() * 60 + sc.nextInt() + sc.nextInt() * 60 + sc.nextInt() + sc.nextInt() * 60 + sc.nextInt())); Arrays.sort(times); out.println(times[0].num); out.println(times[1].num); out.println(times[times.length - 2].num); } } class Time161 implements Comparable<Time161> { int num, time; Time161(int num, int time) { this.num = num; this.time = time; } @Override public int compareTo(Time161 o) { if (this.time < o.time) return -1; if (this.time > o.time) return 1; return 0; } } void AOJ0138() { // Track and Field Competition Time138[] times1st = new Time138[8]; Time138[] times2nd = new Time138[8]; Time138[] times3rd = new Time138[8]; for (int i = 0; i < 8; i++) times1st[i] = new Time138(sc.nextInt(), sc.nextDouble()); for (int i = 0; i < 8; i++) times2nd[i] = new Time138(sc.nextInt(), sc.nextDouble()); for (int i = 0; i < 8; i++) times3rd[i] = new Time138(sc.nextInt(), sc.nextDouble()); Arrays.sort(times1st); Arrays.sort(times2nd); Arrays.sort(times3rd); for (int i = 0; i < 2; i++) out.println(times1st[i].num + " " + times1st[i].time); for (int i = 0; i < 2; i++) out.println(times2nd[i].num + " " + times2nd[i].time); for (int i = 0; i < 2; i++) out.println(times3rd[i].num + " " + times3rd[i].time); Time138[] times = new Time138[6]; times[0] = times1st[2]; times[1] = times1st[3]; times[2] = times2nd[2]; times[3] = times2nd[3]; times[4] = times3rd[2]; times[5] = times3rd[3]; Arrays.sort(times); for (int i = 0; i < 2; i++) out.println(times[i].num + " " + times[i].time); } class Time138 implements Comparable<Time138> { int num; double time; Time138(int num, double time) { this.num = num; this.time = time; } @Override public int compareTo(Time138 o) { if (this.time < o.time) return -1; if (this.time > o.time) return 1; return 0; } } void AOJ0005() { // GCD and LCM while (sc.hasNext()) { int a = sc.nextInt(), b = sc.nextInt(); int temp = gcd(max(a, b), min(a, b)); out.println(temp + " " + max(a, b) / temp * min(a, b)); } } int gcd(int x, int y) { if (y == 0) return x; else return gcd(y, x % y); } int c197 = 0; void AOJ0197() { // Greatest Common Divisor: Euclidean Algorithm while (sc.hasNext()) { int x = sc.nextInt(), y = sc.nextInt(); if (x < 2) break; c197 = 0; out.println(gcd197(max(x, y), min(x, y)) + " " + c197); } } int gcd197(int x, int y) { if (y == 0) return x; c197++; return gcd197(y, x % y); } void AOJ0001() { // List of Top 3 Hills int[] m = new int[10]; for (int i = 0; i < 10; i++) m[i] = sc.nextInt(); Arrays.sort(m); for (int i = 9; i >= 7; i--) out.println(m[i]); } void AOJ10029() { // Sort II int n = sc.nextInt(); int[] ans = new int[n]; for (int i = 0; i < n; i++) ans[i] = sc.nextInt(); Arrays.sort(ans); out.print(ans[0]); for (int i = 1; i < n; i++) out.print(" " + ans[i]); out.println(); } int[][] p26 = new int[10][10]; int c26 = 100; void AOJ0026() { // Dropping Ink int ans = 0; while (sc.hasNext()) { Scanner sc2 = new Scanner(sc.nextLine()).useDelimiter(","); int x = sc2.nextInt(), y = sc2.nextInt(), s = sc2.nextInt(); // out.println("x"+x+" y"+y+" s"+s); ans = max(solve26(x, y), ans); ans = max(solve26(x - 1, y), ans); ans = max(solve26(x + 1, y), ans); ans = max(solve26(x, y - 1), ans); ans = max(solve26(x, y + 1), ans); if (s >= 2) { ans = max(solve26(x - 1, y - 1), ans); ans = max(solve26(x - 1, y + 1), ans); ans = max(solve26(x + 1, y - 1), ans); ans = max(solve26(x + 1, y + 1), ans); } if (s >= 3) { ans = max(solve26(x - 2, y), ans); ans = max(solve26(x + 2, y), ans); ans = max(solve26(x, y - 2), ans); ans = max(solve26(x, y + 2), ans); } } out.println(c26); out.println(ans); } int solve26(int x, int y) { int r = -1; if (0 <= x && x <= 9 && 0 <= y && y <= 9) { c26 -= p26[x][y] == 0 ? 1 : 0; p26[x][y]++; r = p26[x][y]; } return r; // RA } void AOJ0008() { // Sum of 4 Integers while (sc.hasNext()) { int ans = 0, n = sc.nextInt(); for (int a = 0; a < 10; a++) { for (int b = 0; b < 10; b++) { for (int c = 0; c < 10; c++) { for (int d = 0; d < 10; d++) if ((a + b + c + d) == n) ans++; } } } out.println(ans); } } void AOJ0055() { // Sequence while (sc.hasNext()) { double last = sc.nextDouble(), ans = last; for (int i = 2; i <= 10; i++) { ans += i % 2 == 0 ? last * 2 : last / 3; last = i % 2 == 0 ? last * 2 : last / 3; // out.println(last); } out.println(ans); } } void AOJ0084() { // Search Engine StringBuilder sb = new StringBuilder(); while (sc.hasNext()) { String s = sc.next().replaceAll(",", "").replaceAll("\\.", "").replaceAll( " ", ""); // out.println(s); if (s.length() <= 2 || s.length() >= 7) continue; sb.append(s + " "); } out.println(sb.deleteCharAt(sb.length() - 1)); } void AOJ0077() { // Run Length while (sc.hasNext()) { char[] c = sc.nextLine().toCharArray(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < c.length; i++) { if (c[i] == '@') { for (int j = 0; j < Character.digit(c[i + 1], 10); j++) sb.append(c[i + 2]); i += 2; } else sb.append(c[i]); } out.println(sb.toString()); } } void AOJ0064() { // Secret Number int ans = 0; while (sc.hasNext()) { Matcher m = Pattern.compile("\\d+").matcher(sc.nextLine()); while (m.find()) ans += Integer.parseInt(m.group()); } out.println(ans); } void AOJ0050() { // Apple and Peach while (sc.hasNext()) out.println(sc.nextLine() .replaceAll("apple", "PEACH") .replaceAll("peach", "apple") .replaceAll("PEACH", "peach")); } void AOJ0039() { // Roman Figure HashMap<Character, Integer> dic = new HashMap<Character, Integer>(); dic.put('I', 1); dic.put('V', 5); dic.put('X', 10); dic.put('L', 50); dic.put('C', 100); dic.put('D', 500); dic.put('M', 1000); while (sc.hasNext()) { char[] c = sc.next().toCharArray(); int ans = 0, last = dic.get(c[0]); for (int i = 0; i < c.length; i++) { if (last >= dic.get(c[i])) { ans += dic.get(c[i]); last = dic.get(c[i]); } else { ans += dic.get(c[i]) - last - last; last = dic.get(c[i]); } } out.println(ans); } } void AOJ0020() { // Capitalize while (sc.hasNext()) out.println(sc.nextLine().toUpperCase()); } }
[["-", 0, 7, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 17, 19]]
3
5,569
2
import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); for (int i = 1;; i++) { int w = sc.nextInt(); if (w == 0) { break; } int n = sc.nextInt(); int[][] treasure = new int[n][2]; for (int j = 0; j < n; j++) { String str = sc.next(); String[] strArr = str.split(","); treasure[j][0] = Integer.parseInt(strArr[0]); treasure[j][1] = Integer.parseInt(strArr[1]); } int value[][] = new int[n + 1][w + 1]; int weight[][] = new int[n + 1][w + 1]; for (int j = 1; j < n + 1; j++) { for (int k = 1; k < w + 1; k++) { if (k > treasure[j - 1][1] && value[j - 1][k - treasure[j - 1][1]] + treasure[j - 1][0] > value[j - 1][k]) { value[j][k] = value[j - 1][k - treasure[j - 1][1]] + treasure[j - 1][0]; weight[j][k] = weight[j - 1][k - treasure[j - 1][1]] + treasure[j - 1][1]; } else { value[j][k] = value[j - 1][k]; weight[j][k] = weight[j - 1][k]; } } } int weightMin = weight[n][w]; for (int j = w; j >= 0; j--) { if (value[n][j] == value[n][w]) { weightMin = weight[n][j]; } } System.out.println("Case " + i + ":"); System.out.println(value[n][w]); System.out.println(weightMin); } } }
import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); for (int i = 1;; i++) { int w = sc.nextInt(); if (w == 0) { break; } int n = sc.nextInt(); int[][] treasure = new int[n][2]; for (int j = 0; j < n; j++) { String str = sc.next(); String[] strArr = str.split(","); treasure[j][0] = Integer.parseInt(strArr[0]); treasure[j][1] = Integer.parseInt(strArr[1]); } int value[][] = new int[n + 1][w + 1]; int weight[][] = new int[n + 1][w + 1]; for (int j = 1; j < n + 1; j++) { for (int k = 1; k < w + 1; k++) { if (k >= treasure[j - 1][1] && value[j - 1][k - treasure[j - 1][1]] + treasure[j - 1][0] > value[j - 1][k]) { value[j][k] = value[j - 1][k - treasure[j - 1][1]] + treasure[j - 1][0]; weight[j][k] = weight[j - 1][k - treasure[j - 1][1]] + treasure[j - 1][1]; } else { value[j][k] = value[j - 1][k]; weight[j][k] = weight[j - 1][k]; } } } int weightMin = weight[n][w]; for (int j = w; j >= 0; j--) { if (value[n][j] == value[n][w]) { weightMin = weight[n][j]; } } System.out.println("Case " + i + ":"); System.out.println(value[n][w]); System.out.println(weightMin); } } }
[["-", 0, 57, 15, 15, 0, 16, 31, 16, 17, 47], ["+", 0, 57, 15, 15, 0, 16, 31, 16, 17, 20]]
3
511
2
import static java.lang.System.out; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String args[]) { new Main().run(); } int toInt(String s) { return Integer.parseInt(s); } void run() { Scanner sc = new Scanner(System.in); int caseNumber = 1; while (true) { int W = sc.nextInt(); if (W == 0) break; int N = sc.nextInt(); int[] value = new int[N]; int[] weight = new int[N]; sc.skip("\n"); for (int i = 0; i < N; i++) { String line = sc.nextLine(); value[i] = toInt(line.split(",")[0]); weight[i] = toInt(line.split(",")[1]); } /* out.println("W: " + W); out.println("N: " + N); for(int i = 0; i < N; i++) { out.println("item: " + i + " -> " + value[i] + ":" + weight[i]); } */ int[][] dp = new int[N + 1][W + 1]; for (int i = 0; i < N + 1; i++) for (int j = 0; j < W + 1; j++) dp[i][j] = 0; for (int item = 0; item < N; item++) { for (int cur = 0; cur <= W; cur++) { int v = value[item]; int w = weight[item]; if (w <= cur) { dp[item + 1][cur] = Math.max(dp[item][cur], dp[item][cur - w] + v); } else { dp[item + 1][cur] = dp[item][cur]; } } } int ansWeight = W; int ansValue = dp[N][W]; for (int j = W; j >= 0; --j) { if (dp[N][j] == ansValue) ansWeight = j; } out.println("Case :" + caseNumber); caseNumber++; out.println(ansValue); out.println(ansWeight); } } }
import static java.lang.System.out; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String args[]) { new Main().run(); } int toInt(String s) { return Integer.parseInt(s); } void run() { Scanner sc = new Scanner(System.in); int caseNumber = 1; while (true) { int W = sc.nextInt(); if (W == 0) break; int N = sc.nextInt(); int[] value = new int[N]; int[] weight = new int[N]; sc.skip("\n"); for (int i = 0; i < N; i++) { String line = sc.nextLine(); value[i] = toInt(line.split(",")[0]); weight[i] = toInt(line.split(",")[1]); } /* out.println("W: " + W); out.println("N: " + N); for(int i = 0; i < N; i++) { out.println("item: " + i + " -> " + value[i] + ":" + weight[i]); } */ int[][] dp = new int[N + 1][W + 1]; for (int i = 0; i < N + 1; i++) for (int j = 0; j < W + 1; j++) dp[i][j] = 0; for (int item = 0; item < N; item++) { for (int cur = 0; cur <= W; cur++) { int v = value[item]; int w = weight[item]; if (w <= cur) { dp[item + 1][cur] = Math.max(dp[item][cur], dp[item][cur - w] + v); } else { dp[item + 1][cur] = dp[item][cur]; } } } int ansWeight = W; int ansValue = dp[N][W]; for (int j = W; j >= 0; --j) { if (dp[N][j] == ansValue) ansWeight = j; } out.println("Case " + caseNumber + ":"); caseNumber++; out.println(ansValue); out.println(ansWeight); } } }
[["-", 0, 492, 3, 4, 0, 16, 31, 5, 0, 491], ["+", 3, 4, 0, 16, 31, 16, 31, 5, 0, 491], ["+", 0, 1, 0, 492, 3, 4, 0, 16, 17, 72], ["+", 0, 492, 3, 4, 0, 16, 12, 5, 0, 62], ["+", 0, 492, 3, 4, 0, 16, 12, 5, 0, 491]]
3
467
6
import java.util.Scanner; public class Main { public static void main(String[] arg) { Scanner in = new Scanner(System.in); int case_ = 0; while (in.hasNext()) { int w = in.nextInt(); if (w != 0) { System.out.print("case "); System.out.print(++case_); System.out.println(":"); int[] pack = new int[w + 1]; int n = in.nextInt(); int maxNum = 0; for (int j = 0; j < n; j++) { String[] a = in.next().split(","); int p = Integer.valueOf(a[0]); int pw = Integer.valueOf(a[1]); for (int i = w - pw; i >= 0; i--) { pack[i + pw] = Math.max(pack[i + pw], pack[i] + p); if (pack[maxNum] <= pack[i + pw]) { maxNum = i + pw; } } } System.out.println(pack[maxNum]); System.out.println(maxNum); } } } }
import java.util.Scanner; public class Main { public static void main(String[] arg) { Scanner in = new Scanner(System.in); int case_ = 0; while (in.hasNext()) { int w = in.nextInt(); if (w != 0) { System.out.print("Case "); System.out.print(++case_); System.out.println(":"); int[] pack = new int[w + 1]; int n = in.nextInt(); int maxNum = 0; for (int j = 0; j < n; j++) { String[] a = in.next().split(","); int p = Integer.valueOf(a[0]); int pw = Integer.valueOf(a[1]); for (int i = w - pw; i >= 0; i--) { pack[i + pw] = Math.max(pack[i + pw], pack[i] + p); if (pack[maxNum] <= pack[i + pw]) { maxNum = i + pw; } } } System.out.println(pack[maxNum]); System.out.println(maxNum); } } } }
[["-", 0, 1, 0, 492, 3, 4, 0, 5, 0, 491], ["+", 0, 1, 0, 492, 3, 4, 0, 5, 0, 491]]
3
273
45
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); List<String> inputDataList = new ArrayList<String>(); String input = null; try { while ((input = in.readLine()) != null) { inputDataList.add(input); } } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } int cnt = 0; int caseNo = 0; while (inputDataList.size() > cnt && Integer.parseInt(inputDataList.get(cnt)) != 0) { caseNo++; int[] cloth = new int[Integer.parseInt(inputDataList.get(cnt)) + 1]; cnt++; int treasure = Integer.parseInt(inputDataList.get(cnt)); cnt++; cnt = steal(inputDataList, cnt, caseNo, cloth, treasure); } } private static int steal(List<String> inputDataList, int cnt, int caseNo, int[] cloth, int treasure) { for (int i = 0; i < treasure; i++) { String[] treasureData = inputDataList.get(cnt).split(","); cnt++; int value = Integer.parseInt(treasureData[0]); int weight = Integer.parseInt(treasureData[1]); for (int j = cloth.length - weight; j >= 0; j--) { if ((j == 0 || cloth[j] > 0) && cloth[j + weight] < cloth[j] + value) { cloth[j + weight] = cloth[j] + value; } } } printResult(cloth, caseNo); return cnt; } private static void printResult(int[] cloth, int caseNo) { int minWeight = 0; for (int i = 1; i < cloth.length; i++) { if (cloth[i] > cloth[minWeight]) { minWeight = i; } } System.out.println("Case " + caseNo + ":"); System.out.println(cloth[minWeight]); System.out.println(minWeight); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); List<String> inputDataList = new ArrayList<String>(); String input = null; try { while ((input = in.readLine()) != null) { inputDataList.add(input); } } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } int cnt = 0; int caseNo = 0; while (inputDataList.size() > cnt && Integer.parseInt(inputDataList.get(cnt)) != 0) { caseNo++; int[] cloth = new int[Integer.parseInt(inputDataList.get(cnt)) + 1]; cnt++; int treasure = Integer.parseInt(inputDataList.get(cnt)); cnt++; cnt = steal(inputDataList, cnt, caseNo, cloth, treasure); } } private static int steal(List<String> inputDataList, int cnt, int caseNo, int[] cloth, int treasure) { for (int i = 0; i < treasure; i++) { String[] treasureData = inputDataList.get(cnt).split(","); cnt++; int value = Integer.parseInt(treasureData[0]); int weight = Integer.parseInt(treasureData[1]); for (int j = cloth.length - weight - 1; j >= 0; j--) { if ((j == 0 || cloth[j] > 0) && cloth[j + weight] < cloth[j] + value) { cloth[j + weight] = cloth[j] + value; } } } printResult(cloth, caseNo); return cnt; } private static void printResult(int[] cloth, int caseNo) { int minWeight = 0; for (int i = 1; i < cloth.length; i++) { if (cloth[i] > cloth[minWeight]) { minWeight = i; } } System.out.println("Case " + caseNo + ":"); System.out.println(cloth[minWeight]); System.out.println(minWeight); } }
[["+", 0, 7, 502, 503, 49, 200, 51, 16, 17, 33], ["+", 0, 7, 502, 503, 49, 200, 51, 16, 12, 499]]
3
508
2
import java.util.Arrays; import java.util.Scanner; import java.util.Stack; public class Main { static int W, N; static int[][] dp; static int[][] a; static Stack<Integer> stack; public static void main(String[] args) { Scanner cin = new Scanner(System.in); int T = 0; for (;;) { T++; W = cin.nextInt(); if (W == 0) break; stack = new Stack<Integer>(); N = cin.nextInt(); dp = new int[N + 1][W + 2]; a = new int[N + 1][2]; for (int i = 1; i <= N; i++) { // Arrays.fill(dp[i], 1<<30); String[] s = cin.next().split(","); a[i][0] = Integer.parseInt(s[0]); a[i][1] = Integer.parseInt(s[1]); } // dp(0,0); // System.out.println(dp(0,0)); int max = -(1 << 30), w = (1 << 30); for (int i = 1; i < N; i++) { for (int j = 1; j <= W; j++) { if (j - a[i][1] >= 0) { dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - a[i][1]] + a[i][0]); if (max < dp[i][j]) { max = dp[i][j]; w = j; } else if (max == dp[i][j]) { w = Math.min(w, j); } } else { dp[i][j] = dp[i - 1][j]; } } } System.out.println("Case " + T + ":"); System.out.println(max + "\n" + (w - 1)); if (stack.isEmpty()) continue; for (int i = 0; i < N; i++) { for (int j = 0; j <= W; j++) { if (dp[i][j] == 1 << 30) continue; if (max < dp[i][j]) { max = dp[i][j]; w = j; System.out.println(j); } else if (max == dp[i][j]) { w = Math.min(w, j); System.out.println(j); } } } System.out.println(max + " " + w); } } static int dp(int t, int w) { int re = 0; if (t == N) { return 0; } if (dp[t][w] != 1 << 30) return dp[t][w]; re = Math.max(re, dp(t + 1, w)); for (int i = t; i < N; i++) { if (a[i][0] != 0) { int tmp = a[i][0]; if (w + a[i][1] > W) continue; stack.add(i); re = Math.max(re, dp(t + 1, w + a[i][1]) + tmp); stack.pop(); a[i][0] = tmp; } } return dp[t][w] = re; } }
import java.util.Arrays; import java.util.Scanner; import java.util.Stack; public class Main { static int W, N; static int[][] dp; static int[][] a; static Stack<Integer> stack; public static void main(String[] args) { Scanner cin = new Scanner(System.in); int T = 0; for (;;) { T++; W = cin.nextInt(); if (W == 0) break; stack = new Stack<Integer>(); N = cin.nextInt(); dp = new int[N + 1][W + 1]; a = new int[N + 1][2]; for (int i = 1; i <= N; i++) { // Arrays.fill(dp[i], 1<<30); String[] s = cin.next().split(","); a[i][0] = Integer.parseInt(s[0]); a[i][1] = Integer.parseInt(s[1]); } // dp(0,0); // System.out.println(dp(0,0)); int max = -(1 << 30), w = (1 << 30); for (int i = 1; i <= N; i++) { for (int j = 1; j <= W; j++) { if (j - a[i][1] >= 0) { dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - a[i][1]] + a[i][0]); if (max < dp[i][j]) { max = dp[i][j]; w = j; } else if (max == dp[i][j]) { w = Math.min(w, j); } } else { dp[i][j] = dp[i - 1][j]; } } } System.out.println("Case " + T + ":"); System.out.println(max + "\n" + (w)); if (stack.isEmpty()) continue; for (int i = 0; i < N; i++) { for (int j = 0; j <= W; j++) { if (dp[i][j] == 1 << 30) continue; if (max < dp[i][j]) { max = dp[i][j]; w = j; System.out.println(j); } else if (max == dp[i][j]) { w = Math.min(w, j); System.out.println(j); } } } System.out.println(max + " " + w); } } static int dp(int t, int w) { int re = 0; if (t == N) { return 0; } if (dp[t][w] != 1 << 30) return dp[t][w]; re = Math.max(re, dp(t + 1, w)); for (int i = t; i < N; i++) { if (a[i][0] != 0) { int tmp = a[i][0]; if (w + a[i][1] > W) continue; stack.add(i); re = Math.max(re, dp(t + 1, w + a[i][1]) + tmp); stack.pop(); a[i][0] = tmp; } } return dp[t][w] = re; } }
[["-", 0, 11, 12, 227, 497, 505, 0, 16, 12, 499], ["+", 0, 11, 12, 227, 497, 505, 0, 16, 12, 499], ["-", 0, 7, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 17, 19], ["-", 3, 4, 0, 16, 12, 23, 0, 16, 17, 33], ["-", 3, 4, 0, 16, 12, 23, 0, 16, 12, 499]]
3
787
6
#include <stdio.h> int mx(int a, int b) { if (a < b) return b; else return a; } int main() { int N, W, c = 1, mi, max, i, j; int k[1001], w[1001]; while (1) { scanf("%d", &W); if (!W) break; scanf("%d", &N); int dp[1001][1001] = {0}; for (i = 0; i < N; i++) { scanf("%d,%d", &k[i], &w[i]); } for (i = N - 1; i >= 0; i--) { for (j = 0; j <= W; j++) { if (j < w[i]) dp[i][j] = dp[i + 1][j]; else { dp[i][j] = mx(dp[i + 1][j], dp[i + 1][j - w[i]] + k[i]); } } } max = 0; mi = 0; for (i = 0; i < W; i++) { if (max < dp[0][i]) { max = dp[0][i]; mi = i; } } printf("Case %d:\n%d\n%d\n", c++, max, mi); } return 0; }
#include <stdio.h> int mx(int a, int b) { if (a < b) return b; else return a; } int main() { int N, W, c = 1, mi, max, i, j; int k[1001], w[1001]; while (1) { scanf("%d", &W); if (!W) break; scanf("%d", &N); int dp[1001][1001] = {0}; for (i = 0; i < N; i++) { scanf("%d,%d", &k[i], &w[i]); } for (i = N - 1; i >= 0; i--) { for (j = 0; j <= W; j++) { if (j < w[i]) dp[i][j] = dp[i + 1][j]; else { dp[i][j] = mx(dp[i + 1][j], dp[i + 1][j - w[i]] + k[i]); } } } max = 0; mi = 0; for (i = 0; i <= W; i++) { if (max < dp[0][i]) { max = dp[0][i]; mi = i; } } printf("Case %d:\n%d\n%d\n", c++, max, mi); } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
0
316
2
#include <stdio.h> int max(int a, int b) { return a > b ? a : b; } int W, N, v[1000], w[1000], dp[1001][1001]; int main(void) { int n_case = 1; while (1) { int x, y; scanf("%d", &W); if (W == 0) break; scanf("%d", &N); for (x = 0; x < N; x++) { scanf("%d,%d", &v[x], &w[x]); } for (x = 0; x <= W; x++) { dp[0][x] = 0; } for (x = 1; x <= N; x++) { for (y = 0; y <= W; y++) { if (y < w[x]) dp[x][y] = dp[x - 1][y]; else dp[x][y] = max(dp[x - 1][y], dp[x - 1][y - w[x]] + v[x]); } } int max_v = 0, weight; for (x = 0; x <= W; x++) { if (dp[N][x] > max_v) { max_v = dp[N][x]; weight = x; } } printf("Case %d:\n%d\n%d\n", n_case, max_v, weight); n_case++; } return 0; }
#include <stdio.h> int max(int a, int b) { return a > b ? a : b; } int W, N, v[1001], w[1001], dp[1001][1001]; int main(void) { int n_case = 1; while (1) { int x, y; scanf("%d", &W); if (W == 0) break; scanf("%d", &N); for (x = 1; x <= N; x++) { scanf("%d,%d", &v[x], &w[x]); } for (x = 0; x <= W; x++) { dp[0][x] = 0; } for (x = 1; x <= N; x++) { for (y = 0; y <= W; y++) { if (y < w[x]) dp[x][y] = dp[x - 1][y]; else dp[x][y] = max(dp[x - 1][y], dp[x - 1][y - w[x]] + v[x]); } } int max_v = 0, weight; for (x = 0; x <= W; x++) { if (dp[N][x] > max_v) { max_v = dp[N][x]; weight = x; } } printf("Case %d:\n%d\n%d\n", n_case, max_v, weight); n_case++; } return 0; }
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["-", 0, 52, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 7, 10, 11, 12, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
0
328
8
#include <limits.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> int main() { int W, N, i, j, k, max_coin = 0, weg = 0, count = 0; int wg, coin; int dp[102][102] = {}; while (1) { scanf("%d", &W); if (W == 0) break; scanf("%d", &N); for (i = 0; i < N; i++) { scanf("%d,%d", &coin, &wg); for (j = 0; j <= W; j++) { if (wg <= j) dp[i + 1][j] = fmax(dp[i][j], dp[i][j - wg] + coin); else dp[i + 1][j] = dp[i][j]; } } count++; printf("Case %d:\n", count); for (j = 0; j <= W; j++) { if (dp[N][j] == dp[N][W]) { printf("%d\n%d\n", dp[N][j], j); break; } } } return 0; }
#include <limits.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> int main() { int W, N, i, j, k, max_coin = 0, weg = 0, count = 0; int wg, coin; int dp[1002][1002] = {}; while (1) { scanf("%d", &W); if (W == 0) break; scanf("%d", &N); for (i = 0; i < N; i++) { scanf("%d,%d", &coin, &wg); for (j = 0; j <= W; j++) { if (wg <= j) dp[i + 1][j] = fmax(dp[i][j], dp[i][j - wg] + coin); else dp[i + 1][j] = dp[i][j]; } } count++; printf("Case %d:\n", count); for (j = 0; j <= W; j++) { if (dp[N][j] == dp[N][W]) { printf("%d\n%d\n", dp[N][j], j); break; } } } return 0; }
[["-", 0, 43, 49, 50, 49, 80, 49, 80, 81, 13], ["+", 0, 43, 49, 50, 49, 80, 49, 80, 81, 13], ["-", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13]]
0
266
4
#include <stdio.h> #define max(a, b) (a) > (b) ? (a) : (b); main() { int N, W; int v[1000], w[1000]; int dp[2][1001] = { { 0, }, }; int i, j; int maxv, minw; int c = 1; while (scanf("%d", &W) != EOF) { if (!W) break; for (i = 0; i <= W; i++) dp[0][i] = 0; scanf("%d", &N); for (i = 0; i < N; i++) scanf("%d,%d", &v[i], &w[i]); for (i = 1; i <= N; i++) { for (j = 0; j <= W; j++) { if (j - w[i - 1] < 0) dp[i % 2][j] = dp[(i - 1) % 2][j]; else dp[i % 2][j] = max(dp[(i - 1) % 2][j], dp[(i - 1) % 2][j - w[i - 1]] + v[i - 1]); } } maxv = -1; for (i = 0; i <= W; i++) { if (maxv < dp[N % 2][i]) { maxv = dp[N % 2][i]; minw = i; } } printf("Case %d\n%d\n%d\n", c++, maxv, minw); } return 0; }
#include <stdio.h> #define max(a, b) (a) > (b) ? (a) : (b); main() { int N, W; int v[1000], w[1000]; int dp[2][1001] = { { 0, }, }; int i, j; int maxv, minw; int c = 1; while (scanf("%d", &W) != EOF) { if (!W) break; for (i = 0; i <= W; i++) dp[0][i] = 0; scanf("%d", &N); for (i = 0; i < N; i++) scanf("%d,%d", &v[i], &w[i]); for (i = 1; i <= N; i++) { for (j = 0; j <= W; j++) { if (j - w[i - 1] < 0) dp[i % 2][j] = dp[(i - 1) % 2][j]; else dp[i % 2][j] = max(dp[(i - 1) % 2][j], dp[(i - 1) % 2][j - w[i - 1]] + v[i - 1]); } } maxv = -1; for (i = 0; i <= W; i++) { if (maxv < dp[N % 2][i]) { maxv = dp[N % 2][i]; minw = i; } } printf("Case %d:\n%d\n%d\n", c++, maxv, minw); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
348
2
t[9999][9999], j; main(p, W, a, b, c) { for (; scanf("%d", &W), W; printf("Case %d:\n%d\n%d\n", p++, t[0][W], j)) { for (scanf("%d", &c); c && scanf("%d,%d", &b, &a); c--) { for (j = 0; j <= W; j++) { t[c - 1][j] = (j < a) ? t[c][j] : (t[c][j] > t[c][j - a] + b) ? t[c][j] : (t[c][j - a] + b); } } for (j = 0; t[0][j] == t[0][W]; j++) ; } }
t[9999][9999], j; main(p, W, a, b, c) { for (; scanf("%d", &W), W; printf("Case %d:\n%d\n%d\n", p++, t[0][W], j)) { for (scanf("%d", &c); c && scanf("%d,%d", &b, &a); c--) { for (j = 0; j <= W; j++) { t[c - 1][j] = (j < a) ? t[c][j] : (t[c][j] > t[c][j - a] + b) ? t[c][j] : (t[c][j - a] + b); } } for (j = 0; t[0][j] != t[0][W]; j++) ; } }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 60], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 79]]
0
208
2
t[9999][9999], j; main(p, W, a, b, c) { for (; scanf("%d", &W), W; printf("Case %d:\n%d\n%d\n", p++, t[0][W], j)) { for (scanf("%d", &c); c && scanf("%d,%d", &b, &a); c--) for (j = 0; j <= W; j++) t[c - 1][j] = (j < a) ? t[c][j] : (t[c][j] > t[c][j - a] + b) ? t[c][j] : (t[c][j - a] + b); for (j = 0; t[0][j] == t[0][W]; j++) ; } }
t[9999][999], j; main(p, W, a, b, c) { for (; scanf("%d", &W), W; printf("Case %d:\n%d\n%d\n", p++, t[0][W], j)) { for (scanf("%d", &c); c && scanf("%d,%d", &b, &a); c--) for (j = 0; j <= W; j++) t[c - 1][j] = (j < a) ? t[c][j] : (t[c][j] > t[c][j - a] + b) ? t[c][j] : (t[c][j - a] + b); for (j = 0; t[0][j] - t[0][W]; j++) ; } }
[["-", 36, 36, 0, 30, 0, 1, 0, 69, 71, 13], ["+", 36, 36, 0, 30, 0, 1, 0, 69, 71, 13], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 60], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 33]]
0
204
29
t[9999], W, N, a, b, i; main(c) { for (; scanf("%d", &W), W; printf("Case %d:\n%d\n%d", c++, t[W], i), memset(t, 0, W * 9)) { for (scanf("%d", &N); N-- && scanf("%d,%d", &a, &b);) { for (i = W; i >= b; i--) t[i] = fmax(t[i], t[i - b] + a); } for (i = 0; t[i] - t[W]; i++) ; } }
t[9999], W, N, a, b, i; main(c) { for (; scanf("%d", &W), W; printf("Case %d:\n%d\n%d\n", c++, t[W], i), memset(t, 0, W * 9)) { for (scanf("%d", &N); N-- && scanf("%d,%d", &a, &b);) { for (i = W; i >= b; i--) t[i] = fmax(t[i], t[i - b] + a); } for (i = 0; t[i] - t[W]; i++) ; } }
[["+", 26, 34, 31, 2, 3, 4, 0, 5, 0, 44]]
0
156
27
#include <stdio.h> #include <string.h> #define Max(x, y) (((x) > (y)) ? (x) : (y)) #define min(x, y) (((x) < (y)) ? (x) : (y)) int dp[1005][1005], w[1005], v[1005]; int main() { int i, j, n, m, wl; n = 1; while (1) { scanf("%d", &wl); if (wl == 0) break; scanf("%d", &m); for (i = 0; i < m; i++) scanf("%d,%d", &v[i], &w[i]); memset(dp, 0, sizeof(dp)); for (i = 1; i <= m; i++) { for (j = 0; j <= wl; j++) { if (j >= w[i]) { dp[i][j] = Max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); } else { dp[i][j] = dp[i - 1][j]; } } } j = wl; for (i = 0; i <= wl; i++) if (dp[m][wl] == dp[m][i]) j = min(j, i); printf("Case %d:\n", n++); printf("%d\n%d\n", dp[m][wl], j); } return 0; }
#include <stdio.h> #include <string.h> #define Max(x, y) (((x) > (y)) ? (x) : (y)) #define min(x, y) (((x) < (y)) ? (x) : (y)) int dp[1005][1005], w[1005], v[1005]; int main() { int i, j, n, m, wl; n = 1; while (1) { scanf("%d", &wl); if (wl == 0) break; scanf("%d", &m); for (i = 1; i <= m; i++) scanf("%d,%d", &v[i], &w[i]); memset(dp, 0, sizeof(dp)); for (i = 1; i <= m; i++) { for (j = 0; j <= wl; j++) { if (j >= w[i]) { dp[i][j] = Max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); } else { dp[i][j] = dp[i - 1][j]; } } } j = wl; for (i = 0; i <= wl; i++) if (dp[m][wl] == dp[m][i]) j = min(j, i); printf("Case %d:\n", n++); printf("%d\n%d\n", dp[m][wl], j); } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 7, 10, 11, 12, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
0
319
4
#include <math.h> #include <stdio.h> int tre[1000][2]; int mem[1000][10000]; int main(void) { int w, n, aw, av, k = 0, m, i, j; while (scanf("%d", &w) * w) { scanf("%d", &n); for (i = 0; i < n; i++) { for (j = 0; j <= w; j++) { mem[i][j] = 0; } } for (i = 0; i < n; i++) { scanf("%d,%d", &tre[i][0], &tre[i][1]); mem[i][tre[i][1]] = tre[i][0]; } for (m = 1; m < n; m++) { aw = tre[m][1]; for (i = 1; i <= w; i++) { av = mem[m - 1][i]; if (av > 0) { if (mem[m][i] < av) mem[m][i] = av; if (i + aw <= w && mem[m][i + aw] < av + tre[m][0]) mem[m][i + aw] = av + tre[m][0]; } } av = 0; aw = 0; for (i = 0; i <= w; i++) { if (mem[m][i] <= av) mem[m][i] = 0; if (mem[m][i] > av) { av = mem[m][i]; aw = i; } } } if (n == 1) if (tre[0][1] <= w) printf("Case %d:\n%d\n%d\n", ++k, tre[0][0], tre[0][1]); else printf("Case %d:\n%d\n%d\n", ++k, av, aw); } return 0; }
#include <math.h> #include <stdio.h> int tre[1000][2]; int mem[1000][10000]; int main(void) { int w, n, aw, av, k = 0, m, i, j; while (scanf("%d", &w) * w) { scanf("%d", &n); for (i = 0; i < n; i++) { for (j = 0; j <= w; j++) { mem[i][j] = 0; } } for (i = 0; i < n; i++) { scanf("%d,%d", &tre[i][0], &tre[i][1]); mem[i][tre[i][1]] = tre[i][0]; } for (m = 1; m < n; m++) { aw = tre[m][1]; for (i = 1; i <= w; i++) { av = mem[m - 1][i]; if (av > 0) { if (mem[m][i] < av) mem[m][i] = av; if (i + aw <= w && mem[m][i + aw] < av + tre[m][0]) mem[m][i + aw] = av + tre[m][0]; } } av = 0; aw = 0; for (i = 0; i <= w; i++) { if (mem[m][i] <= av) mem[m][i] = 0; if (mem[m][i] > av) { av = mem[m][i]; aw = i; } } } if (n == 1) { if (tre[0][1] <= w) printf("Case %d:\n%d\n%d\n", ++k, tre[0][0], tre[0][1]); } else printf("Case %d:\n%d\n%d\n", ++k, av, aw); } return 0; }
[["+", 0, 52, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 0, 52, 8, 9, 0, 57, 64, 9, 0, 46]]
0
451
2
#include <bits/stdc++.h> using namespace std; int main() { int W, N; int dp[1001]; int num = 1; while (scanf("%d", &W), W) { scanf("%d", &N); memset(dp, 0, sizeof(dp)); for (int i = 0; i < N; i++) { int v, w; scanf("%d,%d", &v, &w); for (int j = W; j >= 0; j--) { if (j + w <= W) dp[j + w] = max(dp[j + w], dp[j] + v); } } cout << "Case " << num << ":\n"; cout << dp[W] << endl << (lower_bound(dp, dp + W + 1, dp[W]) - dp) << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int W, N; int dp[1001]; int num = 1; while (scanf("%d", &W), W) { scanf("%d", &N); memset(dp, 0, sizeof(dp)); for (int i = 0; i < N; i++) { int v, w; scanf("%d,%d", &v, &w); for (int j = W; j >= 0; j--) { if (j + w <= W) dp[j + w] = max(dp[j + w], dp[j] + v); } } cout << "Case " << num++ << ":\n"; cout << dp[W] << endl << (lower_bound(dp, dp + W + 1, dp[W]) - dp) << endl; } }
[["+", 0, 1, 0, 16, 31, 16, 12, 27, 17, 29]]
1
192
1
while True: cnt = 0 cnt += 1 maxweight = int(input()) if maxweight == 0: break length = int(input()) tresure = [] dp = [[0 for n in range(length+1)] for m in range(maxweight + 1)] answeight = 0 ansvalue = 0 for l in range(length): v,w = (int(n) for n in input().split(",")) tresure.append([v,w]) for outer in range(length + 1): if outer == 0: continue weight = tresure[outer-1][1] value = tresure[outer-1][0] dp[weight][outer] = max(dp[weight][outer],value) for inner in range(maxweight + 1): if dp[inner][outer - 1] != 0: beforevalue = dp[inner][outer - 1] beforeweight = inner dp[inner][outer] = max(beforevalue,dp[inner][outer]) if beforeweight + weight <= maxweight: nowvalue = dp[beforeweight + weight][outer] dp[beforeweight + weight][outer] = max(nowvalue,beforevalue+value) #print(dp) for a in range(maxweight+1): #print(dp[a][length]) if ansvalue < dp[a][length]: ansvalue = dp[a][length] answeight = a print("Case {0}:".format(cnt)) print(ansvalue) print(answeight)
cnt = 0 while True: cnt += 1 maxweight = int(input()) if maxweight == 0: break length = int(input()) tresure = [] dp = [[0 for n in range(length+1)] for m in range(maxweight + 1)] answeight = 0 ansvalue = 0 for l in range(length): v,w = (int(n) for n in input().split(",")) tresure.append([v,w]) for outer in range(length + 1): if outer == 0: continue weight = tresure[outer-1][1] value = tresure[outer-1][0] dp[weight][outer] = max(dp[weight][outer],value) for inner in range(maxweight + 1): if dp[inner][outer - 1] != 0: beforevalue = dp[inner][outer - 1] beforeweight = inner dp[inner][outer] = max(beforevalue,dp[inner][outer]) if beforeweight + weight <= maxweight: nowvalue = dp[beforeweight + weight][outer] dp[beforeweight + weight][outer] = max(nowvalue,beforevalue+value) #print(dp) for a in range(maxweight+1): #print(dp[a][length]) if ansvalue < dp[a][length]: ansvalue = dp[a][length] answeight = a print("Case {0}:".format(cnt)) print(ansvalue) print(answeight)
[["-", 36, 36, 36, 36, 0, 656, 0, 52, 0, 89], ["-", 36, 36, 36, 36, 0, 656, 0, 52, 15, 146], ["-", 36, 36, 36, 36, 0, 656, 0, 52, 0, 102], ["+", 36, 36, 36, 36, 0, 656, 0, 52, 0, 89], ["+", 36, 36, 36, 36, 0, 656, 0, 52, 15, 146], ["+", 36, 36, 36, 36, 0, 656, 0, 52, 0, 102]]
5
312
6
c=1 while 1: W=int(input()) if W==0:break n=int(input()) dp=[0]*(W+1) for i in range(n): v,w = map(int, input().split(',')) for j in range(W,w-1,-1): dp[j]=max(dp[j-w]+v,dp[j]) for i in range(W): if dp[W]==dp[i]:break print('Case %d:\n%d\n%d'%(c,dp[W],i)) c+=1
c=1 while 1: W=int(input()) if W==0:break n=int(input()) dp=[0]*(W+1) for i in range(n): v,w = map(int, input().split(',')) for j in range(W,w-1,-1): dp[j]=max(dp[j-w]+v,dp[j]) for i in range(W+1): if dp[W]==dp[i]:break print('Case %d:\n%d\n%d'%(c,dp[W],i)) c+=1
[["+", 0, 7, 12, 652, 3, 4, 0, 657, 17, 72], ["+", 0, 7, 12, 652, 3, 4, 0, 657, 12, 612]]
5
146
2
c=1 while 1: W=int(input()) if W==0:break dp=[0]*(W+1) for i in range(int(input())): v,w = map(int, input().split(',')) for j in range(W,w-1,-1): if dp[j]<dp[j-w]:dp[j]=dp[j-w] for i in range(W+1): if dp[W]==dp[i]:break print('Case %d:\n%d\n%d'%(c,dp[W],i)) c+=1
c=1 while 1: W=int(input()) if W==0:break dp=[0]*(W+1) for i in range(int(input())): v,w=map(int, input().split(',')) for j in range(W,w-1,-1): if dp[j]<dp[j-w]+v:dp[j]=dp[j-w]+v for i in range(W+1): if dp[W]==dp[i]:break print('Case %d:\n%d\n%d'%(c,dp[W],i)) c+=1
[["+", 8, 196, 0, 57, 15, 666, 0, 657, 17, 72], ["+", 8, 196, 0, 57, 15, 666, 0, 657, 12, 22], ["+", 64, 196, 0, 1, 0, 662, 12, 657, 17, 72], ["+", 64, 196, 0, 1, 0, 662, 12, 657, 12, 22]]
5
148
4
""" 0-1ナップザック dp[i][j]...i個目までの宝で重さjの時の価値の最大 wi = wlst[i]...i個目の重さ vi = vlst[i]...i個目の価値 dp[i][j] = max(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - wi] + vi) (wi <= j) dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) (wi > j) """ def main(): case = 0 while True: w = int(input()) if not w: break case += 1 n = int(input()) vlst = [0] wlst = [0] for _ in range(n): vin, win = map(int, input().split(",")) vlst.append(vin) wlst.append(win) dp = [[0 for _ in range(w + 1)] for _ in range(n + 1)] for i in range(1, n + 1): vi, wi = vlst[i], wlst[i] dpi = dp[i] dpim = dp[i - 1] for j in range(1, w + 1): if j >= wlst[i]: dpi[j] = max(dpim[j], dpi[j - 1], dpim[j - wi] + vi) else: dpi[j] = max(dpi[j], dpi[j - 1]) print("Case " + str(case) + ":") print(dp[n][w]) for i in range(w, -1, -1): if dp[n][i] != dp[n][w]: print(i + 1) break main()
""" 0-1ナップザック dp[i][j]...i個目までの宝で重さjの時の価値の最大 wi = wlst[i]...i個目の重さ vi = vlst[i]...i個目の価値 dp[i][j] = max(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - wi] + vi) (wi <= j) dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) (wi > j) """ def main(): case = 0 while True: w = int(input()) if not w: break case += 1 n = int(input()) vlst = [0] wlst = [0] for _ in range(n): vin, win = map(int, input().split(",")) vlst.append(vin) wlst.append(win) dp = [[0 for _ in range(w + 1)] for _ in range(n + 1)] for i in range(1, n + 1): vi, wi = vlst[i], wlst[i] dpi = dp[i] dpim = dp[i - 1] for j in range(1, w + 1): if j >= wi: dpi[j] = max(dpim[j], dpi[j - 1], dpim[j - wi] + vi) else: dpi[j] = max(dpim[j], dpi[j - 1]) print("Case " + str(case) + ":") print(dp[n][w]) for i in range(w, -1, -1): if dp[n][i] != dp[n][w]: print(i + 1) break main()
[["-", 8, 196, 0, 57, 15, 666, 0, 206, 51, 22], ["-", 8, 196, 0, 57, 15, 666, 0, 206, 0, 70], ["-", 8, 196, 0, 57, 15, 666, 0, 206, 206, 22], ["-", 8, 196, 0, 57, 15, 666, 0, 206, 0, 73], ["+", 0, 7, 8, 196, 0, 57, 15, 666, 0, 22], ["-", 0, 662, 12, 652, 3, 4, 0, 206, 51, 22], ["+", 0, 662, 12, 652, 3, 4, 0, 206, 51, 22]]
5
286
7
def get(): p,w = list(map(int, input().split(","))) for i in range(W-w+1,-1,-1): if bag[i] > 0 and bag[i]+p > bag[i+w]: bag[i+w] = bag[i]+p else: if p > bag[w]: bag[w] = p return c = 1 while True: W = int(input()) if W == 0: break bag = [0]*(W+1) N = int(input()) for i in range(N): get() w_all = max(bag) money = bag.index(w_all) print("Case {}:\n{}\n{}".format(c,w_all,money)) c += 1
def get(): p,w = list(map(int, input().split(","))) for i in range(W-w,-1,-1): if bag[i] > 0 and bag[i]+p > bag[i+w]: bag[i+w] = bag[i]+p else: if p > bag[w]: bag[w] = p return c = 1 while True: W = int(input()) if W == 0: break bag = [0]*(W+1) N = int(input()) for i in range(N): get() w_all = max(bag) money = bag.index(w_all) print("Case {}:\n{}\n{}".format(c,w_all,money)) c += 1
[["-", 0, 7, 12, 652, 3, 4, 0, 657, 17, 72], ["-", 0, 7, 12, 652, 3, 4, 0, 657, 12, 612]]
5
183
2
function Puzzle() { var obj = {}; var arrA = "123,234,345,456,567,678,789,111,222,333,444,555,666,777,888,999".split( ","); var arrB = "11,22,33,44,55,66,77,88,99".split(","); for (var i = 0; i < 16; i++) for (var j = 0; j < 16; j++) for (var k = 0; k < 16; k++) for (var l = 0; l < 16; l++) for (var m = 0; m < 9; m++) { var str = arrA[i] + arrA[j] + arrA[k] + arrA[l] + arrB[m]; str = str.split("") .map(Number) .sort(function(a, b) { return a - b; }) .join(""); obj[str] = true; } return obj; } var P = Puzzle(); var input = require('fs').readFileSync('/dev/stdin', 'utf8'); var Arr = (input.trim()).split("\n"); while (true) { if (Arr.length == 0) break; var str = Arr.shift(); var ans = []; for (var i = 1; i <= 9; i++) { var arr = str.split("").map(Number); arr.push(i); arr.sort(function(a, b) { return a - b; }); var cnt = [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ]; arr.forEach(function(v) { cnt[v]++; }); var max = Math.max.apply(null, cnt); if (max >= 5) continue; if (P.hasOwnProperty(arr.join(""))) ans.push(i); } console.log((ans.length == 0) ? "0" : ans.join(" ")); }
function Puzzle() { var obj = {}; var arrA = "123,234,345,456,567,678,789,111,222,333,444,555,666,777,888,999".split( ","); var arrB = "11,22,33,44,55,66,77,88,99".split(","); for (var i = 0; i < 16; i++) for (var j = 0; j < 16; j++) for (var k = 0; k < 16; k++) for (var l = 0; l < 16; l++) for (var m = 0; m < 9; m++) { var str = arrA[i] + arrA[j] + arrA[k] + arrA[l] + arrB[m]; str = str.split("") .map(Number) .sort(function(a, b) { return a - b; }) .join(""); obj[str] = true; } return obj; } var P = Puzzle(); var input = require('fs').readFileSync('/dev/stdin', 'utf8'); var Arr = (input.trim()).split("\n"); while (true) { if (Arr.length == 0) break; var str = Arr.shift(); var ans = []; for (var i = 1; i <= 9; i++) { var arr = str.split("").map(Number); arr.push(i); arr.sort(function(a, b) { return a - b; }); var cnt = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]; arr.forEach(function(v) { cnt[v]++; }); var max = Math.max.apply(null, cnt); if (max >= 5) continue; if (P.hasOwnProperty(arr.join(""))) ans.push(i); } console.log((ans.length == 0) ? "0" : ans.join(" ")); }
[["+", 8, 556, 0, 198, 0, 200, 51, 516, 0, 21], ["+", 8, 556, 0, 198, 0, 200, 51, 516, 0, 555]]
2
434
85
r, i, f, N[]; C(n) { int *p = N + 9; for (r += !n; p-- > N; ++*p, ++p[1], ++p[2]) (*p -= 3) < 0 || C(n - 1), (*p += 2) < 0 | !p[1]-- | !p[2]-- || C(n - 1); } main(j) { for (; j = getchar() + 1;) if (++i == 14 || !++N[j - 50]) { for (i = -1; ++i < 9; N[i]--) if (++N[i] < 5) { for (r = 0, j = 9; j--; N[j] += 2) (N[j] -= 2) < 0 || C(4); f = r ? printf(" %d" + !f, 1 + i) : f; } puts("0" + !!f); memset(N, f = i = 0, 36); } }
r, i, f, N[11]; C(n) { int *p = N + 9; for (r += !n; p-- > N; ++*p, ++p[1], ++p[2]) (*p -= 3) < 0 || C(n - 1), (*p += 2) < 0 | !p[1]-- | !p[2]-- || C(n - 1); } main(j) { for (; j = getchar() + 1;) if (++i == 14 || !++N[j - 50]) { for (i = -1; ++i < 9; N[i]--) if (++N[i] < 5) { for (r = 0, j = 9; j--; N[j] += 2) (N[j] -= 2) < 0 || C(4); f = r ? printf(" %d" + !f, 1 + i) : f; } puts("0" + !!f); memset(N, f = i = 0, 36); } exit(0); }
[["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 0, 14, 8, 9, 0, 1, 0, 2, 63, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35]]
0
243
39
#include <cstdio> #include <iostream> using namespace std; bool ans; void solve(int cntnum[], int progress) { int cntnum_c[10], cntcont = 0; /*printf("%d: ",progress); for(int i=0;i<10;i++){ printf("%d",cntnum[i]); }puts("");*/ for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) cntnum_c[j] = cntnum[j]; if (cntnum[i] >= 1) { cntcont++; } else cntcont = 0; if (cntnum[i] >= 2 && progress == 0) { cntnum_c[i] -= 2; solve(cntnum_c, progress + 1); } if (progress >= 1) { if (cntcont >= 3) { if (progress == 4) { ans = true; return; } for (int j = i - 2; j <= i; j++) cntnum_c[j]--; solve(cntnum_c, progress + 1); } else if (cntnum[i] >= 3) { if (progress == 4) { ans = true; return; } cntnum_c[i] -= 3; solve(cntnum_c, progress + 1); } } } return; } int main() { char input; while (cin >> input) { int cntnum[10] = {}, cntans = 0, outputans[10]; input -= '0'; cntnum[input]++; for (int i = 1; i < 13; i++) { scanf(" %c", &input); input -= '0'; cntnum[input]++; } for (int i = 0; i < 10; i++) { ans = false; cntnum[i]++; if (cntnum[i] < 5) solve(cntnum, 0); cntnum[i]--; if (ans) { outputans[cntans] = i; cntans++; } } if (cntans == 0) printf("0\n"); else { printf("%d", outputans[0]); for (int i = 1; i < cntans; i++) { printf(" %d", outputans[i]); } puts(""); } } return 0; }
#include <cstdio> #include <iostream> using namespace std; bool ans; void solve(int cntnum[], int progress) { int cntnum_c[10], cntcont = 0; /*printf("%d: ",progress); for(int i=0;i<10;i++){ printf("%d",cntnum[i]); }puts("");*/ for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) cntnum_c[j] = cntnum[j]; if (cntnum[i] >= 1) { cntcont++; } else cntcont = 0; if (cntnum[i] >= 2 && progress == 0) { cntnum_c[i] -= 2; solve(cntnum_c, progress + 1); } if (progress >= 1) { if (cntcont >= 3) { if (progress == 4) { ans = true; return; } for (int j = i - 2; j <= i; j++) cntnum_c[j]--; solve(cntnum_c, progress + 1); } else if (cntnum[i] >= 3) { if (progress == 4) { ans = true; return; } cntnum_c[i] -= 3; solve(cntnum_c, progress + 1); } } } return; } int main() { char input; while (cin >> input) { int cntnum[10] = {}, cntans = 0, outputans[10]; input -= '0'; cntnum[input]++; for (int i = 1; i < 13; i++) { scanf(" %c", &input); input -= '0'; cntnum[input]++; } for (int i = 1; i < 10; i++) { ans = false; cntnum[i]++; if (cntnum[i] < 5) solve(cntnum, 0); cntnum[i]--; if (ans) { outputans[cntans] = i; cntans++; } } if (cntans == 0) printf("0\n"); else { printf("%d", outputans[0]); for (int i = 1; i < cntans; i++) { printf(" %d", outputans[i]); } puts(""); } } return 0; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
445
2
#include <bits/stdc++.h> using namespace std; using db = double; using ll = long long; using vi = vector<int>; #define op operator #define pb push_back const int N = 15; int c[N]; bool ans; void dfs(int a, int b) { if (!a && !b) { ans = 1; return; } int i = find_if(c, c + N, [](int i) { return i > 0; }) - c; if (a) { if (c[i] >= 3) { c[i] -= 3; dfs(a - 1, b); c[i] += 3; } if (c[i] && c[i + 1] && c[i + 2]) { c[i]--; c[i + 1]--; c[i + 2]--; dfs(a - 1, b); c[i]++; c[i + 1]++; c[i + 2]++; } } if (b && c[i] >= 2) { c[i] -= 2; dfs(a, b - 1); c[i] += 2; } } int main() { ios ::sync_with_stdio(0); for (string s; cin >> s;) { memset(c, 0, sizeof c); for (char i : s) c[i - '0']++; vector<int> v; for (int i = 1; i <= 9; i++) if (c[i] < 4) { c[i]++; ans = 0; dfs(4, 1); if (ans) v.pb(i); c[i]--; } if (v.size() == 0) cout << "0\n"; else { for (int i = 0; i < v.size(); i++) cout << v[i] << (i + 1 == v.size() ? '\n' : ' '); cout << '\n'; } } return 0; }
#include <bits/stdc++.h> using namespace std; using db = double; using ll = long long; using vi = vector<int>; #define op operator #define pb push_back const int N = 15; int c[N]; bool ans; void dfs(int a, int b) { if (!a && !b) { ans = 1; return; } int i = find_if(c, c + N, [](int i) { return i > 0; }) - c; if (a) { if (c[i] >= 3) { c[i] -= 3; dfs(a - 1, b); c[i] += 3; } if (c[i] && c[i + 1] && c[i + 2]) { c[i]--; c[i + 1]--; c[i + 2]--; dfs(a - 1, b); c[i]++; c[i + 1]++; c[i + 2]++; } } if (b && c[i] >= 2) { c[i] -= 2; dfs(a, b - 1); c[i] += 2; } } int main() { ios ::sync_with_stdio(0); for (string s; cin >> s;) { memset(c, 0, sizeof c); for (char i : s) c[i - '0']++; vector<int> v; for (int i = 1; i <= 9; i++) if (c[i] < 4) { c[i]++; ans = 0; dfs(4, 1); if (ans) v.pb(i); c[i]--; } if (v.size() == 0) cout << "0\n"; else { for (int i = 0; i < v.size(); i++) cout << v[i] << (i + 1 == v.size() ? '\n' : ' '); } } return 0; }
[["-", 75, 76, 0, 9, 0, 1, 0, 16, 31, 22], ["-", 75, 76, 0, 9, 0, 1, 0, 16, 17, 151], ["-", 0, 9, 0, 1, 0, 16, 12, 103, 0, 104], ["-", 0, 9, 0, 1, 0, 16, 12, 103, 0, 44], ["-", 0, 57, 75, 76, 0, 9, 0, 1, 0, 35]]
1
444
6
#include <bits/stdc++.h> #define e(i, n) for (int i = 0; i < n; i++) using namespace std; int m[9], t[9], d; bool F() { e(i, 9) { e(j, 9) t[j] = m[j]; if (t[i] >= 2) { t[i] -= 2; bool o = 1; e(j, 4) { int p = 0; d = 0; while (!t[p]) p++; if (!d && t[p] >= 3) t[p] -= 3, d = 1; if (!d && p < 7) if (t[p + 1] && t[p + 2]) t[p]--, t[p + 1]--, t[p + 2]--, d = 1; if (!d) { o = 0; break; } } if (o) return 1; } } return 0; } int main() { string s; while (cin >> s) { e(i, 9) m[i] = 0; d = 0; e(i, s.size()) m[s[i] - '1']++; e(i, 9) { m[i]++; if (m[i] < 5) if (F()) { if (d) cout << " "; cout << i + 1; d = 1; } m[i]--; } if (!d) cout << 0; cout << endl; } return 0; }
#include <bits/stdc++.h> #define e(i, n) for (int i = 0; i < n; i++) using namespace std; int m[9], t[9]; bool F() { e(i, 9) { e(j, 9) t[j] = m[j]; if (t[i] >= 2) { t[i] -= 2; bool o = 1; e(j, 4) { int p = 0, d = 0; while (!t[p]) p++; if (!d && t[p] >= 3) t[p] -= 3, d = 1; if (!d && p < 7) if (t[p + 1] && t[p + 2]) t[p]--, t[p + 1]--, t[p + 2]--, d = 1; if (!d) { o = 0; break; } } if (o) return 1; } } return 0; } int main() { string s; while (cin >> s) { e(i, 9) m[i] = 0; int d = 0; e(i, s.size()) m[s[i] - '1']++; e(i, 9) { m[i]++; if (m[i] < 5) if (F()) { if (d) cout << " "; cout << i + 1; d = 1; } m[i]--; } if (!d) cout << 0; cout << endl; } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 43, 0, 21], ["-", 36, 36, 36, 36, 0, 30, 0, 43, 49, 22], ["-", 0, 57, 64, 9, 0, 9, 0, 43, 0, 35], ["+", 0, 57, 64, 9, 0, 9, 0, 43, 0, 21], ["+", 8, 9, 0, 52, 8, 9, 0, 43, 39, 40]]
1
337
5
#include <algorithm> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; int x[14]; bool r[14][100]; bool tempai(int num, int jan, int chou, bool make) { for (int i = 0; i < 14; i++) { if (!r[i][num]) { if (jan == 1 && i < 13) { if (x[i] == x[i + 1] && !(r[i + 1][num])) { for (int j = 0; j < 14; j++) { r[j][num + 1] = r[j][num]; } r[i][num + 1] = 1; r[i + 1][num + 1] = 1; make = tempai(num + 1, 0, chou, make); } } if (chou > 0 && i < 12) { if (x[i] == x[i + 1] && x[i + 1] == x[i + 2] && !(r[i + 1][num]) && !(r[i + 2][num])) { for (int j = 0; j < 14; j++) { r[j][num + 1] = r[j][num]; } r[i][num + 1] = 1; r[i + 1][num + 1] = 1; r[i + 2][num + 1] = 1; make = tempai(num + 1, jan, chou - 1, make); } } if (chou > 0 && x[i] < 8) { int a = -1, b = -1; for (int j = i + 1; j < 14; j++) { if (x[j] == x[i] + 1 && !(r[j][num])) { a = j; } if (x[j] == x[i] + 2 && !(r[j][num])) { b = j; } } if (a != -1 && b != -1) { for (int j = 0; j < 14; j++) { r[j][num + 1] = r[j][num]; } r[i][num + 1] = 1; r[a][num + 1] = 1; r[b][num + 1] = 1; make = tempai(num + 1, jan, chou - 1, make); } } } } if (!(jan) && !(chou)) { make = true; } return make; } int main() { bool ant = false; for (;;) { int hai[14] = {0}; vector<int> ans; for (int i = 0; i < 13; i++) { if (scanf("%1d", &hai[i]) == EOF) { ant = true; break; } } if (ant = true) { break; } for (int i = 1; i < 10; i++) { hai[13] = i; for (int j = 0; j < 14; j++) { x[j] = hai[j]; } sort(x, x + 14); bool ax = false; for (int j = 0, r = -1, cou = 0; j < 14; j++) { if (r != x[j]) { r = x[j]; cou = 1; } else { cou++; } if (cou > 4) { ax = true; break; } } if (!ax) { bool f = tempai(0, 1, 4, false); if (f) { ans.push_back(i); } } } if (ans.empty()) { cout << "0" << endl; } else { for (int i = 0; i < ans.size(); i++) { if (!i) { cout << ans[i]; } else { cout << " " << ans[i]; } } cout << endl; } } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; int x[14]; bool r[14][100]; bool tempai(int num, int jan, int chou, bool make) { for (int i = 0; i < 14; i++) { if (!r[i][num]) { if (jan == 1 && i < 13) { if (x[i] == x[i + 1] && !(r[i + 1][num])) { for (int j = 0; j < 14; j++) { r[j][num + 1] = r[j][num]; } r[i][num + 1] = 1; r[i + 1][num + 1] = 1; make = tempai(num + 1, 0, chou, make); } } if (chou > 0 && i < 12) { if (x[i] == x[i + 1] && x[i + 1] == x[i + 2] && !(r[i + 1][num]) && !(r[i + 2][num])) { for (int j = 0; j < 14; j++) { r[j][num + 1] = r[j][num]; } r[i][num + 1] = 1; r[i + 1][num + 1] = 1; r[i + 2][num + 1] = 1; make = tempai(num + 1, jan, chou - 1, make); } } if (chou > 0 && x[i] < 8) { int a = -1, b = -1; for (int j = i + 1; j < 14; j++) { if (x[j] == x[i] + 1 && !(r[j][num])) { a = j; } if (x[j] == x[i] + 2 && !(r[j][num])) { b = j; } } if (a != -1 && b != -1) { for (int j = 0; j < 14; j++) { r[j][num + 1] = r[j][num]; } r[i][num + 1] = 1; r[a][num + 1] = 1; r[b][num + 1] = 1; make = tempai(num + 1, jan, chou - 1, make); } } } } if (!(jan) && !(chou)) { make = true; } return make; } int main() { bool ant = false; for (;;) { int hai[14] = {0}; vector<int> ans; for (int i = 0; i < 13; i++) { if (scanf("%1d", &hai[i]) == EOF) { ant = true; break; } } if (ant == true) { break; } for (int i = 1; i < 10; i++) { hai[13] = i; for (int j = 0; j < 14; j++) { x[j] = hai[j]; } sort(x, x + 14); bool ax = false; for (int j = 0, r = -1, cou = 0; j < 14; j++) { if (r != x[j]) { r = x[j]; cou = 1; } else { cou++; } if (cou > 4) { ax = true; break; } } if (!ax) { bool f = tempai(0, 1, 4, false); if (f) { ans.push_back(i); } } } if (ans.empty()) { cout << "0" << endl; } else { for (int i = 0; i < ans.size(); i++) { if (!i) { cout << ans[i]; } else { cout << " " << ans[i]; } } cout << endl; } } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 11, 17, 32], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60]]
1
898
2
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int cnt[10]; string str; bool done; bool ok(int pos) { if (pos == 10) return true; if (cnt[pos] == 0) return ok(pos + 1); if (cnt[pos] >= 3) { cnt[pos] -= 3; if (ok(pos)) { cnt[pos] += 3; return true; } cnt[pos] += 3; } if (cnt[pos] >= 2 && !done) { done = true; cnt[pos] -= 2; if (ok(pos)) { done = false; cnt[pos] += 2; return true; } cnt[pos] += 2; done = false; } if (pos > 7) return false; if (cnt[pos] && cnt[pos + 1] && cnt[pos + 2]) { cnt[pos]--; cnt[pos + 1]--; cnt[pos + 2]--; if (ok(pos)) { cnt[pos]++; cnt[pos + 1]++; cnt[pos + 2]++; return true; } cnt[pos]++; cnt[pos + 1]++; cnt[pos + 2]++; } return false; } int main() { while (cin >> str) { done = false; fill(cnt, cnt + 10, 0); vector<int> ans; for (int i = 0; i < 13; i++) { cnt[str[i] - '0']++; } for (int i = 1; i <= 9; i++) { if (cnt[i] == 4) continue; cnt[i]++; if (ok(1)) ans.push_back(i); cnt[i]--; } for (int i = 0; i < ans.size(); i++) { cout << ans[i]; if (i != ans.size() - 1) cout << " "; } if (ans.size() == 0) cout << 0 << endl; } return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int cnt[10]; string str; bool done; bool ok(int pos) { if (pos == 10) return true; if (cnt[pos] == 0) return ok(pos + 1); if (cnt[pos] >= 3) { cnt[pos] -= 3; if (ok(pos)) { cnt[pos] += 3; return true; } cnt[pos] += 3; } if (cnt[pos] >= 2 && !done) { done = true; cnt[pos] -= 2; if (ok(pos)) { done = false; cnt[pos] += 2; return true; } cnt[pos] += 2; done = false; } if (pos > 7) return false; if (cnt[pos] && cnt[pos + 1] && cnt[pos + 2]) { cnt[pos]--; cnt[pos + 1]--; cnt[pos + 2]--; if (ok(pos)) { cnt[pos]++; cnt[pos + 1]++; cnt[pos + 2]++; return true; } cnt[pos]++; cnt[pos + 1]++; cnt[pos + 2]++; } return false; } int main() { while (cin >> str) { done = false; fill(cnt, cnt + 10, 0); vector<int> ans; for (int i = 0; i < 13; i++) { cnt[str[i] - '0']++; } for (int i = 1; i <= 9; i++) { if (cnt[i] == 4) continue; cnt[i]++; if (ok(1)) ans.push_back(i); cnt[i]--; } for (int i = 0; i < ans.size(); i++) { cout << ans[i]; if (i != ans.size() - 1) cout << " "; } if (ans.size() == 0) cout << 0; cout << endl; } return 0; }
[["+", 0, 52, 8, 9, 0, 57, 64, 1, 0, 35], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 31, 22]]
1
455
2
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <vector> using namespace std; int u[10]; bool solve() { bool f = false; int sum = 0; for (int i = 1; i < 10; i++) sum += u[i]; if (sum == 0) return true; for (int i = 1; i < 10; i++) { if (u[i] > 2) { u[i] -= 3; if (solve()) { f = true; } u[i] += 3; } } for (int i = 1; i < 8; i++) { if (u[i] > 0 && u[i + 1] > 0 && u[i + 2] > 0) { u[i]--; u[i + 1]--; u[i + 2]--; if (solve()) { f = true; } u[i]++; u[i + 1]++; u[i + 2]++; } } return f; } int main(void) { char buff[20]; while (scanf("%s", buff) != EOF) { int d[10] = {0}; vector<int> v; for (int i = 0; i < 13; i++) { d[buff[i] - '0']++; } for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { u[j] = d[j]; } if (u[i] == 4) continue; if (u[i] > 4) { v.clear(); break; } u[i]++; if (u[i] > 4) continue; bool f = false; for (int j = 1; j < 10; j++) { if (u[j] > 1) { u[j] -= 2; if (solve()) { f = true; } u[j] += 2; } } if (f) v.push_back(i); } if (v.size() == 0) cout << "0" << endl; for (int i = 0; i < v.size() - 1; i++) cout << v[i] << " "; cout << v[v.size() - 1] << endl; } }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <vector> using namespace std; int u[10]; bool solve() { bool f = false; int sum = 0; for (int i = 1; i < 10; i++) sum += u[i]; if (sum == 0) return true; for (int i = 1; i < 10; i++) { if (u[i] > 2) { u[i] -= 3; if (solve()) { f = true; } u[i] += 3; } } for (int i = 1; i < 8; i++) { if (u[i] > 0 && u[i + 1] > 0 && u[i + 2] > 0) { u[i]--; u[i + 1]--; u[i + 2]--; if (solve()) { f = true; } u[i]++; u[i + 1]++; u[i + 2]++; } } return f; } int main(void) { char buff[20]; while (scanf("%s", buff) != EOF) { int d[10] = {0}; vector<int> v; for (int i = 0; i < 13; i++) { d[buff[i] - '0']++; } for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { u[j] = d[j]; } if (u[i] == 4) continue; if (u[i] > 4) { v.clear(); break; } u[i]++; if (u[i] > 4) continue; bool f = false; for (int j = 1; j < 10; j++) { if (u[j] > 1) { u[j] -= 2; if (solve()) { f = true; } u[j] += 2; } } if (f) v.push_back(i); } if (v.size() == 0) { cout << "0" << endl; continue; } for (int i = 0; i < v.size() - 1; i++) cout << v[i] << " "; cout << v[v.size() - 1] << endl; } }
[["+", 0, 52, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35], ["+", 8, 9, 0, 57, 64, 9, 0, 116, 0, 117], ["+", 0, 52, 8, 9, 0, 57, 64, 9, 0, 46]]
1
518
4
#include <algorithm> #include <cmath> #include <complex> #include <iostream> #include <map> #include <queue> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); ++i) #define ALL(v) (v).begin(), (v).end() #define PB push_back #define EPS 1e-8 static const double PI = 6 * asin(0.5); typedef complex<double> P; typedef long long ll; string s; int c[9]; int ct[9]; vector<int> ans; bool puzzle(int k) { bool f; rep(i, 9) { if (k == 4) { if (c[i] == 2) return true; } if (i <= 6 && c[i] && c[i + 1] && c[i + 2]) { c[i]--; c[i + 1]--; c[i + 2]--; f = puzzle(k + 1); c[i]++; c[i + 1]++; c[i + 2]++; if (f) return f; } if (c[i] > 2) { c[i] -= 3; f = puzzle(k + 1); c[i] += 3; if (f) return f; } } return false; } int main() { while (cin >> s) { rep(i, 9) { ct[i] = 0; } rep(i, 13) { ct[s[i] - '1']++; } rep(i, 9) { rep(j, 9) { c[j] = ct[j]; } if (c[i] >= 4) continue; c[i]++; if (puzzle(0)) ans.PB(i + 1); } if (ans.empty()) cout << 0 << endl; else rep(i, ans.size()) cout << (i == 0 ? "" : " ") << ans[i]; cout << endl; memset(ct, 0, sizeof(ct)); ans.clear(); } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <iostream> #include <map> #include <queue> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); ++i) #define ALL(v) (v).begin(), (v).end() #define PB push_back #define EPS 1e-8 static const double PI = 6 * asin(0.5); typedef complex<double> P; typedef long long ll; string s; int c[9]; int ct[9]; vector<int> ans; bool puzzle(int k) { bool f; rep(i, 9) { if (k == 4) { if (c[i] == 2) return true; } if (i <= 6 && c[i] && c[i + 1] && c[i + 2]) { c[i]--; c[i + 1]--; c[i + 2]--; f = puzzle(k + 1); c[i]++; c[i + 1]++; c[i + 2]++; if (f) return f; } if (c[i] > 2) { c[i] -= 3; f = puzzle(k + 1); c[i] += 3; if (f) return f; } } return false; } int main() { while (cin >> s) { rep(i, 9) { ct[i] = 0; } rep(i, 13) { ct[s[i] - '1']++; } rep(i, 9) { rep(j, 9) { c[j] = ct[j]; } if (c[i] >= 4) continue; c[i]++; if (puzzle(0)) ans.PB(i + 1); } if (ans.empty()) cout << 0; else rep(i, ans.size()) cout << (i == 0 ? "" : " ") << ans[i]; cout << endl; memset(ct, 0, sizeof(ct)); ans.clear(); } return 0; }
[["-", 8, 9, 0, 57, 64, 1, 0, 16, 17, 151], ["-", 8, 9, 0, 57, 64, 1, 0, 16, 12, 22]]
1
441
2
def check(table) result = 0 if table.sum <= 2 table.find {|x| x == 2} else (1..7).each do |i| tmp = table.dup if tmp[i].nonzero? and tmp[i + 1].nonzero? and tmp[i + 2].nonzero? 3.times {|x| tmp[i + x] -= 1} return true if check(tmp) end end (1..9).each do |j| tmp1 = table.dup if tmp1[j] >= 3 tmp1[j] -= 3 return true if check(tmp1) end end false end end io.readlines.map(&:chomp).map {|n| n.chars.map(&:to_i)}.each do |data| table = Array.new(10, 0) data.each {|i| table[i] += 1} result = (1..9).map do |i| added = table.dup added[i] += 1 next if added[i] > 4 check(added) ? i : nil end.compact puts result.empty? ? 0 : result.join(" ") end
def check(table) result = 0 if table.sum <= 2 table.find {|x| x == 2} else (1..7).each do |i| tmp = table.dup if tmp[i].nonzero? and tmp[i + 1].nonzero? and tmp[i + 2].nonzero? 3.times {|x| tmp[i + x] -= 1} return true if check(tmp) end end (1..9).each do |j| tmp1 = table.dup if tmp1[j] >= 3 tmp1[j] -= 3 return true if check(tmp1) end end false end end $<.readlines.map(&:chomp).map {|n| n.chars.map(&:to_i)}.each do |data| table = Array.new(10, 0) data.each {|i| table[i] += 1} result = (1..9).map do |i| added = table.dup added[i] += 1 next if added[i] > 4 check(added) ? i : nil end.compact puts result.empty? ? 0 : result.join(" ") end
[["-", 0, 652, 486, 652, 486, 652, 486, 652, 486, 22], ["+", 0, 652, 486, 652, 486, 652, 486, 652, 486, 744]]
4
247
2
import sys def pokakito(num_line): if not num_line and flag == 1: result_lis.append(check_num) return True for num in num_line: if three(num, num_line): return True if two(num, num_line): return True if straight(num, num_line): return True def three(num, num_line): count = 0 for check in num_line[:3]: if check == num: count += 1 else: if count == 3: if pokakito(num_line[3:]): return True def two(num, num_line): global flag count = 0 for check in num_line[:2]: if check == num: count += 1 else: if count == 2: flag += 1 if pokakito(num_line[2:]): return True flag -= 1 def straight(num, num_line): num_lis = [num,num+1,num+2] for i in range(3): for check in num_lis: if check < 0 or (not check in num_line): for i in range(3): num_lis[i] = num_lis[i]-1 break else: for n in num_lis: index = 0 while num_line: if num_line[index] == n: del num_line[index] break index += 1 else: if pokakito(num_line): return True flag = 0 result_lis = [] check_num = 0 for input_line in sys.stdin: for i in range(9): check_num = i+1 input_line = input_line.rstrip() line = sorted(input_line + str(check_num)) line = ''.join(line) index = line.find(str(check_num)) if line[index:index+5] == str(check_num)*5: continue pokakito([int(char) for char in line]) result = sorted([str(num) for num in result_lis]) flag = 0 else: if result_lis: print(' '.join(result)) else: print(0) else: flag = 0 result_lis = [] check_num = 0
import sys def pokakito(num_line): if not num_line and flag == 1: result_lis.append(check_num) return True for num in num_line: if three(num, num_line): return True if two(num, num_line): return True if straight(num, num_line): return True def three(num, num_line): count = 0 for check in num_line[:3]: if check == num: count += 1 else: if count == 3: if pokakito(num_line[3:]): return True def two(num, num_line): global flag count = 0 for check in num_line[:2]: if check == num: count += 1 else: if count == 2: flag += 1 if pokakito(num_line[2:]): return True flag -= 1 def straight(num, num_line): num_lis = [num,num+1,num+2] for i in range(3): for check in num_lis: if check < 0 or (not check in num_line): for i in range(3): num_lis[i] = num_lis[i]-1 break else: for n in num_lis: index = 0 while num_line: if num_line[index] == n: del num_line[index] break index += 1 else: if pokakito(num_line): return True flag = 0 result_lis = [] check_num = 0 for input_line in sys.stdin: for i in range(9): check_num = i+1 input_line = input_line.rstrip() line = sorted(input_line + str(check_num)) line = ''.join(line) index = line.find(str(check_num)) if line[index:index+5] == str(check_num)*5: continue pokakito([int(char) for char in line]) result = sorted([str(num) for num in result_lis]) flag = 0 else: if result_lis: print(' '.join(result)) else: print(0) flag = 0 result_lis = [] check_num = 0
[["-", 36, 36, 0, 656, 0, 7, 75, 76, 0, 95], ["-", 36, 36, 0, 656, 0, 7, 75, 76, 0, 102]]
5
418
56
import java.io.*; class Main { public static void main(String args[]) { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String buf; try { while ((buf = br.readLine()) != null) { int n = Integer.parseInt(buf); boolean list[] = new boolean[2 * n + 1]; for (int i = 2; i <= 2 * n; i++) list[i] = true; for (int i = 2; i <= 2 * n; i++) { for (int j = 2; i * j <= 2 * n; j++) { list[i * j] = false; } } for (int i = n; i >= 2; i--) { if (list[i]) { System.out.print(i + " "); break; } } for (int i = n; i <= 2 * n; i++) { if (list[i]) { System.out.println(i); break; } } } } catch (Exception e) { e.printStackTrace(); } } }
import java.io.*; class Main { public static void main(String args[]) { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String buf; try { while ((buf = br.readLine()) != null) { int n = Integer.parseInt(buf); boolean list[] = new boolean[2 * n + 1]; for (int i = 2; i <= 2 * n; i++) list[i] = true; for (int i = 2; i <= 2 * n; i++) { for (int j = 2; i * j <= 2 * n; j++) { list[i * j] = false; } } for (int i = n - 1; i >= 2; i--) { if (list[i]) { System.out.print(i + " "); break; } } for (int i = n + 1; i <= 2 * n; i++) { if (list[i]) { System.out.println(i); break; } } } } catch (Exception e) { e.printStackTrace(); } } }
[["+", 0, 7, 502, 503, 49, 200, 51, 16, 17, 33], ["+", 0, 7, 502, 503, 49, 200, 51, 16, 12, 499], ["+", 0, 7, 502, 503, 49, 200, 51, 16, 17, 72]]
3
251
4
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { static List<Integer> primes = findPrimes(50021); private static List<Integer> findPrimes(int n) { boolean[] isPrime = new boolean[n]; Arrays.fill(isPrime, true); isPrime[0] = false; for (int i = 0; i < isPrime.length; i++) { if (isPrime[i]) { for (int j = 2; (i + 1) * j < isPrime.length; j++) { isPrime[(i + 1) * j - 1] = false; } } } List<Integer> result = new ArrayList<Integer>(); for (int i = 1 - 1; i < isPrime.length; i++) if (isPrime[i]) { result.add(i + 1); } return result; } static int getPrimeLessThan(int n) { int i; for (i = 0; i < primes.size(); i++) { if (primes.get(i) >= n) { break; } } return primes.get(i - 1); } static int getPrimeMoreThan(int n) { int i; for (i = 0; i < primes.size(); i++) { if (primes.get(i) > n) { break; } } return primes.get(i); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { int n = sc.nextInt(); System.out.print(getPrimeLessThan(n) + " " + getPrimeMoreThan(n)); } sc.close(); } }
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { static List<Integer> primes = findPrimes(50021); private static List<Integer> findPrimes(int n) { boolean[] isPrime = new boolean[n]; Arrays.fill(isPrime, true); isPrime[0] = false; for (int i = 0; i < isPrime.length; i++) { if (isPrime[i]) { for (int j = 2; (i + 1) * j < isPrime.length; j++) { isPrime[(i + 1) * j - 1] = false; } } } List<Integer> result = new ArrayList<Integer>(); for (int i = 1 - 1; i < isPrime.length; i++) if (isPrime[i]) { result.add(i + 1); } return result; } static int getPrimeLessThan(int n) { int i; for (i = 0; i < primes.size(); i++) { if (primes.get(i) >= n) { break; } } return primes.get(i - 1); } static int getPrimeMoreThan(int n) { int i; for (i = 0; i < primes.size(); i++) { if (primes.get(i) > n) { break; } } return primes.get(i); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { int n = sc.nextInt(); System.out.println(getPrimeLessThan(n) + " " + getPrimeMoreThan(n)); } sc.close(); } }
[["-", 0, 52, 8, 196, 0, 1, 0, 492, 141, 22], ["+", 0, 52, 8, 196, 0, 1, 0, 492, 141, 22]]
3
383
2
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i, n, j; boolean f[] = new boolean[6000]; for (i = 2; i < f.length; i++) f[i] = true; for (i = 2; i < f.length; i++) { if (f[i]) { for (j = 2; j * i < f.length; j++) { f[j * i] = false; } } } // hurui // for(i=2;i<f.length;i++){ // if(f[i]) System.out.println(i); // } while (sc.hasNext()) { n = sc.nextInt(); for (i = n - 1;; i--) { if (f[i]) { System.out.print(i + " "); break; } } // for for (j = n + 1;; j++) { if (f[j]) { System.out.println(j); break; } } } // while } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i, n, j; boolean f[] = new boolean[60000]; for (i = 2; i < f.length; i++) f[i] = true; for (i = 2; i < f.length; i++) { if (f[i]) { for (j = 2; j * i < f.length; j++) { f[j * i] = false; } } } // hurui while (sc.hasNext()) { n = sc.nextInt(); for (i = n - 1;; i--) { if (f[i]) { System.out.print(i + " "); break; } } // for for (j = n + 1;; j++) { if (f[j]) { System.out.println(j); break; } } } // while } }
[["-", 0, 503, 49, 200, 51, 227, 497, 505, 0, 499], ["+", 0, 503, 49, 200, 51, 227, 497, 505, 0, 499]]
3
225
2
import java.util.*; class Main { Scanner sc = new Scanner(System.in); public boolean isPrime(int n) { if (n < 2) return false; for (int i = 2; i * i <= n; i++) { if (n % 2 == 0) return false; } return true; } public void run() { while (sc.hasNext()) { int val = sc.nextInt(); int lessP = 0; int greatP = 0; for (int i = val - 1;; i--) { if (isPrime(i)) { lessP = i; break; } } for (int i = val + 1;; i++) { if (isPrime(i)) { greatP = i; break; } } ln(lessP + " " + greatP); } } public static void main(String[] args) { new Main().run(); } public static void pr(Object o) { System.out.print(o); } public static void ln(Object o) { System.out.println(o); } public static void ln() { System.out.println(); } }
import java.util.*; class Main { Scanner sc = new Scanner(System.in); public boolean isPrime(int n) { if (n < 2) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } public void run() { while (sc.hasNext()) { int val = sc.nextInt(); int lessP = 0; int greatP = 0; for (int i = val - 1;; i--) { if (isPrime(i)) { lessP = i; break; } } for (int i = val + 1;; i++) { if (isPrime(i)) { greatP = i; break; } } ln(lessP + " " + greatP); } } public static void main(String[] args) { new Main().run(); } public static void pr(Object o) { System.out.print(o); } public static void ln(Object o) { System.out.println(o); } public static void ln() { System.out.println(); } }
[["-", 0, 57, 15, 15, 0, 16, 31, 16, 12, 499], ["+", 0, 57, 15, 15, 0, 16, 31, 16, 12, 22]]
3
254
2
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String arg[]) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); boolean a[] = new boolean[n + n]; Arrays.fill(a, true); a[0] = a[1] = false; for (int i = 2; i * i <= n + n; i++) { if (!a[i]) continue; for (int j = i + i; j <= n + n; j += i) { a[j] = false; } } for (int i = n - 1; i >= 0; i--) { if (a[i]) { System.out.print(i); break; } } for (int i = n + 1; i <= n + n; i++) { if (a[i]) { System.out.println(" " + i); break; } } } } }
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String arg[]) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); boolean a[] = new boolean[n + n + 1]; Arrays.fill(a, true); a[0] = a[1] = false; for (int i = 2; i * i <= n + n; i++) { if (!a[i]) continue; for (int j = i + i; j <= n + n; j += i) { a[j] = false; } } for (int i = n - 1; i >= 0; i--) { if (a[i]) { System.out.print(i); break; } } for (int i = n + 1; i <= n + n; i++) { if (a[i]) { System.out.println(" " + i); break; } } } } }
[["+", 49, 200, 51, 227, 497, 505, 0, 16, 17, 72], ["+", 49, 200, 51, 227, 497, 505, 0, 16, 12, 499]]
3
235
2
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int num = sc.nextInt(); for (int i = num - 1; i >= 0; i--) { if (i != 2 && i % 2 == 0) continue; boolean ok = true; for (int j = 3; j <= Math.sqrt(i); j++) { if (i % j == 0) { ok = false; break; } } if (ok) { System.out.print(i + " "); break; } } for (int i = num + 1;; i++) { if (i != 2 && i % 2 == 0) continue; boolean ok = true; for (int j = 3; j <= Math.sqrt(i); j++) { if (i % j == 0) { ok = false; break; } } if (ok) { System.out.print(i); break; } } } sc.close(); } }
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int num = sc.nextInt(); for (int i = num - 1; i >= 0; i--) { if (i != 2 && i % 2 == 0) continue; boolean ok = true; for (int j = 3; j <= Math.sqrt(i); j++) { if (i % j == 0) { ok = false; break; } } if (ok) { System.out.print(i + " "); break; } } for (int i = num + 1;; i++) { if (i != 2 && i % 2 == 0) continue; boolean ok = true; for (int j = 3; j <= Math.sqrt(i); j++) { if (i % j == 0) { ok = false; break; } } if (ok) { System.out.println(i); break; } } } sc.close(); } }
[["-", 0, 57, 64, 196, 0, 1, 0, 492, 141, 22], ["+", 0, 57, 64, 196, 0, 1, 0, 492, 141, 22]]
3
243
2
#include <math.h> #include <stdio.h> int prime(long x) { int key = 1; long i; for (i = 3; i <= sqrt(x); i += 2) { if (x % i == 0) { key = 0; break; } } return key; } int main(void) { long n; while (scanf(" %ld", &n) != EOF) { long a = n, b = n; if (n % 2 == 0) { --a; ++b; } a -= 2; b += 2; if (n == 3) { a = 2; b = 5; } else { while (prime(a) != 1) a -= 2; while (prime(b) != 1) b += 2; } printf("%ld %ld\n", a, b); } return 0; }
#include <math.h> #include <stdio.h> int prime(long x) { int key = 1; long i; for (i = 3; i <= sqrt(x); i += 2) { if (x % i == 0) { key = 0; break; } } return key; } int main(void) { long n; while (scanf(" %ld", &n) != EOF) { long a = n, b = n; if (n % 2 == 0) { --a; ++b; } else { a -= 2; b += 2; } if (n == 3) { a = 2; b = 5; } else { while (prime(a) != 1) a -= 2; while (prime(b) != 1) b += 2; } printf("%ld %ld\n", a, b); } return 0; }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95], ["+", 8, 9, 0, 57, 75, 76, 0, 9, 0, 45], ["+", 8, 9, 0, 57, 75, 76, 0, 9, 0, 46]]
0
177
3
#include <stdio.h> #define MAX 50001 int n; int p[MAX]; int prime[1000]; int main() { int i, j, count = 0; for (i = 0; i < MAX; i++) { p[i] = 1; } for (i = 2; i * i < MAX; i++) { if (p[i] == 1) { for (j = i + i; j < MAX; j = j + i) { p[j] = 0; } } } for (i = 2; i < MAX; i++) { if (p[i] == 1) { prime[count] = i; count++; } } while (scanf("%d", &n) != EOF) { for (i = 0; i < count; i++) { if (n == prime[i]) { printf("%d %d\n", prime[i - 1], prime[i + 1]); break; } if (n < prime[i]) { printf("%d %d\n", prime[i - 1], prime[i]); break; } } } return 0; }
#include <stdio.h> #define MAX 50100 int n; int p[MAX]; int prime[10000]; int main() { int i, j, count = 0; for (i = 0; i < MAX; i++) { p[i] = 1; } for (i = 2; i * i < MAX; i++) { if (p[i] == 1) { for (j = i + i; j < MAX; j = j + i) { p[j] = 0; } } } for (i = 2; i < MAX; i++) { if (p[i] == 1) { prime[count] = i; count++; } } while (scanf("%d", &n) != EOF) { for (i = 0; i < count; i++) { if (n == prime[i]) { printf("%d %d\n", prime[i - 1], prime[i + 1]); break; } if (n < prime[i]) { printf("%d %d\n", prime[i - 1], prime[i]); break; } } } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13]]
0
250
4
#include <cstdio> using namespace std; int main() { int i, n, m, total = 0, num = 0; for (i = 1; scanf("%d,%d", &n, &m) != EOF; i++) { total += n * m; num += m; } printf("%d\n%d\n", total, (int)(num / (double)i + 0.5)); }
#include <cstdio> using namespace std; int main() { int i, n, m, total = 0, num = 0; for (i = 0; scanf("%d,%d", &n, &m) != EOF; i++) { total += n * m; num += m; } printf("%d\n%d\n", total, (int)(num / (double)i + 0.5)); }
[["-", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13]]
1
89
2
#include <iostream> using namespace std; int main() { int sum = 0, ave = 0; int i; int n, m; for (i = 0; cin >> n; ++i) { char a; cin >> a >> m; sum += n; ave += m; } if (i != 0) { double a; a = double(ave) / i + 0.5; ave = a; cout << sum << "\n" << ave << endl; } else cout << sum << "\n" << ave << endl; }
#include <iostream> using namespace std; int main() { int sum = 0, ave = 0; int i; int n, m; for (i = 0; cin >> n; ++i) { char a; cin >> a >> m; sum += n * m; ave += m; } if (i != 0) { double a; a = double(ave) / i + 0.5; ave = a; cout << sum << "\n" << ave << endl; } else cout << sum << "\n" << ave << endl; }
[["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 48], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 22]]
1
112
2
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int main() { int n = 0; int x = 0; int y = 0; for (;;) { int a, b; char c; if (!(cin >> a >> c >> b)) break; ++n; x += a; y += b; } cout << x << endl; cout << (y + n / 2) / n << endl; return 0; }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int main() { int n = 0; int x = 0; int y = 0; for (;;) { int a, b; char c; if (!(cin >> a >> c >> b)) break; ++n; x += a * b; y += b; } cout << x << endl; cout << (y + n / 2) / n << endl; return 0; }
[["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 48], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 22]]
1
121
2
#include <stdio.h> int main() { int kiro, s, ksum, ssum, n; double h; ksum = ssum = n = 0; while (scanf("%d,%d", &kiro, &s) != EOF) { ksum += kiro * s; s++; ssum += s; } h = (double)ssum / (double)s; if ((h - (double)((int)h)) >= 0.5) s = (int)h + 1; else s = (int)h; printf("%d\n%d\n", ksum, s); return 0; }
#include <stdio.h> int main() { int kiro, s, ksum, ssum, n; double h; ksum = ssum = n = 0; while (scanf("%d,%d", &kiro, &s) != EOF) { ksum += kiro * s; n++; ssum += s; } h = (double)ssum / (double)n; if ((h - (double)((int)h)) >= 0.5) s = (int)h + 1; else s = (int)h; printf("%d\n%d\n", ksum, s); return 0; }
[["-", 0, 52, 8, 9, 0, 1, 0, 27, 28, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 27, 28, 22], ["-", 0, 1, 0, 11, 12, 16, 12, 74, 51, 22], ["+", 0, 1, 0, 11, 12, 16, 12, 74, 51, 22]]
1
126
4
#include <cstdio> #include <iostream> using namespace std; int main() { int value, num, i = 0, total = 0, ave = 0; while (scanf("%d, %d", &value, &num) != EOF) { total += value * num; ave += num; i++; } cout << total << endl << (int)(ave / i + 0.5) << endl; return 0; }
#include <cstdio> #include <iostream> using namespace std; int main() { int value, num, i = 0, total = 0, ave = 0; while (scanf("%d, %d", &value, &num) != EOF) { total += value * num; ave += num; i++; } cout << total << endl << (int)(ave / (double)i + 0.5) << endl; return 0; }
[["+", 51, 23, 0, 16, 31, 16, 12, 74, 0, 24], ["+", 0, 16, 31, 16, 12, 74, 39, 77, 39, 40], ["+", 51, 23, 0, 16, 31, 16, 12, 74, 0, 25]]
1
85
3
#include <iostream> using namespace std; int main(void) { int value, num; int sum = 0, ave = 0, row = 0; while (cin >> value >> num) { sum += value * num; ave += num; row++; } cout << sum << endl << (int)(ave / (double)row + 0.5) << endl; return 0; }
#include <iostream> using namespace std; int main(void) { int value, num; int sum = 0, ave = 0, row = 0; char a; while (cin >> value >> a >> num) { sum += value * num; ave += num; row++; } cout << sum << endl << (int)(ave / (double)row + 0.5) << endl; return 0; }
[["+", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 49, 22], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 0, 35], ["+", 0, 52, 15, 339, 51, 16, 31, 16, 17, 152], ["+", 0, 52, 15, 339, 51, 16, 31, 16, 12, 22]]
1
79
5
#include <cstdio> #include <iostream> using namespace std; int main() { int price, num, sump = 0, sum = 0, i = 0; double ave; while (scanf("%d,%d", &price, &num) != EOF) { sump += price; sum += num; i++; } ave = sum / (double)i; ave += 0.5; cout << sump << endl << (int)ave << endl; return 0; }
#include <cstdio> #include <iostream> using namespace std; int main() { int price, num, sump = 0, sum = 0, i = 0; double ave; while (scanf("%d,%d", &price, &num) != EOF) { sump += price * num; sum += num; i++; } ave = sum / (double)i; ave += 0.5; cout << sump << endl << (int)ave << endl; return 0; }
[["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 48], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 22]]
1
93
2
total = [] average = [] while tmp = gets break if tmp == nil p tmp = gets.chomp.split(",").map(&:to_i) # puts tmp.split(",").map(&:to_i) total << tmp[0] * tmp[1] average << tmp[1] end puts total.inject(:+) puts (average.inject(:+)/average.size).round
total = [] average = [] while tmp = gets break if tmp == nil tmp = tmp.chomp.split(",").map(&:to_i) # puts tmp.split(",").map(&:to_i) total << tmp[0] * tmp[1] average << tmp[1] end puts total.inject(:+) puts (average.inject(:+).to_f/average.size).round
[["-", 0, 493, 0, 89, 8, 170, 0, 652, 735, 22], ["-", 0, 662, 12, 652, 486, 652, 486, 652, 486, 22], ["+", 0, 662, 12, 652, 486, 652, 486, 652, 486, 22], ["+", 0, 652, 486, 739, 0, 738, 31, 652, 17, 131], ["+", 0, 652, 486, 739, 0, 738, 31, 652, 735, 22]]
4
77
5
def main sum = 0 sumnum = 0 count = 0 while arg = gets arg = arg.split(",").map(&:to_i) sum += arg[0] * arg[1] sumnum += arg[0] count += 1 end puts sum, (sumnum.to_f / count).round end main
def main sum = 0 sumnum = 0 count = 0 while arg = gets arg = arg.split(",").map(&:to_i) sum += arg[0] * arg[1] sumnum += arg[1] count += 1 end puts sum, (sumnum.to_f / count).round end main
[["-", 0, 89, 8, 170, 0, 755, 12, 742, 0, 612], ["+", 0, 89, 8, 170, 0, 755, 12, 742, 0, 612]]
4
66
2
A = [] B = [] while True: try: x,y = map(int,input().split(',')) A.append(x) B.append(y) except EOFError: break N = len(A) print(sum(A [i] * B [i] for i in range(N)),sum(B) * 2 // N - sum(B) // N)
A = [] B = [] while True: try: x,y = map(int,input().split(',')) A.append(x) B.append(y) except EOFError: break N = len(A) print(sum(A [i] * B [i] for i in range(N))) print(sum(B) * 2 // N - sum(B) // N)
[["-", 0, 656, 0, 1, 0, 652, 3, 4, 0, 21], ["+", 0, 656, 0, 1, 0, 652, 3, 4, 0, 25], ["+", 36, 36, 0, 656, 0, 1, 0, 652, 63, 22], ["+", 0, 656, 0, 1, 0, 652, 3, 4, 0, 24]]
5
92
4
import sys s=[list(map(int,e.split(',')))for e in sys.stdin] print(sum(a*n for a,n in s),int([sum(x)for x in zip(*s)][1]/len(s)+.5))
import sys s=[list(map(int,e.split(',')))for e in sys.stdin] print(sum(a*n for a,n in s)) print(int([sum(x)for x in zip(*s)][1]/len(s)+.5))
[["-", 0, 656, 0, 1, 0, 652, 3, 4, 0, 21], ["+", 0, 656, 0, 1, 0, 652, 3, 4, 0, 25], ["+", 36, 36, 0, 656, 0, 1, 0, 652, 63, 22], ["+", 0, 656, 0, 1, 0, 652, 3, 4, 0, 24]]
5
71
4
i, all, sum = 0, 0, 0 while True: try: price, n = list(map(int, input().split(","))) i += 1; all += n; sum += price except: break print(sum) print(int(round(float(all)/i)))
i, all, sum = 0, 0, 0 while True: try: price, n = list(map(int, input().split(","))) i += 1; all += n; sum += price*n except: break print(sum) print(int(round(float(all)/i)))
[["+", 8, 196, 0, 1, 0, 677, 12, 657, 17, 48], ["+", 8, 196, 0, 1, 0, 677, 12, 657, 12, 22]]
5
71
2
import static java.lang.Math.*; import static java.util.Arrays.*; import java.io.*; import java.lang.*; import java.math.*; import java.util.*; public class Main { Scanner sc; static final int INF = 1 << 28; static final double EPS = 1e-9; void run() { sc = new Scanner(System.in); double max = 0, min = Double.MAX_VALUE; for (; sc.hasNext();) { double h = sc.nextDouble(); max = max(max, h); min = min(min, h); break; } println("" + (max - min)); sc.close(); } void print(String s) { System.out.print(s); } void println(String s) { System.out.println(s); } public static void main(String[] args) { new Main().run(); } }
import static java.lang.Math.*; import static java.util.Arrays.*; import java.io.*; import java.lang.*; import java.math.*; import java.util.*; public class Main { Scanner sc; static final int INF = 1 << 28; static final double EPS = 1e-9; void run() { sc = new Scanner(System.in); double max = 0, min = Double.MAX_VALUE; for (; sc.hasNext();) { double h = sc.nextDouble(); max = max(max, h); min = min(min, h); } println("" + (max - min)); sc.close(); } void print(String s) { System.out.print(s); } void println(String s) { System.out.println(s); } public static void main(String[] args) { new Main().run(); } }
[["-", 8, 196, 0, 7, 8, 196, 0, 93, 0, 94], ["-", 8, 196, 0, 7, 8, 196, 0, 93, 0, 35]]
3
213
2
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { private static double x; private static double max; private static double min; private static String val; public static void main(String args[]) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); max = 0; min = 0; while ((val = br.readLine()) != null) { x = Double.parseDouble(val); if (x > max) { max = x; } if (x < min) { min = x; } } System.out.println(max - min); } }
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { private static double x; private static double max; private static double min; private static String val; public static void main(String args[]) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); max = 0; min = 1000000000; while ((val = br.readLine()) != null) { x = Double.parseDouble(val); if (x > max) { max = x; } if (x < min) { min = x; } } System.out.println(max - min); } }
[["-", 0, 195, 8, 196, 0, 1, 0, 11, 12, 499], ["+", 0, 195, 8, 196, 0, 1, 0, 11, 12, 499]]
3
136
2
#include <stdio.h> int main(void) { double max = 0, a, min = 0; while (scanf("%lf", &a) != EOF) { if (max < a) max = a; if (min > a) min = a; } printf("%lf\n", max - min); return 0; }
#include <stdio.h> int main(void) { double max = 0, a, min = 10000; while (scanf("%lf", &a) != EOF) { if (max < a) max = a; if (min > a) min = a; } printf("%lf\n", max - min); return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
0
71
2
#include <stdio.h> int main() { float max, min, m[10000]; int i, count = 0; while (scanf("%f", &m[i]) != EOF) { count++; } max = min = m[0]; for (i = 1; i < count; i++) { if (max < m[i]) max = m[i]; if (min > m[i]) min = m[i]; } printf("%g\n", max - min); return 0; }
#include <stdio.h> int main() { float max, min, m[10000]; int i, count = 0; while (scanf("%f", &m[count]) != EOF) { count++; } max = min = m[0]; for (i = 1; i < count; i++) { if (max < m[i]) max = m[i]; else if (min > m[i]) min = m[i]; } printf("%g\n", max - min); return 0; }
[["-", 31, 2, 3, 4, 0, 66, 28, 69, 71, 22], ["+", 31, 2, 3, 4, 0, 66, 28, 69, 71, 22], ["+", 0, 7, 8, 9, 0, 57, 75, 76, 0, 95]]
0
118
3
#include <stdio.h> int main(void) { float h, l, n; int y = 0; while (scanf("%f", &n) != EOF) { if (y = 0) { h = n; l = n; y = 1; } else { if (h < n) h = n; if (l > n) l = n; } } printf("%f\n", h - l); return 0; }
#include <stdio.h> int main(void) { float h, l, n; int y = 0; while (scanf("%f", &n) != EOF) { if (y == 0) { h = n; l = n; y = 1; } else { if (h < n) h = n; if (l > n) l = n; } } printf("%f\n", h - l); return 0; }
[["-", 8, 9, 0, 57, 15, 23, 0, 11, 17, 32], ["+", 8, 9, 0, 57, 15, 23, 0, 16, 17, 60]]
0
95
2
#include <stdio.h> int main() { int x, min, max; min = 10000000; max = 0; while (scanf("%lf", &x) != EOF) { if (max < x) max = x; if (min > x) min = x; } printf("%.2f\n", max - min); return 0; }
#include <stdio.h> int main() { double x, min, max; min = 10000000; max = 0; while (scanf("%lf", &x) != EOF) { if (max < x) max = x; if (min > x) min = x; } printf("%.2f\n", max - min); return 0; }
[["-", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40]]
0
74
2
#include <stdio.h> int main(void) { double h[1000], Max, Min; int i = 1; scanf("%lf", &h[0]); Max = Min = h[0]; while (scanf("%lf", &h[i]) != EOF) { if (h[i] <= Max) Max = h[i]; if (h[i] >= Min) Min = h[i]; i++; } printf("%.1lf", Max - Min); return 0; }
#include <stdio.h> int main(void) { double h[1000], Max, Min; int i = 1; scanf("%lf", &h[0]); Max = Min = h[0]; while (scanf("%lf", &h[i]) != EOF) { if (h[i] >= Max) Max = h[i]; if (h[i] <= Min) Min = h[i]; i++; } printf("%.1lf", Max - Min); return 0; }
[["-", 8, 9, 0, 57, 15, 23, 0, 16, 17, 19], ["+", 8, 9, 0, 57, 15, 23, 0, 16, 17, 20], ["-", 8, 9, 0, 57, 15, 23, 0, 16, 17, 20], ["+", 8, 9, 0, 57, 15, 23, 0, 16, 17, 19]]
0
114
4
#include <stdio.h> int main(void) { double lo, hi, a; scanf("%f", &a); lo = hi = a; while (scanf("%f", &a) != EOF) { if (a > hi) hi = a; else if (a < lo) lo = a; } printf("%ld", hi - lo); return 0; }
#include <stdio.h> int main(void) { float lo, hi, a; scanf("%f", &a); lo = hi = a; while (scanf("%f", &a) != EOF) { if (a > hi) hi = a; else if (a < lo) lo = a; } printf("%.1f", hi - lo); return 0; }
[["-", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
83
4
#include <stdio.h> int main(void) { double t, ans, max, min; int i; max = -1.0; min = -1.0; while ((scanf("%lf", &t)) != EOF) { if (t > max || max < 0) max = t; if (t < min || max < 0) max = t; } printf("%lf\n", max - min); return 0; }
#include <stdio.h> int main(void) { double t, ans, max, min; int i; max = -1.0; min = -1.0; while ((scanf("%lf", &t)) != EOF) { if (t > max || max < 0) max = t; if (t < min || min < 0) min = t; } printf("%lf\n", max - min); return 0; }
[["-", 0, 57, 15, 23, 0, 16, 12, 16, 31, 22], ["+", 0, 57, 15, 23, 0, 16, 12, 16, 31, 22], ["-", 8, 9, 0, 57, 64, 1, 0, 11, 31, 22], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 31, 22]]
0
90
4
#include <stdio.h> int main(void) { int flag = 0; float max, min; float hill; while (scanf("%f", &hill) != EOF) { if (flag == 0) { max = min = hill; flag++; } if (max < hill) { max = hill; } else if (min > hill) { min = hill; } } printf("%1.f\n", max - min); return (0); }
#include <stdio.h> int main(void) { int flag = 0; float max, min; float hill; while (scanf("%f", &hill) != EOF) { if (flag == 0) { max = min = hill; flag++; } if (max < hill) { max = hill; } else if (min > hill) { min = hill; } } printf("%f\n", max - min); return (0); }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
97
2
#include <stdio.h> int main() { double max, min; double n; scanf("%lf", &max); min = max; while (~scanf("%lf", &n)) { if (min < n) min = n; else if (max > n) max = n; } printf("%lf\n", max - min); return 0; }
#include <stdio.h> int main() { double max, min; double n; scanf("%lf", &max); min = max; while (~scanf("%lf", &n)) { if (min > n) min = n; else if (max < n) max = n; } printf("%lf\n", max - min); return 0; }
[["-", 8, 9, 0, 57, 15, 23, 0, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 23, 0, 16, 17, 47], ["-", 75, 76, 0, 57, 15, 23, 0, 16, 17, 47], ["+", 75, 76, 0, 57, 15, 23, 0, 16, 17, 18]]
0
81
4
#include <stdio.h> int main() { int counter = 0; double yama = 0; double max = 0; double low = 0; while (scanf("%lf", &yama) != EOF) { if (counter == 0) { max = yama; low = yama; } if (max < yama) { max = yama; } if (low > yama) { low = yama; } } printf("%f\n", max - low); return 0; }
#include <stdio.h> int main() { int counter = 0; double yama = 0; double max = 0; double low = 0; while (scanf("%lf", &yama) != EOF) { if (counter == 0) { max = yama; low = yama; } if (max < yama) { max = yama; } if (low > yama) { low = yama; } counter++; } printf("%f\n", max - low); return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 27, 28, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 27, 17, 29], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
0
99
3
#include <stdio.h> int main() { long double n; long double high, low; long double ans; scanf("%lf", &n); high = n; low = n; while (scanf("%lf", &n) != EOF) { if (high < n) { high = n; } if (low > n) { low = n; } } ans = high - low; printf("%lf\n", ans); return 0; }
#include <stdio.h> int main() { double n; double high, low; double ans; scanf("%lf", &n); high = n; low = n; while (scanf("%lf", &n) != EOF) { if (high < n) { high = n; } if (low > n) { low = n; } } ans = high - low; printf("%f\n", ans); return 0; }
[["-", 0, 14, 8, 9, 0, 43, 39, 86, 0, 96], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
99
5
#include <algorithm> #include <iostream> using namespace std; int main(void) { ios::sync_with_stdio(false); double maxh = 114514810, minh = -1; double h; while (cin >> h) { maxh = max(maxh, h); minh = min(minh, h); } cout << maxh - minh << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int main(void) { ios::sync_with_stdio(false); double maxh = -1, minh = 114514810; double h; while (cin >> h) { maxh = max(maxh, h); minh = min(minh, h); } cout << maxh - minh << endl; return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
1
71
4
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; void Solution() { int n = 0; double a[n]; while (cin >> a[n]) { n++; } // for (int i = 0; i < n - 1; i++) // { // for (int j = i + 1; j < n; j++) // { // if (a[i] < a[j]) // { // double temp = a[i]; //swap // a[i] = a[j]; // a[j] = temp; // } // } // } sort(a, a + n); // sorting as ascedning sequence // cout.setf(ios::fixed); //"setf" is used for “half-adjusting” // cout.precision(2); // cout<<a[0] - a[n - 1]<<endl; cout << a[n - 1] - a[0] << endl; } int main() { Solution(); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; void Solution() { int n = 0; double a[100]; // notice the definition. while (cin >> a[n]) { n++; } // for (int i = 0; i < n - 1; i++) // { // for (int j = i + 1; j < n; j++) // { // if (a[i] < a[j]) // { // double temp = a[i]; //swap // a[i] = a[j]; // a[j] = temp; // } // } // } sort(a, a + n); // sorting as ascedning sequence // cout.setf(ios::fixed); //"setf" is used for “half-adjusting” // cout.precision(2); // cout<<a[0] - a[n - 1]<<endl; cout << a[n - 1] - a[0] << endl; } int main() { Solution(); return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 80, 81, 22], ["+", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13]]
1
97
2
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <string> #include <vector> #define shosu(x) fixed << setprecision(x) using namespace std; typedef pair<int, int> P; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, P> pip; typedef long long ll; const int inf = (1 << 31) - 1; const double pi = acos(-1); double a, M = -1, m = -1; int main() { while (cin >> a) { M = max(M, a); m = min(m, a); } cout << M - m << endl; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <string> #include <vector> #define shosu(x) fixed << setprecision(x) using namespace std; typedef pair<int, int> P; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, P> pip; typedef long long ll; const int inf = (1 << 31) - 1; const double pi = acos(-1); double a, M = -1, m = inf; int main() { while (cin >> a) { M = max(M, a); m = min(m, a); } cout << M - m << endl; }
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 22]]
1
133
2
#include <iostream> using namespace std; int main() { float max = 0, min = 1000000, in; while (true) { cin >> in; if (cin.eof()) { break; } if (max < in) { max = in; } else if (min > in) { min = in; } cout << max - min << endl; } return 0; }
#include <iostream> using namespace std; int main() { float max = 0, min = 1000000, in; while (true) { cin >> in; if (cin.eof()) { break; } if (max < in) { max = in; } else if (min > in) { min = in; } } cout << max - min << endl; return 0; }
[["+", 0, 14, 8, 9, 0, 52, 8, 9, 0, 46], ["-", 0, 14, 8, 9, 0, 52, 8, 9, 0, 46]]
1
81
2
#include <algorithm> #include <iostream> using namespace std; int main() { double high[50] = {}; int i = 0; while (cin >> high[i]) { i++; } sort(high, high + i); cout << high[i] - high[0] << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int main() { double high[50] = {}; int i = 0; while (cin >> high[i]) { i++; } sort(high, high + i); cout << high[i - 1] - high[0] << endl; return 0; }
[["+", 12, 16, 31, 69, 341, 342, 0, 16, 17, 33], ["+", 12, 16, 31, 69, 341, 342, 0, 16, 12, 13]]
1
68
2
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { double n, a, b; vector<double> v; while (cin >> n) { v.push_back(n); } a = v[0]; b = v[0]; for (int i = 0; i < n; i++) { a = max(a, v[i]); b = min(b, v[i]); } double ans = a - b; cout << ans << endl; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { double n, a, b; vector<double> v; while (cin >> n) { v.push_back(n); } a = v[0]; b = v[0]; for (int i = 0; i < v.size(); i++) { a = max(a, v[i]); b = min(b, v[i]); } double ans = a - b; cout << ans << endl; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 15, 16, 12, 2, 63, 118, 28, 22], ["+", 0, 7, 15, 16, 12, 2, 63, 118, 17, 131], ["+", 0, 7, 15, 16, 12, 2, 63, 118, 119, 120], ["+", 0, 7, 15, 16, 12, 2, 3, 4, 0, 24], ["+", 0, 7, 15, 16, 12, 2, 3, 4, 0, 25]]
1
111
6
#include <iostream> using namespace std; int main() { double max = -1.0, min = 10000.0; double tmp; while (cin >> tmp) { if (max < tmp) max = tmp; if (min > tmp) min = tmp; } printf("%ld\n", max - min); return 0; }
#include <iostream> using namespace std; int main() { double max = -1.0, min = 10000.0; double tmp; while (cin >> tmp) { if (max < tmp) max = tmp; if (min > tmp) min = tmp; } printf("%lf\n", max - min); return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
67
2
#include <algorithm> #include <iostream> using namespace std; int main() { double x; cin >> x; double minimum = x, maximum = x; while (cin >> x) { minimum = min(minimum, x); maximum = max(maximum, x); } cout << maximum - maximum << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int main() { double x; cin >> x; double minimum = x, maximum = x; while (cin >> x) { minimum = min(minimum, x); maximum = max(maximum, x); } cout << maximum - minimum << endl; return 0; }
[["-", 0, 1, 0, 16, 31, 16, 12, 16, 12, 22], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 12, 22]]
1
67
2
from sys import * maxnum = 60000 primes = [True]*maxnum primes[0] = primes[1] = False for i in range(maxnum): if i >= maxnum**0.5 : break if not primes[i]: continue for j in range(i*2,maxnum,i): primes[j] = False for s in stdin: d = int(s) for i in reversed(range(2,d-1)): if primes[i]: print(i, end=' '); break for i in range(d+1,maxnum): if primes[i]: print(i);break
from sys import * maxnum = 60000 primes = [True]*maxnum primes[0] = primes[1] = False for i in range(maxnum): if i >= maxnum**0.5 : break if not primes[i]: continue for j in range(i*2,maxnum,i): primes[j] = False for s in stdin: d = int(s) for i in reversed(range(2,d)): if primes[i]: print(i, end=' '); break for i in range(d+1,maxnum): if primes[i]: print(i);break
[["-", 3, 4, 0, 652, 3, 4, 0, 657, 17, 33], ["-", 3, 4, 0, 652, 3, 4, 0, 657, 12, 612]]
5
137
2
def sieve(n): p=[True]*(n+1) p[0]=p[1]=False for i in range(2,int((n+1)*0.5)+1): if p[i]==True: for j in range(i*i,n+1,i): p[j]=False prime=[] for i in range(n+1): if p[i]==True: prime.append(i) return prime def solve(n): i=0 while True: if n>prime[i]: a=prime[i] elif n==prime[i]: a=prime[i-1] else: b=prime[i] break i+=1 print(a,b) prime=sieve(5003) while True: try: n=int(input()) solve(n) except EOFError: break
def sieve(n): p=[True]*(n+1) p[0]=p[1]=False for i in range(2,int((n+1)*0.5)+1): if p[i]==True: for j in range(i*i,n+1,i): p[j]=False prime=[] for i in range(n+1): if p[i]==True: prime.append(i) return prime def solve(n): i=0 while True: if n>prime[i]: a=prime[i] elif n==prime[i]: a=prime[i-1] else: b=prime[i] break i+=1 print(a,b) prime=sieve(50021) while True: try: n=int(input()) solve(n) except EOFError: break
[["-", 0, 1, 0, 662, 12, 652, 3, 4, 0, 612], ["+", 0, 1, 0, 662, 12, 652, 3, 4, 0, 612]]
5
196
2
import bisect MAX = 60000 #エラトステネスの篩 is_prime = [True for _ in range(MAX)] is_prime[0] = is_prime[1] = False for i in range(2, int(MAX ** (1 / 2)) + 1): if is_prime[i]: for j in range(i ** 2, MAX, i): is_prime[j] = False primes = [i for i in range(MAX) if is_prime[i]] while True: try: n = int(input()) ind = bisect.bisect_left(primes, n) if primes[ind] == n: print(primes[ind - 1], primes[ind + 1]) else: print(primes[ind], primes[ind + 1]) except EOFError: break
import bisect MAX = 60000 #エラトステネスの篩 is_prime = [True for _ in range(MAX)] is_prime[0] = is_prime[1] = False for i in range(2, int(MAX ** (1 / 2)) + 1): if is_prime[i]: for j in range(i ** 2, MAX, i): is_prime[j] = False primes = [i for i in range(MAX) if is_prime[i]] while True: try: n = int(input()) ind = bisect.bisect_left(primes, n) if primes[ind] == n: print(primes[ind - 1], primes[ind + 1]) else: print(primes[ind - 1], primes[ind]) except EOFError: break
[["+", 0, 652, 3, 4, 0, 206, 206, 657, 17, 33], ["+", 0, 652, 3, 4, 0, 206, 206, 657, 12, 612], ["-", 0, 652, 3, 4, 0, 206, 206, 657, 17, 72], ["-", 0, 652, 3, 4, 0, 206, 206, 657, 12, 612]]
5
160
4