华为笔试题我发现真的很喜欢考字符串输入转变为其他的
    一共三题,好像都是要处理一下这个输入中的字符串,就硬不给你空格空出来

    行吧,那既然考察字符串的处理熟练度,那就必须学呀

    场景复现;
    “-1,2,-11,30,0,2”
    给你一个这样的串,转化为数组。。

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. const int maxn=1005;
    4. char a[maxn];
    5. int lin[maxn];
    6. int Pow(int j){
    7. int x=1;
    8. for(int i=0;i<j;i++)x*=10;
    9. return x;
    10. }
    11. int handle(char temp[],int a[]){
    12. int cnt=0;
    13. for(int i=strlen(temp)-1;i>=0;){
    14. if(temp[i]==','){i--;continue;}
    15. int sum=0,pos=0;
    16. bool mark=false;
    17. while(temp[i]!=',' && i>=0){
    18. if(temp[i]=='-'){
    19. mark=true;
    20. i--;
    21. break;
    22. }
    23. else{
    24. sum+=((temp[i]-'0')*Pow(pos));
    25. pos++;i--;
    26. }
    27. }
    28. if(mark==true)a[cnt++]=-sum;
    29. else a[cnt++]=sum;
    30. }
    31. return cnt;
    32. }
    33. int main(){
    34. while(gets(a)){
    35. int len=handle(a,lin);
    36. sort(lin,lin+len);
    37. for(int i=0;i<len;i++)cout<<lin[i]<<" ";
    38. cout<<endl;
    39. }
    40. return 0;
    41. }

    ————————————————
    版权声明:本文为CSDN博主「傻子是小傲娇」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/love_phoebe/article/details/83509651

    但是后来发现一个更加简单的read int 方法:

    1. int readint() { //自定义一个读取输入的方法
    2. int sn = 1;
    3. char ch = getchar();
    4. while (!(isdigit(ch) || ch == '-')) ch = getchar();
    5. int ret = 0;
    6. if (ch == '-') sn = -1;
    7. else ret = ch - '0';
    8. while (isdigit(ch = getchar())) {
    9. ret = ret * 10 + ch - '0';
    10. }
    11. return sn * ret;
    12. }