zoukankan      html  css  js  c++  java
  • 序列求和_蓝桥杯

    问题

    问题描述
    求1+2+3+...+n的值。
    输入格式
    输入包括一个整数n。
    输出格式
    输出一行,包括一个整数,表示1+2+3+...+n的值。
    样例输入
    4
    样例输出
    10
    样例输入
    100

    说明:有一些试题会给出多组样例输入输出以帮助你更好的做题。

    一般在提交之前所有这些样例都需要测试通过才行,但这不代表这几组样例数据都正确了你的程序就是完全正确的,潜在的错误可能仍然导致你的得分较低。

    样例输出
    5050
    数据规模与约定
    1 <= n <= 1,000,000,000。

    说明:请注意这里的数据规模。

    本题直接的想法是直接使用一个循环来累加,然而,当数据规模很大时,这种“暴力”的方法往往会导致超时。此时你需要想想其他方法。你可以试一试,如果使用1000000000作为你的程序的输入,你的程序是不是能在规定的上面规定的时限内运行出来。

    本题另一个要值得注意的地方是答案的大小不在你的语言默认的整型(int)范围内,如果使用整型来保存结果,会导致结果错误。

    如果你使用C++或C语言而且准备使用printf输出结果,则你的格式字符串应该写成%I64d以输出long long类型的整数。

    解决

      数列求和:(首项 + 尾项)* 项数  / 2;

      大整数表示以及运算:java.math.BigInteger;(以下BigInteger的应用转自:https://www.cnblogs.com/wulitaotao/p/11360386.html)

    BigInteger 对象的创建

    BigInteger 类在 java.math.BigInteger 包中,首先引用该包。

    import java.math.BigInteger;

    BigInteger 对象的创建

    BigInteger a = new BigInteger("123"); // 这里是字符串

    改变 BigInteger 的值

    String str = "123";
    BigInteger a = BigInteger.valueOf(str);
    
    int num = 456;
    BigInteger a = BigInteger.valueOf(num);

    基本常量

    a = BigInteger.ONE // 1
    b = BigInteger.TEN // 10
    c = BigInteger.ZERO // 0

    BigInteger 的输入输出

    直接读入 BigInteger

    Scanner in = new Scanner(System.in); 
    while(in.hasNext()) //等同于!=EOF
    {
        BigInteger a;
        a = in.nextBigInteger();
        System.out.print(a.toString());
    }

    间接读入 BigInteger

    Scanner in = new Scanner(System.in); 
    while(in.hasNext()) //等同于!=EOF
    {
        String s = in.nextLine();
        BigInteger a = new BigInteger(s);
        System.out.print(a.toString());
    }

    BigInteger 直接输出

    System.out.print(a);

    BigInteger 转化成十进制表示的 String

    System.out.print(a.toString());

    BigInteger 转化成 p 进制表示的 String

    int p = 2;
    System.out.print(a.toString(p)); // 输出a的二进制

    BigInteger 二进制下的长度

    BigInteger n = new BigInteger("12");
    System.out.println(n.bitLength()); // 4

    BigInteger 的基本运算

    BigInteger 之间的比较

    BigInteger a = new BigInteger("123");
    BigInteger b = new BigInteger("456");
    
    System.out.println(a.equals(b)); // a == b 时为 true 否则为 false
    BigInteger a = new BigInteger("123");
    BigInteger b = new BigInteger("456");
    
    if(a.compareTo(b) == 0) System.out.println("a == b"); // a == b
    else if(a.compareTo(b) > 0) System.out.println("a > b"); // a > b
    else if(a.compareTo(b) < 0) System.out.println("a < b"); // a < b

    加法

    BigInteger a = new BigInteger("123");
    BigInteger b = new BigInteger("456");
    
    a.add(b);

    减法

    BigInteger a = new BigInteger("123");
    BigInteger b = new BigInteger("456");
    
    a.subtract(b);

    乘法

    BigInteger a = new BigInteger("123");
    BigInteger b = new BigInteger("456");
    
    a.multiply(b);

    除法

    BigInteger a = new BigInteger("123");
    BigInteger b = new BigInteger("456");
    
    a.divide(b);

    取余

    BigInteger a = new BigInteger("123");
    BigInteger b = new BigInteger("456");
    
    BigInteger c = b.remainder(a);
    System.out.println(c);

    除法取余

    BigInteger a = new BigInteger("123");
    BigInteger b = new BigInteger("456");
    
    BigInteger result[] = b.divideAndRemainder(a); // 该函数返回的是数组
    System.out.println("商是:" + result[0] + ";余数是:" + result[1]);

    最大公约数

    BigInteger a = new BigInteger("12");
    BigInteger b = new BigInteger("56");
            
    System.out.println(a.gcd(b)); // 4

    绝对值

    BigInteger a = new BigInteger("-12");
            
    System.out.println(a.abs()); // 12

    取反数

    BigInteger a = new BigInteger("-12");
            
    System.out.println(a.negate()); // 12

    BigInteger a = new BigInteger("2");
            
    System.out.println(a.pow(3)); // 8

    代码:

    package RuMenXunLian;
    
    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class XuLieQiuHe {
        public static void main(String args[]){
            Scanner in = new Scanner(System.in);
            BigInteger n = in.nextBigInteger();
            BigInteger res = (n.multiply(n.add(BigInteger.ONE))).divide(BigInteger.ONE.add(BigInteger.ONE));
            System.out.print(res);
        }
    }
  • 相关阅读:
    Kafka遇到30042ms has passed since batch creation plus linger time at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.valueOrError(FutureRecordMetadata.java:94)
    postgreSQL学习(二):pgsql的一些基础操作
    postgreSQL学习(一):在Linux下安装postgreSQL
    黑苹果 技嘉 B250M-DS3H-CF i57500 HD630 EFI引导驱动发布
    记录一次垃圾短信网站短链分析
    MYSQL timestamp NOT NULL插入NULL的报错问题
    Permissions 0755 for '/home/lonecloud/.ssh/id_rsa' are too open.
    工作笔记:/bin/bash^M: 坏的解释器: 没有那个文件或目录 问题解决
    nginx常用配置
    VMware配置centos虚拟机静态ip
  • 原文地址:https://www.cnblogs.com/LieYanAnYing/p/12037819.html
Copyright © 2011-2022 走看看