Home > Article > Web Front-end > Javascript implements C language classic program questions_javascript skills
I have been learning the Javascript language recently, and I have seen a lot of codes on the Internet that introduce how Javascript can solve problems on web pages, so I want to find a new way to use Javascript code to implement classic programming problems in C language. Of course, these C language programming questions are also relatively simple. They are mainly implemented through Javascript language to serve as grammar exercises. I also want to compare the similarities and differences between C language and Javascript language implementation, so as to consolidate memory and enhance learning effect! ! !
1. C language classic programming questions 1
1. Question description:
There is such an interesting mathematical problem in Marx's manuscript: There are 30 people, including men, women, and children. They ate in a restaurant and spent a total of 50 shillings. If it costs 3 shillings for each man to eat, 2 shillings for each woman, and 1 shilling for each child, how many men, women, and children are there?
2. Javascript code:
var man, woman, child; for (man = 0; man < 17; man ++) { for (woman = 0; woman <= 25; woman ++) { child = 30 - man - woman; if ( (man + woman + child == 30) && (man * 3 + woman * 2 + child == 50) ) { document.write("男人:"+man+","+"女人:"+woman+","+"小孩:"+child +"</br>"); } } }
3. Problem-solving instructions:
This question is a very classic question. It is the same type of question as "Change Change". It only requires multiple loops and one judgment to list each result. From this question, there is basically no difference between Javascript code and C language code, because the for statement and if statement, Javascript language and C language are the same. The main difference is reflected in the definition of variables and output statements. The overall feeling is that Javascript language is more convenient to implement. The main reason is that Javascript is a weakly typed language and C language is a strongly typed language.
2. C language classic programming questions 2
1. Question description:
Calculate e=1 1/1! 1/2! 1/3! ... 1/n! The top 50 items
2. Javascript code:
var n; var s = 1; var e = 1; for (n = 1; n <= 50; n ++) { s = s * n; e = e + (1 / s); } document.write(e);
3. Problem-solving instructions:
This question is a relatively simple question, and the implementation only uses a for loop. Compared with the C language code, the difference lies in the variable definition and output.
3. C language classic programming questions 3
1. Question description:
Input a number (no limit on digits) and output the number of digits in a number.
2. Javascript code:
<html> <head lang="en"> <meta charset="UTF-8"> <title>C语言经典题目3</title> <script> function demo() { var n = document.getElementById("number").value; if (!isNaN(n)) {var len = n.length; } else{ alert("请输入数字!"); return; } document.getElementById("number").value = len; } </script> </head> <body> <input type="text" id="number" width="100" height="50"> <button onclick="demo()">点我啊</button> </body> </html>
3. Problem-solving instructions:
This question is quite interesting. If it is implemented in C language, it is necessary to continuously divide this number by 10 to achieve the purpose of obtaining the length of the number. However, the weakly typed nature of the Javascript language makes it very simple to solve this problem with Javascript. The default number is a string type, and you only need to determine the length of the string. Although it is a bit lazy, it also achieves the function. In addition, the implementation of this question also presents something different from the C language, which is to solve the input problem. The C language uses scanf, but Javascript does not have such a function, so the input of numbers can only be achieved with the help of web pages.
4. C language classic programming questions 4
1. Question description:
Print the multiplication table
2. Javascript code:
function demo() { var i, j, s; for(i = 1; i < 10; i ++) { for(j = 1; j <= i; j ++) { s = i * j; document.write(j+"*"+i+"="+s+" "); } document.write("<br/>"); } }
3. Problem-solving instructions:
This question is also a very classic question, but it is very simple to implement. It only requires two loops to be nested. In addition to variable definition and output, there is another difference from C language, which is line break. For line breaks in C language, just use It works, but the web page doesn’t recognize it , so you can only use html's 076402276aae5dbec7f672f8f4e5cc81 to achieve line breaks.
5. C language classic programming questions 5
1. Question description:
Young singers participate in the Song Grand Prix. There are 10 judges to score. Try programming to find the average score of the contestants (removing the highest score and the lowest score)
2. Javascript code:
<body> <input id="getScore" type="text"> <button onclick="demo()">平均分</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. Problem solving instructions
This question should be considered to have the largest number of codes. Although the question is very simple, it encounters difficulties when entering ten scores because it cannot be entered one by one like C language. Therefore, I entered 1,2,3,4,5,6,7,8,9,10, and entered 10 at a time using commas. After the input is entered, the string must be split, so the split function is used.
5. Summary
I finally finished writing 5 classic C language questions in Javascript, which is also a good start for learning Javascript. Looking back on the process of solving each problem, I feel that Javascript is really similar to the C language, so it is relatively easy to get started, but it is a little different when processing input and output. If you ask me to evaluate, which one is better, Javascript or C language? I think Javascript is really easier and faster to solve problems. I prefer its weak type feature. I no longer have to worry about declaring variable types incorrectly. Of course, C language, as an eternal classic language, is also very good.
The above is the entire content of this article, I hope it will be helpful to everyone’s study.