没能参加当时的比赛,比赛结束之后当练习做了一下。
A:水题,就是比较麻烦一点:
import java.util.Scanner;
import java.util.Vector;
public class A {
public final static String[] num = {"", "", "double", "triple", "quadruple", "quintuple", "sextuple", "septuple",
"octuple", "nonuple", "decuple"};
public final static String[] digit = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
String rawString = "";
String pat = "";
for(int i=1; i<=t; i++){
rawString = cin.next();
pat = cin.next();
String[] len = pat.split("-");
Vector<Integer> vec = new Vector<Integer>();
vec.clear();
for(String s : len){
vec.add(Integer.parseInt(s));
}
Vector<String> str = new Vector<String>();
str.clear();
for(Integer n : vec){
str.add(rawString.substring(0, n));
rawString = rawString.substring(n);
}
String res = "";
for(String s : str){
int length = s.length();
for(int j=0; j<length; j++){
int counter = 0;
int k = j;
while(k<length && s.charAt(k)==s.charAt(j)){
counter++;
k++;
}
if(counter == 1)
res += digit[s.charAt(j)-'0']+" ";
else if(counter > 10){
for(int kk=0; kk<counter; kk++)
res += digit[s.charAt(j)-'0']+" ";
}
else
res += num[counter]+" "+digit[s.charAt(j)-'0']+" ";
j = k-1;
}
}// end external for loop
System.out.println("Case #"+i+": "+res.trim());
}// end for loop
}
}
B题:从完全二叉树里面找规律就行了。
int main(){
int t;
unsigned long long p, q, res, n;
stack<int> s;
cin>>t;
for(int i=1; i<=t; i++){
int ty;
cin>>ty;
res = 0;
while(s.empty() == false)
s.pop();
if(ty == 1){
cin>>n;
p = 1; q = 1;
unsigned long long j = 63;
unsigned long long cur = 1;
while(true){
unsigned long long tmp = (cur<<j)&n;
if(tmp != 0)
break;
else
j--;
}
j--;
for(int k=j; k>=0; k--){
unsigned long long tmp = (cur<<k)&n;
if(tmp != 0)
p = p+q;
else
q = p+q;
}
cout<<"Case #"<<i<<": "<<p<<" "<<q<<endl;
}// end if
else if(ty == 2){
cin>>p>>q;
while(p!=q){
if(p > q){
p = p-q;
s.push(1);
}
else{
q = q-p;
s.push(0);
}
}
s.push(1); res = 0;
while(s.empty() == false){
int num = s.top();
s.pop();
res = (res<<1)+num;
}
cout<<"Case #"<<i<<": "<<res<<endl;
}// end else if
}
return 0;
}
C题:也很水啊,就是排序嘛。。。
int main(){
int t, ptr1, ptr2, n;
int val[1005];
vector<int> alex, bob, res;
cin>>t;
for(int i=1; i<=t; i++){
cin>>n;
alex.clear(); bob.clear(); res.clear();
for(int j=0; j<n; j++){
cin>>val[j];
if(abs(val[j])%2 == 1)
alex.push_back(val[j]);
else
bob.push_back(val[j]);
}
ptr1 = 0; ptr2 = 0;
sort(alex.begin(), alex.end());
sort(bob.begin(), bob.end(), greater<int>());
for(int j=0; j<n; j++){
if(abs(val[j])%2 == 1){
res.push_back(alex[ptr1++]);
}
else
res.push_back(bob[ptr2++]);
}
cout<<"Case #"<<i<<":";
for(int j=0; j<res.size(); j++)
cout<<" "<<res[j];
cout<<endl;
}
return 0;
}
D题没看懂什么意思。。。。
E题就是最短路径的变种,相同颜色的房间可以视为图中的一个顶点,后来懒得写了。。。贴一下cgy4ever大神的代码,思路和我一样的:
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
//#include <ctime>
#include <map>
using namespace std;
int trans(int x)
{
if('a' <= x && x <= 'z') return x - 'a' + 10;
return x-'0';
}
int transA(string s)
{
//cout << trans(s[1]) << " " << trans(s[0]) << endl;
return 36*trans(s[1]) + trans(s[0]);
}
int n, m;
int mapTo[80001];
int dist[1301][1301];
int inf = 10000000;
int A[3001], B[3001], len[3001];
void solve()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
string s;
cin >> s;
mapTo[i] = transA(s);
//cout << mapTo[i] << endl;
}
memset(dist, 0xe, sizeof(dist));
for(int i = 1; i <= 1300; i++)
dist[i][i] = 0;
cin >> m;
for(int i = 1; i <= m; i++)
{
cin >> A[i] >> B[i] >> len[i];
A[i] = mapTo[A[i]];
B[i] = mapTo[B[i]];
}
for(int iteration = 1; iteration <= 1300; iteration ++)
for(int i = 0; i <= 1300; i++)
for(int j = 1; j <= m; j++)
dist[i][B[j]] = min(dist[i][B[j]], dist[i][A[j]] + len[j]);
cout << endl;
int nQ;
cin >> nQ;
for(int i = 1; i <= nQ; i++)
{
int id1, id2;
cin >> id1 >> id2;
int ans = dist[mapTo[id1]][mapTo[id2]];
if(ans >= inf) ans = -1;
cout << ans << endl;
}
}
int MAIN()
{
int TestCase;
cin >> TestCase;
for(int caseID = 1; caseID <= TestCase; caseID ++)
{
cout << "Case #" << caseID << ": ";
solve();
}
return 0;
}
int main()
{
//srand((unsigned)time(NULL));
#ifdef LOCAL_TEST
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios :: sync_with_stdio(false);
cout << fixed << setprecision(16);
return MAIN();
}