洛谷题单 【入门1】顺序结构刷题C/C++

目录

B2002 Hello,World!

B2025 输出字符菱形

P1000 超级玛丽游戏

P1001 A+B Problem

B2005 字符三角形

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

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

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

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

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

P5707 【深基2.例12】上学迟到

B2029 大象喝水

P1425 小鱼的游泳时间

P1421 小玉买文具

P3954 [NOIP2017 普及组] 成绩


B2002 Hello,World!

#include <iostream>
using namespace std;
int main()
{
    cout<<"Hello,World!"<<endl;//cout是输出函数
    return 0;
}

B2025 输出字符菱形

这道题可以把菱形分成上下两个部分,已知菱形一共5行,用一个大的循环控制行数,接下来的循环都在这个大循环里面

上部分三行,每一行包括空格和*号,可知要用两个循环,第一个循环控制空格,第二个循环控制*号,并且*号输出够了以后输出换行;

用点数学找规律可知判断循环执行的条件;

C语言版本:

#include <stdio.h>
int main()
{
	int i,j,k;   //这几个用来循环
	for(i=0;i<=2;i++)
{                            //菱形上部分有三行,用来控制行数,一共三行
		for(k=0;k<=1-i;k++){//用来控制前三行的空格数
			printf(" ");
		}
		for(j=0;j<1+2*i;j++){//用来控制前三行的*数
			printf("*");
}printf("\n");}//*号后输出换行
	for(i=0;i<=1;i++){//下部分和上部分差不多
		for(k=0;k<1+i;k++){
			printf(" ");
		} 
		for(j=0;j<3-2*i;j++){
			printf("*");
		}printf("\n");
	}
	return 0;
}

C++版本:

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

P1000 超级玛丽游戏

#include <stdio.h>
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;
}

P1001 A+B Problem

好吧,同志们,我们就从这一题开始,向着大牛的路进发。

任何一个伟大的思想,都有一个微不足道的开始。

C语言:

#include <stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b);//输入a,b
int sum=a+b;//算a+b的和
printf("%d",sum);//输出
return 0;
}

C++版本:

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

B2005 字符三角形

输出字符就行,和前面的那道有异曲同工之妙

#include <iostream>
using namespace std;
int main()
{
	int i,j,k;
	char a;
	scanf("%c",&a);
	for(i=0;i<=2;i++){
		for(k=0;k<=1-i;k++){
			cout<<" ";
		}
		for(j=0;j<1+2*i;j++){
			cout<<a;
		}cout<<endl;
	    
	}
	return 0; 
}

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

其实就是输入的两个数相乘啦!

C语言版本:

#include <stdio.h>
int main()
{
  int a,b,s;
  scanf("%d %d",&a,&b);
  s=a*b;
  printf("%d",s);
return 0;
}

C++版:

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

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

这个看ascii表

发现大写字母减掉32就成小写字母了

C语言:

#include <stdio.h>
int main()
{
    char x;
    scanf("%c",&x);
    if(x>97)
      x-=32;//x=x-32
    printf("%c",x);
    return 0;
}

C++版本:

#include <iostream>
using namespace std;
int main()
{
    char x;
    cin>>x;
    if(x>97)
    cout<<(char)(x-32);//记得强制转换
    return 0;
}

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

C++版:

#include <bits/stdc++.h>
using namespace std;
int main(void)
{
    string s;
    cin>>s;
    reverse (s.begin(),s.end());//有现成的函数用,可以自己搜一下reverse函数
    cout<<s;
    return 0;
}

C语言:

#include <stdio.h>
#include <string.h>
int main()
{
	char a[10]={'\0'};
	int i;
	scanf("%s",a);
	for(i=strlen(a)-1;i>=0;i--){//注意i从strlen(a)-1开始
		printf("%c",a[i]);
	}
	return 0;
}

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

读懂题就行

#include <stdio.h>
int main()
{
double a,b;
int c,d;
scanf("%lf %d",&a,&c);
b=a/c;
d=c*2;
printf("%.3lf\n%d",b,d);
return 0;
}

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

C语言:

#include <stdio.h>
#include <math.h>//有根号记得要用这个头文件
int main()
{
double a,b,c;
double p,s;
scanf("%lf %lf %lf",&a,&b,&c);
p=(a+b+c)/2.0;
if(a+b>c&&a+c>b&&b+c>a)
{
s=sqrt(p*(p-a)*(p-b)*(p-c));//sqrt就代表根号
printf("%.1lf",s);
    
}
return 0;
}

P5707 【深基2.例12】上学迟到

第一道普及-题!

题目描述

学校和 yyy 的家之间的距离为 s 米,而 yyy 以 v 米每分钟的速度匀速走向学校。

在上学的路上,yyy 还要额外花费 10分钟的时间进行垃圾分类。

学校要求必须在上午 8:00 到达,请计算在不迟到的前提下,yyy 最晚能什么时候出门。

由于路途遥远,yyy 可能不得不提前一点出发,但是提前的时间不会超过一天。

输入格式

一行两个正整数 s,v,分别代表路程和速度。

输出格式

输出一个 24 小时制下的时间,代表 yyy 最晚的出发时间。

输出格式为 HH:MM,分别代表该时间的时和分。必须输出两位,不足前面补 0。

输入输出样例

输入 #1复制

100 99

输出 #1复制

07:48

C语言:

#include <stdio.h>
#define end 480+24*60//时间全部化成分钟,在这个时间内要到达
int main()
{
    int s,v,t,a,b;
    scanf("%d %d",&s,&v);
    t=s/v;//计算要多少时间
    a=s%v;//看输入样例的意思不是整数要向上取整
    if(a>0)
    {
        t+=1;
    }
    if(t<480){
    b=end-t-10-24*60;
    printf("%02d:%02d",b/60,b%60);//得出H和M
    }
    if(t>480)
    {
        b=end-t-10;
         printf("%02d:%02d",b/60,b%60);
    }
    
    return 0;
}

C++:

#include <bits/stdc++.h>
#define end 480+24*60
using namespace std;
int main()
{
    int t,b;
    double s,v;
    cin>>s>>v;
    t=ceil(s/v);//取整函数,ceil向上取整,floor向下取整,round四舍五入
    if(t<=480){
    b=end-t-10-24*60;
    printf("%02d:%02d",b/60,b%60);
	}
    if(t>480){
        b=end-t-10;
         printf("%02d:%02d",b/60,b%60);
    }
    
    return 0;
}

B2029 大象喝水

C语言:

#include <stdio.h>
#define PI 3.14
int main()
{
    int t,r,h,a;
    double sum;
    scanf("%d %d",&h,&r);
    sum=PI*r*r*h;//圆桶体积
    t=20000/sum;
    a=sum*t;
    if(a<20000){
        t+=1;
    }printf("%d",t);
    return 0;
}

C++:

#include <bits/stdc++.h>
using namespace std;
#define PI 3.14
int main()
{
    int r,h;
    double t,sum;
    cin>>h>>r;
    sum=PI*r*r*h;
    t=ceil(20000/sum);//这个函数真的好用
    cout<<t<<endl;
    return 0;
}

P1425 小鱼的游泳时间

C语言:

#include <stdio.h>
int main()
{
    int a,b,c,d;
    scanf("%d %d %d %d",&a,&b,&c,&d);
    int t1=c-a;
    int t2=d-b;
    if(t2<0){
        t1-=1;
        t2+=60;
    }
    printf("%d %d",t1,t2);
    return 0;
}

C++:

#include <iostream>
using namespace std;
int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    int t1=c-a;
    int t2=d-b;
    if(t2<0){
        t1-=1;
        t2+=60;
    }
    cout<<t1<<" "<<t2<<endl;
    return 0;
}

P1421 小玉买文具

#include <stdio.h>
int main()
{
    int a,b,c;
    scanf("%d %d",&a,&b);
    c=(a*10+b)/19;
    printf("%d",c);
    return 0;
}

P3954 [NOIP2017 普及组] 成绩

没什么技术含量

#include <iostream>
using namespace std;
int main()
{
    int a,b,c,d;
    cin>>a>>b>>c;
    d=a*0.2+b*0.3+c*0.5;
    cout<<d<<endl;
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值