通读getchar_unlocked失败案例
Reading through getchar_unlocked -failed case
这是我计算 no. 乘以 A[i]>B[j] 的代码,这样,我<·
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n,c=0,j=1,i;
cin>>n;
int*a,*b;
a=(int*)malloc(sizeof(int)*n);
b=( int*)malloc(sizeof( int)*n);
for( i=0;i<n;i++)
{
cin>>a[i];
}
for( i=0;i<n;i++)
{
cin>>b[i];
}
while(j<n)
{
for( i=0;i<j;i++)
{
if(a[i]>b[j])
c++;
}
j++;
}
cout<<c;
return 0;
}
这很好用,我尝试使用getchar_unlocked
来减少时间,但我在网站上得到了错误的答案。最初的测试用例向我展示了正确的输出,我确实使用输入流对所有情况进行了相同的算法工作cin
但我未能使用 getchar_unlocked
我的输入函数中是否有任何错误,或者我在哪里做错了?
我的getchar_unlock
看起来像这样
int readInt()
{
char c=getchar_unlocked();
while(c<'1'||c>'9')
c=getchar_unlocked();
int ret=0;
while(c>='1'&&c<='9')
{
ret=ret*10+c-48;
c=getchar_unlocked();
}
return ret;
}
PS:我不知道失败的测试用例:(
如果存在非前导零,您的readInt()
不起作用。例如,如果输入为 10
,则将返回 1
。
这是我久经考验的实现(仅适用于非负整数)。
int get() {
int res = 0;
int c;
do {
c = getchar_unlocked();
} while (c <= 32);
do {
res = 10*res + c - '0';
c = getchar_unlocked();
} while (c > 32);
return res;
}
此外,我建议使用std::vector
而不是直接调用malloc
或new
。
相关文章:
- 如果没有malloc,链表实现将失败
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 视图中的参数推导失败:take_while
- 链接到自行创建的dll失败
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- gcc和c++17的过载解析失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 从父数组测试用例构造二叉树失败
- LibGit2 SSH身份验证失败
- 如何让LLDB在成功时退出,在失败时等待
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- shared_ptr和weak_ptr失败的琐碎案例
- 通读getchar_unlocked失败案例
- 单例破坏的atexit:失败案例