蒟蒻的博客-番外一-洛谷题单-顺序结构详解

P1001 A+B Problem

#include<cstdio>
using namespace std;
int main()
{
	long long a,b;
	scanf("%lld %lld",&a,&b);
	printf("%lld",a+b);
	return 0;
}

唯一坑点,a、b的范围是在longlong以内,而非int,使用int会造成溢出导致答案错误

P1000 超级玛丽游戏

#include<cstdio>
using namespace std;
int main()
{
	printf("                ********\n               ************\n               ####....#.\n             #..###.....##....\n             ###.......######              ###            ###\n                ...........               #...#          #...#\n               ##*#######                 #.#.#          #.#.#\n            ####*******######             #.#.#          #.#.#\n           ...#***.****.*###....          #...#          #...#\n           ....**********##.....           ###            ###\n           ....****    *****....\n             ####        ####\n           ######        ######\n##############################################################\n#...#......#.##...#......#.##...#......#.##------------------#\n###########################################------------------#\n#..#....#....##..#....#....##..#....#....#####################\n##########################################    #----------#\n#.....#......##.....#......##.....#......#    #----------#\n##########################################    #----------#\n#.#..#....#..##.#..#....#..##.#..#....#..#    #----------#\n##########################################    ############");
	return 0;
}

这就不过多赘述了,题目的输出都给你了,直接按照题目格式输出完事

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

#include<cstdio>
using namespace std;
long long a,b,c;
int main()
{
	scanf("%lld%lld",&a,&b);
	printf("%lld",a*b);
}

就是简单的乘法,但是特别注意,题目只是说了输入在int内,但是完全有可能两个int的乘积超过int,所以我们用longlong

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

#include<cstdio>
using namespace std;
char c;
int main()
{
	scanf("%c",&c);
	printf("%c",c+'A'-'a');
}

这个算法其实很简单,但是你要清楚ascll码与其对应关系(详细可见这篇博客

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

#include<cstdio>
using namespace std;
char s[10];
int flag=0;
int main()
{
	scanf("%s",&s);
	for(int i=8;i>=0;i--)
	{
		if(s[i]) flag=1;
		if(flag) printf("%c",s[i]);
	}
	return 0;
}

这是不使用其他库函数的方法,先以字符串的形式读取进来,然后通过反向便利与达标计的方法相结合,即可输出正确答案
在这里,我们也可以使用 < string > 中的reverse函数来实现,具体方法请参考www.cplusplus.com

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

#include<cstdio>
using namespace std;
int a;
double b;
int main()
{
	scanf("%lf%d",&b,&a);
	printf("%.3lf\n%d",b/a,a*2);
}

没有什么说的,就是一个浮点数和整数的运算,唯一提示的一点,就是在运算中,如果有多个变量类型混合运算,系统会强制将这些变量类型转换为其中最高级的类型
具体规则如下:

char -> int -> unsigned -> long long -> double <- float

P1425 小鱼的游泳时间

#include<cstdio>
using namespace std;
int main()
{
	int a,b,c,d,h,m;
	scanf("%d%d%d%d",&a,&b,&c,&d);
	h=c-a;	m=d-b;
	if(m<0)
	{
		m+=60;	h--;
	}
	printf("%d %d",h,m);
	return 0;
}

就不过多解释了,相信大家看了题目且具有小学数学知识就都可以看懂吧

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

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main() {
    int T;
    cin >> T;
    if (T == 1) {
        cout << "I love Luogu!";
    }
	else if (T == 2) {
        cout << 2 + 4 << " " << 10 - 2 - 4;
    }
	else if (T == 3) {
    	cout<<3<<endl<<12<<endl<<2<<endl;
    }
	else if (T == 4) {
    	printf("%.3lf\n",500.0/3.0);
    }
	else if (T == 5) {
    	cout<<15<<endl;
    }
	else if (T == 6) {
    	cout<<sqrt(6*6+9*9)<<endl;
    }
	else if (T == 7) {
    	cout<<110<<endl<<90<<endl<<0<<endl;
    }
	else if (T == 8) {
    	double const pi=3.141593;
    	double const r=5;
    	cout<<pi*r*2<<endl<<pi*r*r<<endl<<4.0/3*pi*r*r*r<<endl;
    }
	else if (T == 9) {
    	cout<<22<<endl;
    }
	else if (T == 10) {
    	cout<<9<<endl;
    }
	else if (T == 11) {
    	cout<<100.0/(8-5)<<endl;
    }
	else if (T == 12) {
    	cout<<13<<endl<<"R"<<endl;
    }
	else if (T == 13) {
    	double const pi=3.141593;
    	double V=pi*4*4*4*4/3+pi*10*10*10*4/3;
    	cout<<floor(pow(V,1.0/3))<<endl;
    }
	else if (T == 14) {
    	cout<<50<<endl;
    }
    return 0;
}

题目P话太多,看懂意思,知道if…else if…的原理就可以了,代码随便抄一下(这代码也是抄的)

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

#include<cstdio>
#include<cmath>
using namespace std;
double a,b,c,p;

int main()
{
	scanf("%lf%lf%lf",&a,&b,&c);
	p=0.5*(a+b+c);
	printf("%.1lf",sqrt(p*(p-a)*(p-b)*(p-c)));
	return 0;
}

套公式就完事儿

P1421 小玉买文具

#include<cstdio>
using namespace std;
int main()
{
	int a=0,b=0;
	int c;
	int n=0;
	scanf("%d%d",&a,&b);
	c=a*10+b;
	n=c/19;
	printf("%d",n);
	return 0;
}

这里输入数据既有元,也有角,不方便我们的运算,于是我们先统一单位,全部转化为用角来计算,这样就方便很多了

P5709 【深基2.习6】Apples Prologue

#include<cstdio>
using namespace std;
int m,t,s;


int upper(int a,int b)
{
	int c;
	if(b==0) return m;
	c=a/b;
	if((double)c<((double)a/b))	c++;
	if(c>=m) c=m;
	return c;
}

int main()
{
	scanf("%d%d%d",&m,&t,&s);
	printf("%d",m-upper(s,t));
	return 0;
}

注意题目是剩余完整苹果,也就是说,我们一个苹果如果吃了一半,那么这个苹果也是不能计算在内的

P2181 对角线

#include<cstdio>
using namespace std;
unsigned long long a;
unsigned long long n;
int main()
{
	scanf("%lld",&n);
	a=n*(n-1)/2*(n-2)/3*(n-3)/4;
	printf("%lld",a);
}

暴力枚举肯定爆,所以直接套公式(注意longlong)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值