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 ...
随机推荐
- C# 得到本周的第一天和最后一天
1.得到本周的第一天和最后一天 /// <summary> /// 得到本周第一天(以星期一为第一天) /// </summary> /// <param name=&q ...
- 19、属性赋值-@PropertySource加载外部配置文件
19.属性赋值-@PropertySource加载外部配置文件 加载外部配置文件的注解 19.1 [xml] 在原先的xml 中需要 导入context:property-placeholder 声明 ...
- win10 UWP 等级控件Building a UWP Rating Control using XAML and the Composition API | XAML Brewer, by Diederik Krols
原文:Building a UWP Rating Control using XAML and the Composition API | XAML Brewer, by Diederik Krols ...
- HR#4 题解
既然考这么差就来写题啦OTZ T1 猜结论?猜nm! 一直到考试结束都没猜出来=.=我就好奇别人如何猜出来的 我们来说DP(from ZBK) 设\(dp[i][j]\)表示胜or负 那我们来看一下代 ...
- 018_查看有多少远程的 IP 在连接本机(不管是通过 ssh 还是 web 还是 ftp 都统计)
#!/bin/bash#使用 netstat -atn 可以查看本机所有连接的状态,-a 查看所有,-t 仅显示 tcp 连接的信息,-n 数字格式显示# Local Address(第四列是本机的 ...
- codeforces#1217D. Coloring Edges(图上染色)
题目链接: https://codeforces.com/contest/1217/problem/D 题意: 给图染上$k$种颜色,相同颜色不能形成一个环 数据范围: $1\leq n \leq 5 ...
- python中的匿名函数
python 使用 lambda 来创建匿名函数. 所谓匿名,意即不再使用 def 语句这样标准的形式定义一个函数. lambda 只是一个表达式,函数体比 def 简单很多. lambda的主体是一 ...
- Javascript中数组查重的方法总结大全
数组查重:简单点说,就是找出数组中重复的元素然后去除,最后得到一个没有重复元素的数组. // 方法一思路: 1.构建一个新的数组,用于存放结果. 2.for循环中每次从数组取出一个 ...
- Ubuntu无法找到add-apt-repository问题的解决方法
网上查了一下资料,原来是需要 python-software-properties 于是 apt-get install python-software-properties 除此之外还要安装 s ...
- 使用hibernate利用实体类生成表和利用表生成实体类
1,配置数据库,这里以oracle数据库为例.点击右侧Database图标: