我靠以后能用string还用char【】我就是狗
string好太多了哭泣
还是string不会用啊;
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
int n;
string dealS(string s,int&e)
{
e=0;
int k = 0;
while(s.length() > 0 && s[0] == '0')
s.erase(s.begin());
if(s[0] == '.')
{
s.erase(s.begin());
while(s.length() > 0 && s[0] == '0')
{
s.erase(s.begin());
e--;
}
}
else
{
while(k<s.length() && s[k]!='.')
{
k++;
e++;
}
if(s[k] == '.')
s.erase(s.begin()+k);
}
if(s.length()==0)
{
s = "0";
e=0;
}
int num = 0;
k=0;
string ans;
while(k<s.length() && num<n)
{
ans+=s[k++];
num++;
}
while(num<n)
{
ans+="0";
num++;
}
return ans;
}
int main()
{
scanf("%d",&n);
int e1,e2;
string s1,s2,s3,s4;
cin>>s1>>s2;
s3 = dealS(s1,e1);
s4 = dealS(s2,e2);
if(s3 == s4)
cout<<"YES 0."<<s3<<"*10^"<<e1;
else
cout<<"NO 0."<<s3<<"*10^"<<e1<<" 0."<<s4<<"*10^"<<e2;
return 0;
}
=========================================================
好好整理一下,19分,感觉string用起来还不如char数组,可能还是不熟练吧;
ans数组太小改到110,21分
漏洞很多,改着很麻烦;
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
int getWei(char k1[])
{
int s1 = 0;
int f = 0;
if(k1[0] == '0')
f=1;
else
f=2;
if(f==1)
{
for(int i = 2;i<strlen(k1);i++)
{
if(k1[i] != '0')
break;
else
s1--;
}
}
else if(f==2)
{
for(int i = 0;i<strlen(k1);i++)
{
if(k1[i] != '.')
s1++;
else
break;
}
}
return s1;
}
char ans[10];
int n;
void getAns(char k[])
{
int f = 0;
int index = 0;
if(k[0] == '0')
f=1;
else
f=2;
if(f==1)
{
int flag = 0;
for(int i = 2;i<strlen(k);i++)
{
if(k[i] != '0')
{
flag = 1;
}
if(flag == 1)
{
ans[index++] = k[i];
if(index > n - 1)
break;
}
}
while(index <= n - 1)
{
ans[index++] = '0';
}
ans[index] = '\0';
}
else
{
for(int i = 0;i<strlen(k);i++)
{
if(k[i] != '.')
{
ans[index++] = k[i];
if(index > n - 1)
break;
}
else
continue;
}
ans[index] = '\0';
}
}
int main()
{
scanf("%d",&n);
int s1,s2;
char k1[110],k2[110];
char a1[10],a2[10];
scanf("%s %s",&k1,&k2);
s1 = getWei(k1);
s2 = getWei(k2);
getAns(k1);
strcpy(a1,ans);
getAns(k2);
strcpy(a2,ans);
if(strcmp(a1,a2) == 0)
printf("YES 0.%s*10^%d",a1,s1);
else
printf("NO 0.%s*10^%d 0.%s*10^%d",a1,s1,a2,s2);
return 0;
}
============================================================
感觉陷入了死局。。。对string理解不够,越写越烦,可以做出来但是时间花的太久,用了2个小时得了7分。。。
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int n,cn=0,flag = 0,index=0,k1=0,k2=0;
scanf("%d",&n);
string n1,n2;
char ans[110],ans2[110];
cin>>n1;
cin>>n2;
//处理1
if(n1[0] == '0')
flag = 0;
else
flag = 1;
if(flag == 1)
{
for(int i = 0;i<n1.size();i++)
{
if(n1[i]!='.')
k1++;
else
break;
}
}
else
{
for(int i = 2;i<n1.size();i++)
{
if(n1[i] == '0')
k1--;
}
}
//处理ans
if(flag == 1)
{
for(int i = 0;i<n1.size();i++)
{
if(n1[i] == '.')
continue;
cn++;
if(cn>3)
break;
ans[index++] = n1[i];
}
ans[index] = '\0';
}
else
{
int t = 0;
for(int i = 2;i<n1.size();i++)
{
if(n1[i] != '0')
{
t=1;
}
if(t==1)
{
ans[index++] = n1[i];
if(index>2)
break;
}
}
}
//处理2
if(n2[0] == '0')
flag = 0;
else
flag = 1;
if(flag == 1)
{
for(int i = 0;i<n2.size();i++)
{
if(n2[i]!='.')
k2++;
else
break;
}
}
else
{
for(int i = 2;i<n2.size();i++)
{
if(n2[i] == '0')
k2--;
}
}
//ans2
index = 0;
cn=0;
for(int i = 0;i<n2.size();i++)
{
if(n2[i] == '.' && n2[0]!='0')
{
flag = 1;
continue;
}
cn++;
if(cn>3)
break;
ans2[index++] = n2[i];
}
ans2[index] = '\0';
if(strcmp(ans,ans2) == 0)
{
printf("YSE 0.%s*10^%d",ans2,k2);
}
else
printf("NO 0.%s*10^%d 0.%s*10^%d",ans,k1,ans2,k2);
return 0;
}