[ 2020.11.24 ] 洛谷【入门1】顺序结构

本文档展示了作者在学习C++编程过程中完成的一些基础题目,包括简单的算术操作、字符转换、数字反转、苹果采购、图形打印、小学数学问题、时间计算等。通过这些例子,作者旨在巩固C++的基础知识,如输入输出、字符串操作、算术运算等。
摘要由CSDN通过智能技术生成


前言

大二了,编程基础有点差,从洛谷的最简单开始重学C++

还是比较菜的…有什么可以改进的,师傅们可以提出来。


1. P1001 A+B Problem

在这里插入图片描述

代码如下:

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

2. P1000 超级玛丽游戏

在这里插入图片描述

代码如下:

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

3. P5703 【深基2.例5】苹果采购

在这里插入图片描述


代码如下:

#include<iostream>
using namespace std;
int main(){
    int x,y;
    cin>>x>>y;
    cout<<x*y<<endl;
    return 0;
}

4. P5704 【深基2.例6】字母转换

在这里插入图片描述

大写字母和小写字母的ASCII码相差32,这里复习了ASCII码对照表

在这里插入图片描述

代码如下:

#include<iostream>
using namespace std;
int main(){
    char a,b;
    cin>>a;
    b=a-32;
    cout<<b;
    return 0;
}

5. P5705 【深基2.例7】数字反转

在这里插入图片描述

如果用数字做的话会很麻烦,实际上这道题考的是字符串的应用。

代码如下:

#include<iostream>
using namespace std;
int main(){
	string a;
	cin >> a; 
	for(int i=a.size()-1; i>=0; i--){ 
		cout << a[i];
	}
	return 0;
}

6. P5706 【深基2.例8】再分肥宅水

在这里插入图片描述
代码如下:

#include<bits/stdc++.h>
using namespace std;

int main(){
    double t;
    int n;
    cin>>t>>n;
    cout<<setprecision(3)<<fixed<<t/n<<endl<<n*2;
    return 0;
}

这里用到的是setpricision()函数:

setprecision()用法

在头文件 iomanip 中
单独使用setprecision(n),是保留n位有效数字
而setprecision(n),和fixed合用是保留小数点后几位。
示例代码后续再贴上~

7. P1425 小鱼的游泳时间

在这里插入图片描述
代码如下:

#include<bits/stdc++.h>
using namespace std;

int main(){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    int tmp;
    tmp = c*60+d-(a*60+b);
    cout<<tmp/60<<' '<<tmp%60<<endl;
    return 0;
}

8. P2433 【深基1-2】小学数学 N 合一

…就是很麻烦,代码不算很难,认真一点就可以AC

#include<iostream>
using namespace std;
#include<cstdio>
#include<cmath>
#include <iomanip>
int main() {
    int T;
    cin >> T;
    if (T == 1) {
        // 粘贴问题 1 的主函数代码,除了 return 0
        cout << "I love Luogu!";
    } else if (T == 2) {
        // 粘贴问题 2 的主函数代码,除了 return 0
        cout << 2 + 4 << " " << 10 - 2 - 4;
    } else if (T == 3) {
        cout<<3<<endl<<12<<endl<<2;
    } else if (T == 4) {
        float a=500.0/3.0;
        cout<<setprecision(6)<<a;
    } else if (T == 5) {
        cout<<(260+220)/(20+12)<<endl;
    } else if (T == 6) {
        cout<<sqrt(pow(6,2)+pow(9,2));
    } else if (T == 7) {
        cout<<100+10<<endl<<100+10-20<<endl<<0<<endl;
    } else if (T == 8) {
        float pi=3.141593;
	    float l=2.0*pi*5.0,s=pi*25.0,v=4.0/3.0*pi*125.0;
	    cout<<l<<endl<<s<<endl<<v;
    } else if (T == 9) {
        cout<<22<<endl;
    } else if (T == 10) {
        cout<<9<<endl;
    } else if (T == 11) {
        cout<<100.0/3.0<<endl;
    } else if (T == 12) {
        cout<<13<<endl<<'R'<<endl;
    } else if (T == 13) {
        float V=4.0/3.0*3.141593*(64.0+1000.0),a;
	    a=pow(V,1.0/3.0);
	    a=int(a);
	    cout<<a;
    } else if (T == 14) {
        float x=0;
	for(int i=0;i<110;i++){
		x++;
		if(120*x-x*x==3500){
			cout<<x;
			break;
		}
	}
    }
    return 0;
}

9. P5708 【深基2.习2】三角形面积

在这里插入图片描述

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main() {
	double a, b, c;
	cin >> a >> b >> c;
	double p = 1.0 / 2 * (a + b + c);
	double s = sqrt(p * (p - a) * (p - b) * (p - c));
	cout<<setprecision(1)<<fixed<<s;
	return 0;
}

10. P1421 小玉买文具

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    int x = (a*10+b)/19;
    cout<<x<<endl;
    return 0;
}

11. P5709 【深基2.习6】Apples Prologue

在这里插入图片描述

#include<iostream>
#include<cmath>
using namespace std;
int main(){
	int m; //m个苹果
	int t; //吃一个苹果t分钟
	int s; //总用时s分钟
	cin>>m>>t>>s;
	if(s>=m*t){ //坑点1.首先判断过去的时间是否吃完了苹果,吃完直接输出0
		cout<<0;
	}
    else{
	if(s%t == 0){  //坑点2.判断过去的时间是否能整除吃一个苹果的时间
		cout<<m-(s/t); //可以即剩下的全是完整的苹果
	} else{
		cout<<m-1-(s/t);//不可以,即有一个不完整的苹果让结果减1
	} 
	}
	return 0;
} 

12.P2181 对角线

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main() {
    unsigned long long ans, n;
    cin >> n;
    ans = n * (n-1) /2 * (n-2) /3 * (n-3) /4;
    printf("%lld\n", ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值