A+B Problem Plus and A-B Problem Plus and A*B Problem Plus


//we have defined the necessary header files here for this problem.
//If additional header files are needed in your program, please import here. #include <stdio.h>
#include <string.h> #define LENGHT 1000 void sumPlus(char *a, char *b); void reverse(char *a); int main() {
int times;
scanf("%d", ×);
char operator[][LENGHT] = {{''},
{''}};
for (int i = ; i < times; ++i) {
scanf("%s %s", operator[], operator[]);
printf("Case %d:\n", i + );
printf("%s + %s = ", operator[], operator[]);
sumPlus(operator[], operator[]);
if (i != times - ) {
printf("\n");
printf("\n");
}
}
return ;
} void sumPlus(char *a, char *b) {
int res[LENGHT + ] = {};
reverse(a);
reverse(b);
int flag_a = , flag_b = , carry = ; //进位
for (int i = ; i < LENGHT; ++i) {
int x = , y = ;
if (a[i] == '\0')
flag_a = ;
if (b[i] == '\0')
flag_b = ;
if (!flag_a) {
x = a[i] - '';
}
if (!flag_b) {
y = b[i] - '';
}
int temp = x + y + carry; //字符类型转int类型
res[i] = temp % ;
carry = temp / ;
}
int i = LENGHT;
while (res[i] == ) {
i--;
}
while (i >= ) {
printf("%d", res[i]);
i--;
}
} void reverse(char *a) {
int size = strlen(a);
char temp[LENGHT + ] = {''};
for (int i = size - , j = ; i >= ; --i, ++j) {
temp[j] = a[i];
}
memcpy(a, temp, size);
// a[size] = '\0';
}
#include <stdio.h>
#include <string.h>
#define MAX_LEN 1001
void removeZero(char *result, char *input)
{
int len = strlen(input);
for(int i = ; i < len; ++i) {
if(input[i] == '') {
continue;
}
strncpy(result, input+i, len-i);
result[len-i] = '\0';
return;
}
strcpy(result, "");
}
int compare(char *numA, char *numB)
{
int lenA = strlen(numA);
int lenB = strlen(numB);
if(lenA != lenB) {
return lenA - lenB;
}
for(int i = ; i < lenA; ++i) {
if(numA[i] < numB[i]) {
return -;
}
if(numA[i] > numB[i]) {
return ;
}
}
return ;
}
void sub(char *numA, char *numB, char *result)
{
int lenA = strlen(numA);
int lenB = strlen(numB);
int offset = lenA - lenB;
char numBCopy[MAX_LEN];
strcpy(numBCopy, numB);
if(offset) {
for(int i = lenB-; i >= ;--i) {
numB[i+offset] = numB[i];
}
for(int i = ;i < offset;++i) {
numB[i] = '';
}
}
// calculate
int carry = ;
for(int i = lenA-; i >= ; --i) {
int value = numA[i] - numB[i] - carry;
if(value < ) {
result[i] = '' + ( + value);
carry = ;
} else {
result[i] = '' + value;
carry = ;
}
}
result[lenA] = '\0';
strcpy(numB, numBCopy);
}
void minus(char *numA, char *numB, char *result)
{
char number[MAX_LEN];
int ret = compare(numA, numB);
if(ret == ) {
strcpy(result, "");
return;
}
char removeZeroStr[MAX_LEN];
if(ret < ) {
sub(numB, numA, number);
removeZero(removeZeroStr, number);
sprintf(result, "-%s", removeZeroStr);
} else {
sub(numA, numB, number);
removeZero(removeZeroStr, number);
strcpy(result, removeZeroStr);
}
}
int main()
{
int N;
char a[MAX_LEN], b[MAX_LEN];
while(scanf("%d", &N) != EOF) {
for(int i = ; i < N; ++i) {
scanf("%s", a);
scanf("%s", b);
char result[MAX_LEN+];
minus(a, b, result);
printf("Case %d:\n", i+);
printf("%s - %s = %s\n\n", a, b, result);
}
}
return ;
}
#include <stdio.h>
#include <string.h> #define LENGTH 1001
#define RESLENGTH 2002
void multiply(char *a, char *b, int length_a, int length_b); int main() {
char a[LENGTH] = "\0";
char b[LENGTH] = "\0";
int len_a, len_b;
while (scanf("%s %s", a, b) != EOF) {
len_a = strlen(a);
len_b = strlen(b);
multiply(a, b, len_a, len_b);
printf("\n");
}
return ;
} void multiply(char *a, char *b, int length_a, int length_b) {
int res_len = length_a + length_b;
int res[RESLENGTH] = {};
for (int i = ; i < length_a; ++i) {
for (int j = ; j < length_b; ++j) {
res[i + j] += (a[length_a - i - ] - '') * (b[length_b - j - ] - ''); //字符转整数
}
}
for (int k = ; k < res_len; ++k) {
if (res[k] >= ) {
res[k + ] += res[k] / ;
res[k] = res[k] % ;
}
} //去掉头部的0
int index = res_len;
while(res[index]== && index>=){
index--;
}
if(index<){
printf("");
return;
}
for (int i = index; i >= ; --i) {
printf("%d", res[i]);
} }
A+B Problem Plus and A-B Problem Plus and A*B Problem Plus的更多相关文章
- The Solution of UESTC 2016 Summer Training #1 Div.2 Problem A
Link http://acm.hust.edu.cn/vjudge/contest/121539#problem/A Description standard input/output Haneen ...
- The Unsolvable Problem
The Unsolvable Problem 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=45783 题意: ...
- Codeforces Beta Round #17 A - Noldbach problem 暴力
A - Noldbach problem 题面链接 http://codeforces.com/contest/17/problem/A 题面 Nick is interested in prime ...
- hdu----(5055)Bob and math problem(贪心)
Bob and math problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- leetcode problem (2-4)
Problem 2 --- Add Two Numbers 简单的模拟题. Problem 3 --- Longest Substring Without Repeating Characters 题 ...
- 数据结构(主席树):HDU 4729 An Easy Problem for Elfness
An Easy Problem for Elfness Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65535/65535 K (J ...
- cf442B Andrey and Problem
B. Andrey and Problem time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- UVA 100 - The 3n+1 problem (3n+1 问题)
100 - The 3n+1 problem (3n+1 问题) /* * 100 - The 3n+1 problem (3n+1 问题) * 作者 仪冰 * QQ 974817955 * * [问 ...
- Noldbach problem
Description Noldbach problem time limit per test: 2 seconds memory limit per test: 64 megabytes inpu ...
- Codeforces Round #253 (Div. 1) B. Andrey and Problem
B. Andrey and Problem time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
随机推荐
- 多次读取HttpServletRequest的inputstream方法 问题解决
原因:我要收集所有来自前台请求的参数信息,无论在任何地方的.当前请求参数都是json格式,都写在httpservlet的body中.这个只能通过流进行获取.然后问题来了,HttpServletRequ ...
- js查找
//对象克隆 function main_clone(fromObject, toObject) { var copy = toObject || {}; for (var i in fromObje ...
- MySQL5.7.6 general tablespace
摘要: 从5.7.6开始,增加了一种新的 tablespace模式(成为general tablespace),实际上它和共享表空间比较类似:创建一个单独的ibd,ibd中包含多个表,兼容不同的格式. ...
- [CQOI2016]手机号码 数位DP
[CQOI2016]手机号码 用来数位DP入门,数位DP把当前是否需要限制取数范围(是否正在贴着临界值跑,即下面的limited)和一切需要满足的条件全部塞进记忆化搜索参数里面就好了,具体情况转移便好 ...
- POJ 1051 Jury Compromise ——(暴力DP)
题目不难,暴力地dp一下就好,但是不知道我WA在哪里了,对拍了好多的数据都没找出错误= =.估计又是哪里小细节写错了QAQ..思路是用dp[i][j]表示已经选了i个,差值为j的最大和.转移的话暴力枚 ...
- jvm 线程状态
NEW: Just starting up, i.e., in process of being initialized.NEW_TRANS: Corresponding transition sta ...
- 基于Ryu REST API的VLAN实现
目录 0.预备知识 1.实验内容 2.编写脚本addflow.sh一步实现流表下发 3.使用api查看流表 4.实验结果 0.预备知识 ryu控制器的API文档:ryu.app.ofctl_rest ...
- POI的XWPFTableCell的方法
1. XWPFParagraph addParagraph() 在这个表格单元格中添加一个段落 2. void addParagraph(XWPFParagraph p) 给这个表格加一段 3. ja ...
- 笔记四(Competitor Analysis Test小结)
1.关机后启动电脑,测试BIOS的POST time 2.进入睡眠模式后,按任意键,通过Windows logs查看bios的init时间 3.进入BIOS setup的快捷键,一般为F2 4.进入B ...
- Web开发中 MTV模式与MVC模式的区别 联系 概念
MTV 与 MVC模式的区别 联系 概念: MTV: 所谓MTV指的就是: M:model (模型),指的是ORM模型. T:template (模板),一般Python都是使用模板渲染的方式来把HT ...