说实话,我很难找到算法或理解这个问题。 我在网上搜索过,但我找到的答案都没有帮助。 问题说: 编写一个方法来计算名为SpecialSeries的类中的序列之和。 该方法的签名是:
public static double sumOfSeries(double x, int numTerms)
答案 0 :(得分:0)
我认为这段代码足以计算你的系列:
<?php
include('db.php');
if(isset($_POST['update']))
{
$hostname = "localhost";
$username = "root";
$password = "";
$databaseName = "winc sports";
$connect = mysqli_connect($hostname, $username, $password, $databaseName);
$id = $_POST['id'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$age = $_POST['age'];
$country=$_POST['country'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$select = "SELECT * FROM studens WHERE id = '$id'";
$selected = mysqli_query($connect, $select);
$row = mysqli_fetch_assoc($selected);
if (empty($_POST['fname'])) {$fname = $row['fname'];} else {$fname = $_POST['fname'];}
if (empty($_POST['country']))
{
$country = $row['country'];
}
else {
$country = $_POST['country'];
}
if (empty($_POST['id'])) {
$id = $row['id'];
}
else {
$id = $_POST['id'];
}
if (empty($_POST['age'])) {$age = $row['age'];} else {$age = $_POST['age'];}
if (empty($_POST['phone'])) {$phone = $row['phone'];} else {$phone = $_POST['phone'];}
if (empty($_POST['email'])) {$email = $row['email'];} else {$email = $_POST['email'];}
$query = "UPDATE students SET Fname= '$fname', Lname = '$lname', Nationality = '$country', PhoneNumber = '$phone', Email= '$email', Age = '$age' WHERE Id = '$id'";
$result = mysqli_query($connect, $query);
var_dump($result);
if($result)
{
echo 'Data Updated';
}else
{
echo 'Data Not Updated';
}
mysqli_close($connect);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP INSERT DATA USING PDO</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="updating.php" method="post">
<input type="text" name="id" placeholder="Enter new ID"><br><br>
<input type="text" name="fname" placeholder="Enter new First Name"><br><br>
<input type="text" name="lname" placeholder="Enter new Last Name"><br><br>
<input type="number" name="age" placeholder="Enter new age" min="13" max="90"><br><br>
<input type="text" name="country" placeholder="Enter new Nationality"><br><br>
<input type="number" name="phone" placeholder="Enter new Phone Number"><br><br>
<input type="text" name="email" placeholder="Enter new Email"><br><br>
<input type="submit" name="update" value="update">
</form>
</body>
</html>
答案 1 :(得分:0)
public class SpecialSeries {
public static double SumOfSeries(double x, int numTerms) {
if (numTerms == 1) {
return x;
}
int maxOdd = numTerms * 2 - 1;
int maxEven = numTerms * 2 - 2;
double powerTerm = Math.pow(x, maxOdd) / maxOdd;
int numerator = 1;
for (int numMult = maxOdd - 2; numMult > 2; numMult -= 2) {
numerator *= numMult;
}
int denominator = 2;
for (int numMult = maxEven; numMult > 2; numMult -= 2) {
denominator *= numMult;
}
return powerTerm * numerator / denominator + sumOfSeries(x, numTerms - 1);
}
public static void main(String[] args) {
try {
int numTerms = Integer.parseInt(args[0]);
double x = Double.parseDouble(args[1]);
System.out.println(sumOfSeries(x, numTerms));
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 2 :(得分:0)
现在,我们可以进行更多的数学简化,或继续进行实施
Java implimenttion
public class SpecialSeries {
public static double sumOfSeries(double x, int numTerms){
double sum = x;
//System.out.println("u["+0+"] = "+x);
for (int k = 1; k < numTerms; k++) {
double uk = Math.pow(x,2*k+1)/(double)(2*k+1);
for (int i = 2*k-1; i >0; i-=2) {
uk *= i/(double)(i+1);
}
//System.out.println("u["+k+"] = "+uk);
sum += uk;
}
return sum;
}
public static void main(String[] args) {
System.out.println(sumOfSeries(1, 5));
}
}