c - 如何使用循环显示带有模式的整数?
问题描述
我正在尝试显示整数
1111
222
33
4
使用循环,但我不知道我的代码有什么问题,我是 c 编程新手,感谢您的帮助:D 这是我的代码:
#include <stdio.h>
int main()
{
int a, b;
for (int a=4; a<=1; a--)
{
for (int b=1; b<=a; b++) {
printf("%d", b);
}
}
printf("\n");
}
解决方案
怎么了:
#include <stdio.h>
int main()
{
int a, b;
for (int a=4; a<=1; a--) /* a<=1 is false when a=4 */
{
for (int b=1; b<=a; b++) {
printf("%d", b); /* what is printed depends on b while it shouldn't */
}
}
printf("\n"); /* this is in wrong place */
}
固定代码:
#include <stdio.h>
int main()
{
int a, b;
for (int a=4; a>=1; a--)
{
for (int b=1; b<=a; b++) {
printf("%d", 1 + (4 - a));
}
printf("\n");
}
}
推荐阅读
- cefsharp - How to prevent Winform popup box CefSharp.BrowserSubprocess.exe Breakpoint A has been reached?
- axon - 是否可以使用 axon 3.3.3 将所有事件存储在微服务架构中的中央 RDBMS 数据库中?
- python - LSTM with rule-based features?
- c++ - 在 Visual Studio Code 中使用 C++ 进行调试时如何读取输入?
- javascript - 在数组由从 mongodb 获得的值填充后,将数组作为 JSON 响应发送
- regex - 应用带有条件环视的正则表达式
- javascript - How do I get information about whether any sound is playing via Web Audio?
- mysql - WordPress 如何将图像链接到数据库中的“页面”帖子类型?
- node.js - 尝试使用 azure 在 nodejs 中获取访问令牌时出错,AADSTS50058:已发送静默登录请求,但没有用户登录
- java - 如何将 AuthenticationFilter 放在我的第三方 servlet 前面?