Return Negative

In this simple assignment you are given a number and have to make it negative. But maybe the number is already negative?

Example:

Kata.MakeNegative(1); // return -1
Kata.MakeNegative(-5); // return -5
Kata.MakeNegative(0); // return 0

Notes:

  • The number can be negative already, in which case no change is required.
  • Zero (0) can't be negative, see examples above.
using System;

public static class Kata
{
public static int MakeNegative(int number)
{
if(number > )
{
return -number;
}
else if(number < )
{
return number;
}
else
{
return ;
}
}
}

其他解法:使用绝对值函数或者问号表达式

using System;

public static class Kata
{
public static int MakeNegative(int number)
{
return -Math.Abs(number);
}
}
using System;

public static class Kata
{
public static int MakeNegative(int number)
{
return number > ? -number : number;
}
}

Return Negative的更多相关文章

  1. C# ListView点击列头进行排序

    /// <summary> /// This class is an implementation of the 'IComparer' interface. /// </summa ...

  2. 面试题目——《CC150》中等难题

    面试题17.1:编写一个函数,不用临时变量,直接交换两个数. 思路:使用差值或者异或 package cc150.middle; public class Exchange { public stat ...

  3. swfit-扩展语法

    import UIKit extension Double { var km: Double { } var m : Double { return self} var cm: Double { re ...

  4. 关于Android中ArrayMap/SparseArray比HashMap性能好的深入研究

    由于网上有朋友对于这个问题已经有了很详细的研究,所以我就不班门弄斧了: 转载于:http://android-performance.com/android/2014/02/10/android-sp ...

  5. Linux内核--网络栈实现分析(十)--网络层之IP协议(下)

    本文分析基于Linux Kernel 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7552455 更多请查看专栏,地 ...

  6. H5游戏开发之多边形碰撞检测

    2D多边形碰撞检测介绍这是一篇论证如何在2D动作游戏中执行碰撞检测的文章(Mario,宇宙入侵者等),为了保证它的高效性和精确性,碰撞检测是以多边形为基础的,而不是以sprite为基础.这是两种不同的 ...

  7. swift学习笔记之-扩展(Extensions)

    //扩展(Extensions) import UIKit /*扩展(Extensions):扩展 就是为一个已有的类.结构体.枚举类型或者协议类型添加新功能.这包括在没有权限获取原始源代码的情况下扩 ...

  8. Swift2.1 语法指南——扩展

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  9. [CareerCup] 17.7 English Phrase Describe Integer 英文单词表示数字

    17.7 Given any integer, print an English phrase that describes the integer (e.g., "One Thousand ...

随机推荐

  1. hibernate导入大量数据时,为了避免内存中产生大量对象,在编码时注意什么,如何去除?

    Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( i ...

  2. 【Winform】 无法将类型为“System.Windows.Forms.SplitContainer”的对象强制转换为类型“System.ComponentModel.ISupportInitialize”。

    问题:将dotnet framework 4.0 切换到2.0时,编译没有问题,在运行时出现如下错误:System.InvalidCastException: 无法将类型为“System.Window ...

  3. 如何解决NTLDR is missing

    问题:ntldr文件不见了,无法进入系统.本系统win XP 简体中文32位 解决步骤: (1)我找来一张win XP 英文版 32位的系统光盘(用U盘驱动也行) (2)重新启动计算机,并进入CMOS ...

  4. php mysqli多个查询的例子

    php中Mysqli多个查询的例子,感兴趣的朋友可以参考下. php中Mysqli多个查询的例子,感兴趣的朋友可以参考下. mysqli_multi_query(mysqli link,string ...

  5. Flashback删除(闪回删除)

    oracle 9i以前,当drop一个表时,到该表的所有引用都会从数据字典中删除. oracle 10g及以后,当drop一个表时,数据库根本没有删除表,而只是重命名了并放入了回收站.即当发出drop ...

  6. c# 模拟表单提交,post form 上传文件、大数据内容

    表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipart/form-data,而且要设定一个 boundary 参数,这个参数是由应用程序自行产生,它会用来识别每 ...

  7. 【BZOJ 1834】 [ZJOI2010]network 网络扩容

    Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下,1到N的最大流: 2. 将1到N的最大流增加K所需的 ...

  8. shell 后台执行命令

    shell 后台执行命令方法: 1. nohup cmd &          后台会生成 nohup.out 文件 2.cmd >/路径/xx.log &   后台生成 xx. ...

  9. Linux Mint SmoothTask2的安装方法

    首先,先下载smooth task:点击这里下载 下载之后解压缩,里面有个install文件,点击打开: To install plasmoid unpack archive, go to the d ...

  10. json2.js使用参考

    json2.js的源码地址: https://github.com/douglascrockford/JSON-js Visual Studio用户可以直接通过Nuget来获得. json2.js提供 ...