栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

牛客竞赛语法入门班顺序结构习题C++版本参考代码

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

牛客竞赛语法入门班顺序结构习题C++版本参考代码

牛客竞赛语法入门班顺序结构习题

C语言版本的参考代码

1001 这是一道签到题

#include 
using namespace std;
int main()
{
    cout << "zhe" << endl;
    cout << "shi" << endl;
    cout << "yi" << endl;
    cout << "dao" << endl;
    cout << "qian" << endl;
    cout << "dao" << endl;
    cout << "ti" << endl;
    return 0;
}

1002 排列式

#include 
using namespace std;
int main()
{
	//每个cout打印一行
    cout << "4396 = 28 x 157" << endl;
    cout << "5346 = 18 x 297" << endl;
    cout << "5346 = 27 x 198" << endl;
    cout << "5796 = 12 x 483" << endl;
    cout << "5796 = 42 x 138" << endl;
    cout << "6952 = 4 x 1738" << endl;
    cout << "7254 = 39 x 186" << endl;
    cout << "7632 = 48 x 159" << endl;
    cout << "7852 = 4 x 1963" << endl;
    return 0;
}

OJ不会识别字符串里的换行,所以这种写法是错误的

#include 
using namespace std;
int main()
{
    cout << "
    4396 = 28 x 157
    5346 = 18 x 297
    5346 = 27 x 198
    5796 = 12 x 483
    5796 = 42 x 138
    6952 = 4 x 1738
    7254 = 39 x 186
    7632 = 48 x 159
    7852 = 4 x 1963" << endl;
    return 0;
}

1003 小飞机

#include 
using namespace std;
int main()
{
    cout << "     **     " << endl;
    cout << "     **     " << endl;
    cout << "************" << endl;
    cout << "************" << endl;
    cout << "    *  *    " << endl;
    cout << "    *  *    " << endl;
    return 0;
}

1004 学姐的"Helloworld!"

#include 
using namespace std;
int main()
{
    cout << "Helo word!" << endl;
    return 0;
}

1005 乘法表 方法一
需要注意,两数相乘小于10的时候,可以利用空格占位。

#include 
using namespace std;
int main()
{
    for (int i = 1;i <= 9;i++)
    {
        for (int j = 1;j <= i;j++)
        {
        	//这里需要注意的是打印顺序是j,i,i * j 
            if (i * j < 10 ) printf("%d*%d= %d ",j,i,i * j);
            else printf("%d*%d=%d ",j,i,i * j);
        }
        printf("n");
    }
    return 0;
}

仔细思考i与j分别代表的数字,如果打印顺序是i,j,i * j,则结果如下:

1005 乘法表 方法二

OJ会自动忽略每对双引号之间的空格,故改写法等同于

1*1= 1n1*2= 2 2*2= 4n... ... 
#include 
using namespace std;
int main()
{
    cout << "1*1= 1n"
            "1*2= 2 2*2= 4n"
            "1*3= 3 2*3= 6 3*3= 9n"
            "1*4= 4 2*4= 8 3*4=12 4*4=16n"
            "1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25n"
            "1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36n"
            "1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49n"
            "1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64n"
            "1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81n";
    return 0;
}

1006 KiKi学程序设计基础

在双引号“ ”中再次出现单引号或者时,需要在其前面再加上一个才可以输出其本身。例:

  • 输出"的方法:printf(""");
  • 输出的方法:printf("\");
#include 
using namespace std;
int main()
{
    printf("printf("Hello world!\n");n");
    printf("cout << "Hello world!" << endl;n");
    return 0;
}

1007 疫情死亡率

%5.3lf

表示结果占五位字符(包括小数点在内),其中小数部分占三位

#include 
using namespace std;
typedef long long ll;
int main()
{
    ll a,b;
    scanf("%lld %lld",&a,&b);
    double ans = 1.0 * b / a;//需要先乘以1.0转化为小数运算
    printf("%5.3lf",ans * 100);
    printf("%");
    return 0;
}

1008 爱因斯坦的名言
于题号1006一样,在引号中再次出现引号时,需要在其前面再加上一个才可以输出其本身。

#include 
using namespace std;
int main()
{
    cout << ""Genius is 1% inspiration and 99% perspiration."" << endl;
    return 0;
}

1009 字符串输出1.0

注意错误用法:while(3--)
while(x - -):其中,x要求是变量,不可以是常量

#include 
using namespace std;
int main()
{
    int x = 3;
    while (x--) cout << "Welcome to ACM / ICPC!" << endl;
    return 0;
}

1010 牛牛学说话之-整数

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

1011 牛牛学说话之-浮点数

保留x位小数的格式为%.xlf

#include 
using namespace std;
int main()
{
    double d;
    scanf("%lf",&d);
    printf("%.3lf",d);
    return 0;
}

1012 牛牛学加法

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

1013 牛牛学除法

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

1014 牛牛学取余

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

1015 浮点除法
先乘以1.0转换为double类型的小数

#include 
using namespace std;
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    double ans = 1.0 * a / b;
    printf("%.3lf",ans);
    return 0;
}

1016 计算带余除法

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

1017 水题再次来袭:明天星期几

结论: 今天是星期x,则明天是星期x % 7 + 1

#include 
using namespace std;
int main()
{
    int a;
    cin >> a;
    cout << a % 7 + 1 << endl;
    return 0;
}

1018

错误代码: 若a = 7,b = 9,得到的结果应该是2,但实际上9 % 7 + 7 = 9,不符合实际

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

1019 helloworld

注意:① hello world两个单词的中间有一个空格,不能忽略空格字符的下一个字符 ② 输出完空格之后不需要换行

#include 
using namespace std;
int main()
{
    cout << char ('h' + 1);
    cout << char ('e' + 1);
    cout << char ('l' + 1);
    cout << char ('l' + 1);
    cout << char ('o' + 1);
    
    cout << char (' ' + 1);
    
    cout << char ('w' + 1);
    cout << char ('o' + 1);
    cout << char ('r' + 1);
    cout << char ('l' + 1);
    cout << char ('d' + 1);
    return 0;
}

1020 a+b

cout << hex << a与printf("%x",a)等价,前者是C++的表示方法,后者是C语言的表示方法

方法一:

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

方法二:

#include 
using namespace std;
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    printf("%x",a + b);
    return 0;
}

1021 整数的个位

#include 
using namespace std;
int main()
{
    int a; 
    cin >> a;
    cout << a % 10;
    return 0;
}

1022 整数的十位

#include 
using namespace std;
int main()
{
    int a; 
    cin >> a;
    cout << a / 10 % 10;
    return 0;
}

1023 反向输出一个四位数

#include 
using namespace std;
int main()
{
    int a;
    cin >> a;
    int qian = a / 1000;//得到千位数字1
    int bai = a / 100 % 10;//得到百位数字2
    int shi = a / 10 % 10;//得到十位数字3
    int ge = a % 10;//得到个位数字4
    cout << ge << shi << bai << qian;
    return 0;
}

题目要求输出四位数,故1000应该输出0001,若需要舍弃前导0,则取出各个位上的数字进行计算

#include 
using namespace std;
int main()
{
    int a;
    cin >> a;
    int qian = a / 1000;
    int bai = a / 100 % 10;
    int shi = a / 10 % 10;
    int ge = a % 10;
    
    int ans = ge * 1000 + shi * 100 + bai * 10 + qian;
    cout << ans << endl;
    return 0;
}

1024 总成绩和平均分计算

#include 
using namespace std;
int main()
{
    double a,b,c;
    scanf ("%lf %lf %lf",&a,&b,&c);
    printf("%.2lf %.2lf",a + b + c,(a + b + c) / 3);
    return 0;
}

1025 计算平均成绩

#include 
using namespace std;
int main()
{
    int a,b,c,d,e;
    scanf ("%d %d %d %d %d",&a,&b,&c,&d,&e);
    //需要先乘以1.0,否则结果是整数/整数 = 整数
    printf("%.1lf",1.0 * (a + b + c + d + e) / 5);
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/698494.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号