2014年2月22日 星期六

UVA 10008 c++

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=949

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int n,len,temp;
while (cin >> n)
{
int count[26] = { 0 };
char c_temp;
char english[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char input[256];
for (int i = 0; i <= n; i++)
{
cin.getline(input, 256);
len = strlen(input);
for (int j = 0; j <= len; j++)
{
if (input[j] <= 90 && 65 <= input[j])
count[input[j] - 65]++;
else if (input[j] <= 122 && 97<=input[j])
count[input[j] - 97]++;
}
}
for (int i = 0; i <= 25; i++)
{
for (int j = 0; j <= 24; j++)
{
if (count[j]<count[j + 1]) //如果前項大於後項的話交換
{
temp = count[j];
c_temp = english[j];
count[j] = count[j + 1];
english[j] = english[j + 1];
count[j + 1] = temp;
english[j + 1] = c_temp;
}
}
}
for (int i = 0;; i++)
{
if (count[i] == 0)
break;
else
cout << english[i] << " " << count[i] << endl;
}
}
return 0;
}

2014年2月21日 星期五

UVA 10340 c++

#include<iostream>
#include<string>
using namespace std;
int main()
{
string s, t;
int count;
bool *arr, flag=0;
while (cin >> s && cin >> t)
{
arr = new bool[s.length()];
count = 0;
flag = 0;
for (int i = 0; i <= s.length() - 1; i++)
arr[i] = 0;
for (int j = 0; j <= s.length() - 1; j++)
{
for (int k = count; k <= t.length() - 1; k++)
{
if (s[j] == t[k])
{
arr[j] = 1;
count = k+1;
break;
}
}
}
for (int i = 0; i <= s.length() - 1; i++)
{
if (arr[i] == 0)
{
cout << "No" << endl;
flag = 1;
break;
}
}
if (!flag)
cout << "Yes" << endl;
delete arr;
}
return 0;
}

UVA 11364 C++

#include<iostream>
using namespace std;
int main()
{
int n,times,min,max,input,len;
cin >> n;
for (int i = 0; i <= n - 1; i++)
{
cin >> times;
for (int j = 0; j <= times - 1; j++)
{
cin >> input;
if (j == 0)
{
min = input;
max = input;
}
else if (input > max)
max = input;
else if (input <= min)
min = input;
}
len = (max - min) * 2;
cout << len << endl;
}
return 0;
}

2014年2月18日 星期二

UVA 10327 C++

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1268

#include<iostream>
using namespace std;

int main()
{
int *data;
int N = 0,count=0;
while (cin >> N)
{
data = new int[N];
count = 0;
for (int i = 0; i <= N - 1; i++)
cin >> data[i];
for (int j = 0; j <= N - 2;j++)
for (int k = j + 1; k <= N-1;k++)
if (data[k] < data[j])
count++;
delete data;
cout << "Minimum exchange operations : " << count << endl;
}
return 0;
}

2014年2月15日 星期六

UVA 476 C++

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=417

#include<iostream>
#include<stdlib.h>
#include<vector>
#include<string>
using namespace std;
struct point
{
double x;
double y;
};
int main()
{
vector<point>left;
vector<point>right;
point garbage;
garbage.x = 0;
garbage.y = 0;
string input="",tmp="";
double x = 0.0, y = 0.0, number = 0.0;
int line=0,count, pos, point_number=1;
bool flag;
while(getline(cin,input))
{
if (input[0] == '*')
break;
count = 0,pos=2;
int len = input.length();
for (int i = 2; i <= len; i++)
{
if (input[i] == '\0' || input[i]=='\n')
{
for (int j = pos; j <= i-1; j++)
tmp = tmp + input[j];
number = atof(tmp.c_str());
count++;
tmp = "";
}
else if (input[i]== ' ')
{
for (int k = pos; k <= i - 1; k++)
tmp = tmp + input[k];
number = atof(tmp.c_str());
count++;
tmp = "";
pos = i + 1;
}
switch (count)
{
case 1:
left.push_back(garbage);
left[line].x = number;
break;
case 2:
left[line].y = number;
break;
case 3:
right.push_back(garbage);
right[line].x = number;
break;
case 4:
right[line].y = number;
break;
}
}
line++;
}
while (true)
{
cin >> x;
cin >> y;
flag = 0;
if (x == 9999.9 && y == 9999.9)
break;
for (int i = 0; i <= line-1; i++)
{
if (x > left[i].x && x < right[i].x && y< left[i].y && y> right[i].y)
{
cout << "Point " << point_number << " is contained in figure " << i+1 << endl;
flag = 1;
}
}
if (flag == 0)
cout << "Point " << point_number << " is not contained in any figure" << endl;
point_number++;
}
return 0;
}

2014年2月14日 星期五

UVA 458 C++

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=399

#include<iostream>
#include<string>
using namespace std;
int main()
{
string input = "";
string output="";
char change;
int count = 0;
while (getline(cin, input))
{
output = "";
for (int i = 0; i <= input.length() - 1; i++)
{
change = input[i];
change = change - 7;
output = output + change;
}
cout << output << endl;
}
return 0;
}

UVA 272 C++

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=208

#include<iostream>
#include<string>
using namespace std;
int main()
{
string input = "";
string output="";
int count = 0;
//cin >> input;
while (getline(cin, input))
{
output = "";
for (int i = 0; i <= input.length() - 1; i++)
{
if (input[i] == '\"')
{
count++;
if (count % 2 == 1)
output = output + "``";
else
output = output + "''";;
}
else
output = output + input[i];
}
cout << output << endl;
}
return 0;
}