zoukankan      html  css  js  c++  java
  • B1010 一元多项式求导

    设计函数求一元多项式的导数。(注:xn​​(n为整数)的一阶导数为nxn1​​。)

    输入格式:

    以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过 1000 的整数)。数字间以空格分隔。

    输出格式:

    以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是 0,但是表示为 0 0

    输入样例:

    3 4 -5 2 6 1 -2 0
    
     

    输出样例:

    12 3 -10 1 6 0


    #include <iostream>
    #include <vector>
    using namespace std;
    
    /*
    注意:
    坑点:::无任何输出时,也输出'0 0' 
    这便是fp的作用 
    */
    
    int main(){
        int temp;
        vector<int> ans;
        bool flag = true, fp = true; //flag标记是否为第一次输出 
        do{
            scanf("%d", &temp);
            ans.push_back(temp);
        }while(cin.get()!='
    '); //以'
    '结尾的读入整数的方法 
        for(int i=0; i<ans.size(); i=i+2){
            if(ans[i+1]==0){
                continue;
            }
            temp = ans[i] * ans[i+1];
            if(flag){
                printf("%d %d", temp, ans[i+1]-1);
                flag = false;
            }else{
                printf(" %d %d", temp, ans[i+1]-1);
            }
            fp = false;
        }
        if((ans.size()==2 && ans[0]==0 && ans[1]==0) || fp){
            printf("0 0");
        }
        printf("
    ");
        return 0;
    }

    值得注意的是以下代码:

    do{
        scanf("%d", &temp);
        ans.push_back(temp);
    }while(cin.get()!='
    '); //以'
    '结尾的读入整数的方法

    不仅可以在未读到'EOF'时结束while循环,还可以吸收两个整数之间的空格~~例如:1####2(#代表空格),读入后,ans[0]为1;ans[1]为2。



  • 相关阅读:
    python分析文本文件/json
    python中文件操作
    python异常处理
    socket网络模块
    层模型--固定定位
    层模型--相对定位
    层模型--绝对定位
    什么是层模型?
    浮动模型
    流动模型/a标签换行问题
  • 原文地址:https://www.cnblogs.com/heyour/p/12206736.html
Copyright © 2011-2022 走看看