华为笔试题我发现真的很喜欢考字符串输入转变为其他的
一共三题,好像都是要处理一下这个输入中的字符串,就硬不给你空格空出来
行吧,那既然考察字符串的处理熟练度,那就必须学呀
场景复现;
“-1,2,-11,30,0,2”
给你一个这样的串,转化为数组。。
#include<bits/stdc++.h>
using namespace std;
const int maxn=1005;
char a[maxn];
int lin[maxn];
int Pow(int j){
int x=1;
for(int i=0;i<j;i++)x*=10;
return x;
}
int handle(char temp[],int a[]){
int cnt=0;
for(int i=strlen(temp)-1;i>=0;){
if(temp[i]==','){i--;continue;}
int sum=0,pos=0;
bool mark=false;
while(temp[i]!=',' && i>=0){
if(temp[i]=='-'){
mark=true;
i--;
break;
}
else{
sum+=((temp[i]-'0')*Pow(pos));
pos++;i--;
}
}
if(mark==true)a[cnt++]=-sum;
else a[cnt++]=sum;
}
return cnt;
}
int main(){
while(gets(a)){
int len=handle(a,lin);
sort(lin,lin+len);
for(int i=0;i<len;i++)cout<<lin[i]<<" ";
cout<<endl;
}
return 0;
}
————————————————
版权声明:本文为CSDN博主「傻子是小傲娇」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/love_phoebe/article/details/83509651
但是后来发现一个更加简单的read int 方法:
int readint() { //自定义一个读取输入的方法
int sn = 1;
char ch = getchar();
while (!(isdigit(ch) || ch == '-')) ch = getchar();
int ret = 0;
if (ch == '-') sn = -1;
else ret = ch - '0';
while (isdigit(ch = getchar())) {
ret = ret * 10 + ch - '0';
}
return sn * ret;
}