列表

详情


NC223556. MethodicMultiplication

描述

After one computer crash too many,  Alonso has had enough of all this shoddy software and poorly written code! He decides that in order for this situation to improve, the glass house that is modern programming needs to be torn down and rebuilt from scratch using only completely formal axiomatic reasoning. As one of the first steps, he decides to implement arithmetic with natural numbers using the Peano axioms.

The Peano axioms (named after Italian mathematican Giuseppe Peano) are an axiomatic formalization of the arithmetic properties of the natural numbers. We have two symbols:the constant 0, and a unary successor function S. The natural numbers, starting at 0, are then , and so on. With these two symbols , the operations of addition and multiplication are defined inductively by the following axioms: for any natural numbers x and y, we have

The two axioms on the left define addition, and the two on the right define multiplication. For instance, given and we can repeatedly apply these axioms to derive

Write a program which given two natural numbers x and y, defined in Peano arithmetic, computes the product.

输入描述

The input consists of two lines. Each line contains a natural number defined in Peano arithmatic, using at most 1000 characters.

输出描述

Output the product of the two input numbers.

示例1

输入:

S(S(0))
S(S(S(0)))

输出:

S(S(S(S(S(S(0))))))

示例2

输入:

S(S(S(S(S(0)))))
0

输出:

0

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 9ms, 内存消耗: 768K, 提交时间: 2021-08-12 14:48:23

#include<bits/stdc++.h>
using namespace std;
int main()
{
	char s1[1001],s2[1001];
	cin>>s1>>s2;
	int a=(strlen(s1)-1)/3,b=(strlen(s2)-1)/3,c=a*b;
	for(int i=0;i<c;++i)
	cout<<"S(";
	cout<<0;
	for(int i=0;i<c;++i)
	cout<<')';
	return 0;
}

Python3 解法, 执行用时: 50ms, 内存消耗: 7040K, 提交时间: 2021-08-03 13:23:48

a=input().count("S")
b=input().count("S")
ans=a*b
if(ans==0):
	print(0)
else:
	print("S("*ans+'0'+')'*ans)

上一题