简体   繁体   English

在 HackerRank 练习题中使用 Printf 和 Scanf 的问题

[英]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 中解决这个问题已经有一段时间了。我面临的问题是,我是一个初学者,在阅读问题时最终使用cincout而不是scanfprintf ,同时接受用户输入能够检查我的推理是否正确,尽管我已经使用前者正确解决了问题,但在使用后者时遇到了困难。

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 :这是使用cincout的代码:

#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

当我手动提供空白字符时输出很好,但是当我使用 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个问题。

  1. scanf("%d", "%d", &n, &m); will not load the integers into n and m.不会将整数加载到 n 和 m 中。 It will load try to load 1 integer into the second argument(int this case "%d") then ignore n and m.它将加载尝试将 1 integer 加载到第二个参数(在这种情况下为“%d”)然后忽略 n 和 m。 To load multiple integers in the same scanf you need to add all of the %d's in the same string:要在同一个 scanf 中加载多个整数,您需要在同一个字符串中添加所有 %d:

scanf("%d%d", &n, &m);

  1. the second scanf currently has no way of knowing how many digits you want to scan in as the integer and will therefore attempt to load the largest integer possible, because the maximum integer is 2147483647 it has no problem loading 10101 for example as a single integer. the second scanf currently has no way of knowing how many digits you want to scan in as the integer and will therefore attempt to load the largest integer possible, because the maximum integer is 2147483647 it has no problem loading 10101 for example as a single integer. This works differently with std::cin as the ">>" operator will read 1 character at a time until it has enough to create a valid integer(1 is a valid integer).这与 std::cin 的工作方式不同,因为“>>”运算符将一次读取 1 个字符,直到它足以创建一个有效整数(1 是一个有效整数)。 You can force scanf to only load a set number of digits by adding the number of digits to load to the format string:您可以通过将要加载的位数添加到格式字符串来强制 scanf 仅加载一组位数:

scanf("%1d", &a[i][j]);

  1. Your printf function will simply print "sum" not the value of the variable sum.您的 printf function 将简单地打印“sum”而不是变量 sum 的值。 To get it to print the value of the sum variable you need to use the same type of formatting string you use in scanf:要让它打印 sum 变量的值,您需要使用您在 scanf 中使用的相同类型的格式化字符串:

printf("%d", sum);

  1. the original code prints out the value of 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 中打印出来:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM