HDoj Integer Inquiry(大数)

2024-06-15 19:58
文章标签 integer 大数 hdoj inquiry

本文主要是介绍HDoj Integer Inquiry(大数),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  真心要哭了。。这几天在搞大数  高精度计算  昨晚在机房敲 很快敲完了  就是过不了啊过不了  劳资都想骂脏话啊   NMB  一开始不输出前面的0啊 过不了  看discuss 百度 找了个AC的代码 找了几组测试数据  那个代码输出前面的0啊  我的妈  今天有找了个代码 不输出0啊 我的天。。。真心要被逼疯了  幸好还是AC了。。。算是有进步吧  之前的心态肯定坚持不下来啊    

  

杭电ACM 2014暑期集训队——选拔安排~

Integer Inquiry

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11714    Accepted Submission(s): 2942


Problem Description
One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. 
``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.) 

Input
The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative). 

The final input line will contain a single zero on a line by itself.

Output
Your program should output the sum of the VeryLongIntegers given in the input. 


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Sample Input
      
1123456789012345678901234567890 123456789012345678901234567890 123456789012345678901234567890 0

Sample Output
      
370370367037037036703703703670

Source
East Central North America 1996

Recommend


 题意很简单  就是输入一连串数字作为一个大的整数   遇到单个0结束输出

 每个CASE输出之间  输出一个空行

 我的测试数据:

5
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0


000
00
0


02
088
0


9999
99
0


00
00
0


代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;


const int maxn=110;
char s[maxn];
int res[maxn];
int num[maxn];


int main()
{
freopen("test.txt","r",stdin);
    int n;
    cin>>n;
    getchar();
    while(n--)
    {
        memset(res,0,sizeof(res));
        int m=0;
        while(1)
        {
            gets(s);
            if(strlen(s)==1&&s[0]=='0')
                break;
            int k=0;
            for(int i=strlen(s)-1;i>=0;i--)
            {
                num[k++]=s[i]-'0';
                //cout<<num[k-1]<<" ";
            }
//cout<<endl;
            m=max(m,k);
//cout<<"m1="<<m<<endl;
            if(m>k)
            {
                for(int i=k;i<m;i++)
                    num[i]=0;
            }
            for(int i=0;i<m;i++)
            {
                res[i]+=num[i];
                res[i+1]+=res[i]/10;
                res[i]=res[i]%10;
            }


//cout<<"res[m]="<<res[m]<<"res[m-1]"<<res[m-1]<<endl;
            if(res[m]>0)
            {
                res[m]+=res[m-1]/10;
                res[m-1]=res[m-1]%10;
                m++;
            }
        }
//cout<<"m2="<<m<<endl;
//        for(int i=m-1;i>=0;i--)
//            cout<<res[i];
        int flag=0;
        int x=m-1;
        for(;x>=0;x--)
        {
            if(res[x]>0)
                flag=1;
            if(flag)
                cout<<res[x];
        }
        if(flag==0)
            cout<<0;//如果输入的全是0   输出0
        cout<<endl;
        if(n)
            cout<<endl;
    }
    return 0;
}



终究还是AC了  好开心。。。。




这篇关于HDoj Integer Inquiry(大数)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1064428

相关文章

泛型和Integer小结

泛型在Java.util里面找:也可对其他你使用的函数进行查阅 如下:只要函数后面跟有尖括号<>,你都可以拿过来使用泛型     注意:用到集合时尽量使用泛型       int与Integer   integer这个不是关键字,是java的一个类。也就是int的包装类。int是基本数据类型,integer是引用类型,包含很多属性和方法,而int只是一个值,没有其他的任何

[leetcode] 43. Multiply Strings(大数相乘)

Multiply Strings 描述 Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2. Note: 1. The length of both num1 and num2 is < 110. 2. Both num1 an

Integer能表示十进制的多少位?

Java 提供两种不同的类型:引用类型和原始类型(或内置类型)。Int是java的原始数据类型,Integer是java为int提供的封装类。在32位机器上,能表示的最大值是2^32 = 4 294 967 296。 111111111111超过了最大表示范围,所以会报错。   超过了Integer的存值范围,Integer存值的范围是 -2的31次方 到 2的31次方-1 的常量   I

Integer 类型数据比较相等的那些坑

public void testInter() {Integer a = new Integer(200);Integer b = new Integer(200);Integer c = 200;Integer e = 200;int d = 200;Object o=200;System.out.println("基本类型和数字常量 ==判断"+(o==c));System.out.pr

再来一种求大数阶乘的方法

过多的我就不说了,直接看看代码: java版的: /*** */package Factorial;/*** @author 牟尼(昵称)* @blog:http://blog.csdn.net/u012027907**/public class Factorial {/** 大数阶乘计算类*//** 分析:大数阶乘用数组来存储,如5的阶乘可存在数组cal[]中,表示为* 0

String与Integer的相互转化

一、Integer转String //方法一:Integer类的静态方法toString()Integer a = 2;String str = Integer.toString(a)//方法二:Integer类的成员方法toString()Integer a = 2;String str = a.toString();//方法三:String类的静态方法valueOf()Intege

(VB.Net)Integer转 Byte数组

1、Integer转单个字节 Public Function iByte(ByVal i As Integer) As ByteDim b() As Byte = BitConverter.GetBytes(i)Return b(0)End Function 2、Integer转双字节 '低字节在前,高字节在后Public Function iByte2(ByVal i As Integ

java int 与 integer的区别

int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型和复杂数据类型 int 是前者而integer 是后者(也就是一个类);因此在类进行初始化时int类的变量初始为0.而Integer的变量则初始化为null. 2.初始化时:

[leetcode]397. Integer Replacement

[leetcode]397. Integer Replacement第四次周赛第二题 Given a positive integer n and you can do operations as follow:If n is even, replace n with n/2.If n is odd, you can replace n with either n + 1 or n - 1.

Integer类的方法

java.lang 类 Integer java.lang.Object   java.lang.Number       java.lang.Integer 所有已实现的接口: Serializable, Comparable<Integer> public final class Integer extends Number implements Comparable<Integ