㈠ java求1-1/3+1/5-1/7+……
/*
* 创建日期 2012-4-2
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
/**
* @author lenovo
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*
*/
import java.util.Scanner;
public class Test40006 {
public static void main(String[] args) {
int ri, repeat;
int temp, flag;
double eps, item, sum = 0;
Scanner in=new Scanner(System.in);
repeat=in.nextInt();
for(ri=1; ri<=repeat; ri++){
eps=in.nextDouble();
/*--------------------*/
sum= count(repeat,eps);
/*---------------------*/
System.out.println((int)(sum*10000+0.5)/10000.);
}
}
/**
* @param repeat
* @param eps
* @return s 每一次的运行结果
*/
private static double count(int repeat, double eps) {
double s=0;
//首先计算 1-1/3+1/5-1/7+……
//需要考虑符号和分母变化
long count=0;//项目数 1是第一项 -0.3^3是第二项 ……
count=cishu(eps);//求出项目数
for (long j=1;j<=count;j++){//把一个项目数的多项式的和计算出来
s+=fuhao(j-1,(double)1/(2*j-1));
}
return s;
// TODO 自动生成方法存根
}
/**
* @param eps
* @return m 运行次数 从1开始是第一项,-0.3……3是第二项
*/
private static long cishu(double eps) {
long m=0,n=1;
while(Math.abs((double)1/(2*n-1))>=eps){
m++;n++;
}
return m;
}
/**
* @param count
* @return fu 每一项的值
*/
private static double fuhao(long count,double fumu) {
double fu=0;
fu=Math.pow(-1,count)*fumu;
return fu;
}
}
希望对你有帮助
㈡ 已知一组数据,用JAVA JFRAME利用最小二乘法求出该组数据的多项式拟合公式
/**
* 最小二乘法计算类
*
* @author Administrator
*
*/
public class LeastSquareMethod {
private double[] x;
private double[] y;
private double[] weight;
private int m;
private double[] coefficient;
public LeastSquareMethod(double[] x, double[] y, int m) {
if (x == null || y == null || x.length < 2 || x.length != y.length
|| m < 2)
throw new IllegalArgumentException("无效的参数");
this.x = x;
this.y = y;
this.m = m;
weight = new double[x.length];
for (int i = 0; i < x.length; i++) {
weight[i] = 1;
}
}
public LeastSquareMethod(double[] x, double[] y, double[] weight, int m) {
if (x == null || y == null || weight == null || x.length < 2
|| x.length != y.length || x.length != weight.length || m < 2)
throw new IllegalArgumentException("无效的参数");
this.x = x;
this.y = y;
this.m = m;
this.weight = weight;
}
public double[] getCoefficient() {
if (coefficient == null)
compute();
return coefficient;
}
public double fit(double v) {
if (coefficient == null)
compute();
if (coefficient == null)
return 0;
double sum = 0;
for (int i = 0; i < coefficient.length; i++) {
sum += Math.pow(v, i) * coefficient[i];
}
return sum;
}
private void compute() {
if (x == null || y == null || x.length <= 1 || x.length != y.length
|| x.length < m || m < 2)
return;
double[] s = new double[(m - 1) * 2 + 1];
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < x.length; j++)
s[i] += Math.pow(x[j], i) * weight[j];
}
double[] f = new double[m];
for (int i = 0; i < f.length; i++) {
for (int j = 0; j < x.length; j++)
f[i] += Math.pow(x[j], i) * y[j] * weight[j];
}
double[][] a = new double[m][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = s[i + j];
}
}
coefficient = Algorithm.multiLinearEquationGroup(a, f);
}
/**
* @param args
*/
public static void main(String[] args) {
LeastSquareMethod l = new LeastSquareMethod(
new double[] { 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 },
new double[] { 37.84, 44.55, 45.74, 63.8, 76.67, 105.59, 178.48, 355.27, 409.92 },
new double[] { 11, 12, 13, 14, 15, 16, 17, 18, 19 },
2);
double[] x = l.getCoefficient();
for (double xx : x) {
System.out.println(xx);
}
System.out.println(l.fit(2009));
}
}
㈢ 如何在JAVA中实现多次方计算
//呵呵,我也是刚刚才学这个OOP程设计的
//规划的不当,就不要见怪咯,我只是按照我的思想分析
//首先用面向对象的语方分析问题,这里有三个对象
//客户端是一个对象
//计算面积和周长是一个对象
//长方形是一个对象
public class Client{
public static void main(String []args){
ICalculate cal=new Calculate();
MyRectangle rect=new MyRectangle(10,5);
System.out.println(rect+"周长为:"+cal.calcuGirth(rect));
System.out.println(rect+"面积为:"+cal.calcuArea(rect));
rect=new MyRectangle(30,50);
System.out.println(rect+"周长为:"+cal.calcuGirth(rect));
System.out.println(rect+"面积为:"+cal.calcuArea(rect));
}
}
interface ICalculate{
int calcuArea(MyRectangle rect);
int calcuGirth(MyRectangle rect);
}
class Calculate implements ICalculate{
//计算面积的方法
public int calcuArea(MyRectangle rect){
int result=rect.getWidth()*rect.getHeight();
return result;
}
//计算周长的方法
public int calcuGirth(MyRectangle rect){
int result=(rect.getWidth()+rect.getHeight())*2;
return result;
}
}
class MyRectangle{
private int width;
private int height;
public MyRectangle(int width ,int height){
this.width=width;
this.height=height;
}
public int getWidth(){
return width;
}
public void setWidth(int width){
this.width=width;
}
public void setHeight(int height){
this.height=height;
}
public int getHeight(){
return height;
}
public String toString(){
return super.toString()+"[width="+width+",height="+height+"]";
}
}
㈣ 编程,输入一个多项式的项数n,计算下列表达式y=5+55+555+...+555...555
我用java写的一个...类型用的long 最多只能到 n只能是19我的想法可能不是很好..只是用个字符串temp记录,如果输入的是3. 第一次字符串是5...第二次55...第三次555这样的..每次结束后 将字符串转换成long 累加到sum..然后再清空字符串===========================================import java.util.Scanner;
public class nx { public void getY(int num){
long sum = 0; //记录总和的sum
for (int i = 1; i <= num; i++) {
String temp ="";//记录每次的变量
for (int j = 0; j < i; j++) {
temp +=5;//每次得到字符
}
sum += Long.parseLong(temp);//转换后 累加到sum
}
System.out.println(sum);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入n:");
int num = in.nextInt();
nx test = new nx();
test.getY(num);
}
}
==================================================运行结果:请输入n:10
6172839500
㈤ java的crc8校验,按指定多项式,求助
以下是我的分析,不知是否正确,你参考下1、首先来看你打java代码:crc=(byte)((crc>>1)^0x8c);和 crc=(byte)(crc>>1); 导致这个问题是因为byte的最高位符号位,转换的时候就出错了2、示例代码:package com.test;public class test {public static void main(String[] args) {byte[] ptr = { 1, 1, 1, 1, 1, 1 };byte res = getCrc(ptr);System.out.println();System.out.println((byte)( (1 >> 1) ^ 0x8c ) + ":" +( (1 >> 1) ^ 0x8c ) );}public static byte getCrc(byte[] ptr) {int crc = 0;for (int i = 0; i > 1) ^ 0x8c;} else {crc = crc >> 1;}}}return (byte) crc;}}
㈥ 计算多项式1!+2!+3!+.......+n!当和超过10000时输出累加及n的值
#include<stdio.h>
intfactorial(constint&n)
{
if(n==1||n==0)
return1;
returnn*factorial(n-1);
}
intmain()
{
intsum=0,n=1;
intnextSum=0;
while(nextSum<=10000)
{
sum+=factorial(n);
nextSum=sum+factorial(++n);
}
printf("sum=%d,n=%d ",sum,n);
return0;
}
㈦ 求一元n次多项式的值java实现
void AddPolyn(LinkList pa,LinkList pb)//多项式相加 { LinkList ha,hb,void Invert(LinkList L)//逆序输出链表 {LinkList p,q,r; p=L-
㈧ 用Java语言实现求一个矩阵的特征多项式的程序代码怎么写跪求高人指点~~~
矩阵规模如何?
如果矩阵比较小 (~10), 可以直接按照定义来计算, P[x] = det( x*I - A); 其中det是一个矩阵的行列式; 可以按照定义直接迭代求;
如果矩阵比较大(>100), 可以先求特征值, 然后构造矩阵特征多项式 P[x] = (x-x1)*(x-x2)... (x-xn), 其中xi为矩阵的第i个特征值.
㈨ 我想用java来实现一个 一元多项式的输入,我定义了一个类polymail 有两个成员变量 chef 和exp,
请把问题再描述清楚些,给定什么条件,想要什么结果?
最好能举个例子。恐怕很多人对一元多项式对忘了差不多了。
程序已完成。两个类,一个类Node。封装多项式用的。一个计算用的。链表直接用java里面的ArraryList了,不用自己再写结构了。若不知道怎么运行java类,请补充问题。
效果如下:
请输第1个项多式以回车结束: 格式如:2^0+8^3+7^5+6^6+7^9
2^0+8^3+7^5+6^6+7^9
请输第2个项多式以回车结束:
3^6+4^3+5+2^8+6^2
=======以下为输出结果======
多项式1:2+8x^3+7x^5+6x^6+7x^9
多项式2:5+6x^2+4x^3+3x^6+2x^8
多项式和:7+6x^2+12x^3+7x^5+9x^6+2x^8+7x^9
程序如下:
文件 Node.java
public class Node implements Comparable<Node>{
private int xishu;//系数
private int shu; //指数
public int getXishu() {
return xishu;
}
public void setXishu(int xishu) {
this.xishu = xishu;
}
public int getZhishu() {
return shu;
}
public void setZhishu(int shu) {
this.shu = shu;
}
public Node(){
}
public Node(int a,int b){
xishu=a;
shu=b;
}
/**
* 通过指数来比较整个对象大小
*/
public int compareTo(Node node) {
return this.shu-node.shu;
}
/**
* 当 系统与指数都相等时,说明整个对象都相等.
*/
@Override
public boolean equals(Object o) {
Node node = (Node) o;
if(this.shu==node.shu&&this.xishu==node.xishu){
return true;
}else{
return false;
}
}
}
文件: Test.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
/**
* @author Youngao
* @Time 2011-03-18
* @Relue 多项式加法规则:对于两个多项式中指数相同的子项,其系数相加,若系数的和非零,
* 则构成“和多项式”中的一项;对于指数不同的项,直接构成“和多项式”中的一项。
*/
public class Test {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("请输第1个项多式以回车结束: 格式如:2^0+8^3+7^5+6^6+7^9");
String str1 = stdin.nextLine();
List<Node> list1 = splitStr(str1);
System.out.println("请输第2个项多式以回车结束: ");
String str2 = stdin.nextLine();
List<Node> list2 = splitStr(str2);
System.out.println("=======以下为输出结果======");
printPolyn("多项式1:",list1);
printPolyn("多项式2:",list2);
//相加
List<Node> newlist = AddPolyn(list1,list2);
printPolyn("多项式和:",newlist);
}
/**
* 两一元多项式相加
*/
private static List<Node> AddPolyn(List<Node> list1, List<Node> list2) {
List<Node> deletenode1 = new ArrayList<Node>();
List<Node> deletenode2 = new ArrayList<Node>();
List<Node> newlist = new ArrayList<Node>();
for(int i=0;i<list1.size();i++){
Node list1node = list1.get(i);
for(int j=0;j<list2.size();j++){
Node list2node = list2.get(j);
//凭规则 重新组装 '和结果'的链表
if(list1node.getZhishu()==list2node.getZhishu()){
Node newnode = new Node();
newnode.setXishu(list1node.getXishu()+list2node.getXishu());
newnode.setZhishu(list1node.getZhishu());
deletenode1.add(list1node);
deletenode2.add(list2node);
newlist.add(newnode);
break;
}
}
}
//去除已被求和的各项
list1.removeAll(deletenode1);
list2.removeAll(deletenode2);
//拼接剩余所有项
for(int i=0;i<list2.size();i++){
Node list2node = list2.get(i);
if(!newlist.contains(list2node)){
newlist.add(list2node);
}
}
for(int i=0;i<list1.size();i++){
Node list1node = list1.get(i);
if(!newlist.contains(list1node)){
newlist.add(list1node);
}
}
// 按指数从低到高排序
Collections.sort(newlist);
return newlist;
}
/**
* 统一的打印方法
*/
private static void printPolyn(String str, List<Node> list) {
String result = str;
for(int i=0;i<list.size();i++){
Node node = list.get(i);
if(i==list.size()-1){
if(node.getZhishu()==0){
result += node.getXishu()+"";
}else{
result += node.getXishu()+"x^"+node.getZhishu();
}
}else{
if(node.getZhishu()==0){
result += node.getXishu()+"+";
}else{
result += node.getXishu()+"x^"+node.getZhishu()+"+";
}
}
}
System.out.println(result);
}
/**
* 分割字符串,并封装键表
*/
private static List<Node> splitStr(String str1) {
String[] strs = str1.split("\\+");
List<Node> list = new ArrayList<Node>();
for (int i = 0; i < strs.length; i++) {
String[] subnode = strs[i].split("\\^");
int xishu = Integer.valueOf(subnode[0].trim());
int shu = 0;
if(subnode.length>1)
shu = Integer.valueOf(subnode[1].trim());
Node node = new Node(xishu,shu);
list.add(node);
}
// 按指数从低到高排序
Collections.sort(list);
return list;
}
}
㈩ java语言用链表实现多项式减法代码 运行结果总是不对!求大神~
对于多项式减法,不是很了解,不过 你先把b的coef设为负数,然后再进行比较,这样的比较结果还会正确吗?