关闭

【PHP】单词十以内的加法de多种解法

标签: php解决方案switchclass
907人阅读 评论(0) 收藏 举报
分类:

问题描述

把两个英文单词加在一起~

写一个类Arith,实现下面的功能

  var k = new Arith("three");
  k.add("seven"); //返回 "ten"

输入 - zero 到 ten ,肯定是小写
输出 - 返回相加的结果,肯定是小写

分析

  • class
  • __construct
  • array
  • switch case

解决方案

数组取值array_flip反转键值


class Arith { 
    private $number;

    public $numbers = [
      'zero' => 0, 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5,
      'six' => 6, 'seven' => 7, 'eight' => 8, 'nine' => 9, 'ten' => 10, 'eleven' => 11,
      'twelve' => 12, 'thirteen' => 13, 'fourteen' => 14, 'fifteen' => 15, 'sixteen' => 16,
      'seventeen' => 17, 'eighteen' => 18, 'nineteen' => 19, 'twenty' => 20
    ];

    public function __construct(string $number) {
      $this->number = $number;
    }

    public function add(string $number): string {
      $sum = $this->get($this->number) + $this->get($number);
      $flipped = array_flip($this->numbers);

      return $flipped[$sum];
    }

    public function get(string $number): int {
      // can convert to lower
      return (empty($this->numbers[$number])) ? 0 : $this->numbers[$number];
    }
}

const定义关系array_search查找对应关系

class Arith{ 
    private $i;
    const A = [
      'zero' => 0,
      'one' => 1,
      'two' => 2,
      'three' => 3,
      'four' => 4,
      'five' => 5,
      'six' => 6,
      'seven' => 7,
      'eight' => 8,
      'nine' => 9,
      'ten' => 10,
      'eleven' => 11,
      'twelve' => 12,
      'thirteen' => 13,
      'fourteen' => 14,
      'fifteen' => 15,
      'sixteen' => 16,
      'seventeen' => 17,
      'eighteen' => 18,
      'nineteen' => 19,
      'twenty' => 20
    ];

    public function __construct(string $i)
    {
      $this->i = self::A[$i];
    }

    public function add(string $v) {
        return array_search($this->i + self::A[$v], self::A);
    }
}

数组的索引查找关系


class Arith{ 
    private $_numbers = ["zero" => 0, "one" => 1, "two" => 2, "three" => 3, 
      "four" => 4, "five" => 5, "six" => 6, "seven" => 7, "eight" => 8,
      "nine" => 9, "ten" => 10, "eleven" => 11];

    private $_words = ["zero", "one", "two", "three", 
      "four", "five", "six", "seven", "eight",
      "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
      "sixteen", "seventeen", "eighteen", "nineteen", "twenty"];  

    private $_word = '';

    public function __construct($word) {
      $this->_word = $word;
    }

    public function add($word) {
      return $this->_words[$this->_numbers[$this->_word] + $this->_numbers[$word]];
    }
}

switch case确立关系

class Arith
{

    protected $num;

    //初始化数据

    public function __construct($num)
    {
        $this->num = $this->_tonNum($num);

    }

    private function _tonNum($num)
    {
        $num_num = 0;
        switch ($num) {
            case "zero":
                $num_num = 0;
                break;
            case "one":
                $num_num = 1;
                break;
            case "two":
                $num_num = 2;
                break;
            case "three":
                $num_num = 3;
                break;
            case "four":
                $num_num = 4;
                break;
            case "five":
                $num_num = 5;
                break;
            case "six":
                $num_num = 6;
                break;
            case "seven":
                $num_num = 7;
                break;
            case "eight":
                $num_num = 8;
                break;
            case "nine":
                $num_num = 9;
                break;
            case "ten":
                $num_num = 10;
                break;
            case "eleven":
                $num_num = 11;
                break;
            case "twelve":
                $num_num = 12;
                break;
            case "thirteen":
                $num_num = 13;
                break;
            case "fourteen":
                $num_num = 14;
                break;
            case "fifteen":
                $num_num = 15;
                break;
            case "sixteen":
                $num_num = 16;
                break;
            case "seventeen":
                $num_num = 17;
                break;
            case "eighteen":
                $num_num = 18;
                break;
            case "nineteen":
                $num_num = 19;
                break;
            case "twenty":
                $num_num = 20;
                break;
        }
        return $num_num;
    }


    /**
     * 转换成文字
     * @param $num
     */
    public function tostr($num)
    {
        $num_num = "";
        switch ($num) {
            case 0:
                $num_num = "zero";
                break;
            case 1:
                $num_num = "one";
                break;
            case 2:
                $num_num = "two";
                break;
            case 3:
                $num_num = "three";
                break;
            case 4:
                $num_num = "four";
                break;
            case 5:
                $num_num = "five";
                break;
            case 6:
                $num_num = "six";
                break;
            case 7:
                $num_num = "seven";
                break;
            case 8:
                $num_num = "eight";
                break;
            case 9:
                $num_num = "nine";
                break;
            case 10:
                $num_num = "ten";
                break;
            case 11:
                $num_num = "eleven";
                break;
            case 12:
                $num_num = "twelve";
                break;
            case 13:
                $num_num = "thirteen";
                break;
            case 14:
                $num_num = "fourteen";
                break;
            case 15:
                $num_num = "fifteen";
                break;
            case 16:
                $num_num = "sixteen";
                break;
            case 17:
                $num_num = "seventeen";
                break;
            case 18:
                $num_num = "eighteen";
                break;
            case 19:
                $num_num = "nineteen";
                break;
            case 20:
                $num_num = "twenty";
                break;
        }
        return $num_num;
    }

    //增加的方法
    public function add($add_num)
    {
        //做加法
        $add_num = $this->_tonNum($add_num);
        $re = $this->tostr($add_num + $this->num);
        //转换
        return $re;
    }

}

总结

解决问题的整体思路是差不多的,但是在具体实现细节上会有区别,而具体实现的点也就是考察的价值。

0
2
查看评论

设计程序:利用循环编写程序,帮助小学生练习10以内加减法

#include #include "time.h" #include "stdlib.h" int main() { int iNumber1=0,iNumber2=0; int iRightNum=0; const int c...
  • adsyzh
  • adsyzh
  • 2015-02-26 16:04
  • 895

java实现十个数字组成一个加法式

现在小学也不好混哪! 某日我在一本去年的小学四年级寒假作业上看到了这样的一道数学题:”用0,1,2,3,4,5,6,7,8,9这十个数字组成一个加法式子,如X+Y=Z,使得十个数字刚好被用完,并且不被重复使用”. 不过300+700=1000是不符合要求的,因为它把0用了共6次,并且没有用到2,4...
  • susuifeng
  • susuifeng
  • 2015-05-14 11:58
  • 1107

1----编程实现100以内两个随机整数的加法运算练习(rand函数)

问题描述: 编程实现100以内两个随机整数的加法运算练习 算法分析 首先考虑需要定义那些变量。因为是模拟加法a+b=c,所以需要定义a,b,c。 接着考虑数据的获取。随机整数可以通过标准库“stdlib.h”中的伪随机函数int rand()来产生,rand()函数每次产生一个0...
  • u010804317
  • u010804317
  • 2017-06-30 22:25
  • 365

极客 - 博文18 - 小学生练习10以内的加法程序

小学生练习10以内的加法程序
  • sinat_34104446
  • sinat_34104446
  • 2016-09-20 23:03
  • 695

【项目1:利用循环求和】求1000以内所有偶数的和

/* * Copyright (c) 2012, 烟台大学计算机学院 * All rights reserved. * 作 者:解晓东 * 完成日期:2012 年 10 月 31 日 * 版 本 号:v1.0 * * 输入描述: 无 * 问题描...
  • a191030148
  • a191030148
  • 2012-10-31 21:53
  • 1639

10万以下 阿拉伯转大写

/** * 念10万以内的任意数字。例如534念作伍佰叁十肆, * 203念作贰佰零叁。7003念作柒千零叁。 */public class Exec5 { public static void main(String[] args) { int n = 10; ...
  • kook_okko
  • kook_okko
  • 2009-04-20 09:42
  • 2114

网络营销文案标题写作七大原则

撰写一篇好网络营销文案的一个重要环节就是一个好的标题。一个好的标题往往影响着你的网络营销文案是不是吸引人,你的网络营销文案是不是可以被人们记住,你的传播软文是不是可以流传的很广,标题在这里起到了非常大的作用。那怎样才可以写出个非常好的标题。菜根谭谈谈自己的看法。   1. 主题...
  • u013255196
  • u013255196
  • 2013-12-24 14:31
  • 331

10以内的阶乘相加

package com.tuxing; public class Jiecheng { public static void main(String args[ ]){ //第一种方法 int s=0; int q=1; ...
  • changyu7280
  • changyu7280
  • 2016-09-14 15:04
  • 286

leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

Trapping Rain Water Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able...
  • xygy8860
  • xygy8860
  • 2015-07-11 14:04
  • 727

51 NOD 1095 Anigram单词

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1095 1095 Anigram单词 基准时间限制:1 秒 空间限制:131072 KB 分值:...
  • qq_34287501
  • qq_34287501
  • 2017-01-31 23:36
  • 261
    个人资料
    • 访问:640811次
    • 积分:9031
    • 等级:
    • 排名:第2476名
    • 原创:297篇
    • 转载:13篇
    • 译文:4篇
    • 评论:81条
    博客专栏
    微信订阅号
    欢迎加入QQ群
    玩家老黄历(微信小程序)
    谢谢支持~
    最新评论