【每日一练】7


题目描述

正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA。例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6。
 
 现给定A、DA、B、DB,请编写程序计算PA + PB。

输入描述:

输入在一行中依次给出A、DA、B、DB,中间以空格分隔,其中0 < A, B < 1010。

输出描述:

在一行中输出PA + PB的值。

输入例子:

3862767 6 13530293 3

输出例子:

399
#include <iostream>
#include <vector>
//#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
int get_Num(long num1,long num2,long num3,long num4){
    //int => string
    stringstream ss1,ss2;
    string str1,str3;
    ss1 << num1;
    str1 = ss1.str();
    ss2 << num3;
    str3 = ss2.str();
    char char2 = (char)(num2+48);
    char char4 = (char)(num4+48);
//    cout << "str1 = " << str1 << endl;
//    cout << "str3 = " << str3 << endl;
    string num1str{""},num2str{""};
    for(auto i = str1.begin();i != str1.end();i++){
//        cout << *i << endl;
        if(*i == char2){
            num1str += char2;
        }
    }
    //string => int
    for(auto i : str3){
        if(i == char4){
            num2str += char4;
        }
    }
//    cout << "num1str = " << num1str << endl;
//    cout << "num2str = " << num2str << endl;
//    cout << "num2str.size() = " << num2str.size() << endl;
    //string => int
    istringstream tet1{num1str};
    istringstream tet2{num2str};

    int tes1,tes2;
    tet1 >> tes1;
    tet2 >> tes2;
    if(num1str.size() == 0)
        tes1 = 0;
    if(num2str.size() == 0)
        tes2 = 0;
//    cout << "char2 = " << char2 << endl;
//    cout << "char4 = " << char4 << endl;
    cout << "tes1+1 = " << tes1+1 << endl;
    cout << "tes2+1 = " << tes2+1 << endl;
    cout << tes1+tes2 << endl;
    return 0;
}
int main()
{
    string str;
    long num1 = 0,num2 = 0,num3 = 0,num4 = 0;
    cin >> num1 >> num2 >> num3 >> num4;
    if(get_Num(num1,num2,num3,num4))
        return 0;
//    cout << "Hello World!" << endl;
    return 0;
}

知识点总结:

参考:

[1] C++ int与string的相互转换(含源码实现)

[2] C++中字符串string和整数int的互相转化方式

① int转string:

stringstream ss1,ss2;
string str1,str3;
ss1 << num1;
str1 = ss1.str();
ss2 << num3;
str3 = ss2.str();

② string转int:

istringstream tet1{num1str};
istringstream tet2{num2str};

int tes1,tes2;
tet1 >> tes1;
tet2 >> tes2;
if(num1str.size() == 0)
    tes1 = 0;
if(num2str.size() == 0)
    tes2 = 0;

③ 数字转char类型:(例如7转为 '7')

char char2 = (char)(num2+48);
char char4 = (char)(num4+48);

题目描述

本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数。你需要输出商数Q和余数R,使得A = B * Q + R成立。

输入描述:

输入在1行中依次给出A和B,中间以1空格分隔。

输出描述:

在1行中依次输出Q和R,中间以1空格分隔。

输入例子:

123456789050987654321 7

输出例子:

17636684150141093474 3

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值