由於忙着複習期(da)末(you)考(xi),因此訓練略有欠缺,今天特地抽空將將近一個月前的一套水題整理一下。
題目來自於劉汝佳白書的第五章習題的第二部分(簡單排序檢索專題)
下面發題目鏈接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=43037#overview
A.UVA340 題目和猜數字遊戲有關,大意是讓你輸出xAyB,思路有兩種,一種是簡單粗暴地模擬,還有一種是先算出x,再算出答案和兩者相同的數字的個數(不考慮位置),然後將其減去x就能夠得到y
B.UVA10420 數數題,把每一行第一個單詞保存到map再統計
C.UVA10474 簡單的排序題,開兩個數組並記錄下每個大理石在哪一個位置
D.UVA152 題目大意是給你若干三維空間的點,讓你對他們之間兩兩點的距離進行排序,然後算出每個區間內距離的個數
E.UVA299 題目要求是問要做多少次交換相鄰兩數才能讓給定的序列變成升序。由於數據量很小,可以直接模擬,當然也有O(1)的算法,考慮給定序列的逆序數。
F.UVA120 依然是問你需要多少次操作可以將給定序列變成升序,這裏的操作是指將從第i個之後的數字翻轉,簡單模擬。
G.UVA156 不考慮順序和大小寫,讓你把沒有重複的單詞輸出。具體做法是將每一個單詞讀入後按照字典序重組單詞,再將所有單詞sort一遍,那麼前一個單詞和後一個單詞和本身都不一樣的單詞便是答案。
H.UVA400 有點繁的排版題,將輸入的單詞排序後豎着輸出,只需要判斷每一行和每一列各有多少個單詞然後按順序輸出就好了。
I UVA123、J UVA10194 未AC
K UVA755 先將所有輸入的內容轉化成給定格式的編碼,再將出現次數大於1的按順序給出。
L UVA10785 題意是讓你輸出符合要求的長度爲n的字符串,每個字母都有對應的數字,給出的字符串的字符的對應數字要求是奇偶相間,而且該字符串需要是由這些字符所能組合成的字符串中字典序最小的一個,略有麻煩,不過還是隻需模擬即可。
代碼如下:
A:
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
int n,i1 = 1;
cin >> n;
while (n)
{
int ans[1010] = {0},che[1010] = {0},ans2[1010] = {0};
for (int i = 0;i<n;i++)
{
cin >> ans[i];
ans2[i] = ans[i];
}
cout << "Game " << i1++ << ":" << endl;
while (1)
{
for (int i = 0;i<n;i++)cin >> che[i];
if(!che[0]) break;
int a = 0,b = 0,x[15] = {0},y[15] = {0};
for (int i = 0;i<n;i++) if(ans2[i] == che[i])a++;
sort(ans,ans+n);
sort(che,che+n);
int i = 0,j = 0;
while (i <= n-1&&j <= n-1)
{
if (ans[i] == che[j])
{
b++;
i++;
j++;
}
else if(ans[i] > che[j]) j++;
else i++;
}
cout << " (" << a << "," << b-a << ")" << endl;
}
cin >> n;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
B:
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int類型最大值INT_MAX,short最大值爲SHORT_MAX
long long最大值爲LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x) memset(x,0,sizeof(x))
using namespace std;
const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
map <string,int> country;
string cou;
int t;
cin >> t;
for(int i = 0;i<t;i++)
{
cin >> cou;
country[cou]++;
getline(cin,cou);
}
//cout << "OK" << endl;
for(map<string,int> :: iterator it = country.begin();it!=country.end();it++)
cout << it->first << " " << it->second << endl;
//fclose(stdin);
//fclose(stdout);
return 0;
}
C:
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int類型最大值INT_MAX,short最大值爲SHORT_MAX
long long最大值爲LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x) memset(x,0,sizeof(x))
using namespace std;
const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
int m,n,tot = 0;
cin >> n >> m;
while(m && n)
{
int a[10010],b[10010] = {0};
cout << "CASE# " << ++tot << ":" << endl;
for (int i = 0;i<n;i++)cin >> a[i];
sort(a,a+n);
for(int i = 0;i<n;i++) if (!b[a[i]]) b[a[i]] = i+1;
for(int i = 0;i<m;i++)
{
int que;
cin >> que;
if(!b[que]) cout << que << " not found" << endl;
else cout << que << " found at " << b[que] << endl;
}
cin >> n >> m;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
D:
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int類型最大值INT_MAX,short最大值爲SHORT_MAX
long long最大值爲LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x) memset(x,0,sizeof(x))
using namespace std;
const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
struct Mynode
{
int dx;
int dy;
int dz;
};
double dis(Mynode a,Mynode b)
{
return sqrt((a.dx-b.dx)*(a.dx-b.dx)+(a.dy-b.dy)*(a.dy-b.dy)+(a.dz-b.dz)*(a.dz-b.dz))+1e-7;
}
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
int n = 0,result[20] = {0};
int x,y,z;
Mynode node[5050];
cin >> x >> y >> z;
while(x||y||z)
{
node[n].dx = x;
node[n].dy = y;
node[n].dz = z;
cin >> x >> y >> z;
n++;
}
for (int i = 0;i<n;i++)
{
double mind = 1000000;
for (int j = 0;j<n;j++)
{
if(i == j) continue;
if (dis(node[i],node[j]) < mind) mind=dis(node[i],node[j]);
//cout << mind << endl;
}
if(mind<=16)result[int(mind)]++;
}
for (int i = 0;i<10;i++)
cout << setw(4) << result[i];
cout << endl;
//fclose(stdin);
//fclose(stdout);
return 0;
}
E:
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int類型最大值INT_MAX,short最大值爲SHORT_MAX
long long最大值爲LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x) memset(x,0,sizeof(x))
using namespace std;
const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
int t;
cin >> t;
while(t--)
{
int n,a[55],result = 0;
cin >> n;
for (int i = 1;i<=n;i++) cin >> a[i];
for (int i = 1;i<=n;i++)
{
if(a[i] == i) continue;
int pos;
for (pos = i;a[pos]!=i;pos++);
//cout << pos << endl;
for (int j = pos;j>i;j--)
{
swap(a[j],a[j-1]);
//cout << a[4] << endl;
result++;
}
}
cout << "Optimal train swapping takes " << result << " swaps." << endl;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
F:
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int類型最大值INT_MAX,short最大值爲SHORT_MAX
long long最大值爲LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x) memset(x,0,sizeof(x))
using namespace std;
const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
int a[1010] = {0};
void rev(int n)
{
for(int i = 1;i<=n/2;i++)
swap(a[i],a[n+1-i]);
}
void print(int n)
{
cout << endl;
for (int i = 1;i<=n;i++)
if(i == 1) cout << a[i];
else cout << " " << a[i];
cout << endl;
}
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
string s;
while(getline(cin,s))
{
_clr(a);
int n = 1;
for(int i = 0;i<s.length();i++)
{
if(s[i] == ' ') n++;
else a[n] = 10*a[n]+s[i]-'0';
}
for (int i = 1;i<=n;i++)
if(i == 1) cout << a[i];
else cout << " " << a[i];
cout << endl;
int p = 0;
for (int i = n;i>=1;i--)
{
int maxp = -1;
for (int j = 1;j<=i;j++)
if(a[maxp] < a[j]) maxp = j;
if(maxp == i) continue;
if(maxp!=1)
{
rev(maxp);
if(!p)
{
cout << n+1-maxp;
p = 1;
}
else cout << " " << n+1-maxp;
}
rev(i);
if(!p)
{
cout << n+1-i;
p = 1;
}
else cout << " " << n+1-i;
}
if(!p) cout << "0" << endl;
else cout << " 0" << endl;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
G:
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int類型最大值INT_MAX,short最大值爲SHORT_MAX
long long最大值爲LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x) memset(x,0,sizeof(x))
using namespace std;
const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
struct Mynode
{
string s;
string ss;
int num;
};
bool cmp(Mynode a,Mynode b)
{
return a.ss<b.ss;
}
bool cmp2(Mynode a,Mynode b)
{
return a.s<b.s;
}
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
Mynode word[1010];
set<string> dic;
int n = 1;
word[0].s = "";word[0].ss = "";
while(cin >> word[n].s)
{
word[n].num = 0;
if(word[n].s == "#") break;
if(dic.find(word[n].s) != dic.end()) continue;
word[n].ss = word[n].s;
dic.insert(word[n].s);
for(int i = 0;i<word[n].s.length();i++)
word[n].ss[i] = tolower(word[n].s[i]);
sort(&word[n].ss[0],&word[n].ss[0]+word[n].ss.length());
n++;
}
sort(&word[1],&word[1]+n-1,cmp);
for(int i = 1;i<n;i++)
//cout << word[i].ss << endl;
if(word[i].ss!=word[i-1].ss && word[i].ss!=word[i+1].ss) word[i].num = 1;
sort(&word[1],&word[1]+n-1,cmp2);
for(int i = 1;i<n;i++)
if(word[i].num) cout << word[i].s << endl;
//fclose(stdin);
//fclose(stdout);
return 0;
}
H:
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int類型最大值INT_MAX,short最大值爲SHORT_MAX
long long最大值爲LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x) memset(x,0,sizeof(x))
using namespace std;
const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
int main()
{
// freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
string s[110],ss[110];
int n;
while(cin >> n)
{
getchar();
int spa = 0;
for (int i = 0;i<n;i++)
{
getline(cin,s[i]);
if(spa<s[i].length()) spa=s[i].length();
}
cout << "------------------------------------------------------------" << endl;
sort(s,s+n);
spa+=2;
int col = 60/spa;
if(!col){spa-=2;col++;}
int row = (n%col)?(n/col+1):(n/col);
//cout << row << " " << col << endl;
for(int i = 0;i<row;i++)
for(int j = 0;j<col;j++)
ss[i*col+j]=s[i+j*row];
cout.setf(ios::left);
for(int i=0;i<row;i++)
{
cout<< setw(spa)<<s[i];
for(int j=1;j<=col;j++)
{
if(i+j*row>=n) break;
cout<< setw(spa)<<s[i+j*row];
}
cout<<endl;
}
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
K:
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int類型最大值INT_MAX,short最大值爲SHORT_MAX
long long最大值爲LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x) memset(x,0,sizeof(x))
using namespace std;
const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
const string tra = "2223334445556667 77888999";
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
int t;
cin >> t;
while(t--)
{
map<string,int>result;
int n;
cin >> n;
getchar();
while(n--)
{
string s,ss = "";
getline(cin,s);
for (int i = 0;i<s.length();i++)
{
if(isalpha(s[i]))s[i] = tra[s[i]-'A'];
if(isdigit(s[i]))ss+=s[i];
}
ss = ss.substr(0,3)+"-"+ss.substr(3,4);
result[ss]++;
}
int p = 0;
for (map<string,int> :: iterator it = result.begin();it!=result.end();it++)
if(it->second>1)
{
cout << it->first << " " << it->second << endl;
p = 1;
}
if(!p) cout << "No duplicates." << endl;
if(t)cout << endl;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
L:
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int類型最大值INT_MAX,short最大值爲SHORT_MAX
long long最大值爲LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x) memset(x,0,sizeof(x))
using namespace std;
const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
string result1 = "",result2 = "";
string vowel = "AUEOI",consonant = "JSBKTCLDMVNWFXGPYHQZR";
int ch[300] = {0};
int odd = 0,even = 0;
for (int i = 1;i<=211;i++)
{
if(i%2 == 0)
{
result1 += consonant[even];
ch[consonant[even]]++;
if(ch[consonant[even]] == 5) even++;
}
else
{
result2 += vowel[odd];
ch[vowel[odd]]++;
if(ch[vowel[odd]] == 21) odd++;
}
}
int t;
cin >> t;
for(int i1 = 1;i1<=t;i1++)
{
int n;
cin >> n;
cout << "Case " << i1 << ": ";
string s1[300],s2[300],result = "";
s1[0] = result1.substr(0,n/2);s2[0] = result2.substr(0,(n+1)/2);
for(int i = 0;i<s1[0].length();i++)
{
s1[i+1] = s1[i].substr(1,s1[0].length()-1);
s1[i+1]+=s1[i][0];
}
for(int i = 0;i<s2[0].length();i++)
{
s2[i+1] = s2[i].substr(1,s2[0].length()-1);
s2[i+1]+=s2[i][0];
}
sort(&s1[0][0],&s1[0][0]+s1[0].length());
sort(&s2[0][0],&s2[0][0]+s2[0].length());
//cout << s1[0].length() << " " << s2[0].length() << endl;
//cout << s1[0] << endl << s1[1] << endl << s1[2] << endl;
for(int i = 0;i<min(s1[0].length(),s2[0].length());i++)
{
result+=s2[0][i];
result+=s1[0][i];
}
if(s1[0].length()<s2[0].length()) result+=s2[0][s2[0].length()-1];
cout << result << endl;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}