【入门1】顺序结构

【入门1】顺序结构-DAY1
2021/1/10

p5704字母转换,c++ 可以直接小写字符-32得到char型的大写字符;python需要强制转换,还没成功,需要再试。还没用Java写过。

p5705数字反转

  • 法1

刚开始打算硬算,C++得出了答案,但OJ不过。代码如下:

#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
    double z,y,m,n;
    cin >> z;
    int a, b, c, d,tmp;
    tmp = (int)z;
    c = tmp % 10;
    tmp = tmp / 10;
    b = tmp % 10;
    tmp = tmp / 10;
    a = tmp % 10;
    d = (z - a * 100 - b * 10 - c)*10;
    m = a;
    n = 10;
    y =  (float)d * 100 + c * 10 + b +m / n;//
    cout << setprecision(1) << fixed << y;
    return 0;
}

观摩了大佬的代码后,觉得此代码弱爆了。。。
以下为各位大佬的方法含代码:

  • 法2.1:字符串
    思路:首先读入一个字符串(为了方便使用 stringstring )
    接着,获取其长度,即使用 str.size()str.size() 的方法,当中 strstr 是字符串的变量名,这个函数会返回一个值,即字符串的长度
    注意,字符串的长度比起他的最大下标( indexindex )是要大 11 的,所以倒序输出是要注意。同理,第一个字符的下标是 00 。
#include<bits/stdc++.h>
using namespace std;
string a;
int main()
{
	cin>>a;
	for(int i=a.size()-1;i>=0;i--)cout<<a[i];
	return 0;
}
  • 法2.2:字符串升级版,直接用reverse函数做
    首先拿到题目,第一感觉就是字符串,当然一个个读入在反过来输出也可以。
    但字符串的反转操作我们可以用STL也可以直接倒序输出。
    介绍一下STL里面的反转字符串函数 reverse()
    其格式是: reverse( 数组名 .begin(), 数组名 .end());
#include<bits/stdc++.h>   //文件头
using namespace std;
string a;                 //定义字符串
int main() 
{
    cin>>a;               //输入
    reverse(a.begin(),a.end());  //反转
    cout<<a;              //输出
    return 0;             //养成好习惯
}

2021/1/12
Day2
P2433有毒的题目
T11:
代码很简单,但要记得用浮点数表示

#include<iostream>
// 填上你觉得需要的其他头文件
using namespace std;
int main() {
    cout << 100.0 / (8 - 5) << endl;
    return 0;
}

如果写出100/3答案为整数,加.0答案才为小数

T12:char+int =>int,需要char强制转换为字母
T13:开三次方用pow(x,1.0/3),注意要带.0
P2433很基础,小学数学,有些步骤也可以通过数学运算来进行,简化代码工作量

所有题代码

#include<iostream>
#include<iomanip>
#include<cmath>

// 填上你觉得需要的其他头文件
using namespace std;
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) {
            int sum = 14, student = 4;
          int avg=0,rest=sum,share=0;
          while (rest > student)
        {
           share = share + student;
           rest = rest - student;
            avg++;
        }
         cout << avg << endl;
        cout << share << endl;
       cout << rest << endl;
    } else if (T == 4) {
        // 请自行完成问题 4 的代码
       double drink = 500;
       int student = 3;
       cout << setprecision(3) << fixed << drink / 3 << endl;

    } else if (T == 5) {
        // 请自行完成问题 5 的代码
        int lengthA = 260, lengthB = 220, vA = 12, vB = 20;
        int V = vA + vB, Length = lengthA + lengthB;
        int second = Length / V;
       cout << second << endl;

    } else if (T == 6) {
        // 请自行完成问题 6 的代码
        int a = 6, b = 9;
        double c = sqrt(a * a + b * b);
        cout << c << endl;
    } else if (T == 7) {
        // 请自行完成问题 7 的代码
         int count = 100;
        count += 10;
         cout << count << endl;
       count -= 20;
       cout << count << endl;
      count = 0;
       cout << count << endl;

    } else if (T == 8) {
        // 请自行完成问题 8 的代码
       cout << 2 * 3.141593 * 5 << endl;
       cout << 3.141593 * 5 * 5 << endl;
       cout << 4 * 3.141593 * 5 * 5 * 5 / 3 << endl;
    } else if (T == 9) {
        // 请自行完成问题 9 的代码
       int sum=1, day = 4;
      for (int i = day - 1; i > 0; i--)
      {
          sum = (sum + 1) * 2;
       }
       cout << sum << endl;

    } else if (T == 10) {
        // 请自行完成问题 10 的代码
       cout<<9<<endl;
    } else if (T == 11) {
        // 请自行完成问题 11 的代码
       cout << 100.0 / (8 - 5) << endl;

    } else if (T == 12) {
        // 请自行完成问题 12 的代码
      cout << ('M' - 'A') +1<<endl;
      cout << char('A' + 17 )<< endl;

    } else if (T == 13) {
        // 请自行完成问题 13 的代码
      const double PI = 3.141593;
      double V = 4 * PI * (4 * 4 * 4 + 10 * 10 * 10) / 3;
      cout <<int( pow(V,1.0/3) )<< endl;

    } else if (T == 14) {
        // 请自行完成问题 14 的代码
       int x;//降价x
       int people , jiage ;

       for (x = 50; x < 100; x++)
       {
           people = 10 + x, jiage = 110 - x;
           if (people * jiage == 3500)
           {
              cout << 110 - x << endl;
               break;
           }
       }

    }
    return 0;
}

 

P5709apples prologue此题有毒,看上去很简单,但调了好几次才对
注意细节:1.取整,注意区分完整和吃了但没吃完情况
2.判断t是否为0
3注意答案<0的情况
max函数的头文件是#include
代码如下:

#include<iostream>
#include<algorithm>
// 填上你觉得需要的其他头文件
using namespace std;
int m,t,s;
int main(){
	cin>>m>>t>>s;
	if(t==0) cout<<0;
	else{
	    if(s%t==0) cout<<max(0,m-s/t);//如果能整除代表s时间过去后没有不完整的苹果
	    else cout<<max(0,m-s/t-1);//反之则有,要减去
	}
}

P281对角线**【图形问题转换为数字问题】**
此题初看以为要用几何方法,但实际上排列组合即可解决
已知:保证任何三条对角线都不会交于一点
推导如下:
每两个顶点连一条对角线
每两条对角线有一个顶点
所以每一个交点对应四个顶点
则问题转化为:
若从n个顶点之中选出4个,有多少种选法?
因为无序,选择组合数,即计算C4/n

注意
数据范围较大
可用 高精算法 或 将变量定义为“unsigned long long” 类型来解决。

计算时的技巧
可将 n (n-1) (n-2) * (n-3) / 24 转化为 n (n-1) / 2 (n-2) / 3 * (n-3) / 4

#include<iostream>
using namespace std;
int main() 
{
    unsigned long long n;
    cin >> n;
    if (n < 3)
        cin >> n;
    cout << n * (n - 1)/2 * (n - 2)/3 * (n - 3) / 4 << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

颜。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值