首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求解思路?解决方案

2012-04-18 
求解思路??????????I have a very simple problem for you. Given two integers A and B, your job is to

求解思路??????????
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110


[解决办法]
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

enum ExpreOperType
{
EXPRE_ADD = 1,///<加
EXPRE_SUBTRACT,///<减
EXPRE_MULPITLY,///<乘
EXPRE_DIVIDE///<除
};

struct SExpre
{
SExpre()
: m_nIntA(0)
, m_nIntB(0)
, m_eOperType(EXPRE_ADD)
{

}

void SetExpreOperType(ExpreOperType oType)
{
if (EXPRE_ADD <= oType && oType <= EXPRE_DIVIDE)
{
m_eOperType = oType;
}
}

__int64 GetResult(void)
{
switch (m_eOperType)
{
case EXPRE_ADD:
return (m_nIntA + m_nIntB);
case EXPRE_SUBTRACT:
return (m_nIntA - m_nIntB);
case EXPRE_MULPITLY:
return (m_nIntA * m_nIntB);
case EXPRE_DIVIDE:
return (0 == m_nIntB ? 0 : (m_nIntA / m_nIntB));
default:
return 0;
}
}

string GetExpreOper(void)
{
switch (m_eOperType)
{
case EXPRE_ADD:
return _T("+");
case EXPRE_SUBTRACT:
return _T("-");
case EXPRE_MULPITLY:
return _T("×");
case EXPRE_DIVIDE:
return _T("÷");
default:
return _T("+");
}
}

__int64 m_nIntA;
__int64 m_nIntB;
ExpreOperType m_eOperType;
};

bool IsDigtial(string& strExpre)
{
size_t nLen = strExpre.length();
for (size_t i = 0; i < nLen; ++i)
{
if ('1' > strExpre[i] || strExpre[i] > '9')
{
return false;
}
}

return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
bool bTag = true;
unsigned int t = 0;
while (bTag)
{
printf(_T("\r\n请输入一个整数T(1 < T < 20):"));
cin>>t;
if (1 < t && t < 20)
{
bTag = false;
}
}

vector<SExpre*> vecExpre;
vecExpre.clear();

for (unsigned int i = 0; i < t; ++i)
{
bTag = true;

string strIntA = _T("");
string strIntB = _T("");

while (bTag)
{
strIntA = _T("");
strIntB = _T("");

printf(_T("\r\n请输入两个整数(大于0且两个整数用空格分隔):"));
cin>>strIntA>>strIntB;

if (!IsDigtial(strIntA) || !IsDigtial(strIntB))
{
printf(_T("\r\n输入不合法,请重新输入!\r\n"));
continue;
}

if (strIntA.length() > 1000 || strIntB.length() > 1000)
{
printf(_T("\r\n输入整数长度超过1000,请重新输入!\r\n"));
continue;
}

bTag = false;
}

SExpre* pExpre = new SExpre();


if (NULL == pExpre)
{
printf(_T("\r\n内存申请失败,程序中止!"));
return 0;
}

pExpre->m_nIntA = _ttoi64(strIntA.c_str());
pExpre->m_nIntB = _ttoi64(strIntB.c_str());
vecExpre.push_back(pExpre);
}

size_t nExpreCount = vecExpre.size();
for (size_t i = 0; i < nExpreCount; ++i)
{
SExpre* pExpre = vecExpre[i];
if (NULL == pExpre)
{
continue;
}

printf(_T("Case %d:\r\n"),(int)i);
printf(_T("%I64d %s %I64d = %I64d\r\n"),pExpre->m_nIntA,
pExpre->GetExpreOper().c_str(),pExpre->m_nIntB,pExpre->GetResult());
printf(_T("\r\n"));//添加空行
}

return 0;
}
刚做了个例子,你试一下
[解决办法]
这个题目的意思就是:
1、先输入一个数T(1 <= T <= 20),T表示接下来你要输入的整数有T组,每组两个整数。
2、循环T次,每次让用户输入两个整数A和B,A和B必须是正整数,且位数不能超过1000位,A和B的值可能比较大,因此要由64位整数(__int64)保存。将输入的A和B作为一组数据保存下来。
3、对你保存下来的每组数据,以下面的格式(A + B = ?)输出:
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
[解决办法]
长度可能到1000的int,__int64也放不下,比如1000个'1'和1000个'2'相加。
思路应该是自己去做累加。
写了个例子,可以支持负数。

C/C++ code
#include <stdio.h>#define VLI_LEN_MAX 1000#define LO4(c) (((c))&0x0F)#define HI4(c) (((c)>>4)&0x0F)typedef struct very_large_int {    char buf[VLI_LEN_MAX / 2];} vli;static void vli_add(vli *a, vli *b){    int i, c = 0;    for (i = VLI_LEN_MAX / 2 - 1; i >= 0; i--) {        if (LO4(a->buf[i]) + LO4(b->buf[i]) + c >= 10) {            a->buf[i] += LO4(b->buf[i]) + c - 10;            c = 1;        } else {            a->buf[i] += LO4(b->buf[i]) + c;            c = 0;        }        if (HI4(a->buf[i]) + HI4(b->buf[i]) + c >= 10) {            a->buf[i] += ((HI4(b->buf[i]) + c) << 4) - 0xA0;            c = 1;        } else {            a->buf[i] += (HI4(b->buf[i]) + c) << 4;            c = 0;        }    }}static int vli_minus(vli *a){    return HI4(a->buf[0]) > 4 ? 1 : 0;}static void vli_cc(vli *a){    int i, c = 1;    vli b = {0};    b.buf[VLI_LEN_MAX / 2 - 1] = 0x01;    for (i = VLI_LEN_MAX / 2 - 1; i >= 0; i--) {        a->buf[i] = 0x99 - a->buf[i];    }    vli_add(a, &b);}static int str2vli(const char *str, vli *vli){    int i, c = 0, ret, minus = 0;    const char *s = str, *s0, *s1;    for (; *s && isspace(*s); s++) {    }    if ('-' == *s) {        minus = 1;        s++;    }    s0 = s;    for (; *s && isdigit (*s); s++) {    }    s1 = s;    ret = s1 - str;    memset(vli, 0, sizeof(vli));    for (i = VLI_LEN_MAX / 2 - 1; i >= 0; i--) {        if (--s1 < s0) {            break;        }        vli->buf[i] |= *s1 - '0';        if (--s1 < s0) {            break;        }        vli->buf[i] |= (*s1 - '0') << 4;    }    if (minus) {        vli_cc(vli);    }    return ret;}static int vli2str(const vli *a, char *str){    int i, z = 1, len = 0, minus = 0;    vli b = {0};    if (vli_minus(a)) {        b = *a;        vli_cc(&b);        a = &b;        minus = 1;    }    if (minus) {        str[len++] = '-';    }    for (i = 0; i < VLI_LEN_MAX / 2; i++) {        if (z) {            if (!a->buf[i]) {                continue;            }            z = 0;            if (!HI4(a->buf[i])) {                str[len++] = LO4(a->buf[i]) + '0';                continue;            }        }        str[len++] = HI4(a->buf[i]) + '0';        str[len++] = LO4(a->buf[i]) + '0';    }    if (!len) {        str[len++] = '0';    }    str[len] = 0;    return len;}#define CASE_MAX 20vli data[CASE_MAX][3];char line[2048];char vs[3][1024];int main(int argc, char *argv[]){    int t, i, len;    do {        printf("please input the number of cases (T):\n");        gets(line);    } while (!sscanf(line, "%d", &t) || t < 1 || t > 20);    for (i = 0; i < t; i++) {        for (;;) {            gets(line);            len = str2vli(line, &data[i][0]);            len = str2vli(line + len, &data[i][1]);            if (!len) {                printf("please input again:\n");                continue;            }            break;        }    }    for (i = 0; i < t; i++) {        data[i][2] = data[i][0];        vli_add(&data[i][2], &data[i][1]);        vli2str(&data[i][0], vs[0]);        vli2str(&data[i][1], vs[1]);        vli2str(&data[i][2], vs[2]);        printf("Case %d:\n%s + %s = %s\n\n", i + 1, vs[0], vs[1], vs[2]);    }    return 0;} 

热点排行