ZOJ_1205_Martian Addition

DMTF相关协议(一):Redfish和SMBIOS Redfish 是一种管理标准,使用超媒体 RESTful 接口中的数据模型表示。该模型以标准的、机器可读的模式表示,消息的有效负载以 JSON 表示。该协议本身利用了 OData v4。由于它是一个超媒体 API,因此能够通过一致的接口表示各种实现。它具有管理数据中心资源、处理事件、长时间任务和发现的机制。 阅读详情
Martian Addition

Time Limit: 2 Seconds       Memory Limit: 65536 KB

  In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year, they would hold an Arithmetic Contest on Mars (ACM). The task of the contest is to calculate the sum of two 100-digit numbers, and the winner is the one who uses least time. This year they also invite people on Earth to join the contest.
  As the only delegate of Earth, you're sent to Mars to demonstrate the power of mankind. Fortunately you have taken your laptop computer with you which can help you do the job quickly. Now the remaining problem is only to write a short program to calculate the sum of 2 given numbers. However, before you begin to program, you remember that the Martians use a 20-based number system as they usually have 20 fingers. 

Input:
You're given several pairs of Martian numbers, each number on a line. 
Martian number consists of digits from 0 to 9, and lower case letters from a to j (lower case letters starting from a to present 10, 11, ..., 19). 
The length of the given number is never greater than 100.

Output:
For each pair of numbers, write the sum of the 2 numbers in a single line.

Sample Input:
1234567890
abcdefghij
99999jjjjj
9999900001

Sample Output:
bdfi02467j
iiiij00000
 
题意和思路:题目意思很容易读懂,有点类似大数相加的感觉。我做了很久,用的方法也很繁琐,不过看了别人的解题报告发现总体思路也是类似,主要就是先都放入字符数组,然后处理的时候从尾部(最低位)开始处理,要将字符转化为数字来计算,自己控制进位。相信这个很多人能想到思路,但是实现起来确实很麻烦,我说几个易错点吧。
 
1.数组不要只设置101,有种进位方式容易遗漏j+j=1i 结果别两个相加的数多1位
2.注意001+2=3不等于003所以要忽略前面的0
 
Sample Program Here
/******************************************
**
**      Author: Wan KaiMing
**      Date:   2012-10-30-18.38 星期二
**
*******************************************/


#include<iostream>
#include<cstring>
using namespace std;


int main(){
   char a[200],b[200];//相加的两个数
   memset(a,'x',sizeof(a));
   memset(b,'x',sizeof(b));
   while(cin>>a>>b){
	      //初始化全为-1 这样之后可以用来确定长度

      //先将a,b数据倒置下,这样a[0]就是最低位了
      int i=0;
      //先得到a b的长度
      int len_a=0,len_b=0;
      while(a[i++]!='x') len_a++;
      len_a--; //去除多算的\0

      i=0;
      while(b[i++]!='x') len_b++;
      len_b--;


      char t; //用于暂存数据

      //进行元素的交换完成倒置
      for(i=0;i<len_a/2;i++){
          t=a[len_a-i-1];
          a[len_a-i-1]=a[i];
          a[i]=t;
      }

      for(i=0;i<len_b/2;i++){
          t=b[len_b-i-1];
          b[len_b-i-1]=b[i];
          b[i]=t;
      }

      //进行相加的操作
      char sum[101]; //用于保存和
      char add='0'; //保存进位的值
      memset(sum,'x',sizeof(sum));
      int len;
      len=len_a>len_b?len_a:len_b; //取长度大的

      //完成加法操作
      for(i=0;i<len;i++){

        if(a[i]=='x'||a[i]==0) a[i]='0';  //不要忘记是字符串最后会多个0
        if(b[i]=='x'||b[i]==0) b[i]='0';
        if(a[i]>='a'&&a[i]<='j')  a[i]=a[i]-'a'+10;
		else a[i]=a[i]-'0';
        if(b[i]>='a'&&b[i]<='j')  b[i]=b[i]-'a'+10;
		else b[i]=b[i]-'0';
        //转化成整数计算
        int one,two,add2; //add2是进位
        one=a[i];
        two=b[i];
        add2=add-'0';
        add=(one+two+add2)/20+'0';
		if(add!='0'&&i==len-1) len++;  //如果最高位进位了 那么长度增加1位不要忘记这种情况
        if((one+two+add2)%20>=10){
		     sum[i]=(one+two+add2)%20-10+'a';
		}
        else sum[i]=(one+two+add2)%20+'0';

      }



	  //输出前先略去开头的0
	  for(i=len-1;i>=0;i--){
		  if(sum[i]!='0'){
		     len=i+1;//即len-1=i
			 break;
		  }
	  }

      //输出加法的结果,因为倒置过了记得从最后开始输出
      for(i=len-1;i>=0;i--){
         cout<<sum[i];
      }
      cout<<endl;
	  memset(a,'x',sizeof(a));
      memset(b,'x',sizeof(b));

   }
   return 0;
}


 
///////////////////////////////由于我觉得自己算法太过繁琐,特地奉上转载来的一个解法,思路基本相同,但是代码比我更加清晰,简约
////转载自:http://www.cnblogs.com/hoodlum1980/archive/2009/01/10/1373130.html
 
Sample Program Here
/* ZOJ - 1205 , 20进制数想加 */
#include <stdio.h>
#include <string.h>

char* NUMS="0123456789abcdefghij";

/*分别是读取的输入行,加数a,b,计算结果c */
char line[105],a[105],b[105],c[105];

/*字符换算为实际数值,例如'a'->10,'b'->11*/
int CharToVal(char c)
{
    return strchr(NUMS, c)-NUMS;
}

/*解析输入*/
void CopyNum(char* dest, const char* line)
{
    int i,len=strlen(line);
    for(i=0;i<len;i++)
    {
        dest[i]=CharToVal(line[len-1-i]);
    }
}

/*打印加法结果,注意结尾如果为0也要打印0*/
void PrintResult()
{
    int i=101;
    while(c[i]==0) i--;
    
    if(i<0) i=0;        /*非常重要,可以保证至少要打印一个0。*/
    
    for(;i>=0;i--)
    {
        printf("%c", NUMS[c[i]]);
    }
    printf("\n");
}

/*做加法: c=a+b; */
void Add()
{
    int i;
    int k=0,sum;/*进位的数值*/
    for(i=0;i<102;i++)
    {
        sum = a[i] + b[i] + k;
        c[i] = sum%20;
        k = sum/20;
    }
}

int main()
{
    while(scanf("%s",line)!=EOF && strcmp(line,"END")!=0)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        CopyNum(a,line);            /*获取加数A*/
        scanf("%s",line);
        CopyNum(b,line);            /*获取加数B*/
        Add();                                /*C=A+B;*/
        PrintResult();                /*打印出结果*/
    }
    return 0;
}


深入YOLOv8内部核心:用RepNCSPELAN4深度重构主干网络,即插即用的高效涨点方案 本文提出了一种改进YOLOv8目标检测模型的方法,通过在YOLOv8中引入RepNCSPELAN模块来提高检测性能。该模块融合了RepVGG重参数化思想与CSP-ELAN结构,在保持推理速度的同时增强特征表达能力。实验结果表明,改进后的RepNCSPELAN-YOLOv8相比原模型在多项指标上均有提升:准确率从0.84提高到0.86,召回率从0.72提升至0.81,mAP50和mAP50-95也分别提高了0.06和0.04。文章详细介绍了模块设计原理和移植步骤,包括配置文件创建、代码修改等关键环节,为YOL 阅读详情

相关推荐

STM32 使用rtthread的DMA 串口发送失败

在一次使用RTthread的DMA 串口发送时,持续性通过DMA 发送串口数据时,偶发发送过程中后续数据包无法正常发送出去,后经查,发现为RT-Thread的串口中断处理有瑕疵,后做出修改。另,为了更方便的排查为题,建议修改。左为修改前,右为修改后。

qq_39294918的博客 1108

ZOJ_1205 Martian Addition

In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year, they would hold an Arithmetic Contest on Mars (ACM). The task

372

最新Docker GPU支持全解析:为什么nvidia-smi能用但PyTorch找不到CUDA?

本文深入解析了Docker容器中nvidia-smi命令可用但PyTorch无法调用CUDA的根本原因。关键在于默认配置仅暴露了GPU的utility管理能力,而CUDA计算所需的compute能力未被激活。通过正确设置NVIDIA_DRIVER_CAPABILITIES环境变量为compute,utility,并配合--gpus参数,即可解决此问题,确保AI训练等任务顺利进行。

convnet3designer的博客 238

zoj 1205 二十进制数相加

Martian Addition Time Limit:1 Second Memory Limit:32768 KB In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathe...

dinglangwei5539的博客 249

PaddleOCR 3.x 最新版使用教程(2025)——从安装到实战身份证识别,一步到位!

本文针对PaddleOCR 3.x大版本更新带来的参数变化和兼容性问题,提供从环境配置到实际应用的全流程解决方案。

qq_58607032的博客 9786

零基础实战:YOLO检测头自定义替换全攻略

本文为零基础用户提供了YOLO检测头自定义替换的完整实战指南。通过将检测头比作汽车的“方向盘”,文章详细阐述了为何需要替换、如何寻找和集成新检测头模块、修改配置文件以及最终训练验证的全过程,旨在帮助用户针对特定任务(如小目标检测)提升模型性能。

perl8的博客 255

ZOJ 1205 Martian Addition

Martian Addition Time Limit: 2 Seconds      Memory Limit: 65536 KB   In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics

胖子加油! 428

Visual C++与WPF混合开发:C++/CLI桥接技术实现现代化桌面应用

在桌面应用开发领域,混合编程技术是连接不同技术栈、复用核心资产的关键手段。其核心原理在于通过特定的桥接层,实现托管代码与非托管代码之间的互操作,从而打破语言和运行时环境的壁垒。这项技术的核心价值在于,它允许开发者将高性能、高稳定性的原生C++业务逻辑库,与现代化、高表现力的WPF用户界面无缝结合,避免了重写核心代码的巨大成本和风险。在工业控制、数据可视化、上位机软件等对性能和界面均有高要求的应用场景中,这种架构尤为适用。本文以C++/CLI作为桥接技术,详细演示了如何构建一个包含原生C++核心层、C++/C

weixin_33146151的博客 278

Martian Addition

FJNU.1865DescriptionIn the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year, they would hold an Arithmetic Contest o

linyq@BambooFan ~ 750

零基础学simulink仿真建模--基于Simulink进行农业机械用开关磁阻电机的仿真建模

特点结构简单:无电刷、换向器等易损件,维护成本低。坚固耐用:适合恶劣的工作环境,如农业机械作业现场。宽调速范围:通过适当的控制策略可以实现较宽的速度调节范围。挑战非线性特性:SRM的电磁特性和转矩特性具有高度非线性。复杂性:涉及电气、机械动力学以及控制系统的设计与集成。实时性要求:需要快速响应以适应不断变化的操作条件。多变量优化:包括电流控制、速度控制等多个方面需综合考虑。使用Simscape Electrical库来构建一个开关磁阻电机模型。

amy_mhd的博客 909

ECMWF等大气数据批量下载(Python API)详细步骤

ECMWF等大气数据批量下载(Python API) 遥感方向做热红外温度反演,不可避免的要用到大气廓线数据,大气数据具有实时性,比如2018年9月1日的早上8点的影像,为了提高反演精度就需要用到时间上与其相差尽可能接近的大气数据,用于消除大气的影响,如果数据量较少我们可以查看影像的时间和空间范围进行下载,但是如果处理某个较大区域时可能涉及到大量的影像,如果一景一景对应查找去下载会很麻烦,因此针对大量数据的自动批量下载很有必要。本文根据 https://cds.climate.copernicus.eu/#

liuxiaojiangtobeno1的博客 1万+

ZOJ Martian Addition (20进制的两个大数相加)

题目链接: #include &lt;cstdio&gt; #include &lt;cmath&gt; #include&lt;vector&gt; #include&lt;cstring&gt; #include&lt;algorithm&gt; #include&lt;cmath&gt; #include&lt;stack&gt; #include&lt;string&g

ZMST的博客 275

uniapp:微信支付报错requestPayment:fail errors,uniAPP调用uni.requestPayment微信支付提示错误(安卓app、ios苹果app、微信小程序支付报错)

uniapp微信支付报错requestPayment:fail errors,uniApp APP端调起微信支付失败报错requestPayment:fail errors,uniapp微信小程序支付的坑 “errMsg“: “requestPayment:fail errors“,uni.requestPayment微信支付,UNI-APP支付报错“errMsg“: “requestPayment:fail errors“ app微信支付,uniapp 安卓app微信支付报错,uni.requestPay

✅ 精选推荐文章 3916

zoj1205Martian Addition

以20进制相加#include #include #include int main() { int op1[100]; int op2[100]; char result[102]; char str[101]; int carriage; int i,len1,len2,a,b,sum,j,k; while(scanf("%s",str)!=EOF) { len1

andy的专栏 493

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

zoj 1205 Martian Addition

//什么也不说,二十进制加法,简单题目……#include"stdiob.h" #include"string.h" #define Binary 20//宏定义二十进制 int nc[101]; int sum; void str_to_no(char tch[],int tint[],int tlen)//字符转数值,低位存低位,高位存高位 { int i,j=0; for(i=tlen;i>=0;i--) { if(tch[i]19) { nc[i+1]=n

AC007 872

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

ZOJ 1205: Martian Addition

20进制加法问题。 第一次没过,因为输出的时候忘记了考虑 0+0=0 的情况,被测试数据抓个正着 改掉这个bug就AC了 本来就是半路出家的学编程,不停的刷水题,结果每次都是代码写的也比较sloppy...  比单纯高精度稍微麻烦一点,难度系数=1 通过率数据 28.06% (5015/17869) import java.util.*; public class

业余程序员_专业吃货 1070

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

zoj_1205 Martian Addition

In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year, they would hold an Arithmetic Contest on Mars (ACM). The ta

I love Algorithm 561

ZOJ Problem Set–1205 Martian Addition

Time Limit: 2 Seconds Memory Limit: 65536 KB In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year, the...

dcl67283的博客 167

ZOJ Problem Set - 1205 Martian Addition

Martian Addition Time Limit: 2 Seconds      Memory Limit: 65536 KB   In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics

acm_1361677193的专栏 379

ZOJ 1205 Martian Addition

In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year, they would hold an Arithmetic Contest on Mars (ACM). The t...

越努力,越幸运!!! 821

Solution of ZOJ 1205 Martian Addition

In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year, they would hold an Arithmetic Contest on Mars (ACM). The task of the contest is to calculate the sum of two 100-digit

DUAN的专栏 743
上一篇: 写给新手程序员的一封信
下一篇: fixed和setprecision等C++格式控制的函数使用
wankaiming
博客等级 码龄15年 49粉丝 27原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值