计蒜客c++、python刷题笔记1

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值