解题思路:先模拟后调试
注意事项:主要是0和1的情况
参考代码:
import java.util.Scanner;
public class test_cc{
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
int num=input.nextInt();
int[]arr=new int[num+1];
for (int i = 0; i < arr.length; i++) {
arr[i]=input.nextInt();
}
f(num,arr);
}
private static void f(int num, int[] arr) {
// TODO Auto-generated method stub
for (int i = 0; i < arr.length; i++) {
if(arr[i]==0) {
num--;continue;
}
if(num==0) {
System.out.print(arr[i]>=0?("+"+arr[i]):arr[i]);
num--;
continue;
}
else if(num==1 && arr[i]!=1) {
System.out.print(arr[i]>=0?("+"+arr[i]+"x"):arr[i]+"x");
num--;
continue;
}else if(num==1 && arr[i]==1){
System.out.print(arr[i]>=0?("+"+"x"):"x");
num--;
continue;
}
if(arr[i]==-1){
System.out.print("-"+"x^"+num);
num--;
continue;}
if(arr[i]==1){
if(i==0)System.out.print("x^"+num);
else if(arr[i-1]!=0) System.out.print("+"+"x^"+num);
else System.out.print("x^"+num);
num--;
continue;}
if(i>0&& arr[i]>0) System.out.print("+"+arr[i]+"x^"+num);
else System.out.print(arr[i]+"x^"+num);
num--;
}
}
}
0.0分
0 人评分
A+B for Input-Output Practice (II) (C语言代码)浏览:622 |
C语言程序设计教程(第三版)课后习题11.5 (C语言代码)浏览:655 |
钟神赛车 (C++代码)浏览:906 |
简单编码 (C++代码)浏览:731 |
矩形面积交 (Java代码)浏览:1282 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:584 |
WU-蓝桥杯算法提高VIP-Quadratic Equation (C++代码)浏览:1808 |
DNA (C语言描述,数据结构)浏览:912 |
DNA (C语言代码)浏览:564 |
复数求和 (C语言代码)浏览:995 |