[英]Problem using Printf and Scanf in a HackerRank practice problem
I have been trying to solve this problem in Hackerrank for quite a while now.The issue that i am facing is that I being a beginner on reading the question ended up using cin
and cout
instead of scanf
and printf
while taking user input just so as to be able to check as to whether my reasoning was correct or not and although I have solved the problem correctly using the former but am facing difficulty using the latter.我已经尝试在 Hackerrank 中解决这个问题已经有一段时间了。我面临的问题是,我是一个初学者,在阅读问题时最终使用
cin
和cout
而不是scanf
和printf
,同时接受用户输入能够检查我的推理是否正确,尽管我已经使用前者正确解决了问题,但在使用后者时遇到了困难。
Here is the problem that I have been trying to solve:这是我一直试图解决的问题:
https://www.hackerrank.com/challenges/acm-icpc-team/problem https://www.hackerrank.com/challenges/acm-icpc-team/problem
Here is code using cin
and cout
:这是使用
cin
和cout
的代码:
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
int a[n][m]={};
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin>>a[i][j];
}
}
int b[n*(n-1)/2][m]={};
int x=0;
for (int i = 0; i < n-1; i++)
{
for (int j = i+1; j < n; j++)
{
for (int k = 0; k < m; k++)
{
b[x][k] = (a[i][k]|a[j][k]);
}
x=x+1;
}
}
int count = 0;
int c[n*(n-1)/2]={};
for (int i = 0; i < n*(n-1)/2; i++)
{
c[i]=0;
}
for (int i = 0; i < n*(n-1)/2; i++)
{
for (int j = 0; j < m; j++)
{
if (b[i][j]==1)
{
count++;
}
}
c[i]=count;
count=0;
}
for (int i = 0; i < n; i++)
{
int temp=c[i];
int j=i;
while (j>0 && temp<c[j-1])
{
c[j]=c[j-1];
j=j-1;
}
c[j]=temp;
}
int sum=0;
for (int i = 0; i < n*(n-1)/2; i++)
{
if (c[ n*(n-1)/2 - 1]==c[i])
{
sum++;
}
}
cout<<c[ n*(n-1)/2 - 1]<<endl;
cout<<sum;
return 0;
}
While writing the entire code using "printf" and "scanf", I simply replaced "cin and cout":在使用“printf”和“scanf”编写整个代码时,我只是替换了“cin and cout”:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int n, m;
scanf("%d", "%d", &n, &m);
int a[n][m] = {};
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%d", &a[i][j]);
}
}
int b[n * (n - 1) / 2][m] = {};
int x = 0;
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
for (int k = 0; k < m; k++)
{
b[x][k] = (a[i][k] | a[j][k]);
}
x = x + 1;
}
}
int count = 0;
int c[n * (n - 1) / 2] = {};
for (int i = 0; i < n * (n - 1) / 2; i++)
{
c[i] = 0;
}
for (int i = 0; i < n * (n - 1) / 2; i++)
{
for (int j = 0; j < m; j++)
{
if (b[i][j] == 1)
{
count++;
}
}
c[i] = count;
count = 0;
}
for (int i = 0; i < n; i++)
{
int temp = c[i];
int j = i;
while (j > 0 && temp < c[j - 1])
{
c[j] = c[j - 1];
j = j - 1;
}
c[j] = temp;
}
int sum = 0;
for (int i = 0; i < n * (n - 1) / 2; i++)
{
if (c[n * (n - 1) / 2 - 1] == c[i])
{
sum++;
}
}
printf("sum");
return 0;
}
I am also attaching a copy of the output that I am getting using the former code: The output is coming out fine when I am manually giving whitespace characters but when am running the same code using printf
and scanf
it's not working for me for some reason:我还附上了一份 output 的副本,我正在使用以前的代码:当我手动提供空白字符时,output 运行良好,但是当我使用
printf
运行相同的代码时,因为某些原因而无法使用scanf
:
Could someone plz guide me as to where was I going wrong?有人可以指导我哪里出错了吗?
scanf("%d", "%d", &n, &m);
should be应该
scanf("%d %d", &n, &m);
scanf
takes one format string as it's first argument and then (depending on the format string) zero or more arguments after that. scanf
将一个格式字符串作为第一个参数,然后(取决于格式字符串)零个或多个 arguments 之后。
printf
is similar, so printf
类似,所以
printf("sum");
should be应该
printf("%d", sum);
All of this is well documented here and here .所有这些在此处和此处都有很好的记录。
you have 4 problems.你有4个问题。
scanf("%d", "%d", &n, &m);
will not load the integers into n and m. scanf("%d%d", &n, &m);
scanf("%1d", &a[i][j]);
printf("%d", sum);
c[ n*(n-1)/2 - 1]
the second doesn't.c[ n*(n-1)/2 - 1]
的值,第二个没有。 You could either print this out in a second printf: printf("%d\n", c[ n*(n-1)/2 - 1]); //note use of \n to force a new line on output
or in the same printf:或在同一个 printf 中:
printf("%d\n%d", c[ n*(n-1)/2 - 1], sum);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.