#梳理 本站大部分题目题解
2024-10-29 18:00:39
发布于:浙江
该帖子用于学习讨论,请尽量不要讨论别的事情,其次本帖字会梳理大部分题解,谢谢
温馨提示,所有题目作者亲测过已全部AC
该帖子预计7天更新5至7题,请大家广泛关注此贴
This post is for learning discussion, please try not to discuss other things. Secondly, this post will sort out most of the solutions. Thank you
Kind reminder, all questions have been personally tested by the authors and have all been AC
This post is expected to update 5 to 7 questions in 7 days. Please follow this post widely
A1.A+B problem
此题解法有很多种,我在这里呈现最简单的办法:(也算不上最简单,不过已经很简单了)
#include <iostream>
using namespace std;
int main(){
int a,b;
cin >> a >> b;
int c = a + b;
cout << c;
return 0;
}
此题目还有很多解法,更多解法见此:更多解法有需要请私信我
A2.数方格
此题依旧是有很多解法,我这里参考唱跳坤的题解:
#include <iostream>//真·华丽开始
using namespace std;
int main(){
int n,m;//定义长和宽
cin>>n>>m;//输入长和宽
int sum=0;//定义计数器
if(n<=m){//判断某些用(脑)户(瘫)是否输入输反了
for(int i=n,j=m/*将循环变量分别设为长和宽*/;i>0;i--,j--/*范围缩小*/){
sum+=i*j;//计数器增加
}
}else{
for(int i=n,j=m;j>0;i--,j--){
sum+=i*j;
}
}
cout<<sum;//输出计数器变量
return 0;
}
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int M = 10005;
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3f;
int read(){
int x=0,f=1;char c;
while((c=getchar())<'0' || c>'9') {
if(c=='-') f=-1;
}while(c>='0' && c<='9') {
x=(x<<3)+(x<<1)+(c^48);
c=getchar();
}return x*f;
}
int n,z,a[M],b[M],s[M],dp[2][M*55];
void upd(int &x,int y) {
x=min(x,y);
}
signed main(){
n=read();
for(int i=1;i<=n;i++)
a[i]=read();
for(int i=1;i<n;i++)
b[i]=a[i+1]-a[i];
sort(b+1,b+n);
for(int i=1;i<n;i++)
s[i]=s[i-1]+b[i];
for(z=1;z<n && b[z]==0;z++);
memset(dp,0x3f,sizeof dp);
dp[(z-1)&1][0]=0;
for(int i=z;i<n;i++){
int w=i&1;
for(int j=0;j<=a[n]*n;j++)
dp[w][j]=inf;
for(int j=0;j<=a[n]*n;j++)
if(dp[w^1][j]<inf){
//head
upd(dp[w][j+b[i]*i],dp[w^1][j]+2*j*b[i]+b[i]*b[i]*i);
//tail
upd(dp[w][j+s[i]],dp[w^1][j]+s[i]*s[i]);
}
}int ans=inf;
for(int j=0;j<=a[n]*n;j++)
if(dp[(n-1)&1][j]<inf)
upd(ans,n*dp[(n-1)&1][j]-j*j);
printf("%lld\n",ans);
}
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
int n;
scanf("%d", &n);
int a[101] = { 0 };
for (int i = 0; i <= n; i++)
{
scanf("%d", &a[i]);
}
for (int i = 0; i <= n; i++)
{
if (a[0] && i == 0 && abs(a[i]) != 1)
{
printf("%dx^%d", a[i], n);
}
if (a[0] && i == 0 && abs(a[i]) == 1)
{
if (a[i] == 1)
{
printf("x^%d", n);
}
else
{
printf("-x^%d", n);
}
}
if (a[i] > 0 && i != n && i != 0 && i != n - 1 && abs(a[i]) != 1)
{
printf("+%dx^%d", a[i], n - i);
}
if (a[i] < 0 && i != n&&i!=n-1 && i != 0 && abs(a[i]) != 1)
{
printf("%dx^%d", a[i], n - i);
}
if (a[i] && i != n && i != 0 &&i!=n-1&& abs(a[i]) == 1)
{
if (a[i] == 1)
{
printf("+x^%d", n - i);
}
else
{
printf("-x^%d", n - i);
}
}
if (i == n - 1 && a[i] > 0 && abs(a[i]) != 1)
{
printf("+%dx");
}
if (i == n - 1 && a[i] < 0 && abs(a[i]) != 1)
{
printf("%dx");
}
if (i == n - 1 && abs(a[i]) == 1)
{
if (a[i] == 1)
{
printf("+x");
}
else
{
printf("-x");
}
}
if (i == n && a[i] > 0)
{
printf("+%d", a[i]);
}
if (i == n && a[i] < 0)
{
printf("%d", a[i]);
}
}
return 0;
}
// 多年后的极致压行(
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
scanf("%d", &n);
int a[101] = { 0 };
for (int i = 0; i <= n; i++) {
scanf("%d", &a[i]); }
for (int i = 0; i <= n; i++) {
if (a[0] && i == 0 && abs(a[i]) != 1) {
printf("%dx^%d", a[i], n); }
if (a[0] && i == 0 && abs(a[i]) == 1) {
if (a[i] == 1) {
printf("x^%d", n); }
else {
printf("-x^%d", n); } }
if (a[i] > 0 && i != n && i != 0 && i != n - 1 && abs(a[i]) != 1) {
printf("+%dx^%d", a[i], n - i); }
if (a[i] < 0 && i != n && i != n - 1 && i != 0 && abs(a[i]) != 1) {
printf("%dx^%d", a[i], n - i); }
if (a[i] && i != n && i != 0 && i != n - 1 && abs(a[i]) == 1) {
if (a[i] == 1) {
printf("+x^%d", n - i); }
else {
printf("-x^%d", n - i); } }
if (i == n - 1 && a[i] > 0 && abs(a[i]) != 1) {
printf("+%dx"); }
if (i == n - 1 && a[i] < 0 && abs(a[i]) != 1) {
printf("%dx"); }
if (i == n - 1 && abs(a[i]) == 1) {
if (a[i] == 1) {
printf("+x"); }
else {
printf("-x"); } }
if (i == n && a[i] > 0) {
printf("+%d", a[i]); }
if (i == n && a[i] < 0) {
printf("%d", a[i]); } }
return 0; }
我的答案
顺序循环
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,a;
cin>>n;
for(int i=0, j=n; i<=n; i++,j--){
cin>>a;
if(a){
if( a<0 ) cout<<"-";
if( a>0 && i!=0 ) cout<<"+";
if(i==n) cout<<abs(a);
if( abs(a)!=1 && i<n-1 ) cout<<abs(a)<<"x^"<<j;
if( abs(a)==1 && i<n-1 ) cout<<"x^"<<j;
if( abs(a)==1 && i==n-1) cout<<"x";
if( abs(a)>1 && i==n-1) cout<<abs(a)<<"x";
}
}
}
逆序循环
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, a;
cin>>n;
for( int i=n;i>=0; i--)
{
cin >>a;
if(a){
if( a<0 ) cout<<"-";
if( a>0 && i!=n ) cout<<"+";//输出符号
if(i==0) cout<<abs(a);
if( abs(a)!=1 && i>1 ) cout<<abs(a)<<"x^"<<i;
if( abs(a)==1 && i>1 ) cout<<"x^"<<i;
if( abs(a)==1 && i==1) cout<<"x";
if( abs(a)>1 && i==1) cout<<abs(a)<<"x";
}
}
}
全部评论 9
可以稍作修改:
This post is expected to update 5 to 7 questions with-in evert 7 days. Please follow this post closely.
9小时前 来自 美国
114小时前 来自 浙江
014小时前 来自 浙江
0顶
昨天 来自 浙江
033
昨天 来自 浙江
0写题,但没解
3天前 来自 广东
0方差也是抄上了
3天前 来自 广东
06得嘞
2天前 来自 浙江
06
昨天 来自 浙江
0
顶
3天前 来自 浙江
0赞
3天前 来自 浙江
0顶
3天前 来自 浙江
0顶
3天前 来自 广东
0嗯
3天前 来自 浙江
0
有帮助,赞一个