题目1083:特殊乘法
来源:互联网 发布:如何设计软件iphone 编辑:程序博客网 时间:2024/05/21 07:03
- 题目描述:
写个算法,对2个小于1000000000的输入,求结果。
特殊乘法举例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5
- 输入:
两个小于1000000000的数
- 输出:
输入可能有多组数据,对于每一组数据,输出Input中的两个数按照题目要求的方法进行运算后得到的结果。
- 样例输入:
123 45
- 样例输出:
54
代码:
#include <stdio.h>int main() { int m,n,i,j; while(scanf("%d %d",&m,&n)!=EOF) { int sum = 0; int x,y; while(m > 9) { x = m % 10; m /= 10; int tmp = n; while(tmp > 9) { y = tmp % 10; tmp /= 10; sum += x * y; } sum += x * tmp; } while(n > 9) { y = n % 10; n /= 10; sum += m * y; } sum += m * n; printf("%d\n",sum); } return 0;}
另一种解法:
#include <stdio.h>int main() { char a[11], b[11]; int i,j; while(scanf("%s %s",a,b)!=EOF) { int ans = 0; for(i=0;a[i]!=0;i++) for(j=0;b[j]!=0;j++) ans += (a[i] - '0') * (b[j] - '0'); printf("%d\n",ans); } return 0;}
用了一个技巧,把输入作为字符串。 数字字符 - '0'就可以变为数字。
- 题目1083:特殊乘法
- 题目1083:特殊乘法
- 题目1083:特殊乘法
- 题目1083:特殊乘法
- 题目1083:特殊乘法
- 题目1083:特殊乘法
- 题目1083:特殊乘法
- 题目1083:特殊乘法
- 题目1083:特殊乘法
- 39-题目1083:特殊乘法
- 九度题目:特殊乘法(1083)
- 九度OnlineJudge题目1083:特殊乘法
- 九度 题目1083:特殊乘法
- 九度OJ 题目1083:特殊乘法
- 九度题目1083特殊乘法
- 题目1083:特殊乘法[数位拆解]
- 九度OJ题目1083:特殊乘法
- 九度OJ 题目1083:特殊乘法
- poj 1258 Agri-Net
- 自定义控件
- VC学习之注册回调函数
- 7月29日
- Ural 1158. Censored! AC自动机+大数
- 题目1083:特殊乘法
- 怎样在VB 6.0编写代码时使用滚轮
- wpf控件
- asp.net中验证码的生成和引用
- 最新的linux内核启动时保留一段连续物理内存方法
- JavaScript面试题
- Save MySQL query results into a text or CSV file
- CCScheduler调度器调度规则心得
- poj 3177 (双联通)