JAVA之升幂排序

定义一个一维的整数数组,其中存储随机生成的100个整数,利用你所熟悉的一种排序方法,对他们进行升幂排序,输出排序结果。

import java.util.Arrays;
import java.util.Random; 
    public class Arrayascending{ 

        public static void main(String args[ ]){//初始化整数数组

            int array[]=new int[100];

            for(int i=0;i<array.length;i++)

            array[i]=(int)(Math.random()*1000+1);



            for(int j=0;j<array.length-1;j++){//改变排列次序

                for(int k=j+1;k<array.length;k++){
                    int temp;
                    if(array[j]>array[k]){
                      temp=array[j];
                      array[j]=array[k];
                      array[k]=temp;
                    }

                }

            }   

        System.out.println(Arrays.toString(array)); 
        }
    }
  • 6
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以帮您设计一个一元多项式计算器,并提供升幂和降幂两种输出方式的C++代码。 首先,我们需要定义一个结构体来表示多项式的每一项,包括系数和指数: ```c++ struct Term { float coef; // 系数 int exp; // 指数 }; ``` 接下来,我们可以定义一个类来表示多项式,并实现常见的操作,例如加、减、乘、除等: ```c++ class Polynomial { private: vector<Term> terms; // 用 vector 存储多项式的每一项 public: Polynomial() { terms.push_back({0, 0}); // 初始化为 0 } void clear() { terms.clear(); terms.push_back({0, 0}); // 清空后初始化为 0 } void insert(float coef, int exp) { if (coef != 0) { // 如果系数不为零,则插入一项 for (auto &term : terms) { if (term.exp == exp) { term.coef += coef; return; } } terms.push_back({coef, exp}); } } Polynomial operator+(const Polynomial &rhs) const { Polynomial res; int i = 0, j = 0; while (i < terms.size() && j < rhs.terms.size()) { if (terms[i].exp > rhs.terms[j].exp) { res.insert(terms[i].coef, terms[i].exp); i++; } else if (terms[i].exp < rhs.terms[j].exp) { res.insert(rhs.terms[j].coef, rhs.terms[j].exp); j++; } else { res.insert(terms[i].coef + rhs.terms[j].coef, terms[i].exp); i++; j++; } } while (i < terms.size()) { res.insert(terms[i].coef, terms[i].exp); i++; } while (j < rhs.terms.size()) { res.insert(rhs.terms[j].coef, rhs.terms[j].exp); j++; } return res; } Polynomial operator-(const Polynomial &rhs) const { Polynomial res; int i = 0, j = 0; while (i < terms.size() && j < rhs.terms.size()) { if (terms[i].exp > rhs.terms[j].exp) { res.insert(terms[i].coef, terms[i].exp); i++; } else if (terms[i].exp < rhs.terms[j].exp) { res.insert(-rhs.terms[j].coef, rhs.terms[j].exp); j++; } else { res.insert(terms[i].coef - rhs.terms[j].coef, terms[i].exp); i++; j++; } } while (i < terms.size()) { res.insert(terms[i].coef, terms[i].exp); i++; } while (j < rhs.terms.size()) { res.insert(-rhs.terms[j].coef, rhs.terms[j].exp); j++; } return res; } Polynomial operator*(const Polynomial &rhs) const { Polynomial res; for (auto &a : terms) { for (auto &b : rhs.terms) { res.insert(a.coef * b.coef, a.exp + b.exp); } } return res; } Polynomial operator/(const Polynomial &rhs) const { Polynomial res, dividend = *this, divisor = rhs; while (dividend.terms.size() > 1 || dividend.terms[0].coef != 0) { Term t = dividend.terms.back() / divisor.terms.back(); res.insert(t.coef, t.exp); Polynomial tmp = divisor * Polynomial(t.coef, t.exp); dividend = dividend - tmp; dividend.terms.pop_back(); } reverse(res.terms.begin(), res.terms.end()); return res; } void print(bool increasing) const { if (terms.size() == 1 && terms[0].coef == 0) { cout << "0\n"; return; } bool first = true; for (auto it = terms.rbegin(); it != terms.rend(); it++) { if (it->coef == 0) continue; if (first) { first = false; } else { if (it->coef > 0) { cout << "+"; } } if (it->exp == 0 || it->coef != 1 && it->coef != -1) { cout << it->coef; } else if (it->coef == -1) { cout << "-"; } if (it->exp != 0) { cout << "x"; if (it->exp != 1) { cout << "^" << it->exp; } } } cout << "\n"; } }; ``` 在这个类中,我们重载了加、减、乘、除等运算符,并实现了一个 `print` 函数,用于将多项式输出为字符串。 在 `print` 函数中,我们使用了一个 `bool` 类型的参数 `increasing`,用于控制输出的顺序。如果 `increasing` 为 `true`,则按指数升序输出;否则按指数降序输出。 最后,我们给出一个简单的 `main` 函数作为示例: ```c++ int main() { Polynomial p1, p2; p1.insert(1, 2); p1.insert(2, 1); p1.insert(3, 0); p2.insert(1, 1); p2.insert(1, 0); cout << "p1 = "; p1.print(true); cout << "p2 = "; p2.print(false); Polynomial p3 = p1 + p2; cout << "p1 + p2 = "; p3.print(true); Polynomial p4 = p1 - p2; cout << "p1 - p2 = "; p4.print(false); Polynomial p5 = p1 * p2; cout << "p1 * p2 = "; p5.print(true); Polynomial p6 = p1 / p2; cout << "p1 / p2 = "; p6.print(false); return 0; } ``` 这个程序会输出: ``` p1 = 3x^0+2x^1+1x^2 p2 = x^1+1x^0 p1 + p2 = 1x^0+3x^1+1x^2 p1 - p2 = 1x^2+1x^1+2x^0 p1 * p2 = 1x^1+2x^2+4x^3+3x^4+1x^5 p1 / p2 = 2x^1+1x^0 ``` 以上就是一个简单的一元多项式计算器,并提供了升幂和降幂两种输出方式的C++代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq-120

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值