当前位置:网站首页>Chapter 2 control structure and function (programming problem)
Chapter 2 control structure and function (programming problem)
2022-06-30 03:22:00 【True question OK】
7-10 h0053. find the root of the quadratic equation
Use the formula x1 = (-b + sqrt(b* b-4* a* c))/(2* a), x2 = (-b - sqrt(b* b-4* a* c))/(2* a) Find the quadratic equation of one variable ax^2 + bx + c =0 The root of the , among a It's not equal to 0.
Input format :
The first line is the number of equations to be solved n.
rest n Each row contains three floating-point numbers a, b, c( They are separated by spaces ), Express the equations respectively ax^2 + bx + c =0 The coefficient of .
Output format :
The output shares n That's ok , Each row is the root of an equation :
If it's two real roots , The output :x1=...;x2 = ...
If two real roots are equal , The output :x1=x2=...
If it's two imaginary roots , The output :x1= Real component + Imaginary part i; x2= Real component - Imaginary part i
All real parts shall be accurate to the decimal point 5 position , Numbers 、 There is no space between symbols .
x1 and x2 The order of :x1 The real part of >Re The real part of ||(x1 The real part of ==x2 The real part of &&x1 The imaginary part of >=x2 The imaginary part of )
sample input :
3
1.0 3.0 1.0
2.0 -4.0 2.0
1.0 2.0 8.0
sample output :
Here is the corresponding output . for example :
x1=-0.38197;x2=-2.61803
x1=x2=1.00000
x1=-1.00000+2.64575i;x2=-1.00000-2.64575iimport scala.io.StdIn
import math.sqrt
object Main {
def main(args: Array[String]): Unit = {
var n = StdIn.readInt();
for (i <- 1 to n) {
var l = StdIn.readLine();
var s = l.split(" ");
var a = s(0).toDouble;
var b = s(1).toDouble;
var c = s(2).toDouble;
// println(a+" "+b+" "+c);
var part1 : Double = (0 - b) / (2 * a);
// println(part1);
var pd = b * b - 4 * a * c;
if (pd > 0) {
var part2 : Double = sqrt(pd) / (2 * a);
printf("x1=%.5f;x2=%.5f\n" ,part1+part2 ,part1-part2);
}
else if(pd == 0) {
printf("x1=x2=%.5f\n",part1);
}
else {
var part2 : Double = sqrt(-pd) / (2 * a);
printf("x1=%.5f+%.5fi;x2=%.5f%.5fi\n",part1,part2,part1,-part2);
}
}
}
}
7-12 h0016. Precision movement
Xiao Ming Learning modeling . She is interested in models with moving parts . As her first task , She made a size 2×n Rectangular box , It consists of two parallel guide rails and rectangular strips on each rail . The size of the short strip 1×a, Size of the strip 1×b. There is a plug at each end of the long rod , The short one is always between the two plugs .

As long as the short rod is between the stops , The rod can move along the guide rail , One rod at a time . therefore , Xiao Ming moves one of the rectangular bars and moves it , And the other one stays in place . first , Both bars are aligned with the side of the box , Xiao Ming wants them to align with the other side with as little movement as possible . In order to achieve the goal , At least how many times does he have to move ?
Input format :
Enter... On one line 3 It's an integer a、b and c(1<=a<b<=n<=10^7), Separated by spaces .
Output format :
Enter... For each group , Output the minimum number of times Xiaoming needs to move in one line .
sample input :
1 3 6
2 4 9
sample output :
5
7import scala.io.StdIn
object Main{
def main(args: Array[String]): Unit={
while(true){
var l = StdIn.readLine();
var s = l.split(" ");
var a = s(0).toInt;
var b = s(1).toInt;
var c = s(2).toInt;
var ans : Int = 0;
var x : Int = a;
var y : Int = b;
var z : Int = b - a;
while(x < c || y < c) {
if (x < c) {
x += z;
ans += 1;
}
if (y < c) {
y += z;
ans += 1;
}
}
println(ans)
}
}
}
7-13 h0010.sum
This topic requires reading 1 It's an integer n, Then the output 1~n The sum of all integers between .
Input format :
Enter... On one line 1 The absolute value is no more than 10000 The integer of n.
Output format :
Enter... For each group , Output in one line 1~n The sum of all integers between .
sample input :
-3
3
sample output :
Method 1 :
import scala.io.StdIn
object Main {
def main(args: Array[String]): Unit = {
while(true){
var n : Int = StdIn.readInt()
var s : Int = 0
if(n < 1){
for(i<- Range(n,2)) s += i
println(s)
}
else {
for (i <- 1 to n) s += i
println(s)
}
}
}
}Method 2 :
x > 0 1~x (1+x)*x/2
x <0 -x~1 -((1+x)*x/2 - 1)
import scala.io.StdIn
object Main {
def main(args:Array[String]):Unit={
while(true) {
var sum : Int = 0
var x = StdIn.readInt()
if (x >= 0)
sum = (x + 1) * x / 2
else if(x < 0) {
x = -x
sum = -((x + 1) * x / 2) + 1
}
println(sum)
}
}
}7-14 h0080. The diamond
Enter an odd number n, Output a by ‘*’ Composed of n Step hollow diamond .
Input format :
An odd number n.
Output format :
Output a by ‘*’ Composed of n Step solid diamond .
Refer to the output sample for specific format .
sample input :
5
sample output :
*
* *
* *
* *
* Law 1 :
import scala.io.StdIn
object Main{
def main(args: Array[String]): Unit = {
// Print white diamond : Hollow equilateral triangle + Hollow inverted triangle = Hollow diamond
var n : Int = StdIn.readInt()
// Hollow equilateral triangle
// 6 ==12
// 5 7 ==12
// 4 8 ==12
// 3 9 ==12
// 2 10 ==12
//1 11==12
// 2 10 ==12
// 3 9 ==12
// 4 8 ==12
// 5 7 ==12
// 6 ==12
// Each number represents each * No. is the column number of the row
// Because the diamond is symmetrical , So consider the upper part first
// By observing the upper part, it is 6 That's ok 11 Column
// The left half is 1 Behavior 6、 The first 2 Behavior 5、..............、 The first 6 Behavior 1; Therefore, the external circulation should use for(i <- 6 to (1,-1))
// use i Representative line number ,j Representative column number ; When i=j perhaps j=12-i Should output at this position * Number , Output spaces in other places .
for (i <- n / 2 + 1 to (1 ,-1)) {
for (j <- 1 to n) {
if (i == j || j == n + 1 -i) {
print("*")
}
else {
print(" ")
}
}
println()
}
// Inverted triangle
for (i <- 2 to n / 2 + 1) {
for (j <- 1 to n) {
if (i == j || j == n + 1 -i) {
print("*")
}
else print(" ")
}
println()
}
}
}Law two :
import scala.io.StdIn
object Main{
def main(args: Array[String]): Unit = {
var n = StdIn.readInt()
var a = n / 2
// The upper part of a prism
for (i <- 1 to n / 2 + 1) {
// Output * The blank line before No
for (j <- 1 to a) print(" ")
// Output asterisk
for (j <- 1 to 2 * i - 1) {
if (j == 1 || j == 2 * i - 1) print("*")
else print(" ")
}
// Output * The blank line after the No
for (j <- 1 to a) print(" ")
println()
a -= 1
}
// The bottom half of the diamond
a = 1
for (i <- n / 2 to (1, -1)) {
// Output * The blank line before No
for (j <- 1 to a) print(" ")
// Output asterisk
for (j <- 1 to 2 * i - 1) {
if (j == 1 || j == 2 * i - 1) print("*")
else print(" ")
}
// Output * The blank line after the No
for (j <- 1 to a) print(" ")
println()
a += 1
}
}
}7-16 h0157. Fibonacci number
Give you an integer n, Find out its Fibonacci number ?
Input format :
Give... In one line 1 No more than one. 30 The positive integer n.
Output format :
Output the value of Fibonacci number in one line .
sample input :
5
sample output :
1 1 2 3 5 Law 1 :
import scala.io.StdIn
object Main {
def main(args: Array[String]): Unit = {
var n = StdIn.readInt()
def f(x: Int):Int= {
if (x == 1) 1
else if (x == 2) 1
else f(x - 1) + f(x - 2)
}
var flag : Int = 0
for (i <- 1 to n) {
print(f(i) + " ")
}
}
}Law two :
import scala.io.StdIn;
import scala.collection.mutable.ArrayBuffer //ArrayBuffer Packages make arrays mutable
object Main { // class
def main(args: Array[String]): Unit = { //main Method
var n = StdIn.readInt();
def dg(n: Int): Array[Int] = { // The type is Int
def func(n: Int): Int = { // Define methods
if (n<=2) 1;
else func(n - 1) + func(n - 2); // features <=2, The output is 1,>2 by (n-1)+(n-2)
}
// println(func(1)) // Check the special case value data
val array = new ArrayBuffer[Int]; //new An object
for (i <- 1 to n) { // determine i Value range of
array.append(func(i)); // Go to buffer Add in func Data generated
}
array.toArray; //toArray
}
dg(n).foreach(x => print(s"$x ")); // Traverse the output
}
}
7-17 h0158. The problem of monkeys eating peaches
There is a pile of peaches , The monkey ate half of it on the first day , And one more ! Later, every day monkeys eat half of them , And then one more . When it comes to the third n days , When you want to eat again ( Haven't eaten yet ), Only found 1 A peach . problem : How many peaches are there in the first place ?
Input format :
Give... In one line 1 No more than one. 30 The positive integer n.
Output format :
Output the initial number of peaches in one line .
sample input :
10
sample output :
Peach =1534Law 1 :
import scala.io.StdIn
object Main {
def main(args: Array[String]): Unit = {
var n = StdIn.readInt();
val num = f(1,1,n);
print(" Peach ="+num);
}
def f(a : Int ,d : Int ,n : Int): Int = {
var ans = (a + 1) * 2;
var dcnt = d + 1;
if(dcnt == n) return ans;
else f(ans ,dcnt ,n);
}
}Law two :
import scala.io.StdIn
object Main {
def main(args: Array[String]): Unit = {
var n = StdIn.readInt()
var sum : Int = 1
for (i <- 1 to n - 1) {
sum = (sum + 1) * 2
}
println(" Peach =" + sum)
}
}边栏推荐
- Openssl3.0 learning 22 provider decoder
- 简单自定义mvc
- Note the use of export/import and class inheritance in ES6
- What is the metauniverse: where are we, where are we going
- Principle of device driver
- [untitled]
- QT中foreach的使用
- 图的邻接矩阵存储 C语言实现BFS
- 自定义MVC的使用
- 1151_ Makefile learning_ Static matching pattern rules in makefile
猜你喜欢

General paging (2)

mysql 主从数据库同步失败的原因

Compile a DLL without import table

TiDB 6.0:让 TSO 更高效丨TiDB Book Rush

F1c100s self made development board debugging process

【十分钟】manim安装 2022

DC/DC变换器轻载时三种工作模式的原理及优缺点

The next change direction of database - cloud native database
![[ten minutes] manim installation 2022](/img/54/7b895d785c7866271f06ff49cb20aa.png)
[ten minutes] manim installation 2022

The broadcast module code runs normally in autojs4.1.1, but an error is reported in pro7.0 (not resolved)
随机推荐
什么是外链和内链?
Some technology sharing
2022 new test questions for safety management personnel of metal and nonmetal mines (small open pit quarries) and certificate examination for safety management personnel of metal and nonmetal mines (s
1151_ Makefile learning_ Static matching pattern rules in makefile
Regular full match: the password consists of more than 8 digits, upper and lower case letters, and special characters
C#【高级篇】 C# 匿名方法【待补充Lambda表达式。。。】
Global and Chinese markets for active transdermal drug delivery devices 2022-2028: Research Report on technology, participants, trends, market size and share
Use of Arthas
Simple custom MVC
Tri rapide, index groupé, recherche de la plus grande valeur K dans les données
Redis高并发分布式锁(学习总结)
JS 互相引用的问题
X Book 6.97 shield unidbg calling method
Compile a DLL without import table
What are the defaults for Binding. Mode=Default for WPF controls?
C # basic learning (XIII) | breakpoint debugging
PHP two-dimensional array randomly fetches a random or fixed number of one-dimensional arrays
【实战技能】如何撰写敏捷开发文档
OP diode limit swing
[QT] QMap使用详解