首页 > 试题广场 >

Integer Inquiry

[编程题]Integer Inquiry
  • 热度指数:3808 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    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.)

输入描述:
    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.

注意输入数据中,VeryLongInteger 可能有前导0


输出描述:
    Your program should output the sum of the VeryLongIntegers given in the input.
示例1

输入

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

输出

370370367037037036703703703670
import java.math.BigInteger;
import java.util.Scanner;

public class Main
{
    public static void main(String... as)
    {
        Scanner sc = new Scanner(System.in);
        BigInteger sum = BigInteger.ZERO;
        BigInteger b = sc.nextBigInteger();

        do
        {
            sum = sum.add(b);
            b = sc.nextBigInteger();
        }
        while (!b.equals(BigInteger.ZERO));
        System.out.println(sum);

        sc.close();
    }
}

发表于 2018-09-15 22:29:26 回复(1)
#include<iostream>
#include<cstdio>

using namespace std;

void sToArr(int *tmp,string s){
    for(int i=0;i<1000;i++)tmp[i]=0;
    for(int i=999;i>999-s.length();i--)tmp[i]=s[i-1000+s.length()]-'0';
}

void add(int *result,int *a,int *b){
    int jw=0;
    for(int i=999;i>=0;i--){
        if(a[i]+b[i]+jw>9){
            result[i]=a[i]+b[i]+jw-10;
            jw=1;
        }else{
            result[i]=a[i]+b[i]+jw;
            jw=0;
        }
    }
};

int main(){
    int result[1000],a[1000],b[1000],jw=0;
    string s;
    for(int i=0;i<1000;i++){
        result[i]=0;
        a[i]=0;
    }
    while(cin>>s&&s!="0"){
        sToArr(b,s);
        for(int i=0;i<1000;i++)a[i]=result[i];
        add(result,a,b);
    }
    int i=0;
    while(result[i]==0)i++;
    for(;i<1000;i++)cout<<result[i];
    cout<<endl;
}

发表于 2021-03-09 11:01:25 回复(0)
#include<bits/stdc++.h>
using namespace std;
struct bign {
	int d[110],len;
	bign() {
		memset(d,0,sizeof(d));
		len=0;
	}
};
bign change(string s) {
	bign a;
	for(int i=s.length()-1; i>=0; i--) {
		a.d[a.len++]=s[i]-'0';
	}
	return a;
}
bign add(bign a,bign b) {
	bign c;
	int carry=0,temp;
	for(int i=0; i<a.len||i<b.len; i++) {
		temp=a.d[i]+b.d[i]+carry;
		c.d[c.len++]=temp%10;
		carry=temp/10;
	}
	if(carry!=0) c.d[c.len++]=carry;
	return c;
}
void p(bign a) {
	for(int i=a.len-1; i>=0; i--) {
		printf("%d",a.d[i]);
	}
	printf("\n");
}
int main() {
	string s;
	bign a,ans;
	getline(cin,s);
	ans=change(s);
	while(getline(cin,s)) {
		if(s=="0") break;
		a=change(s);
		ans=add(a,ans);
	}
	p(ans);
}

发表于 2020-03-22 18:16:31 回复(0)
#include <stdio.h>
#include <string.h>
int main()
{
    char temp[101]={0};
    int ans[200]={0}; 
    int p=0;                     //p指示当前结果位数
    while(~scanf("%s",temp) && strcmp(temp,"0")!=0)
    {
        int l=strlen(temp),c=0,i;                 //c表示进位
        for(i=0;l-i-1>=0;i++)                     //将新输入的大数从低位(个位)到高位与之前结果相加
        {
            ans[i]=ans[i]+c+temp[l-i-1]-'0';     //即使当前输入的位数超过了当前结果,也不用管,当前结果高位全是0
            c=ans[i]/10;
            ans[i]=ans[i]%10;
        }
        while(c!=0)                   //进位不为0,继续加
        {
            ans[i]=ans[i]+c;         //超过了当前位数p也不用管,因为结果数组高位全是0
            c=ans[i]/10;;
            ans[i]=ans[i]%10;
            i++;
        }
        if(i-1>p)p=i-1;           //i-1指示算完后结果,若大于p,要更新p
    }
    while(ans[p]==0)p--;          //结果有可能还有前导0,比如003+2,输出005,因为p被更新了
    for(int i=p;i>=0;i--)printf("%d",ans[i]);
    printf("\n");
}

编辑于 2020-03-12 12:19:55 回复(0)
try:
    while 1:
        s = 0
        while 1:
            a = raw_input()
            if a == '0':
                break
            s += int(a)
        print s
except:
    pass

发表于 2016-12-27 16:28:46 回复(0)
只学了C语言,别的C答案看不懂,自己写个简单的
#include<stdio.h>
#include<string.h>
#define N 120
int main()
{
	char str[N];
	int ans[N];
	int i, len, flag;
	memset(ans, 0, sizeof(ans)); 
	while(scanf("%s", str))
	{	
		len=strlen(str);
		if( len == 1 && str[ 0 ] == '0' ) break;
		for(i=0; i<len; i++)
		{
			ans[i]=ans[i] + (int)(str[len-1-i]-'0');
			if(ans[i]>=10)
			{
				ans[i]%=10;
				ans[i+1]++;
			}
		}	
		memset(str, '0', sizeof(str));
	}
	for(flag=0, i=N-1; i>=0; i--)//倒序,当不为0时,flag标记1,开始输出
	{
		if(ans[i]==0&&flag==0)
			continue;
		else
			flag=1;
		printf("%d", ans[i]);
	}
	printf("\n");
	return 0;
}

发表于 2020-02-07 15:02:28 回复(0)
一开始漏了最后的进位
#include <bits/stdc++.h>
using namespace std;

vector<string> s;
int main() {
  string a, ans;
  int len = 0, add = 0;
  while (cin >> a) {
    if (a == "0") break;
    len = max(len, (int)a.size());
    reverse(a.begin(), a.end());
    s.push_back(a);
  }
  for (int i = 0; i < len; i++) {
    int sum = add;
    for (auto str: s) {
      if (str.size() <= i)  continue;
      sum += str[i] - '0';
    }
    ans.push_back(sum % 10 + '0');
    add = sum / 10;
  }
  while (add) {
    ans.push_back(add % 10 + '0');
    add /= 10;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}


编辑于 2024-03-13 19:11:26 回复(0)
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<int> add(vector<int> a, vector<int> b){
    int t=0;
    vector<int> res;
    for(int i=0; i<a.size() || i<b.size(); ++i){
        if(i<a.size()) t+=a[i];
        if(i<b.size()) t+=b[i];
        res.push_back(t%10);
        t /= 10;
    }
    if(t==1) res.push_back(1);
    return res;
}
vector<int> reverse(vector<int> str){
    vector<int> res;
    for(int i=str.size()-1; i>=0; --i){
        res.push_back(str[i]);
    }
    return res;
}
int main(){
    vector<string> integers(102);
    string temp;
    int k=0;
    for(int i=0; i<102&&temp!="0"; i++){
        cin >> temp;
        integers[i]=temp;
        if(temp=="0") integers[i].pop_back();
    }
    for(int i=integers.size()-1; integers[i]==""; --i){
        integers.pop_back();
    }
    vector<int> str,res;
    for(int i=0; i<integers.size(); ++i){
        str.clear();
        for(int j=integers[i].size()-1; j>=0; --j){
            str.push_back(integers[i][j]-'0');
        }
        if(i==0) {
            res=str;
        }else{
            res=add(res,str);
        }
    }
    for(int i=0; i<res.size(); ++i){
        cout << reverse(res)[i];
    }
    cout << endl;
}


编辑于 2024-03-03 20:51:04 回复(0)
#include <algorithm>
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
using namespace std;

const int MAXN = 10000;

struct VeryLongInteger {
    int* digit;
    int length;
    VeryLongInteger();
    VeryLongInteger(int x);
    VeryLongInteger operator=(string str);
    bool operator!=(int x);
    bool operator!=(const VeryLongInteger& x);
    VeryLongInteger operator+=(const VeryLongInteger& x);
    friend istream& operator>>(istream& in, VeryLongInteger& x);
    friend ostream& operator<<(ostream& out, const VeryLongInteger& x);
};

istream& operator>>(istream& in, VeryLongInteger& x) {
    string str;
    in >> str;
    x = str;
    return in;
}

ostream& operator<<(ostream& out, const VeryLongInteger& x) {
    for (int i = x.length - 1; i >= 0; i--) {
        out << x.digit[i];
    }
    return out;
}

VeryLongInteger::VeryLongInteger() {
    digit = new int[MAXN]();
    length = 0;
}

VeryLongInteger::VeryLongInteger(int x) {
    digit = new int[MAXN]();
    length = 0;
    if (x == 0) {
        digit[length++] = x;
    }
    while (x != 0) {
        digit[length++] = x % 10;
        x /= 10;
    }
}

VeryLongInteger VeryLongInteger::operator=(string str) {
    length = str.length();
    for (int i = 0; i < length; i++) {
        digit[i] = str[length - i - 1] - '0';
    }
    return *this;
}

bool VeryLongInteger::operator!=(int x) {
    return *this != VeryLongInteger(x);
}

bool VeryLongInteger::operator!=(const VeryLongInteger& x) {
    if (length != x.length) {
        return true;
    } else {
        for (int i = 0; i < length; i++) {
            if (digit[i] != x.digit[i]) {
                return true;
            }
        }
    }
    return false;
}

VeryLongInteger VeryLongInteger::operator+=(const VeryLongInteger& x) {
    int carry = 0;
    for (int i = 0; i < length || i < x.length; i++) {
        int current = digit[i] + x.digit[i] + carry;
        carry = current / 10;
        digit[i] = current % 10;
    }
    length = max(length, x.length);
    if (carry) {
        digit[length++] = carry;
    }
    return *this;
}

int main() {
    VeryLongInteger sum = 0, number;
    while (cin >> number && number != 0) {
        sum += number;
    }
    cout << sum << endl;
    return 0;
}

编辑于 2024-03-01 11:53:00 回复(0)
#include "stdio.h"
#include "string"
#include "algorithm"
using namespace std;
char buf[110][110];
int n = 0;//记录输入的行数为(n-1)

string addString(string num1,string num2){//字符串相加
    int length = num1.size()>num2.size()?num1.size():num2.size();
    while (num1.size() < length)
        num1 = "0" + num1;//补齐高位
    while (num2.size() < length)
        num2 = "0" + num2;//同理,补齐高位
    string num3 = "";int carry = 0;
    int temp;
    for (int i = length-1; i >= 0; --i) {
        temp = num1[i]-'0' + num2[i]-'0'+carry;
        if (temp > 9){
            num3 += temp-10+'0';
            carry = 1;
        } else{
            num3 += temp+'0';
            carry = 0;
        }
    }
    if (carry == 1)
        num3 += '1';
    reverse(num3.begin(),num3.end());
    return num3;
}

void Init(string &num1){//字符串初始化,消去前导0
    int i = 0;
    while (num1[i] == '0')
        ++i;
    num1.erase(0,i);
}

int main(){
    while (scanf("%s",buf[n++])!=EOF){
        string num = buf[n-1];
        if (num == "0")
            break;
    }
    n = n-1;
    string sum = "";
    for (int i = 0; i < n; ++i) {
        string num = buf[i];
        Init(num);
        sum = addString(sum,num);
    }
    printf("%s",sum.c_str());
}
发表于 2023-03-11 15:44:37 回复(0)
s = 0
while n := int(input()):
    s += n
print(s)

发表于 2021-03-30 02:23:58 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        BigInteger sum = new BigInteger("0");
        while ((s = br.readLine()) != null) {
            if (s.equals("0")) break;
            BigInteger num = new BigInteger(s);
            sum = num.add(sum);
        }
        System.out.println(sum);
    }

}


发表于 2021-03-18 09:42:09 回复(0)
大数加法,用字符串保存数字,模仿竖式计算

#include<iostream>
#include<string>
#include<cmath>

using namespace std;

string add(string a, string b){
    string res;
    int tmp=0;
    int size=max(a.size(), b.size());
    
    //数位对齐,便于竖式计算
    while(a.size()<size) a.insert(a.begin(), '0');
    while(b.size()<size) b.insert(b.begin(), '0');
    
    //从最低位开始加,每次将相加结果的个位数字保存,同时除以10获得进位
    //按字符串读入顺序,数位高的在左边(字符串前面),所以每次在字符串头部插入
    for(int i=size-1; i>-1; i--){
        tmp=a[i]+b[i]-2*'0'+tmp;
        
        res.insert(res.begin(),tmp%10+'0');
        tmp/=10;
    }
    
    //相加完成后还有进位
    while(tmp>0) {
        res.insert(res.begin(),tmp%10+'0');
        tmp/=10;
    }
    
    //去除前导零
    while(res[0]=='0') 
        res.erase(res.begin());
    
    return res;
}

int main(){
    string a, b;
    
    //循环相加
    while(cin>>a && a!="0")
      b=add(a, b);
    
    cout<<b<<endl;
    return 0;
}


发表于 2021-03-11 21:03:10 回复(0)
前导0恶心死了,半天没想出来哪有问题
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 110;
int sum[maxn] = {0};//从低位开始存储,便于对其 
int length = 0; //大数字的位数 

int main() {
	char str[maxn];//存储从最高位开始的大数字 
	while(scanf("%s", str) != EOF) {
		if(strlen(str) == 1 && str[0] == '0') break;//长度必须要为1,前导零存在
		int digit = 0;//位数 
		for(int i = strlen(str) - 1; i >= 0; --i) {
			sum[digit] = sum[digit] + str[i] - '0';
			if(sum[digit] >= 10) {
				int carry = sum[digit] / 10; //进位
				sum[digit] = sum[digit] % 10;
				sum[digit + 1] = sum[digit + 1] + carry; 
			}
			digit++;//下一位 
		}
	}
	int i = maxn;
	while(sum[i] == 0) i--;
	while(i >= 0) {
		cout<<sum[i];
		i--;
	}
	
	return 0;
}

发表于 2021-02-08 17:09:13 回复(0)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[110];
struct bign{
    int len;
    int num[110];
    bign(int l){
        memset(num,0,sizeof(num));
        len=l;
    }
}; 
bign change(char str[]){
    bign a(0);
    int len=strlen(str);
    a.len=len;
    for(int i=0;i<len;++i){
        a.num[i]=str[len-i-1]-'0';
    }
    return a;
}
bign add(bign a,bign b){
    int flag=0;
    int len=max(a.len,b.len);
    for(int i=0;i<len;++i){
        int temp=(a.num[i]+b.num[i]+flag)%10;
        flag=(a.num[i]+b.num[i]+flag)/10;
        a.num[i]=temp;
    }
    if(flag==1)
        a.num[len++]=1;
    a.len=len;
    return a;
}
int main(){
    bign a(0);
    while(scanf("%s",str)!=EOF){
    	int len=strlen(str);
        if(len==1&&str[0]=='0')
        {   for(int i=a.len-1;i>=0;--i)
               printf("%d",a.num[i]);
            break;
        }
        bign b=change(str);
        a=add(a,b);
    }
    return 0;
}

编辑于 2020-04-14 17:23:57 回复(0)
大数加法
#include <bits/stdc++.h>
using namespace std;
string cmp(string x, string y){
    int a[10001],b[10001];
    int x1=x.size();
    int y1=y.size();
    string u;
    for(int i=0;i<10001;++i){
        a[i]=0;
        b[i]=0;
    }
    int k=0;
    for(int i=10001-x1;i<10001;++i){
        a[i]=x[k]-'0';
        ++k;
    }
    k=0;
    for(int j=10001-y1;j<10001;++j){
        b[j]=y[k]-'0';
        ++k;
    }
    if(x1<y1){
        for(int i=10000;i>=10001-x1;--i){
            b[i]= b[i]+a[i];
        }
        for(int i=10000;i>0;--i){
            if(b[i]>9){
                b[i-1]++;
                b[i]=b[i]-10;
            }
        }
        int j=0;
        for(int i=0;i<10001;++i){
            if(b[i]!=0){
                j=i;
                break;
            }
        }
        for(int i=j;i<10001;++i)
            u+=b[i]+'0';
    }
    if(x1>=y1){
        for(int i=10000;i>=10001-x1;--i){
            a[i]= a[i]+b[i];
        }
        for(int i=10000;i>=0;--i){
            if(a[i]>9){
                a[i-1]++;
                a[i]=a[i]-10;
            }
        }
        int j=0;
        for(int i=0;i<10001;++i){
            if(a[i]!=0){
                j=i;
                break;
            }
        }
        for(int i=j;i<10001;++i)
            u+=a[i]+'0';
    }
    return u;
}
int main(){
    string s;
    string res="0";
    while(cin>>s){
        if(s=="0")
            break;
        res=cmp(res,s);
    }
    cout<<res<<endl;
    return 0;
}

发表于 2020-03-18 17:46:39 回复(1)
超长整,使用字符串存储,用字符串模拟加法
#include<iostream>
(720)#include<string>
#include<cstdio>
using namespace std;

string Add(string s1,string s2){
	int len1=s1.length();
	int len2=s2.length();
	if(len1>len2){
		for(int i=0;i<(len1-len2);i++){
			s2="0"+s2;
		}
	}else if(len2>len1){
		for(int i=0;i<(len2-len1);i++){
			s1="0"+s1;
		}
	}
	int remainder=0;//上一轮进位
	for(int i=s1.length()-1;i>=0;i--){
		int current=s1[i]-'0'+s2[i]-'0'+remainder;
		s1[i]=(current%10)+'0';
		remainder=current/10;
	}
	if(remainder!=0){
		s1=char(remainder+'0')+s1;
	}
	return s1;
}

int main(){
	string str,ans;
	ans="0";
	while(cin>>str){
		if(str=="0"){
			break;
		}
		ans=Add(ans,str);
	}
	cout<<ans;
	return 0;
}

发表于 2020-03-08 12:04:39 回复(0)
#include<bits/stdc++.h>
using namespace std;
int num[1000]={0},m,len=0;
int main(){
    string a;
    while(cin>>a){
        reverse(a.begin(),a.end());//翻转以方便对齐进位等
        int a1[1000]={0};
        for(int i=0;i<a.size();i++){
            a1[i]=a[i]-'0';//转化成整型
        }
        int jinwei=0;
        for(int i=0;i<a.size()+100;i++){//加100是因为最高位可能会出现不止一位的进位
            if(num[i]+a1[i]+jinwei<10){
                num[i]=num[i]+a1[i]+jinwei;
                jinwei=0;
            }
            else{
                num[i]=(num[i]+a1[i]+jinwei)%10;
                jinwei=1;
            }
        }
        int flag=0;
        if(a=="0"){
        for(int i=1000;i>=0;i--){
            if(num[i]!=0)
                flag=1;
            if(flag==1)
                cout<<num[i];
        }
            break;
        }
    }
    return 0;
}
这个最高位进位可能 不止1位 坑了我好久
发表于 2020-03-07 11:57:55 回复(0)
#include<string>
#include<iostream>
using namespace std;
void AddZero(string &a,string &b)//对其补零
 {int len;
if(a.size()>b.size())
 {len=a.size()-b.size();
for(int i=0;i<len;i++)
  {b='0'+b;}
  }
else if(a.size()<b.size())
{len=b.size()-a.size();
for(int i=0;i<len;i++)
{a='0'+a;}
}}
int main()
{string s;string tmp="";
while(cin>>s)
{if(s=="0"){break;}
 AddZero(s,tmp);int carry=0;
 for(int i=s.size()-1;i>=0;i--)
   {  int t=tmp[i]-'0'+s[i]-'0'+carry;
	   if(t>9){tmp[i]='0'+t-10;carry=1;}
	   else{tmp[i]='0'+t;carry=0;}
   }
 if(carry==1){tmp='1'+tmp;}//最高位可能溢出
  }
cout<<tmp<<endl;
	return 0;
}

编辑于 2020-02-17 21:13:27 回复(0)
简单的大数加法,学会原理就可以。注意补0去0
using namespace std;
string calculate(string num1,string num2)
{
    int delta = abs(int(num1.length()-num2.length()));
    string finalnums = "";
    if(num1.length()<num2.length())
    {
        string tmp = num2;
        num2 = num1;
        num1 = tmp;
    }
    reverse(num1.begin(),num1.end());
    reverse(num2.begin(),num2.end());
    for(int p = 0;p<delta;p++)
    {
        num2 = num2+"0";
    }
    int top = 0;
    for(int i = 0;i<num1.length();i++)
    {
        int var = int(num1[i]-'0')+int(num2[i]-'0')+top;
        char newdigit = var%10+'0';
        top = var / 10;
        finalnums = finalnums+newdigit;
        if(i == num1.length()-1&&top!=0)
        {
            finalnums = finalnums+char(top+'0');
        }
    }
    reverse(finalnums.begin(), finalnums.end());
    return finalnums;
}
int main()
{
    string result = "0",number = "999999";
    while(number!="0")
    {
        cin>>number;
        result = calculate(result, number);
    }
    cout<<result<<endl;
}

发表于 2019-10-01 17:00:20 回复(0)

问题信息

难度:
31条回答 3460浏览

热门推荐

通过挑战的用户

查看代码