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 ...
随机推荐
- selenium报错以及各解决方法
1.driver.findElement(By.name("wd")).sendKeys("selenium"); 报错:The method sendKeys ...
- SVN安装与汉化
官网地址:http://tortoisesvn.net/downloads.html 如地址过期可自行百度官网,下载安装包安装64/32位 如果需要汉化的同学,同样在下载页面,往下拉Language ...
- JQuery-UI组件化开发
===================== 页面相关样式及其脚本的引入先后顺序,如下: 1,layout.css 页面的静态基本框架布局样式 2,base.css 页面的静态细节样式 3,ui.css ...
- 034_非交互自动生成 SSH 密钥文件
#!/bin/bash#-t 指定 SSH 密钥的算法为 RSA 算法;-N 设置密钥的密码为空;-f 指定生成的密钥文件存放在哪里 rm -rf ~/.ssh/{known_hosts,id_rsa ...
- python 文件,文件夹,路径操作
判断路径或文件os.path.isabs(...) # 判断是否绝对路径os.path.exists(...) # 判断是否真实存在os.path.isdir(...) # 判断是否是个目录os.pa ...
- CentOS 安装 MySQL PDO 扩展
yum install php-pdo_mysql sudo service php-fpm restart
- linux C file format analysis
c语言文件格式 source file file.c C source, ASCII text pretreatment 预处理文件 file.i C source, ASCII text assem ...
- 《你不知道的JavaScript(上)》笔记——函数作用域和块作用域
关于函数声明:如果 function 是声明中的第一个词, 那么就是一个函数声明, 否则就是一个函数表达式.例如匿名函数这种形式,函数会被当作函数表达式而不是一个标准的函数声明来处理. (functi ...
- 修改Eclipse启动时的选择工作空间
对于eclipse的默认的工作空间,如果不需要正常切换workspace的用户很方便,打开eclipse便自动进入默认的工作空间.而如果用户经常在多个workspace之间切换的话,启动eclipse ...
- Apache设置静态文件的失效时间
步骤1:启用expires模块 [root@zlinux logs]# vim httpd.conf LoadModule expires_module modules/mod_expires.so ...