001.计算A+B(新手教程)
c++
#include<iostream>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<<a+b<<endl;
return 0;
}
python3
print(sum(int(x) for x in input().split()))
002.输出马里奥
python3
a = ''' ********
************
####....#.
#..###.....##....
###.......######
...........
##*#######
####*******######
...#***.****.*###....
....**********##.....
....**** *****....
#### ####
###### ######'''
print(a)
003.输出字符菱形
c++
#include<iostream>
using namespace std;
int main(){
char c;
cin >> c;
cout << " " << c << endl;
cout <<" " << c << c << c << endl;
cout << c << c << c << c << c << endl;
cout <<" " << c << c << c << endl;
cout << " " << c << endl;
return 0;
}
python3
str1 = input()
b = [1,3,5,3,1]
for i in b:
print((str1*i).center(5))
004.输出Hello, World!
c++
#include<iostream>
#include<iostream>
using namespace std;
int main(){
cout << "Hello, World!" << endl;
return 0 ;
}
python3
print("Hello, World!")
005.输出字符三角形
python3
str1 = input()
b = [1,3,5]
for i in b:
print((str1*i).center(5))
006.对齐输出
c++
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int a,b,c;
cin >> a >> b >> c;
printf("%8d %8d %8d",a,b,c);
return 0;
}
python3
a,b,c = map(int, input().split())
print('{: >8}{}{: >8}{}{: >8}'.format(a,' ',b,' ',c))
007.整型与布尔型的转换
c++
#include<iostream>
using namespace std;
int main(){
int a;
bool b;
cin>>a;
b = (bool)a;
a = b;
cout << a << endl;
return 0;
}
python3
a = int(input())
b = int(bool(a))
print(b)
008.打印字符
c++
#include<iostream>
using namespace std;
int main(){
int a;
char b;
cin >> a;
b = a;
cout << b << endl;
return 0;
}
python3
a = int(input())
print(chr(a))
009.等差数列末项计算
c++
#include<iostream>
using namespace std;
int main(){
int a,b,n,c;
cin >> a >> b >> n ;
c = a + (n-1)*(b-a);
cout << c << endl;
return 0;
}
python3
a,b,n = map(int,input().split())
c = a + (n-1)*(b-a)
print(c)
010.计算线段长度
c++
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main(){
double x1,y1,x2,y2,d;
cin >> x1 >> y1;
cin >> x2 >> y2;
d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
cout << fixed << setprecision(3) << d << endl;
return 0;
}
//当使用float型时,有5组数据通不过,下溢的话还是会有差异的
python3
import math
x1,y1 = map(float,input().split())
x2,y2 = map(float,input().split())
d = math.pow( math.pow(x1-x2,2)+ math.pow(y1-y2,2),0.5)
print('%.3f'%d)