小白求问为什么报错了

P1307 [NOIP2011 普及组] 数字反转

Zy521_125 @ 2023-02-25 17:04:15

得了100分,有一个红了,请大神解答一下哪里错了。我确实写的罗里吧嗦,有点麻烦。谢谢了```

include<bits/stdc++.h>//万能头文件

include<algorithm>

using namespace std; int main(){ int num; //输入num cin>>num; int num1=num; if(num<0) num=-num; //负数反转 vector<int> ans; //ans数组 while(num!=0){ //输入的数字存入数组中 ans.push_back(num%10); num=num/10; } int len=ans.size(); int answer[len]; for(int k=0;k<len;k++){ answer[k]=ans[k]; } int j=0; while(answer[j]==0){ for(int k=j;k<len;k++) answer[k]=answer[k+1]; len--; } if(num1>0){ for(int i=0;i<len;i++) cout<<answer[i]; } else { cout << '-'; for (int i = 0; i < len; i++) cout << answer[i]; } return 0; }


by Smg18 @ 2023-02-25 17:05:29

az


by Smg18 @ 2023-02-25 17:05:46

@Zy521_125 能重发吗


by AKPC @ 2023-02-25 17:12:39

@Zy521_125 看了一下代码,真的有必要用字符串?

直接 int 简单多了

#include <bits/stdc++.h>
using namespace std;
int a=0,b=0;
int main(){
    cin>>a;  
    while(a!=0) {b=b*10+a%10;a/=10;}
    cout<<b;
    return 0; 
}

by _Adolf_Hitler_ @ 2023-02-25 17:17:06

没多大必要,毕竟没超过long long的范围```cpp

include <bits/stdc++.h>

using namespace std; int main() { long long x,y=0; cin>>x; if (x<0) { cout<<"-"; x=x(-1); } while (x>0) { y=y10+x%10; x=x/10; } cout<<y; return 0; }


by Vigilant_Yaksha @ 2023-02-25 17:18:33

@Zy521_125 建议重写,这种做法不能判断输入为0的情况


by _Adolf_Hitler_ @ 2023-02-25 17:20:03

你这个也忒麻烦了吧@Zy521_125


by InversionShadow @ 2023-02-25 17:35:10

e,望丰展,用 md


by Zy521_125 @ 2023-02-25 18:32:10

@JODAN_POOLE 我就是想到哪写到哪了,没怎么思考,确实麻烦。


|