python和C++ NOI程式設計題解1.1

xiong_sc發表於2019-01-21

01:Hello, World!

python程式設計

print("Hello, World!")

C++程式設計

#include<iostream>
using namespace std;
int main()
{
	cout<<"Hello, World!"<<endl;
}

 

02:輸出第二個整數

python程式設計

a, b, c = map(int, input().split())
print(b)

C++程式設計

#include<iostream>
using namespace std;
int main()
{
	int a,b,c;
	cin>>a>>b>>c;
	cout<<b<<endl;
	return 0;
}

03:對齊輸出

 

python程式設計

a, b, c = map(int, input().split())
print(format(a, '>8'), format(b, '>8'), format(c, '>8'))

C++程式設計

#include<iostream>
#include<iomanip>
using namespace std;
int  main()
{
	int a,b,c;
	cin>>a>>b>>c;
	cout << setw(8) <<right << a << " " << setw(8) <<right << b << " "<< setw(8) <<right << c;
	cout << endl;
}

 

04:輸出保留3位小數點浮點數  

 

python程式設計

a = float(input())
print('%.3f' % a)

C++程式設計

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	double a;
	cin>>a;
	cout<<fixed<<setprecision(3)<<a<<endl;
}

05:輸出保留12位小數的浮點數

 

python程式設計

a = float(input())
print('%.12f' % a)

C++程式設計

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	double a;
	cin>>a;
	cout<<fixed<<setprecision(12)<<a<<endl;
}

06:空格分隔輸出

 

python程式設計

a = input()
b = int(input())
c = float(input())
d = float(input())
print('%c %d %.6f %.6f' % (a, b, c, d))

該處有錯,希望有大佬可以改正

C++程式設計

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	char a;
	int b;
	float c;
	double d;
	cin>>a>>b>>c>>d;
	cout<<a<<" "<<b<<" "<<fixed<<setprecision(6)<<c<<" "<<fixed<<setprecision(6)<<d<<endl;
}

07:輸出浮點數

 

python程式設計

a = float(input())
print('%f\n%.5f\n%e\n%g' % (a, a, a, a))

C++程式設計

#include <cstdio>
using namespace std;
int main()
{
    double x;
    scanf("%lf",&x);
    printf("%f\n%.5f\n%e\n%g\n",x,x,x,x);
    return 0;
}

08:字元三角形

 

python程式設計

a = input()
print('  '+a+'  ')
print(' '+a+a+a+' ')
print(a+a+a+a+a)

C++程式設計

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	char ch;
	cin>>ch;
	for(int i=1;i<=3;++i)
	{
		for(int j=1;j<=3-i;++j)
			cout<<" ";
		for(int k=1;k<=2*i-1;++k)
			cout<<ch;
	cout<<endl;
	}
}

09:字元菱形

 

python程式設計

a = input()
print('  '+a+'  ')
print(' '+a+a+a+' ')
print(a+a+a+a+a)
print(' '+a+a+a+' ')
print('  '+a+'  ')

C++程式設計

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	char ch;
	cin>>ch;
	for(int i=1;i<=3;++i)
	{
		for(int j=1;j<=3-i;++j)
			cout<<" ";
		for(int k=1;k<=2*i-1;++k)
			cout<<ch;
	cout<<endl;
	}
	for(int i=1;i<=2;++i)
	{
		for(int j=1;j<=i;++j)
			cout<<" ";
		for(int k=1;k<=5-2*i;++k)
			cout<<ch;
	cout<<endl;
	}
}

10:超級瑪麗遊戲

 

python程式設計

 

print("                ********")
print("               ************")
print("               ####....#.")
print("             #..###.....##....")
print("             ###.......######              ###                 ###           ###           ###")
print("                ...........               #...#               #...#         #...#         #...#")
print("               ##*#######                 #.#.#               #.#.#         #.#.#         #.#.#")
print("            ####*******######             #.#.#               #.#.#         #.#.#         #.#.#")
print("           ...#***.****.*###....          #...#               #...#         #...#         #...#")
print("           ....**********##.....           ###                 ###           ###           ###")
print("           ....****    *****....")
print("             ####        ####")
print("           ######        ######")
print("##############################################################              ##################################")
print("#...#......#.##...#......#.##...#......#.##------------------#              #...#......#.##------------------#")
print("###########################################------------------#              ###############------------------#")
print("#..#....#....##..#....#....##..#....#....#####################              #..#....#....#####################")
print("##########################################    #----------#                  ##############    #----------#")
print("#.....#......##.....#......##.....#......#    #----------#                  #.....#......#    #----------#")
print("##########################################    #----------#                  ##############    #----------#")
print("#.#..#....#..##.#..#....#..##.#..#....#..#    #----------#                  #.#..#....#..#    #----------#")
print("##########################################    ############                  ##############    ############")

 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;
}

相關文章