入门模拟 B1010——一元多项式求导

2019-12-20

09:38:48

What The Fuck? 只通过一个样例?

scanf("%d",&t)!=EOF这句很重要!
#include <bits/stdc++.h>
#include<math.h>
using namespace std;
const int MAX_LEN = 10005;
int main(){
    int temp[MAX_LEN];
    for(int i=0;i<MAX_LEN;++i){
        temp[i] = 0;
    } 
    int t;
    int count = 0;//计数器 
    while(scanf("%d",&t)!=EOF){
        temp[count] = t;
        count++;
    }
    int result[count];
    for(int i =0;i<count;++i){
        result[i] = 0;
    }
    for(int i=0;i<count-1;){
        int temp1 = i;
        int temp2 = i+1;
        result[i]  = temp[temp1] * temp[temp2];
        result[i+1] = temp[temp2]-1;
        i += 2;
    }
    for(int i = 0;i<count-1;++i){
        if(result[i]!=0){
            cout<<result[i]<<" "; 
        }else{
            cout<<result[i];
        }
    }
    system("pause");
    return 0;
} 

修复了一个BUG后: 如果求导之后没有任何非零项,需要输出0 0,这是本题的一个陷阱

#include <bits/stdc++.h>
#include<math.h>
using namespace std;
const int MAX_LEN = 10005;
int main(){
    int temp[MAX_LEN];
    for(int i=0;i<MAX_LEN;++i){
        temp[i] = 0;
    } 
    int t;
    int count = 0;//计数器 
    while(scanf("%d",&t)!=EOF){
        temp[count] = t;
        count++;
    }
    int result[count];
    for(int i =0;i<count;++i){
        result[i] = 0;
    }
    for(int i=0;i<count-1;){
        int temp1 = i;
        int temp2 = i+1;
        result[i]  = temp[temp1] * temp[temp2];
        result[i+1] = temp[temp2]-1;
        i += 2;
    }
    for(int i = 0;i<count-1;++i){
        if(result[0] == 0){
            printf("0 0");
            break;
        }
        if(result[i]!=0){
            cout<<result[i]<<" "; 
        }else{
            cout<<result[i];
            break;
        }
    }
    system("pause");
    return 0;
} 

原文地址:https://www.cnblogs.com/JasonPeng1/p/12071549.html