zoukankan      html  css  js  c++  java
  • PAT——甲级1065:A+B and C(64bit) 乙级1010一元多项式求导

    甲级1065

    1065 A+B and C (64bit) (20 point(s))

    Given three integers A, B and C in [263​​,263​​], you are supposed to tell whether A+B>C.

    Input Specification:

    The first line of the input gives the positive number of test cases, T (10). Then T test cases follow, each consists of a single line containing three integers A, B and C, separated by single spaces.

    Output Specification:

    For each test case, output in one line Case #X: true if A+B>C, or Case #X: false otherwise, where X is the case number (starting from 1).

    Sample Input:

    3
    1 2 3
    2 3 4
    9223372036854775807 -9223372036854775808 0
    

    Sample Output:

    Case #1: false
    Case #2: true
    Case #3: false

    这道题的意思就是要比较A+B与C的大小,看似很简单,但ABC都是64位的,也就是long long型的,A+B之后还可能越界。

    重点就是在处理越界问题上。

     1 #include<cstdio>
     2 int main() {
     3     int n;
     4     scanf("%d", &n);
     5     for(int i=1;i<=n;i++)
     6     {
     7         long long a, b, c;
     8         scanf("%lld %lld %lld", &a, &b, &c);
     9         long long temp = a + b;
    10         bool flag;
    11         if (a > 0 && b > 0 && temp < 0) flag = true;//当正溢出的时候
    12         else if (a < 0 && b < 0 && temp >= 0) flag = false;//当负溢出的时候
    13         else if (temp > c)flag = true;//当不溢出的时候
    14         else flag = false;
    15         if (flag) printf("Case #%d: true
    ",i);
    16         else printf("Case #%d: false
    ",i);
    17     }
    18     return 0;
    19 }

    代码很好理解,主要记住一点

    正越界的时候值是负的。

    负越界的时候值是正的。这要根据数据储存的符号位和负数的补码来理解。

    乙级的一道题

    1010 一元多项式求导 (25 point(s))

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

    输入格式:

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

    输出格式:

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

    输入样例:

    3 4 -5 2 6 1 -2 0
    

    输出样例:

    12 3 -10 1 6 0

    这道题也很简单,但是我的处理方法跟教材不一样。

     1 #include<cstdio>
     2 
     3 int main()
     4 {
     5     int count = 0;
     6     int a, b;
     7     while (scanf("%d%d", &a, &b)!=EOF)
     8     {
     9         if (count == 0) {
    10             if (b == 0) printf("0 0");
    11             else printf("%d %d", a*b, b - 1);
    12         }
    13         else if(b) printf(" %d %d", a*b, b - 1);
    14         count++;
    15     }
    16     return 0;
    17 }

    我是一次取两位数,再判断是不是第一位,和指数是不是0进行输出。

    注意:当只有指数为0的一项时,要输出0,0

    并且最后一个还不让输出空格。

    教材上是用的大小为1010的数组,储存n次项的系数。然后得到一个求导后的数组,再输出。这样反而有点麻烦了。

    在注意一点。。。while()后面别加;哈哈,这种低级错误让我一脸懵逼。

    scanf("%d%d", &a, &b)!=EOF一直读取到文档末尾。在命令行界面对应着ctrl+z;

  • 相关阅读:
    undo表空间
    SQL*Plus快速入门
    win10用命令net启动服务没权限解决办法
    Oracle表空间管理
    Oracle数据泵(上)
    windows的bat脚本
    Servlet映射细节
    JSP技术(一)
    690. Employee Importance
    BFS和DFS详解以及java实现(转载)
  • 原文地址:https://www.cnblogs.com/albert-yzp/p/9979816.html
Copyright © 2011-2022 走看看