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 ...
随机推荐
- Codeforces Round #585 (Div. 2) D. Ticket Game
链接: https://codeforces.com/contest/1215/problem/D 题意: Monocarp and Bicarp live in Berland, where eve ...
- 边学边体验django--HttpRequest 对象
每个view函数的第一个参数是一个HttpRequest对象. HttpRequest对象包含当前请求URL的一些信息: 属性 描述 path 请求页面的全路径,不包括域名'/hello/' meth ...
- mybatis 批量删除添加
mybatis使用foreach进行批量插入和删除操作 转发与 https://www.cnblogs.com/Amaris-Lin/p/8615977.html 一.批量插入 1. ...
- 008_linuxC++之_类的静态变量和静态函数
(一)看程序 #include <iostream> #include <string.h> #include <unistd.h> using namespace ...
- PhpStorm 使用 Stylus 回车自动缩进的问题
如图所示,取消勾选即可换行自动缩进,不用再一个个打空格了!
- 7zip使用相关
造冰箱的大熊猫@cnblogs 2019/11/2 1.仅存储不压缩 7z a -mx0 compressed.7z FileFolderPath 将FileFolderPath指向的文件或文件夹打包 ...
- 虚拟机Linux无法查看本地ip地址 解决办法
解决方案: 1.虚拟机与本机采用的连接方式为:Host-only模式,其中几种连接模式的区别我不做介绍,自己百度.如果之前连接方式不为Host-only,更改之后需要重新启动虚拟机. 2.将本机的两块 ...
- nodejs基础 用http模块 搭建一个简单的web服务器 响应纯文本
首先说一下,我们平时在浏览器上访问网页,所看到的内容,其实是web服务器传过来的,比如我们访问www.baidu.com.当我们在浏览器地址栏输入之后,浏览器会发送请求到web服务器,然后web服务器 ...
- SpringMVC配置数据验证(JSR-303)
这篇文章已经过时了. 请参考比较合适的前后端交互方式. 1.pom.xml中追加hibernate-validator 2.在dto类的域上追加JSR-303的注解 public class Data ...
- Ubuntu常用命令及git常用命令
1. CMakeLists.txt中指定OpenCV路径 set(OPENCV_DIR /***/***/opencv-2.4.9) 2. cmake工程编译安装 mkdir build cd bui ...