若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1526: Primary Arithmetic

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,272

题目描述

Children are taught to add multi-digit numbers from right to left, one digit at a time. Many find the “carry" operation, where a 1 is carried from one digit position to the next, to be a significant challenge. Your job is to count the number of carry operations for each of a set of addition problems so that educators may assess their difficulty.

输入

Each line of input contains two unsigned integers less than 10 digits. The last line of input contains “0 0''.

输出

For each line of input except the last, compute the number of carry operations that result from adding the two numbers and print them in the format shown below.

样例输入

123 456
555 555
123 594
0 0


样例输出

No carry operation.
3 carry operations.
1 carry operation.


参考代码

#include <stdio.h>
#include <string.h>
void exchange( char a[], int b[] );
int main() 
{
    int a[20], b[20];
    char c[20], d[20];
    int i;
    int count;
    int t1, t2;
    while( scanf("%s%*c%s", c, d) != EOF ) 
    {
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        count = 0;
        if( c[0] == '0' && d[0] == '0' ) 
        {
            break;
        }
        exchange(c, a);
        exchange(d, b);
        t1 = strlen(c);
        t2 = strlen(d);
        t1 = t2 > t1 ? t2 : t1;
        for ( i = 0; i < t1+1; i++ ) 
        {
            if( a[i] + b[i] > 9 ) 
            {
                a[i+1] += 1;
                count++;
            }
        }
        if(count == 0) 
        {
            printf("No carry operation.n");
    }
    if(count == 1)
    {
        printf("1 carry operation.n");
    }
    if(count > 1)
    {
      printf("%d carry operations.n",count);
    }
    }
    return 0;
}
void exchange( char a[], int b[] )
{
    int i;
    for( i= strlen(a)-1 ; i >= 0; i-- )
    {
        b[strlen(a)-1-i] = a[i] - '0';
    }
}

解析

暂无

hustoj

发表评论 取消回复

邮箱地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

Dreams孤独患者 Skip 原站点
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist