本文从客观角度出发,用较为【数据删除】的说法来说明。
1.代码结构
1|1 代码非主体部分
以高精加为例
C++:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char a1[100],b1[100];
int a[100],b[100],c[100],lena,lenb,lenc,i,x;
int main(){
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
gets(a1);
gets(b1); //输入加数与被加数
lena=strlen(a1);
lenb=strlen(b1);
for (i=0;i<=lena-1;i++) a[lena-i]=a1[i]-48; //加数放入a数组
for (i=0;i<=lenb-1;i++) b[lenb-i]=b1[i]-48; //加数放入b数组
lenc =1;
x=0;
while (lenc <=lena||lenc <=lenb){
c[lenc]=a[lenc]+b[lenc]+x; //两数相加
x=c[lenc]/10;
c[lenc]%=10;
lenc++;
}
c[lenc]=x;
if (c[lenc]==0)
lenc--; //处理最高进位
for (i=lenc;i>=1;i--)
cout<<c[i]; //输出结果
cout<<endl;
return 0;
}
Python:
from functools import reduce#导入reduce用于将整数列表中的内容转换为字符串
def add(a, b):
if len(a) > len(b):#将输入的数字左端补齐为同样的长度
b = b.zfill(len(a) - len(b) + 1)
else:
a = a.zfill(len(b) - len(a) + 1)
#("0" + str(a))前面加"0"的原因是防止最高位进位是超过列表的容量限制
#比如说9+9会向前面进一位,如果没有添加额外的0的话进位1就没地方存储
a = list(map(lambda x : int(x), ("0" + str(a)).strip()))
b = list(map(lambda x : int(x), ("0" + str(b)).strip()))
for i in range(len(a) - 1, 0, -1):
temp = a[i] + b[i];
a[i - 1] += temp // 10
a[i] = temp % 10
return int(reduce(lambda x, y : str(x) + str(y), a))#转换为字符串
a, b = input("input a: "), input("input b: ")
print(add(a, b))
搞错了,Python是下面一个:
print(int(input())+int(input()))
我:服了服了
没想到Python如此的简单(滑稽)
首先先看一下C++的非代码主体部分(头文件+变量定义):
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char a1[100],b1[100];
int a[100],b[100],c[100],lena,lenb,lenc,i,x;
but!
Python并没有这些东西!
1|2 代码主体结构
此处的主体结构指C++的main()
函数和别的函数和Python的非定义变量语句。
C++:
int main(){
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
gets(a1);
gets(b1); //输入加数与被加数
lena=strlen(a1);
lenb=strlen(b1);
for (i=0;i<=lena-1;i++) a[lena-i]=a1[i]-48; //加数放入a数组
for (i=0;i<=lenb-1;i++) b[lenb-i]=b1[i]-48; //加数放入b数组
lenc =1;
x=0;
while (lenc <=lena||lenc <=lenb){
c[lenc]=a[lenc]+b[lenc]+x; //两数相加
x=c[lenc]/10;
c[lenc]%=10;
lenc++;
}
c[lenc]=x;
if (c[lenc]==0)
lenc--; //处理最高进位
for (i=lenc;i>=1;i--)
cout<<c[i]; //输出结果
cout<<endl;
return 0;
}
但是!
Python就那么一句话!
print(int(input())+int(input()))
大惊。
2.输出语句的差异
首先看一下某谷经典题目
这题当年坑死我也(输出这么大一张字符画,复制都累死了)
C++正解:
#include<iostream>
using namespace std;
int main()
{
cout<<" ********"<<endl;
cout<<" ************"<<endl;
cout<<" ####....#."<<endl;
cout<<" #..###.....##...."<<endl;
cout<<" ###.......###### ### ###"<<endl;
cout<<" ........... #...# #...#"<<endl;
cout<<" ##*####### #.#.# #.#.#"<<endl;
cout<<" ####*******###### #.#.# #.#.#"<<endl;
cout<<" ...#***.****.*###.... #...# #...#"<<endl;
cout<<" ....**********##..... ### ###"<<endl;
cout<<" ....**** *****...."<<endl;
cout<<" #### ####"<<endl;
cout<<" ###### ######"<<endl;
cout<<"##############################################################"<<endl;
cout<<"#...#......#.##...#......#.##...#......#.##------------------#"<<endl;
cout<<"###########################################------------------#"<<endl;
cout<<"#..#....#....##..#....#....##..#....#....#####################"<<endl;
cout<<"########################################## #----------#"<<endl;
cout<<"#.....#......##.....#......##.....#......# #----------#"<<endl;
cout<<"########################################## #----------#"<<endl;
cout<<"#.#..#....#..##.#..#....#..##.#..#....#..# #----------#"<<endl;
cout<<"########################################## ############"<<endl;
return 0;
}
但是,但是……
Python居然可以写成这样:
print(''' ********
************
####....#.
#..###.....##....
###.......###### ### ###
........... #...# #...#
##*####### #.#.# #.#.#
####*******###### #.#.# #.#.#
...#***.****.*###.... #...# #...#
....**********##..... ### ###
....**** *****....
#### ####
###### ######
##############################################################
#...#......#.##...#......#.##...#......#.##------------------#
###########################################------------------#
#..#....#....##..#....#....##..#....#....#####################
########################################## #----------#
#.....#......##.....#......##.....#......# #----------#
########################################## #----------#
#.#..#....#..##.#..#....#..##.#..#....#..# #----------#
########################################## ############''')
多行字符串就是好啊
(但是山外有山,PHP更方便)
but
C也可以写多行字符串……
3.是否需要编译
C++是典型的要编译的语言,但是Python并不需要编译,直接解释运行……
Python:C++他就是个屑啦
C++:Python你欺人太甚
总结
所以Python才会强到全员都学
所以Python才会被CCF给ban掉
所以……
#include<iostream>
using namespace std;
int main(){
cout<<"C++永垂不朽!"<<endl;
return 0;
}
(手动滑稽)