BNUOJ 1006 Primary Arithmetic

简介: Primary Arithmetic 来源:BNUOJ 1006http://www.bnuoj.com/v3/problem_show.php?pid=1006 当你在小学学习算数的时候,老师会教你把两个数由右至左按位加起来。

Primary Arithmetic


来源:BNUOJ 1006
http://www.bnuoj.com/v3/problem_show.php?pid=1006

当你在小学学习算数的时候,老师会教你把两个数由右至左按位加起来。
很多时候,加法过程中会出现进位。对于一部分孩子,理解这个“进位”
在当时是很困难的事情。现在你的工作就是编写一个程序来判断两个数
相加的过程中会产生多少次进位,用以确定这两个数相加的“难度”。
Input
每一行有两个无符号整数(最大不超过1000000000),当输入0 0的时候,程序结束。
Output
对于每一行的输入两个整数,你需要判断会产生有多少个进位,每一个输出占一行。
Sample Input
123 456
555 555
123 594
0 0
Sample Output
No carry operation.
3 carry operations.
1 carry operation.

Source
第七届北京师范大学程序设计竞赛热身赛第一场




(1)注意输出结果里面的细微区别:
operations和operation 
(2)这个题目大多数人可能会想:直接把两个数当字符串输入再从后往前扫描处理。
其实这个题可以重复下面的过程:取余数直接得到最低位,然后消掉最低位。
题目描述
 1 #include<stdio.h>
 2 int main()
 3 {
 4     int a,b,x,y,c;
 5     int result;
 6     freopen("input.txt","r",stdin);
 7     while(scanf("%d%d",&a,&b)!=EOF)
 8     {
 9         if(a==0&&b==0) break;
10         result=0;
11         c=0;
12         while(a>0||b>0)
13         {
14             x=a%10;
15             y=b%10;
16             a=a/10;
17             b=b/10;
18             c=x+y+c;
19             if(c>=10)
20             {
21                 result++;
22                 c=c/10;
23             }
24             else
25             {
26                 c=0;
27             }
28         }
29         if(result==0)    printf("No carry operation.\n");
30         else
31         {
32             if(result>1) printf("%d carry operations.\n",result);
33             else printf("%d carry operation.\n",result);
34         }
35     }
36     return 0;
37 }
View Code

 

 

相关文章
|
9天前
|
存储 关系型数据库 索引
Primary Key Optimization
表的主键是在重要查询中使用的列或列集,并附有索引以确保快速查询性能。由于不允许NULL值,所以能进行NOT NULL优化。在InnoDB存储引擎下,表数据被物理组织,实现基于主键的高效查找与排序。对于大型且重要的表,若无明显主键,可创建自动增量列作为唯一标识符,在外键联接时作为指向其他表行的指针。
|
9天前
|
缓存
Foreign Key Optimization
当查询涉及大量不同列组合时,可将不常用数据拆分至独立表中,每个表包含若干列,并通过复制主表的数字ID作为关联纽带。这样每个小表都有一个主键以快速检索数据,并可通过联接操作实现按需查询,减少I/O操作和缓存占用,从而优化查询性能,提升磁盘读取效率。
|
4月前
|
SQL Oracle 关系型数据库
实时计算 Flink版操作报错之遇到 "The column 'AdminCode' is referenced as PRIMARY KEY, but a matching column is not defined in table 'tx.dbo.MS_tkBa'!" 是什么情况
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
uva10035 Primary Arithmetic
uva10035 Primary Arithmetic
28 0
|
数据库
Incorrect table definition; there can be only one auto column and it must be defined as a key
Incorrect table definition; there can be only one auto column and it must be defined as a key
160 0
Incorrect table definition; there can be only one auto column and it must be defined as a key
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
305 0
|
PHP
【PHP报错集锦】Integrity constraint violation: 1052 Column 'vt_id' in where clause is ambiguous
【PHP报错集锦】Integrity constraint violation: 1052 Column 'vt_id' in where clause is ambiguous
307 0
【PHP报错集锦】Integrity constraint violation: 1052 Column 'vt_id' in where clause is ambiguous
SAP WM初阶LQ02报错 - Movement Type 901 for manual transfer orders does not exist -
SAP WM初阶LQ02报错 - Movement Type 901 for manual transfer orders does not exist -
SAP WM初阶LQ02报错 - Movement Type 901 for manual transfer orders does not exist -
Value 'EN' violates facet information 'maxlength=1'
Value 'EN' violates facet information 'maxlength=1'
160 0
Value 'EN' violates facet information 'maxlength=1'
why My Lead OPA test add Lead fails
why My Lead OPA test add Lead fails
123 0
why My Lead OPA test add Lead fails