PAT1060——Are They Equal,利用字符串解题

因为不常用string,于是尝试用char数组解题,由于无法erase,于是需要定位操作,我感觉写出来十分麻烦,看的也不舒服,但是既然写了还是po出来吧,以后要练习多用string啊。

int n;
char num1[110] = {0}, num2[110] = {0}, num1_temp[110] = {0}, num2_temp[110] = {0};

int smallZero(char a[], int pos, int t){
    char num_temp[110] = {0};
    int b = 0;
    while(a[pos] == '0') {b++;pos++;}
    if(a[pos] == '\0') b = 0;
    num_temp[0] = '0'; num_temp[1] = '.';
    bool flag = false;
    for(int i = 2; i<=n+1; i++){
        if(flag || a[pos] == '\0') {
            num_temp[i] = '0';
            flag = true;
            continue;
        }
        num_temp[i] = a[pos++];
    }
    
    num_temp[n+2] = '\0';
    if(t == 1) strcpy(num1_temp, num_temp);
    else strcpy(num2_temp, num_temp);
    return -b;
}

int largeZero(char a[], int pos, int t){
    char num_temp[110] = {0};
    int pos_temp = pos;
    int b = 0;
    int len = (int)strlen(a);
    while(pos < len){
        if(a[pos++] != '.') {b++;}
        else break;
    }
    num_temp[0] = '0'; num_temp[1] = '.';
    
    bool flag = false;
    for(int i = 2; i<=n+1;  i++){
        if(flag || a[pos_temp] == '\0') {
            num_temp[i] = '0';
            flag = true;
            continue;
        }
        if(a[pos_temp] == '.') pos_temp++;
        num_temp[i] = a[pos_temp++];
    }
    num_temp[n+2] = '\0';
    if(t == 1) strcpy(num1_temp, num_temp);
    else strcpy(num2_temp, num_temp);
    return b;
}

int main(){
    scanf("%d %s %s", &n, num1, num2);
    
    int i1 = 0;
    while(num1[i1] == '0') i1++;
    int b1, b2;
    if(num1[i1] == '.') b1 = smallZero(num1, i1+1, 1);
    else b1 = largeZero(num1, i1, 1);
    
    int i2 = 0;
    while(num2[i2] == '0') i2++;
    if(num2[i2] == '.') b2 = smallZero(num2, i2+1, 2);
    else b2 = largeZero(num2, i2, 2);

    if(strcmp(num1_temp, num2_temp) == 0 && b1 == b2){
        printf("YES %s*10^%d\n", num1_temp, b1);
    }
    else{
        printf("NO %s*10^%d %s*10^%d", num1_temp, b1, num2_temp, b2);
    }
        
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值