PAT乙级1010

题目链接

https://pintia.cn/problem-sets/994805260223102976/problems/994805313708867584


我有点看不懂题目……,在网上找题解后,经测试推导题意发现:

如果输入的式子(一项或多项均可)中的常数是0时,输入是应该有0 0的。

而导数是多项时,如果最后一项是0,是不用输出0 0的;如果导数是一项时..

反正极其奇怪,反正是5个测试点都过了,主要我不是很懂题目的要求。

我的方法(题解1)和网上的一个方法(题解2)也有可能是错的,因为当输入是1 1时,程序都会继续等待输入,而非终止。或者我没找到正确的题解。

题解一

// PAT BasicLevel 1010
// https://pintia.cn/problem-sets/994805260223102976/problems/994805313708867584

#include <iostream>
using namespace std;

int main()
{
    // 系数
    int coefficient;
    // 指数
    int exponent;

    // 获取第一项
    cin >> coefficient >> exponent;

    // exponent==0说明是最后一项,即只有一项
    if (exponent==0){
        cout << "0 0";
    }
    // 多于一项
    else{
        // 输出第一项的导数
        cout << coefficient * exponent << ' ' << exponent - 1 ;

        // 输出后几项的导数
        while (cin >> coefficient >> exponent)
        {
            // 是最后一项
            if (exponent == 0)
            {
                break;
            }
            // 不是最后一项
            else
            {
                cout << ' ' << coefficient * exponent << ' ' << exponent - 1;
            }
        }
        
    }
  
    system("pause");
    return 0;
}

题解二

参考链接:https://blog.csdn.net/song68753/article/details/81710228

#include <iostream>
using namespace std;
int main()
{
    int a, b;
    int x = 1;
    while (cin >> a >> b) //输入
    {
        if (b == 0)
            break; //舍弃
        if (!x)
            cout << " ";
        else
            x = 0;
        cout << a * b << " " << b - 1;
    }
    if (x)
        cout << "0 0";
    system("pause");
    return 0;
}

作者:@臭咸鱼

转载请注明出处:https://www.cnblogs.com/chouxianyu/

欢迎讨论和交流!


posted @ 2019-08-04 23:47  臭咸鱼  阅读(405)  评论(0编辑  收藏  举报