【发布时间】:2018-08-26 11:43:26
【问题描述】:
链接到Problem - LuckyFour
代码在我自己的系统上运行良好,但提交时显示错误答案?
#include <stdio.h>
void main()
{
int t, n, count;
scanf("%d", &t);
while(t--)
{
count=0;
scanf("\n%d",&n);
while(n>0)
{
if(n%10==4)
{
count++;
}
n=n/10;
}
printf("\n%d", count);
}
}
【问题讨论】:
-
const auto str = std::to_string(t); std::cout << std::count(str.begin(), str.end(), '4') << '\n'; -
int main(void) { /* ... */ return 0; } -
请不要标记多种语言,只使用与您真正编程的语言相对应的标记。
-
建议:使用
'\n'终止行。printf("\n%d", 42);不像printf("%d\n", 42);那样平常。 -
为什么不使用 getline 直接计算 '4' 字符呢?无需转换。您还假设您正在从标准输入读取。这是给的吗?您链接到的描述中没有明确说明。
标签: c