구현(Implementation)

백준 1225번: 이상한 곱셈

cepiloth 2018. 6. 20. 14:47
반응형

https://www.acmicpc.net/problem/1225


1. 문제

A*B를 계산하다 지겨워진 형택이는 A*B를 새로운 방법으로 정의하려고 한다.
A에서 한 자리를 뽑고 * B에서 임의로 한 자리를 뽑아 곱한다.
의 가능한 모든 조합 (A가 n자리, B가 m자리 수라면 총 가능한 조합은 n*m개)을 더한 수로 정의하려고 한다.
예를 들어 121*34는 1*3 + 1*4 + 2*3 + 2*4 + 1*3 + 1*4 = 28 이 된다.
이러한 형택이의 곱셈 결과를 구하는 프로그램을 작성하시오.


2. 알고리즘

문제 그대로 풀면 됩니다.


3. 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
 
int main() {
    //std::ios::sync_with_stdio(false); cin.tie(0);
 
    char a[10001];
    char b[10001];
 
    long long cnt = 0;
    scanf("%s %s",a,b);
 
    int i,j, t = 0;
 
    for(i=0;b[i];i++)
        t += b[i]-'0';
 
    long long sol = 0;
    for(i=0;a[i];i++) {
        sol += (a[i]-'0'* t;
    }
 
    printf("%lld",sol);
    return 0;
}
cs

반응형