首先介绍一下CodeHunt这个平台,题目是一些数据,或者说是测试用例,你的编程结果只要保证能让测试用例通过测试即可。

https://www.codehunt.com/ 没事的同学可以进去看看,可以用各种账号登录,只是不能访问到编程之美2014挑战赛的试题罢了。

P.S. 服务器托管在国外的Azure,可能会访问较慢

每次仅给出很少的测试用例,当且仅当你的程序让这些测试用例通过测试并有未通过测试的用例时才会给你错误提示。

通过测试的代码,平台会根据代码质量给出1,2,3等级的评分,我仅一道题为1,其他均为满分3分。

本次比赛分为Fire,Metal,Earth三个模块,每个模块难度不同。

Fire 01 为指导,并不是真题。

Fire 02,03非常简单,仅列出答案

using System;
public class Program {
public static int Puzzle(int[] a) {
return a[];
}
}

Fire 02

using System;
public class Program {
public static int Puzzle(int n) {
return n+(-n%)%;
}
}

Fire 03

  • Fire 04

Fire 04 这道题我仅得了一分,这道题要把一个字符串中的数字提取出来生成一个新的字符串,以下是我的某3种方法。求高人指导更好的方法!!顺便求解错误方法错在哪?

s ans
p  
   
0 0
<:  
0 0
"0 " 0
   
000p00 0
3 3
5 5
p0 0
using System;
using System.Linq;
public class Program {
public static string Puzzle(string s) {
return new string(c.Where(i => i >= '' && i <= '').ToArray());
}
}

Fire 04-1

using System;
using System.Text.RegularExpressions;
public class Program {
public static string Puzzle(string s) {
Regex regexpattern = new Regex(@"[0-9]*");
string ans="";
foreach (Match match in regexpattern.Matches(s))
{
ans+= match.Value;
}
return ans;
}
}

Fire 04-2

using System;
public class Program
{
public static string Puzzle(string s)
{
string ss = "";
for (int i = ; i < s.Length; i++)
{
if (s[i] >= '' && s[i] <= '')
{
ss += s[i];
}
}
return ss;
}
}

Fire 04-3

如果s==“p0”,下面的代码无法正常运行,求解释!
public static string Puzzle(string s)
{
return new string(s.TakeWhile(i => i >= '' && i <= '').ToArray());
}

Fire 04 错误方法

  • Metal 01
s ans
\0\0\0\0 \0\0\0\0
/\0\0/ /\0\0/
/*\0\0 /*\0\0
/\0/*/// /\0/*///
/\u0a00/* /\u0a00/*
/**/*  *
\0/**// \0 /
/\0/**// /\0 /

思路:乍一看确实不知道是什么,仔细看,发现把“/**/”都换成了空格,那么我准备考虑是不是删掉字符串中的注释内容。但实际上,这题仅是一个简单替换而已。

using System;
public class Program
{
public static string Puzzle(string s)
{
return s.Replace("/**/", " "); ;
}
}

Metal 01

  • Metal 02
a b ans
1 1 1
2 2 2
65 32 2080
23 14 322

思路:就是求最小公倍数

提示:用乘积除以最大公因数

如果最大公因数求解过程中使用辗转相除法的话得分是2分

using System;
public class Program
{
public static int Puzzle(int a, int b)
{
int mul = a * b;
while (a != b)
{
if (a > b)
a = a - b;
if (b > a)
b = b - a;
}
return mul / a;
}
}

Metal 02

  • Metal 03
a ans
{0,0} null
{0,0,0} null
{0,0,0,0} null
{0,0,0,0,0} 0
{17,4,23,13,-9} 4
{18,18,18,18,18} 0
{50,18,18,18,18} 32

思路:注意到a的长度达到5后就有结果了,否则会提示应该throw IndexOutOfRangeException。

对于长度大于等于5数组,结果是该数组间两个数差值的最小值。

最开始用 a=a.OrderBy(item=>item)排序,但平台提示出错。(已经引用System.Linq)

using System;
using System.Linq;
public class Program
{
public static int Puzzle(int[] a)
{
if (a.Length <= )
throw new IndexOutOfRangeException();
int min = int.MaxValue;
for (int i = ; i < a.Length; i++)
{
for (int j = i + ; j < a.Length; j++)
{
int b = Math.Abs(a[i] - a[j]);
min = Math.Min(min, b);
}
}
return min;
}
}

Metal 03

  • Earth 01
a b c ans
0 0 1 0
916 914 49 10
3 4 5 2
7 13 4 3
3 522 411 333
3 4 1 0
3 0 1 0
916 914 1 0
916 0 1 0
1 0 1 0
0 1 1 0

思路:注意到(3,522,411)这一组,很容易发现 ans=a*(b-c),但是(916,914,49)这组好像不对,取个余数,就得到 ans=(a*(b-c))%c,如果b<c就出现负数,而C#中取余的符号和前者相同,这时要进行相应处理。

using System;
public class Program
{
public static int Puzzle(int a, int b, int c)
{
return (((a * (b - c))) % c + c) % c;
}
}

Earth 01

  • Earth 02
n ans
0 0
1 1
12 2
13 3
20 2
34 2
512 1
256 1
128 1
64 1
32 1
16 1
8 1
4 1
2

1

思路:发现所有2的指数幂都是1,而12=8+4,2个数相加;13=8+4+1,3个数相加。

则认为是n的二进制表示中1的个数。

using System;
public class Program
{
public static int Puzzle(int n)
{
int ans = ;
while (n > )
{
if (n % == )
{
ans++;
}
n /= ;
}
return ans;
}
}

Earth 02

  • Earth 03

    v1 v2 ans
    {} {} 0
    {0} {0} 0
    {0} {7} 49
    {8} {9} 1
    {8,0} {9,0} 1
    {0,0} {0,0} 0
    {8,8} {9,9} 2

思路,如果定义距离为对应元素的平方和的话,ans就是v1和v2的距离。

using System;

public class Program
{
public static int Puzzle(int[] v1, int[] v2)
{
int sum = ;
for (int i = ; i < v1.Length; i++)
{
sum += Math.Abs(v1[i] - v2[i]) * Math.Abs(v1[i] - v2[i]);
}
return sum;
}
}

Earth 03

编程之美2014挑战赛 复赛 Codehunt平台试题答案的更多相关文章

  1. 2017“编程之美”终章:AI之战勇者为王

    编者按:8月15日,第六届微软“编程之美”挑战赛在选手的火热比拼中圆满落下帷幕.“编程之美”挑战赛是由微软主办,面向高校学生开展的大型编程比赛.自2012年起,微软每年都在革新比赛命题.紧跟时代潮流, ...

  2. LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)

    题目链接 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...

  3. 编程之美2.5:寻找最大的K个数

    编程之美2.5:寻找最大的K个数 引申:寻找第k大的数: 方法一: // 选择第k大的数(通过改进快速排序来实现) public static void SelectShort(int[] array ...

  4. 24点C++程序实现 编程之美1.16

    解法1,对于任意输入的四个数字,给出一个24点的解法,若无解,则没有输出. 原理参照下图(编程之美原书) 代码如下,仅供参考 // 1.16.cpp : Defines the entry point ...

  5. Python编程之美:最佳实践指南PDF高清完整版免费下载|百度云盘|Python新手到进阶

    百度云盘:Python编程之美:最佳实践指南PDF高清完整版免费下载 提取码:1py6 内容简介 <Python编程之美:最佳实践指南>是Python用户的一本百科式学习指南,由Pytho ...

  6. hihocoder #1170 机器人 &amp;&amp; 编程之美2015复赛

    题意: 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小冰的N个机器人兄弟排成一列,每一个机器人有一个颜色. 如今小冰想让同一颜色的机器人聚在一起.即随意两个同颜色的 ...

  7. 生成CPU使用率 sin 曲线 控制cpu使用率 编程之美

    入职Oracle 以后想着把之前写过的<编程之美>中控制CPU使用率曲线的程序再写一边, 可是总是由于入职须要学习的东西太多, 没有时间. 程序早就写好了. 最终有机会贴出来了.o(∩∩) ...

  8. 编程之美Q1

    题目 和数书页有点类似,就直接数吧 #include<iostream> using namespace std; class q1 { public: size_t func(size_ ...

  9. 编程之美:1.9高效率安排见面会 图的m着色问题 回溯法

    原书问题,可以转换为图的m着色问题 ,下面该问题的代码 这里有参考ppt与code,免积分载 http://download.csdn.net/detail/u011467621/6341195 // ...

随机推荐

  1. 70. Implement strStr() 与 KMP算法

    Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...

  2. 基于mini2440的boa服务器移植

    win7 系统  开发板:mini2440 虚拟机:ubuntu12.04 准备工作:确保主机与开发板之间能够正常通信,即能够ping通,具体的操作课参考我上一篇随笔. 1.首先从 http://ww ...

  3. 在Linux下给mysql创建用户并分配权限及问题解决方案

    在linux下安装mysql请参考在linux系统中安装mysql服务器详细步骤 1.新建用户 //登录MYSQL @>mysql -u root -p @>密码 //创建用户 mysql ...

  4. 【Linux】netdata监控组件

    github:https://github.com/firehol/netdata 安装:https://github.com/firehol/netdata/wiki/Installation 内存 ...

  5. DCOM中的APPID的用处,以及RemoteServerName的传递问题

      DCOM中的APPID的用处,以及RemoteServerName的传递问题  

  6. Visual Studio 换皮肤

    通过字体和颜色修改 Visual Studio 提供了修改配色的入口,你完全可以根据自己的喜好进行自定义,下面就通过该方法把编辑器背景设置成 “豆沙绿”. 选择 工具 / 选项 / 环境 / 字体和颜 ...

  7. Eclipse is running in a JRE, but a JDK is required 解决方法(转)

    转自:http://comeonbabye.iteye.com/blog/1186239 安装Maven后每次启动出现警告信息: Eclipse is running in a JRE, but a ...

  8. ORACLE 数据块dump

    1. rdba(Tablespace relative database block address) 是相对数据块地址,是数据所在的地址,rdba可就是rowid 中rfile#+block#. 根 ...

  9. SSL/TLS 协议详解

    SSL(Secure Sockets Layer,安全套接层),及其继任者 TLS(Transport Layer Security,传输层安全)是为网络通信提供安全及数据完整性的一种安全协议.TLS ...

  10. modelsim基本操作步骤及每步骤问题解决1(后续有改动会更新)

    ①File ->New =>Project出现工程对话框->1)工程命名,2)安放路径自己设置,3)库默认work.点击OK 然后出现添加文件到工程对话框->可新建文件或直接添 ...