ZOJ_1205_Martian Addition

STM32CubeMX之定时器输入捕获 上一章介绍了STM32定时器的PWM输出功能,定时器的几个通道除了有输出功能外,还可以做输入用,可捕获信号的上升沿或下降沿,可以用于测量信号的频率、周期、占空比等。本篇文章将介绍用STM3... 阅读详情
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;
}


微信小程序 - {errCode: 10008, errno: 1500104, errMsg: “writeBLECharacteristicValue:fail write characteris 微信小程序errCode: 10008, errno: 1500104, errMsg: "writeBLECharacteristicValue:fail write characteristics error. 10008, errMsg=The value's length is invalid.",uni-app微信小程序报错,uniApp小程序调用蓝牙api报错,向低功耗蓝牙设备的某个特征写入数据时错误,UniApp微信小程序蓝牙API调用报错10008解决方案,蓝牙设备进行通信时,向设备写入数据 阅读详情

相关推荐

【Arduino】入门篇——交通信号灯

模拟交通信号灯一、实验目的二、实验材料三、电路图四、实验过程五、代码块六、实验结果 一、实验目的 我想大家都看见过交通灯,就是马路上十字路口的红绿灯。如果您开过车,我想您一定仔细观察过交通灯,如果您还没有驾驶过车,您是否仔细观察过交通灯呢?交通灯包括三种个不同颜色的灯:红灯、黄灯和绿灯。 每个路口的红绿灯切换的时间并不是相同,这个是根据不同路口的车流情况来设定的,下面就是模仿交通灯的一个实验 二...

漠宸离若的博客 1万+

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

ArcGIS将以大西洋居中的世界地图以中国居中显示

ArcGIS将以大西洋居中的世界地图以中国居中显示 并进行经纬度格网的调整输出

jiekeheiguanglan的博客 5704

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

Calibre LVS错误排查实战:从Innovus到GDS的完整调试流程(附常见错误解析)

本文详细介绍了数字IC后端设计中,从Innovus工具输出数据到Calibre LVS物理验证的完整调试流程。文章系统性地讲解了LVS错误排查的方法论,包括报告解读、图形化定位以及针对器件不匹配、电源网络错误、短路开路等常见问题的深度解析与解决方案,旨在帮助工程师高效完成版图与原理图的一致性检查。

weixin_29218963的博客 775

【WSL2 Win10】解决子系统中nividia-smi出现的Failed to initialize NVML GPU access blocked by the operating systeM

环境:WIN10专业版 WSL2 Ubuntu x64架构 尝试在WSL2中装CUDA,装好以后在wsl系统中输入nvidia-smi命令,结果报错。但是在Win10的CMD中正常显示 Failed to initialize NVML: GPU access blocked by the operating system Failed to properly shut down NVML: GPU access blocked by the operating system在网上搜了一下,没太多类似的情况

6745

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

如何用Python+Librosa实现轴承异响检测?5步搞定工业设备故障预警

本文详细介绍了如何利用Python和Librosa库,通过5个步骤构建工业轴承异响检测与故障预警系统。从音频采集、预处理、频谱特征提取,到构建机器学习模型并进行实时监测,提供了完整的代码级解决方案。该方法能有效识别轴承早期故障特征,实现低成本、高效的设备预测性维护。

rnn9storyteller的博客 632

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

12027.linux flash驱动之IS25LP01G/IS25WWP01G

一页:512字节 --> 扇区 4096字节 --> 块 32k,64k, 层层递进关系。

写给自己的笔记 2289

【小白深度教程 1.3】使用 BP-Net 深度补全网络,进行 KITTI 稠密点云和图像融合(Depth Completion 实战)

在上一节中,我们展示了如何将从激光雷达获取的稀疏点云(深度)映射到图像。因此这篇论文我们尝试使用 Depth Completion(深度补全)技术,来将稀释的点云深度,补全为稠密深度。

2401_87064292的博客 1198

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

华为OD机考双机位C卷 - 员工派遣 (Java & Python& JS & C/C++ & GO )

华为OD机考双机位C卷 - 员工派遣 (OD上机考试双机位C卷)。提供在线OJ刷题。Java、C++、Python、GO语言、C语言、JavaScript题解,并提供完整思路讲解。

算法大师! 1032

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

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

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

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

ZOJ 1205: Martian Addition

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

业余程序员_专业吃货 1070

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

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

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、付费专栏及课程。

余额充值