无法解决代码挑战。 [英] Not able to solve a code challenge.

查看:81
本文介绍了无法解决代码挑战。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在学习编码,而我正在解决挑战。然后我遇到了这个挑战,以了解数据类型。这是一个基本的但我感到惭愧的是,即使在编码了几个月之后我也无法解决它。



挑战要求声明3个变量:一个整数,一个双和一个字符串。我们提供了3个变量,我们必须添加声明的变量,这些变量将通过输入来初始化。我们必须在屏幕上打印它们。



这是我的代码:



I am still learning to code and I was solving challenges. Then I came across this challenge to get to know about data types. It's a basic one but I feel ashamed that I am not able to solve it even after coding for months.

The challenge asks to declare 3 variables: one integer, one double and a string. We are provided with 3 variables which we have to add the declared variables which will be initialized by taking input. And we have to print them on the screen.

This is my code:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int i = 4;
    double d = 4.0;
    char s[] = "HackerRank ";

    int number;
    double decimal;
    char name[50];

    scanf("%d",&number);
    scanf("%lf",&decimal);
    scanf("%*[\n] %[^\n]",name);
 	
 	printf("\n%d\n%.01lf\n%s%s\n",number+i,decimal+d,s,name);

    return 0;
}





我得到了相同的输出,但系统不接受它。然后我找到了提供解决方案的社论



这是提供的解决方案:





I am getting the same output as required but the system is not accepting it. Then I found the editorial which gave the solution

This is the solution provided :

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int i = 4;
    double d = 4.0;
    char s[] = "HackerRank ";

int i2;
double d2;
char s2[100]; // this is not scalable for input of unknown size

// Read inputs from stdin
scanf("%d", &i2);
scanf("%lf", &d2);
scanf("%*[\n] %[^\n]", s2); 

// Print outputs to stdout
printf("%d\n", i + i2);
printf("%.01lf\n", d + d2);
printf("%s%s", s, s2);

    return 0;
}





如果有人告诉我scanf中%* [\ n]的目的,我将不胜感激。 />


我尝试了什么:



我试图将它们并置但是我已经开始了没有发现任何差异。我首先将十进制变量声明为浮点数。后来我把它改成了两倍,但问题仍然存在。



I would appreciate if anyone tells me the purpose of %*[\n] in scanf.

What I have tried:

I tried to juxtapose them but I couln't find any disparity. I had first declared the decimal variable as a float. Later I changed it to double but the problem remains.

推荐答案

引用:

如果有人告诉我scanf中%* [\ n]的目的,我将不胜感激。

I would appreciate if anyone tells me the purpose of %*[\n] in scanf.



见这里: C库函数 - scanf() [ ^ ]它说:


See here: C library function - scanf()[^] it says:

Quote:

*



这是一个可选的起始星号表示数据将从流中读取但被忽略,即它不存储在相应的参数中。

*

This is an optional starting asterisk indicates that the data is to be read from the stream but ignored, i.e. it is not stored in the corresponding argument.



并且两段代码的输出会略有不同:你的开始和结束都是换行,解决方案没有。将其添加到不同的输入内存大小:


And the output from the two pieces of code will be slightly different: yours starts and ends with a newline, the solution doesn't. Add that to the different input memory size:

char name[50];




char s2[100];

他们可能足以让你失败。

And they may be enough to give you a fail.


这篇关于无法解决代码挑战。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆