//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", &times);
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的更多相关文章

  1. 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 ...

  2. The Unsolvable Problem

    The Unsolvable Problem 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=45783 题意: ...

  3. Codeforces Beta Round #17 A - Noldbach problem 暴力

    A - Noldbach problem 题面链接 http://codeforces.com/contest/17/problem/A 题面 Nick is interested in prime ...

  4. hdu----(5055)Bob and math problem(贪心)

    Bob and math problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. leetcode problem (2-4)

    Problem 2 --- Add Two Numbers 简单的模拟题. Problem 3 --- Longest Substring Without Repeating Characters 题 ...

  6. 数据结构(主席树):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 ...

  7. cf442B Andrey and Problem

    B. Andrey and Problem time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  8. UVA 100 - The 3n+1 problem (3n+1 问题)

    100 - The 3n+1 problem (3n+1 问题) /* * 100 - The 3n+1 problem (3n+1 问题) * 作者 仪冰 * QQ 974817955 * * [问 ...

  9. Noldbach problem

    Description Noldbach problem time limit per test: 2 seconds memory limit per test: 64 megabytes inpu ...

  10. 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 ...

随机推荐

  1. mysql数据表结构查询

    select * from ( select '表名', '字段名', '字段类型', '默认值', '是否可空', '注释', '主键' UNION ( SELECT a.TABLE_NAME '表 ...

  2. 使用C++定义一个万能类型

    分享一个类似于Qt中QVariant类. 目录: 1 类型定义 2 数值操作 3 万能类型包装 4 使用 ——————————————————Begain—————————————————— 类型定义 ...

  3. re匹配 [\s\S][\w\W]的使用.

    本来想提取一个字符串写了一堆正则都提取不出来. 因为有特殊字符 后来使用 [\s\S]* 或 [\w\W]* 匹配出来. \s 空白字符 [ \t\n\r\f\v] \S 非空白字符 相当于 [^ \ ...

  4. List对象遍历时null判断逻辑梳理

          凡是对集合list,set,map,数组等进行循环一定要判断是否为null,增强代码的健壮性.下面以list为例, 使用for循环遍历list对象,处理其中的元素时,需要对null值判断: ...

  5. PHP 之文件上传类封装

    一.前端代码 <!doctype html> <html lang="en"> <head> <meta charset="UT ...

  6. phpstorm+xdebug安装配置

    这个问题也困惑了我好久 烦死了 今天看了qing师傅的博客 跟着安装 运行环境: phpStorm 2018 PHP 5.45 nts VC9 Xdebug 2.4.1 0x01 PHP安装xdebu ...

  7. Jmeter Web 性能测试入门 (二):Fiddler 抓取 http/https 请求

    jmeter自带了拦截request的功能,并且也有对应的tool:badboy 可以用.但由于我经常做移动端的项目,个人还是习惯用fiddler来收集request. 官网下载并安装Fiddler ...

  8. H5本地存储详解

    H5之前存储数据一般是通过 cookie ,但是 cookie 存的数据容量比较少.H5 中扩充了文件存储能力,可存储多达 5MB 的数据.现在就实际开发经验来对本地存储 ( Storage ) 的使 ...

  9. 【English】What is a Java StringWriter, and when should I use it?(转帖)

    转帖地址:http://www.it1352.com/989366.html Question: What is a Java StringWriter, and when should I use ...

  10. ubuntu 命令行以及目录的显示改为英文

    在安装ubuntu的时候原本以为把ubuntu改为中文的好理解一些,最后发现命令行的报错一边中文一边英文确实难受,也不宜于学习,所以想把命令行以及目录的显示都改为英文的,我是这样先把命令行的该为英文的 ...