以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是0,但是表示为“0 0”。
3 4 -5 2 6 1 -2 0
12 3 -10 1 6 0
//每次输两个数n,e,注意以下几点:
//1.第一次输入没有空格,之后输入之前都需要一个空格,flag判断是否第一次
//2.如果求导系数为零,则不输出,判断语句if (n*e)
//3.如果输入的为零多项式,flag=false,则输出0 0
#include <iostream>
using namespace std;
int main()
{
int n, e;
bool flag = false;
while (cin>>n>>e)
{
if (n*e)
{
if (flag)
cout<<" ";
cout<<n*e<<" "<<e-1;
flag = true;
}
}
if (!flag)
cout<<"0 0"<<endl;
system("pause");
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
int t = 1,coef,ex;
while(scanf("%d%d",&coef,&ex)!=EOF){
if(ex==0&&t==1) {printf("%d %d",0,0);break;}
else if(ex==0&&t>1) continue;
else{
if(t>1) printf(" ");
printf("%d %d",coef*ex,ex-1);
}
t++;
}
return 0;
} 这道题就是个坑
第一个坑:数字之间可能有多个空格 如果你是用Java切割字符串的话
第二个坑:当系数项是0的时候输出0 0 *如:3 4 -5 2 6 1 0 1 对应输出是12 3 -10 1 6 0 0 但是题目给出的输出是 12 3 -10 1 6 0
第三个坑:当系数项不是0,指数是0的时候 什么也不输出 *如:3 4 -5 2 6 1 -2 0 对应输出是12 3 -10 1 6 0 (-2 0没对应的数字输出)
import java.util.ArrayList;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[]newStr = str.split("\\s+"); //切割字符串
ArrayList<Integer>alist = new ArrayList<Integer>();
for(int i=0 ;i<newStr.length ;i+=2){
int j=i+1;
if(Integer.valueOf(newStr[i])==0){ //常数项为0
alist.add(0);
alist.add(0);
}
if(Integer.valueOf(newStr[i])!=0&&Integer.valueOf(newStr[j])==0){ //指数为0且常数项不为0
}
if(Integer.valueOf(newStr[i])!=0&&Integer.valueOf(newStr[j])!=0){ // 常数项和指数项都不为0
alist.add(Integer.valueOf(newStr[i])*Integer.valueOf(newStr[j]));
alist.add(Integer.valueOf(newStr[j])-1);
}
}
if(alist.isEmpty()){ //如果将要输出的是空字符串,那么就输出0 0
System.out.println("0 0");
}else{
for(int i=0 ;i<alist.size() ;i++){
System.out.print(alist.get(i));
if(i!=alist.size()-1){
System.out.print(" "); //行末不能有空格 控制空格的输出
}
}
System.out.println();
}
}
}
#include<stdio.h>
int main()
{
int i=1;
int a,a1;
int b,b1;
while (scanf("%d%d", &a, &b) != EOF)
{
a1 = a*b;
b1 = b - 1;
if (i)
{
if (b1 != -1)
{
printf("%d %d", a1, b1); //第一个数前面无空格
i = 0;
}
}
else
{
if (b1 != -1)
printf(" %d %d", a1, b1); //之后的数设置前空格,这样最后一个数后面就不会有空格
}
if (i)
printf("%d %d", 0, 0);
}
}
#include<stdio.h>
int main(int argc, char** argv){
int c,e; //系数,指数
int prespace=0; //是否打印前置空格
int iszero=1; //是否为0多项式
while(scanf("%d%d",&c,&e)!=EOF){
if(c==0 || e==0) continue;
if(prespace) printf(" ");
prespace=1;
iszero=0;
printf("%d %d",c*e,e-1);
}
if(iszero) printf("0 0");
return 0;
}
又是水题一道。。。
根据一阶导数的规则 ==x^n^(n为整数)的一阶导数为n*x^n-1^== 进行求导即可。
#include <iostream>
using namespace std;
int main() {
int n = 0, m = 0;
//用于记录是否是第一次输出
bool isFirst = true;
while (scanf("%d %d", &n, &m) != EOF) {
if (m == 0) {
//常数的导数为0,不输出
continue;
}
//导数规则 x^n(n为整数)的一阶导数为n*x^(n-1)
n *= m;
m -= 1;
if (!isFirst) {
//非第一次输出需要输出空格间开
printf(" ");
}
printf("%d %d", n, m);
isFirst = false;
}
//如果所有数据读取完毕后,然后没有合格的输出,此时需要输出结果0 0
if (isFirst) {
printf("0 0\n");
} else {
printf("\n");
}
return 0;
}
————————————————
版权声明:本文为CSDN博主「hestyle」的原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://hestyle.blog.csdn.net/article/details/104763133
#include<stdio.h>
(737)#include<stdlib.h>
#include<string.h>
int main()
{
char nums[100];
char index[10];
int j=0;//j数字长度
int k=0;//整型数组计数器
int temp;
int answers[20];
memset(answers,-1,sizeof(answers));
int m=0;
gets(nums);
for(int i=0;i<100 && nums[i] != '\0';i++)
{
if(nums[i] == ' ')
{
m=0;
for(;m<j;m++)
{
index[m] = nums[i-j+m];
}
index[m] ='\0';
temp = atoi(index);
answers[k] = temp;
k++;
j=-1;
}
if(nums[i+1] == '\0')
{
i=i+1;
m=0;
for(;m<=j;m++)
{
index[m] = nums[i-j-1+m];
}
index[m] ='\0';
temp = atoi(index);
answers[k] = temp;
i=i-1;
}
j++;
}
for(int s=0;s<k && answers[s] != -1 && answers[s+1] != -1 ;s+=2)
{
//if()
{
if(answers[s+1]== 0)
answers[s]=answers[s+1]=0;
else
{
answers[s] *=answers[s+1];
answers[s+1]--;
}
}
}
for(int s=0;s<k;s +=2)
{if(answers[s+1]!= 0 && answers[s]!= 0)
{
if(s+1 == k)
printf("%d %d",answers[s],answers[s+1]);
else
printf("%d %d ",answers[s],answers[s+1]);
}
else
{
if(answers[s+1]== 0) {printf("%d %d",answers[s],answers[s+1]);break;}
if(s==0) printf("0 0");break;
//else
}}
return 0;
}
求解答!!!为什么提交了答案正确,但是有一个段错误??
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 10000
int main()
{
char func[MAX] = {0};
int res[MAX] = {0};
while(gets(func))
{
int fun[MAX] = {0};
int i = 0, j = 0;
int temp = 1;//记录当前值的正负
while(1)
{
if(func[i] == ' ' || func[i] == '\0')
{
if(func[i] == ' ' && func[i + 1] == ' ')
{
i++;
continue;
}
if(func[i] == ' ' && func[i + 1] == '\0')
break;
fun[j] *= temp;
j++;
temp = 1;
if(func[i] == '\0')
break;
}
else if(func[i] == '-')
{
temp = -1;
}
else
{
fun[j] = fun[j] * 10 + (func[i] - '0');
}
i++;
}
/*
for(int x = 0; x < j; x++)
printf("%d ", fun[x]);
printf("\n");
*/
for(i = 0; i < j; i++)
{
res[i * 2] = fun[i * 2] * fun[i * 2 + 1];
res[i * 2 + 1] = fun[i * 2 + 1] - 1;
}
/////////////////////////////////////
/*for(int x = 0; x < j; x++)
printf("%d ", res[x]);
printf("\n");
*/
/////////////////////////////////////
//printf("%d\n", j);
if(res[j - 1] == -1)
{
j -= 2;
if(j <= 0)
{
res[0] = 0;
res[1] = 0;
j = 2;
}
}
for(i = 0; i < j; i++)
{
if(i == 0)
printf("%d", res[i]);
else
printf(" %d", res[i]);
}
printf("\n");
}
return 0;
} 唉,看了大佬的回答我才发现我写的太繁琐了import java.util.ArrayList;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String buffer = in.nextLine();
String[] strings = buffer.split("\\s+");
ArrayList<Integer> source = new ArrayList<>();
for (String s : strings)
{
source.add(Integer.parseInt(s));
}
ArrayList<Integer> out = new ArrayList<>();
for (int i = 0; i < source.size(); i += 2)
{
if (source.get(i + 1) != 0)
{
if (source.get(i) == 0)
{
out.add(0);
out.add(0);
} else
{
out.add(source.get(i) * source.get(i + 1));
out.add(source.get(i + 1) - 1);
}
}
}
if (out.isEmpty())
System.out.println("0 0");
else
{
for (int i = 0; i < out.size(); i++)
{
System.out.print(out.get(i));
if (i != out.size() - 1)
System.out.print(' ');
}
}
}
}
#include <iostream>
using namespace std;
int main() {
int i, j;
bool flag = 0;
while (cin >> i >> j) {
if (j == 0 || i == 0)continue;
if (flag)cout << " ";
cout << i * j << " " << j - 1;
flag = 1;
}
if (!flag)cout << "0 0";
return 0;
}
#include <iostream>
using namespace std;
struct Poly
{ int coe; int exp;
} poly[1000 + 10];
int main()
{ int len = 0; int coe, exp;
while (cin >> coe >> exp)
{ poly[len].coe = coe;
poly[len++].exp = exp;
if (getchar() == '\n')
{ break; } }
for (int i = 0; i < len; ++i)
{ if (!poly[i].exp && len == 1)
{ cout << "0 0"; continue; }
else if (!poly[i].exp) { continue; }
cout << poly[i].coe * poly[i].exp << " " << poly[i].exp - 1 << (i == len - 1 ? "" : " "); }
cout << endl; return 0;
}
//1042.一元多项式求导
#include <iostream>
#include <string> //c++11 std::to_string
//string to_string(int val);
using namespace std;
int main()
{
int m,n,space=1;
string out;
while(cin>>m>>n){
if(n==0){
if(space!=1) continue;
else{ m=0;n=0;}
}else{
m=n*m;
n=n-1;
}
if(space==1){out=to_string(m)+' '+to_string(n); space=0;}
else {out=out+' '+to_string(m)+' '+to_string(n);}
}
cout<<out<<endl;
return 0;
}