本地正确运行的代码会因为选择不同的输出方式而在测评机上WA
查看原帖
本地正确运行的代码会因为选择不同的输出方式而在测评机上WA
859403
ll_ll_ll_ll楼主2023/4/12 16:03

非常奇怪的是,以下的代码(c++)是可以AC的,但是当打开ios::sync_with_stdio(false)以解除缓冲区后,亦或者是改用printf或者scanf对答案做读取和输出后,那么就会WA:

#include <iostream>
#include <algorithm>
#include <vector>
#define int long long 
using namespace std;
typedef long long ll;
#define endl "\n"
#define debug(x) cerr << #x << " = " << x << '\n';
#define debug_(x) cerr << #x << " = " << x << ' ';
const ll mod = 1000000007;
const ll N = 2e5; 

vector<int> read()
{
    char c;
    vector<int> num;
    
    while ( ( c = getchar() ) < '0' || c > '9' )        // 吃掉垃圾字符 
    {
        ;
    }
    int tmp = c - '0';
    num.push_back(tmp); 
     
    while ( ( c = getchar() ) >= '0' && c <= '9' )      // 不要乱调顺序!!!
    {
        tmp = c - '0';
        num.push_back(tmp); 
    }
    
    reverse( num.begin(), num.end() );      // 反向存储才能计算(该函数在algorithm内) 
    return num;
}

vector<int> divide( vector<int> a, int b, int &r )   // 余数:remainder
{
    vector<int> c;
    r = 0;              // 你传什么都没用
    for ( int i = a.size() - 1; i >= 0; i-- )   // 不同于其他计算,除法是从头到尾算的(所以算出来的商也是正序)
    {
        r = r * 10 + a[i];                      // 上次没除尽的,进位留着这次除
//    	debug_(i);
//    	debug(r);
        c.push_back( r / b );                   // 在这一位除,得到这一位的商(b ≠ 0,警惕/0错误!)
        r %= b;                                 // 没除尽的留着下次除,最后一次操作都没除尽就是余数r
    }
    
    reverse( c.begin(), c.end() );              // 为了让商能参与其他高精度计算,将商从正序转为逆序存储
    while ( c.size() > 1 && c.back() == 0 )     // 剔除前导0
    {
        c.pop_back();
    }
    return c;
}

signed main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
//	cout.tie(0);
	
	vector<int> a = read();
	int b, c = 0;
	cin >> b;
//	scanf("%d", &b);

//	debug(b);
//	for ( int i = 0; i < a.size(); i++ )
//	{
//		cout << a[i];
//	}
//	cout << endl;

	vector<int> d = divide( a, b, c );
	reverse( d.begin(), d.end() );
	for ( int i = 0; i < d.size(); i++ )
	{ 
		cout << d[i];
//		printf("%d", d[i]);
	}
	
	return 0;
}

请问为什么输出方式的选择会导致测评机给出WA呢,上述的另两种输入输出方式在dev上运行,使用报错的样例都是能给出正确答案的(都用样例2和样例7测试)

2023/4/12 16:03
加载中...