JavaScript implements C language Classical program problem _javascript skill

Source: Internet
Author: User

Recently in learning JavaScript language, see a lot of online is introduced JavaScript how to solve the problem on the Web page code, so want to find a way to use JavaScript code to achieve the classic C language program problems. Of course, these C language program is relatively simple, mainly want to achieve through JavaScript language, play the role of grammar, but also want to compare the C and JavaScript language to achieve the same point and different points, so as to consolidate memory, strengthen the learning effect!!!

First, the C language Classic program question 1

1. Title Description:

Marx's manuscript has such an interesting mathematical problem: There are 30 people, including men, women, children. They dined in a restaurant for a total of 50 shillings. If each man had to spend 3 shillings for dinner, each woman would have to spend 2 shillings, and each child would spend 1 shillings, asking men, women, and children how many people?

2. JavaScript code:

var mans, woman, child;
for (man = 0; mans < man + +) 
{for
  (woman = 0; Woman <= woman + +) {child
    = 30-man-woman ;
    if (mans + woman + child = = && (man * 3 + woman * 2 + child = =))
    {
      document.write ("Men: +man+") + "Woman:" +woman+ "," + "Child:" +child + "</br>");
    }
   

3. Description of the problem:

This topic is a very classic topic, and "change" is the same type of topic, only need multiple loops plus a judge can list out each result. From this question, JavaScript code and C language code are basically indistinguishable, because the for statement and the IF statement, the JavaScript language and the C language are the same. Major different bodies now, the definition of variables and output statements, the overall sense of JavaScript language implementation is more convenient, mainly because of JavaScript weak type language, C language is strongly typed language.

Second, the C language Classic program question 2

1. Title Description:

Calculate e=1+1/1! +1/2! +1/3! ... +1/n! The first 50 items

2. JavaScript code:

var N;
var s = 1;
var e = 1;
for (n = 1; n <= n + +)
{
  s = s * n;
  E = e + (1/s);
}

3. Description of the problem:

This topic is a relatively simple topic, implementation also uses only a For loop, compared with C language code, the difference is in the definition and output of variables.

Third, C language Classic program question 3

1. Title Description:

Enter a number (unlimited digits) to output the number of digits in a number.

2. JavaScript code:


function Demo ()
{
  var i, J, S;
  for (i = 1; i < i + +)
  {for
    (j = 1; J <= I; j + +)
    {
      s = i * j;
      document.write (j+ "*" +i+ "=" +s+ ");
    }
    document.write ("<br/>");
  }
  

3. Description of the problem:

This topic is also a very classic topic, but the implementation is particularly simple, only need two loops nested on it. and C language differences in addition to variable definition and output, here is another, is the line. C-line wrapping is only used \ n, but the Web page does not recognize \ n, so you can only use HTML <br/> to implement line wrapping.

Five, C language Classic Procedural Questions 5

1. Title Description:

The young singer participates in the song Grand Prix, has 10 judges to score, the trial programming asks the athlete's average score (removes one highest score and one lowest cent)

2. JavaScript code:

<body>
<input id= "Getscore" type= "text" >
<button onclick= "demo ()" > Average </button>
<p id= "txt" ></p>
<script>
  function Demo () {
    var str = document.getElementById (" Getscore "). Value;
    var score = new Array ();
    Score= str.split (",");
    var max = 0;
    var min = 10000;
    var sum = 0;
    var ave = 0;
    for (i=0;i<score.length;i++) {
      if (Score[i]>max)
      {
        max = score[i];
      }
      if (score[i]<min)
      {
        min = score[i];
      }
      sum = Sum+score[i];
    }
    Ave = (sum-max-min)/8;
    document.getElementById ("TXT"). InnerHTML = Ave;
  }
</script>
</body>

3. Explanation of Problem solving

This topic should be considered the most code, although the topic is very simple, but in the implementation of the input 10 results encountered difficulties, because it can not be like the C language as an input. So, I use the input 1,2,3,4,5,6,7,8,9,10, with a comma to enter 10 of the way in a one-time. When input comes in, it is necessary to split the string, so the Split function is used.

V. Summary

Finally in the JavaScript language finished 5 C language Classic topics, but also a good start to learn JavaScript. Recall in the process of solving each topic, feeling JavaScript really very much like C language, so start relatively simple, just in the processing of input and output, a little different. If I were to evaluate the JavaScript and C language that good? I would think that JavaScript is really easier to solve the problem faster, I like its weak type characteristics, no longer worry about declaring variable type error. Of course, the C language as the classic language of immortality, of course, is also very good.

The above is the entire content of this article, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.