为什么开了O2优化全RE,不开反而全AC

P1603 斯诺登的密码

Cysheper @ 2024-07-27 16:46:21

#include<cstdio>
#include<iostream>
#include<cmath>
#include <stdio.h>
#include <math.h>
#include<string>

using namespace std;

string s[27] = { "zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen ","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","a","both","another","first","second","third"};
int a[27] = {0,1,4,9,16,25,36,49,64,81,00,21,44,69,96,25,56,89,24,61,0,1,4,1,1,4,9};
string s1[7];
int b[6] = {0};
int c = 0;
int main() {
    for (int i = 0; i < 7; ++i) {
        cin >> s1[i];       //数据输入
    }
    int num = 0;
    for (int i = 0; i < 6; ++i) {
        for (int j = 0; j < 26; ++j) {
            if (s1[i] == s[j]) {        //判断是否为英文数字
                b[num] = a[j];      
                num++;
            }
        }
    }
    int num2 = 6;
    for (int j = 0; j < 6; ++j) {           //冒泡排序,把大的数往后排
        for (int i = 0; i < num2; ++i) {
            if (b[i] > b[i + 1]) {
                c = b[i];
                b[i] = b[i + 1];
                b[i + 1] = c;
            }
        }
        num2--;
    }
    int num3 = 0,num4=0;
    for (int i = 0; i < 6; ++i) {       //判断是否全零
        if (b[i] == 0) num4++;
    }
    if (num4 == 6) cout << 0;
    for (int i = 0; i < 6; ++i) {       
        if (b[i] != 0) {
            if (b[i] < 10 && num3!=0) {     //非首项小于10的数前补0
                cout << "0" << b[i];
            }
            if (b[i] < 10 && num3 == 0) {   //首项小于10的数不补0
                cout << b[i];
                num3++;
            }
            if (b[i] >= 10) {       //大于10的数正常输出
                cout << b[i];
            }
        }
    }

    return 0;
}

by 沐咕 @ 2024-07-27 16:56:35

O2的原理是调整了一些代码(也可以理解成老师帮你纠错),所以有可能代码被O2调整错了(也可以理解成老师把你的代码纠错了)


by leo120306 @ 2024-07-27 17:00:28

       for (int j = 0; j < 6; ++j) {            //冒泡排序,把大的数往后排
        for (int i = 0; i < num2; ++i) {
            if (b[i] > b[i + 1]) {
                c = b[i];
                b[i] = b[i + 1];
                b[i + 1] = c;
            }
        }
        num2--;
    }

这一段取 i=5 时访问 b[i+1] 会越界。 @Cysheper


by ymdheliyang @ 2024-07-27 17:06:42

@Cysheper 冒泡那段写的有点问题吧 可以加个#include<bits/stdc++.h> 直接改成sort(b,b+num);


by Cysheper @ 2024-07-27 17:10:13

@leo120306 感谢,已经改好了


by Cysheper @ 2024-07-27 17:11:06

@ymdheliyang 确实,已经改好了,谢谢


by panrong @ 2024-08-01 11:01:20

厌氧代码


by SunShengxuan @ 2024-08-01 19:14:10

@Cysheper 问一下, 题目中没提到"zero"啊, 不用判断吧?


by Cysheper @ 2024-08-02 13:09:47

@SunShengxuan 有一个测试点最后的数字全是零,那样的话只需要输出一个零就好了


by zhangwenjun @ 2024-08-08 15:35:28

#include<cstdio>
#include<iostream>
#include<cmath>
#include <stdio.h>
#include <math.h>
#include<string>
using namespace std;
string s[27] = { "zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen ","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","a","both","another","first","second","third"};
int a[27] = {0,1,4,9,16,25,36,49,64,81,00,21,44,69,96,25,56,89,24,61,0,1,4,1,1,4,9};
string s1[7];
int b[6] = {0};
int c = 0;
int main() {
    for (int i = 0; i < 7; ++i) {
        cin >> s1[i];       //数据输入
    }
    int num = 0;
    for (int i = 0; i < 6; ++i) {
        for (int j = 0; j < 26; ++j) {
            if (s1[i] == s[j]) {        //判断是否为英文数字
                b[num] = a[j];      
                num++;
            }
        }
    }
    int num2 = 6;

    int num3 = 0,num4=0;
    for (int i = 0; i < 6; ++i) {       //判断是否全零
        if (b[i] == 0) num4++;
    }
    if (num4 == 6) cout << 0;
    for (int i = 0; i < 6; ++i) {       
        if (b[i] != 0) {
            if (b[i] < 10 && num3!=0) {     //非首项小于10的数前补0
                cout << "0" << b[i];
            }
            if (b[i] < 10 && num3 == 0) {   //首项小于10的数不补0
                cout << b[i];
                num3++;
            }
            if (b[i] >= 10) {       //大于10的数正常输出
                cout << b[i];
            }
        }
    }

    return 0;
}

by taomingen @ 2024-11-08 21:22:40

@沐咕 解释有点牵强不过好像也只能这么解释了所以我说了些什么


|