C++2024寒假J312实战班2.5

题目列表:

#1多项式输出

            #2龙虎斗

                             #3表达式求值

                                                #4解密

#1多项式输出

这是第一个题目很简单,我也作对了。

我们下来看一下题目:
 

 我们先来看一下样例:

5

100 -1 1 -3 0 10

首先100是第一项,所以不输出加号,输出100,x是未知数,^,5是次数题目中说了,这是个自变量,每次递减1. -1 为什么输出-x^4不输出1?题目中说了1,输出正号,-1输出负号,系为数0的项不输出。

所以这是模拟对吧。

这是不分步处理的:

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    int a[n+1];
    for(int &i : a) cin >> i;
    if(n == 0)return cout<<a[0],0;
    int flag=0;
    for(int i = 0;i <=n;i++){
        if(a[i]!=0)break;
        else flag++;
    }
    if(flag==n+1)return cout<<0,0;
    for(int i = n - 1,j = 0; j <= n; i--,j++){
        if(j == 0 && a[j] == -1)cout<<'-';
        else if(j == 0 && a[j] == 1) ; 
        else if(a[j] != 0 && j != 0 && abs(a[j]) != 1 && j != n)cout<<(a[j] > 0 ? '+' : '-')<<abs(a[j]); //非第一项
        else if(a[j] != 0 && j == 0)cout<<a[j]; //第一项
        else if (abs(a[j]) == 1 && j != n) cout<<(a[j] > 0 ? '+' : '-');
        if(a[j] == 0) continue; //系数为0
        if(j == n)cout<<(a[j] > 0 ? '+' : '-')<<abs(a[j]);
        //先处理符号和系数
        if(j != n) cout<<'x'; // 如果不是最后一项
        else continue; //如果是最后一项就不用输出次数
        //未知数
        if(j == n-1) continue; //如果是倒数第2项也不用输出
        else cout<<'^'<<i+1;
    }
    return 0;
}

看着就很恶心对吧。

我们再来看一下分布处理的:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin >> n;
    for(int a,i = n;i >= 0;i--){
        cin>>a;
        if(a == 0) continue;            //多项式中只包含系数不为0的项
        if(a < 0) cout << '-', a = -a;  //如果是负数
        else if(i < n) cout<<+;         //不是第一项
        if(a > 1 || i == 0) cout<<a;    //i不是1,也不是最后一项
        if(i > 0) cout<<'x';            //不是最后一项输出未知数
        if(i > 1) cout<<'^'<<i;         //不是倒数第二个就不输出^和次数
    }
    return 0;
}

简洁吧。

#2龙虎斗

题目读起来 很费劲对吧。

这里可以去洛谷看一下。

我这里只讲题解了:

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int n, m, p1, p2;
  LL s1, s2, sum = 0;
  cin >> n;             // 总数
  vector<LL> C(n + 1);  // Ci: i号有C[i]名士兵
  for (int i = 1; i <= n; i++) cin >> C[i];
  cin >> m >> p1 >> s1 >> s2;
  C[p1] += s1, p2 = 1;  // s1个神兵天降突然出现在了p1号
  for (int i = 1; i <= n; i++) sum += C[i] * (i - m);  // 两边统一计算
  for (int i = 1; i <= n; i++)                         // p2选哪个?
    if (abs(sum + s2 * (i - m)) < abs(sum + s2 * (p2 - m))) p2 = i;
  return printf("%d\n", p2), 0;
}

#3表达式求值

第三道题了:

一个很恶心的题:

看着简单吧,做一下你就疯狂了。

我试了一千种方法,一种都没成功,我就看了一下标程。

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e4;
int mul(stack<int> &s) {  // 栈上的所有数字相乘
  int a = 1;
  while (!s.empty()) (a *= s.top()) %= MOD, s.pop();
  return a;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  string e;
  cin >> e;
  stack<int> s;
  int n = e.size(), x = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    char c = e[i];
    if (!isdigit(c)) {  // 数字结束了入栈
      s.push(x), x = 0;
      if (c == '+') ans += mul(s);  // 遇到加法,把栈里面的数字都乘起来
    } else
      (x = x * 10 + (c - '0')) %= MOD;  // 数字拼接
  }
  assert(x), s.push(x);
  (ans += mul(s)) %= MOD;
  printf("%d\n", ans);
  return 0;
}

用栈,知道吧。栈就是FILO表,说人话就是先进去的后出来。这里就是想象成被+号分开的乘法算式。 

 #4解密

非初中人民不要看,不然CPU就爆了。

就是一元二次方程。

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
LL solve(LL n, LL e, LL d) {
  LL m = n - e * d + 2, r = m * m - 4 * n;
  if (r < 0) return -1;
  LL s = sqrt(r);
  if (s * s != r or (m - s) % 2) return -1;
  return (m - s) / 2;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int k;
  cin >> k;
  for (LL n, e, d; k--;) {
    cin >> n >> e >> d;
    LL p = solve(n, e, d);
    if (p != -1)
      printf("%lld %lld\n", p, n / p);
    else
      puts("NO");
  }
  return 0;
}

纯数学题对吧。 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值