Powerful Calculator(上海交大上机考试)

2021年上海交通大学计算机系考研机试题 674. 最长连续递增序列 leecode原题 https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/ class Solution { public: int findLengthOfLCIS(vector<int>& nums) { if(nums.size()==0)return 0; vector<int>dp(nums.s 阅读详情

题目描述

Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, developed and appeared in future market shortly. SJTU now invites you attending such amazing research and development work. In most business applications, the top three useful calculation operators are Addition (+), Subtraction (-) and Multiplication (×) between two given integers. Normally, you may think it is just a piece of cake. However, since some integers for calculation in business application may be very big, such as the GDP of the whole world, the calculator becomes harder to develop. For example, if we have two integers 20 000 000 000 000 000 and 4 000 000 000 000 000, the exact results of addition, subtraction and multiplication are: 20000000000000000 + 4000000000000000 = 24 000 000 000 000 000 20000000000000000 - 4000000000000000 = 16 000 000 000 000 000 20000000000000000 × 4000000000000000 = 80 000 000 000 000 000 000 000 000 000 000 Note: SJTU prefers the exact format of the results rather than the float format or scientific remark format. For instance, we need “24000000000000000” rather than 2.4×10^16. As a programmer in SJTU, your current task is to develop a program to obtain the exact results of the addition (a + b), subtraction (a - b) and multiplication (a × b) between two given integers a and b.

输入描述:
Each case consists of two separate lines where the first line gives the integer a and the second gives b (|a| <10^400 and |b| < 10^400).

输出描述:
For each case, output three separate lines showing the exact results of addition (a + b), subtraction (a - b) and multiplication (a × b) of that case, one result per lines.

输入例子:
20000000000000000
4000000000000000

输出例子:
24000000000000000
16000000000000000
80000000000000000000000000000000

#include<stdio.h>
#include<string.h>
#define MAX_SIZE 1000
#define DIVIDE_UNIT 10000//每个数组元素的阀值
int indexes;//用于高精度整数计算乘法时腾出尾数为零的个数,4个为一组,刚好是一个数组元素,减小计算量
struct bigInteger{
  int data[MAX_SIZE];
  int size;
  void init(){//清零
     for(int i=0;i<MAX_SIZE;i++)data[i]=0;
     size=0;
  }
  void set(int x){//初始化
      init();
      do{
       data[size++]=x%DIVIDE_UNIT;
       x/=DIVIDE_UNIT;
      }while(x);
  } 
   bigInteger operator -(const bigInteger A){//高精度整形的减法
     bigInteger result;
     result.init();
     bool pos=false;//结果是否为正
     if(size>A.size )pos=true;//前者更大
     else if(size<A.size) pos=false;//后者更大
     else {//占的数组元素个数相等
      int k=size-1;
      while(k>=0 && data[k]==A.data[k])k--;//对应数组元素相等则向后比较
      if(k==-1)pos=true;//表明两个数相等
      else if(data[k]>A.data[k]) pos=true;//前者大
      else pos=false;//后者大
     }
     int carry=0;//借位
     int down_size=0;//相减后减少的数组元素个数
     if(pos){//结果为正

    for(int i=0;i<A.size || i<size;i++)
     {

       int tmp=data[i]-A.data[i]+carry;
       if(tmp<0){//对应数组元素相减为负的计算
          carry=-1;
          result.data[result.size++]=DIVIDE_UNIT+tmp;
       }
       else {//对应数组元素相减为正的计算
         carry=0;
        result.data[result.size++]=tmp;
       }
     }
     }
     else{//结果为负

     for(int j=0;j<A.size || j<size;j++)
     {

       int tmp=A.data[j]-data[j]+carry;
       if(tmp<0){//对应数组元素相减为负的计算
          carry=-1;
          result.data[result.size++]=DIVIDE_UNIT+tmp;
       }
       else {//对应数组元素相减为正的计算
         carry=0;
        result.data[result.size++]=tmp;
       }
     }

     }
     for(int k=result.size-1;k>=0 && result.data[k]==0;k--)down_size++;//计算结果的size减少的个数
     result.size-=down_size;//修正结果所占的数据元素的个数,把高位的全零清掉
     if(pos==false)result.data[result.size-1]=0-result.data[result.size-1];//如果是负数,最高位数组元素取相反数
     return result;

  }
  bigInteger operator +(const bigInteger A){//高精度整形的加法
     bigInteger result;
     result.init();
     int carry=0;
     for(int i=0;i<A.size || i<size;i++)
     {

       int tmp=data[i]+A.data[i]+carry;
       result.data[result.size++]=tmp%DIVIDE_UNIT;
       carry=tmp/DIVIDE_UNIT;
     }
     if(carry)result.data[result.size++]=carry;
     return result;

  }
  bigInteger operator *(const bigInteger A){//大整型乘以大整型
     bigInteger result;
     result.set(0);//结果初始化为0
     for(int i=0;i<A.size;i++) {
         indexes=i;
         result=result+(*this)*(A.data[i]);
     }
     indexes=0;//重置index
     return result;
  }
 bigInteger operator *(const int x){//大整型乘以一个整数
     bigInteger result;
     result.init();
     result.size=indexes;////用于高精度整数计算乘法时腾出尾数为零的个数,4个为一组,刚好是一个数组元素,减小计算量
     int carry=0;
     for(int i=0;i<size;i++){
     int tmp=data[i]*x+carry;
     result.data[result.size++]=tmp%DIVIDE_UNIT;
     carry=tmp/DIVIDE_UNIT;
     }
     if(carry)result.data[result.size++]=carry;
     return result;

  }
 void output(){//输出数字
     for(int i=size-1;i>=0;i--){
      if(i!=size-1)printf("%04d",data[i]);
      else printf("%d",data[i]);
     }
     printf("\n");
 }
}num1,num2,a,b;

int main(){
    char s1[MAX_SIZE];
    char s2[MAX_SIZE];
    int len1;
    int len2;
    bigInteger multi_res;//乘法的结果
    bigInteger sub_res;//减法的结果
    bigInteger add_res;//加法的结果
    while(scanf("%s",s1)!=EOF){
      scanf("%s",s2);
      len1=strlen(s1);
      len2=strlen(s2);
      num1.set(0);
      num2.set(0);
      a.set(1);
      b.set(1);
      int max_len,min_len;
      if(len1>len2)
      for(int i=len1-1;i>=0;i--){//把数字1转化为高精度整形
          num1=num1+a*(s1[i]-'0');
          a=a*10;
      }
     for(int j=len2-1;j>=0;j--){//把数字2转化成高精度整形
          num2=num2+b*(s2[j]-'0');
          b=b*10;
      }
     add_res.init();
     sub_res.init();
     multi_res.init();
     add_res=num1+num2;
     sub_res=num1-num2;
     multi_res=num1*num2;
     add_res.output();
     sub_res.output();
     multi_res.output();
    }
return 0;
}
上海交通银行 Java面试题(上机考) 交行java的面试时候的机试题,解压后test1是答案直接复制到src下可运行。 立即下载

相关推荐

自学考试C++上机考试例题

北京大学,c++上机考试例题,工14个例子 基本包含啦所有题型!

九度 oj 题目1037:Powerful Calculator

题目描述:     Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, developed and appeared in future market shortly. SJTU now invites you atte

binyuapple的专栏 591

自考操作系统上机考试题

自考操作系统题目总结,代码,肯定有利于你,如果大家快要考试了,可以跟我联系下,我也正要考试,qq:494665228,我也马上要考了,可以相互交流下

c++实现大数运算

刷上交大的题遇到大数运算的问题(权当记录) 题目描述如下: Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, developed and appeared in future market shortly. SJTU ...

keep studying 4580

C++上机考试系统(包括100道题)

这是C++上机考试系统(包括100道题),和c++正式上机考试题类型、难度都相似。

手势控制 Python !Leap Motion Python 开发教程 (二)

手势控制 Python !Leap Motion Python 开发教程 (二) 现在进入正题,我来给大家总结一下我在使用 Leap Motion 开发过程中的经验以及学到的东西,其中我会概括以及翻译官方给出的注释,为了让这些注释更加通俗易懂,让大家更好理解。 回顾 在上一篇文章中我们讲了如何配置 Leap Motion 的 Python 2.7 的开发环境,如果您还没有配置好您的开发环境,可以参...

成畅的博客 3193

问题 C: 神奇的口袋 (背包问题)

题目链接:http://codeup.cn/problem.php?cid=100000583&pid=2 题目描述 有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40。John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an。John可以从这些物品中选择一些,如果选出的物体的总体积是40,那么利用这个神奇的口袋,John就可以得到这些...

Rhao的博客 595

C++上机——2023年“信息与未来”小学生编程思维展示官方题解

本次活动前三题是“根据问题描述实现代码”类型的问题,题解和示例代码由人工智能GPT-4直接阅读题面后生成――只要熟悉C++的语法和基本的编程、调试,即可解决p1、p2、p3。后三题有一定的算法性,我们给出命题思路和一些可能的解法。注意:本题解是面向指导教师的;指导教师在讲解时可以根据学生掌握程度进行调整。

qq_39127687的博客 2296

自学考试-上海交通大学-数据结构实践课,自考,计算机科学与技术

c 自考 计算机科学与技术

weixin_43023282的博客 2495

MQTT与物联网平台(一):基础知识和设备接入物联网平台的实现(以华为云为例)

本文简单介绍了MQTT的基础知识和使用方法,以蓝牙WIFI双模芯片W801作为硬件设备,接入到华为云。

weixin_50622833的博客 1万+

【嵌入式笔试题】C语言笔试题(4)

嵌入式C语言综合笔试题

m0_74055118的博客 1067

大整数运算(Powerful Calculator)

题目描述:     Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, developed and appeared in future market shortly. SJTU now invites you atten...

chengsilin666的博客 577

vue接入海康4G版摄像头通过萤石云平台推送视频流教程

vue-video-player接入海康视频,videojs-contrib-hls,vue播放视频

renkai721的专栏 7278

九度OJ 1037:Powerful Calculator(强大的计算器) (大整数运算)

时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1821 解决:528 题目描述: Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, dev...

weixin_30679823的博客 208

铁路场景雷达点云目标识别与追踪系统

本文介绍了一种基于毫米波雷达的铁路货运车站安全监控系统。该系统采用分布式架构,通过多个毫米波雷达节点采集点云数据,经过坐标转换、多雷达融合等处理后,利用机器学习算法实现列车、行人等目标的实时识别与追踪。系统包含数据采集层、处理层、目标识别层、追踪层、预警决策层和可视化层,能够克服传统视频监控受环境影响大的缺点,实现全天候可靠监测。文中详细展示了雷达数据解析和点云预处理的代码实现,包括坐标转换、数据格式解析和体素网格滤波等关键技术。该系统可有效预警铁路场景下的碰撞风险,提升货运车站的安全管理水平。

huanghm88的专栏 125

手把手入门YOLO到改进优化教程 | 【P6进阶篇】YOLOv8模型剪枝优化实战-LAMP剪枝

本节首先介绍LAMP剪枝相关理论。然后结合YOLO框架,手把手搭建剪枝工程,包括配置文件修改、剪枝脚本构建等内容。

嵌入式阿齐Archie 2万+

考研机试真题--Coincidence--上海交通大学

关键字:最长公共子序列 题目描述 Find a longest common subsequence of two strings. 输入描述: First and second line of each input case contain two strings of lowercase character a…z. There are no spaces before, inside...

不行不至 1160

RK3588 部署 YOLO26 目标检测:PT → ONNX → RKNN 全流程

YOLO26n 在 RK3588 上 INT8 量化后 NPU 推理约 15ms,系统 FPS 约 55,可以覆盖边缘端实时检测需求。模型从 PT 到 RKNN 的转换要用opset=11避免算子兼容问题。先用 YOLO26n + 640 输入跑通全链路,确认推理结果正确后再做性能调优。

m0_64661770的博客 304

大数运算

大数运算 有时候我们会遇到一些非常大的数,正常的加减运算无法解决问题,所以我们常常自己模拟运算过程。下面是我自己总结的大数运算的模版,提供给大家学习。 加法 #include<iostream> #include<vector> using namespace std; string calculate(string a, string b) { int len_a...

cp0328的博客 3279

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

【九度】题目1037:Powerful Calculator

题目1037:Powerful Calculator 时间限制:1 秒内存限制:32 兆特殊判题:否提交:1397解决:392 题目描述:     Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, developed

wangzq的专栏 805

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

[引用]一个台湾人看上海交大

一个台湾人看上海交大(本文来源老校友的电子邮件,作者不详)  看一个人,可以看他的学识,他的气质,他的丰采,他的谈吐。   看一个国家,可以看它的大学。   我每到一个国家,一个城市,风景区可以不看,但大学和博物馆一定要看。   台大的老校长傅斯年掌理台大的时间其实不长,但这位胡适的得意门生,前北大校长,却给台大奠立了很好的基础,把北大的学术风气和自由学风带到了...

lssm163的博客 461

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

上海交通大学计算机研究生复试题——回文数

问题 C: 回文数 时间限制: 1 Sec 内存限制: 128 MB 题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数。 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数。 又如:对于10进制数87: STEP1:87+78 = 165 STEP2:165+561 = 726 STEP3:726+627 ...

phthon1997的博客 1480

题目1037:Powerful Calculator

题目1037:Powerful Calculator 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2284 解决:669 题目描述:     Today, facing the rapid development of business, SJTU recognizes that more powerful calculato

lyj2014211626的博客 495

上海交通大学11年机考复试题

明天去交大保研复试,这是一个意外的惊喜,之前都没想到...... http://ac.jobdu.com/problem.php?pid=1103 这一题我坑在求根公式忘了,外加交换次序没对 题目描述: 设计一个二次方程计算器 输入: 每个案例是关于x的一个二次方程表达式,为了简单,每个系数都是整数形式。 输出: 每个案例输出两个实数(由

Pilgrim 1577
上一篇: 九度题目1431:Sort
下一篇: 二维数组回形遍历(北大信科2015上机考试)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值