当前位置: 首页 > article >正文

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;
}

纯数学题对吧。 


http://www.kler.cn/news/233799.html

相关文章:

  • 正点原子--STM32通用定时器学习笔记(2)
  • 速盾:海外服务器用了cdn还是卡怎么办
  • 【CSS】什么是BFC?BFC有什么作用?
  • Android 11 webview webrtc无法使用问题
  • cool 框架 node 后端封装三方Api post请求函数
  • NLP_Bag-Of-Words(词袋模型)
  • 如何进行游戏服务器的负载均衡和扩展性设计?
  • VUE学习——事件参数
  • 每天一个数据分析题(一百五十六)
  • moduleID的使用
  • 【学习笔记】【内核】offsetof 的用法
  • 算法学习——LeetCode力扣二叉树篇1
  • c# 时间帮助类
  • 2.6:冒泡、简选、直插、快排,递归,宏
  • VMware虚拟机安装openEuler系统(一)(2024)
  • Idea里自定义封装数据警告解决 Spring Boot Configuration Annotation Processor not configured
  • Qt QML学习(一):Qt Quick 与 QML 简介
  • 【资料分享】基于单片机大气压监测报警系统电路方案设计、基于飞思卡尔的无人坚守点滴监控自动控制系统设计(程序,原理图,pcb,文档)
  • MySQL-SQL优化
  • JAVA设计模式之观察者模式详解
  • GPT原始论文:Improving Language Understanding by Generative Pre-Training论文翻译
  • Unity UGUI实现点击事件穿透
  • java多线程的四种创建方式、程序、线程、进程、并行、串行、Thread、Runnable、Callable、线程池技术
  • 二分查找的应用
  • 二维火API连接,实现无代码开发广告推广与用户运营集成
  • thinkphp数据批量提交(群发消息)
  • 烟火可禁却难禁,灵境难及终将及
  • 17、ELK
  • 785. 快速排序
  • 【数据分享】1929-2023年全球站点的逐日平均风速数据(Shp\Excel\免费获取)