- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- int main(){
- int n, mx, sz;
- string file;
- vector <string> srt;
- while(cin >> n){
- mx = 0;
- for(int i = 1; i <= n; i++){
- cin >> file, srt.push_back(file);
- sz = file.size();
- if(sz > mx)mx = sz;
- }
- sort(srt.begin(), srt.end());
- int row, col, temp;
- col = 62 / (mx+2);
- row = ceil(n/(double)col);
- for(int i = 1; i <= 60; i++)cout << "-";
- cout << endl;
- for(int i = 0; i < row; i++){
- for(int k = i; k < n; k += row){
- cout << srt[k];
- for(int m = 1; m <= (mx-(srt[k].size()))+2; m++)cout << " ";
- }
- cout << endl;
- }
- srt.clear();
- }
- return 0;
- }
Sunday, September 28, 2014
UVa - 400 - Unix ls
UVa - 357 - Let Me Count The Ways
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int Coin[] = {1, 5, 10, 25, 50};
long long dp[7][30003];
long long N;
long long makeCoin(long long I, long long H){
if(I >= 5){
if(H == 0)return 1;
else return 0;
}
if(dp[I][H] != -1)return dp[I][H];
long long Way1 = 0, Way2 = 0;
if(H-Coin[I] >= 0)Way1 = makeCoin(I, H-Coin[I]);
Way2 = makeCoin(I+1, H);
return dp[I][H] = Way1+Way2;
}
int main(){
memset(dp, -1, sizeof(dp));
while(cin >> N){
long long ans = makeCoin(0, N);
if(ans == 1)cout << "There is only 1 way to produce " << N << " cents change." << endl;
else cout << "There are " << ans << " ways to produce " << N << " cents change." << endl;
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int Coin[] = {1, 5, 10, 25, 50};
long long dp[7][30003];
long long N;
long long makeCoin(long long I, long long H){
if(I >= 5){
if(H == 0)return 1;
else return 0;
}
if(dp[I][H] != -1)return dp[I][H];
long long Way1 = 0, Way2 = 0;
if(H-Coin[I] >= 0)Way1 = makeCoin(I, H-Coin[I]);
Way2 = makeCoin(I+1, H);
return dp[I][H] = Way1+Way2;
}
int main(){
memset(dp, -1, sizeof(dp));
while(cin >> N){
long long ans = makeCoin(0, N);
if(ans == 1)cout << "There is only 1 way to produce " << N << " cents change." << endl;
else cout << "There are " << ans << " ways to produce " << N << " cents change." << endl;
}
return 0;
}
UVa - 294 - Divisors
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
int t, l, u, ans, mx, cnt;
cin >> t;
while(t--){
cin >> l >> u;
mx = -1;
for(int i = l; i <= u; i++){
cnt = 0;
for(int j = 1; j <= sqrt(i); j++){
if(i%j == 0){
cnt++;
if(i/j != j)cnt++;
}
}
if(cnt > mx){
mx = cnt;
ans = i;
}
}
cout << "Between " << l << " and " << u << ", " << ans << " has a maximum of " << mx << " divisors." << endl;
}
return 0;
}
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
int t, l, u, ans, mx, cnt;
cin >> t;
while(t--){
cin >> l >> u;
mx = -1;
for(int i = l; i <= u; i++){
cnt = 0;
for(int j = 1; j <= sqrt(i); j++){
if(i%j == 0){
cnt++;
if(i/j != j)cnt++;
}
}
if(cnt > mx){
mx = cnt;
ans = i;
}
}
cout << "Between " << l << " and " << u << ", " << ans << " has a maximum of " << mx << " divisors." << endl;
}
return 0;
}
UVa - 336 - A Node Too Far!
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#define M 100003
using namespace std;
vector <int> node[M];
int cs = 1, mx, cnt;
void bfs(int src, int TTL, int cnt){
queue <int> container;
int visited[M] = {0}, ans = 1, power[M] = {0};
visited[src] = 1;
container.push(src);
while(!container.empty()){
int u = container.front();
int sz = node[u].size();
for(int i = 0; i < sz; i++){
int v = node[u][i];
if(!visited[v]){
visited[v] = 1;
power[v] = power[u]+1;
if(power[v] <= TTL)ans++;
container.push(v);
}
}
container.pop();
}
cout << "Case " << cs << ": " << (cnt - ans) << " nodes not reachable from node " << src << " with TTL = " << TTL << "." << endl;
}
int main(){
int N, E, x, y, src, TTL, m, num[M] = {0};
while(cin >> N ){
mx = 0, cnt = 0;
if(!N)break;
for(int i = 0; i < N; i++){
cin >> x >> y,node[x].push_back(y), node[y].push_back(x);
m = max(x, y);
mx = max(mx, m);
}
for(int i = 0; i < mx+1; i++){
int sz = node[i].size();
if(sz > 0)cnt++;
}
while(cin >> src >> TTL){
if(!src && !TTL)break;
bfs(src, TTL, cnt);
cs++;
}
for(int i = 0; i <= M; i++)node[i].clear();
}
return 0;
}
#include <cstring>
#include <vector>
#include <queue>
#define M 100003
using namespace std;
vector <int> node[M];
int cs = 1, mx, cnt;
void bfs(int src, int TTL, int cnt){
queue <int> container;
int visited[M] = {0}, ans = 1, power[M] = {0};
visited[src] = 1;
container.push(src);
while(!container.empty()){
int u = container.front();
int sz = node[u].size();
for(int i = 0; i < sz; i++){
int v = node[u][i];
if(!visited[v]){
visited[v] = 1;
power[v] = power[u]+1;
if(power[v] <= TTL)ans++;
container.push(v);
}
}
container.pop();
}
cout << "Case " << cs << ": " << (cnt - ans) << " nodes not reachable from node " << src << " with TTL = " << TTL << "." << endl;
}
int main(){
int N, E, x, y, src, TTL, m, num[M] = {0};
while(cin >> N ){
mx = 0, cnt = 0;
if(!N)break;
for(int i = 0; i < N; i++){
cin >> x >> y,node[x].push_back(y), node[y].push_back(x);
m = max(x, y);
mx = max(mx, m);
}
for(int i = 0; i < mx+1; i++){
int sz = node[i].size();
if(sz > 0)cnt++;
}
while(cin >> src >> TTL){
if(!src && !TTL)break;
bfs(src, TTL, cnt);
cs++;
}
for(int i = 0; i <= M; i++)node[i].clear();
}
return 0;
}
UVa - 146 - ID Codes
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- int main(){
- string code;
- while(cin >> code){
- if(code == "#")break;
- if(next_permutation(code.begin(), code.end()))cout << code << endl;
- else cout << "No Successor" << endl;
- }
- return 0;
- }
UVa - 119 - Greedy Gift Givers
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <map>
using namespace std;
int main(){
int money, tk, x, n, sum[10], bl = 0;
string name[10], temp;
while(cin >> n){
if(bl == 1)cout << endl;
for(int i = 0; i < n; i++)cin >> name[i];
memset(sum, 0, sizeof(sum));
for(int k = 0; k < n; k++){
cin >> temp >> money >> x;
for(int z = 0; z < n; z++)if(x != 0 && (temp == name[z]))sum[z] -= ((money / x) * x);
if(x != 0)tk = (money / x);
for(int i = 0; i < x; i++){
cin >> temp;
for(int j = 0; j < n; j++)if(temp == name[j])sum[j] += tk;
}
}
for(int i = 0; i < n; i++)cout << name[i] << " " << sum[i] << endl;bl = 1;
}
return 0;
}
#include <cstdio>
#include <string>
#include <cstring>
#include <map>
using namespace std;
int main(){
int money, tk, x, n, sum[10], bl = 0;
string name[10], temp;
while(cin >> n){
if(bl == 1)cout << endl;
for(int i = 0; i < n; i++)cin >> name[i];
memset(sum, 0, sizeof(sum));
for(int k = 0; k < n; k++){
cin >> temp >> money >> x;
for(int z = 0; z < n; z++)if(x != 0 && (temp == name[z]))sum[z] -= ((money / x) * x);
if(x != 0)tk = (money / x);
for(int i = 0; i < x; i++){
cin >> temp;
for(int j = 0; j < n; j++)if(temp == name[j])sum[j] += tk;
}
}
for(int i = 0; i < n; i++)cout << name[i] << " " << sum[i] << endl;bl = 1;
}
return 0;
}
UVa - 113 - Power of Cryptography
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
int n, mod, get, root, temp, m;
vector <int> ans;
string pran;
while(cin >> n >> pran){
get = n*n, mod = 0, m = 0;
int l = pran.size();
for(int i = 0; i < l; i++){
temp = (mod * 10) + (pran[i]-48);
mod = ((mod * 10) + (pran[i]-48)) % n;
if(m == 1 && temp < get)ans.push_back(0);
if(temp >= get){
root = temp / get;ans.push_back(root);
m = 1;
}
}
for(int i = 0; i < ans.size(); i++)cout << ans[i];cout << endl;
ans.clear();
}
return 0;
}
#include <string>
#include <vector>
using namespace std;
int main(){
int n, mod, get, root, temp, m;
vector <int> ans;
string pran;
while(cin >> n >> pran){
get = n*n, mod = 0, m = 0;
int l = pran.size();
for(int i = 0; i < l; i++){
temp = (mod * 10) + (pran[i]-48);
mod = ((mod * 10) + (pran[i]-48)) % n;
if(m == 1 && temp < get)ans.push_back(0);
if(temp >= get){
root = temp / get;ans.push_back(root);
m = 1;
}
}
for(int i = 0; i < ans.size(); i++)cout << ans[i];cout << endl;
ans.clear();
}
return 0;
}
Subscribe to:
Posts (Atom)