programmecantrun 2022-11-06 14:44 采纳率: 60%
浏览 49
已结题

输出N位水仙花数pta

为什么不是scanf 的返回值declared with attribute 就是printf 就是if

水仙花数是指一个N位正整数(N≥3),它的每个位上的数字的N次幂之和等于它本身。例如:153=1
本题要求编写程序,计算所有N位水仙花数。

输入格式:
输入在一行中给出一个正整数N(3≤N≤7)。

输出格式:
按递增顺序输出所有N位水仙花数,每个数字占一行。

输入样例:
3
输出样例:
153
370
371
407
#include<stdio.h>
int main()
{
int n,a,x,b,i,e,S=0,c;
printf("please input a suitable integer:");
scanf("%d",&n);
switch(n){
case 3:a=100,b=999;break;
case 4:a=1000,b=9999;break;
case 5:a=10000,b=99999;break;
case 6:a=100000,b=999999;break;
case 7:a=1000000,b=9999999;break;
default:printf("error\n");break;
}
for(x=a;x<=b;x++)
{
for(i=1;i<n;i++)
{ e=x/10%10;
S=S+eee;
}
c=x%10;
S=S+ccc;
if (S==x)
printf("%d\n",x);
}
return 0;
}

pta说的编译错误:a.c: In function ‘main’:
a.c:23:10: error: stray ‘\357’ in program
if (S==x)
^
a.c:23:11: error: stray ‘\274’ in program
if (S==x)
^
a.c:23:12: error: stray ‘\210’ in program
if (S==x)
^
a.c:23:13: error: expected ‘(’ before ‘S’
if (S==x)
^
a.c:23:13: warning: statement with no effect [-Wunused-value]
a.c:23:7: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
if (S==x)
^~
a.c:23:17: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘if’
if (S==x)
^
a.c:23:17: error: expected statement before ‘)’ token
a.c:6:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);

  • 写回答

2条回答 默认 最新

  • Hacker_徐 2022-11-06 15:51
    关注

    修改了你的代码,试试对不对

    
    #include<stdio.h>
    int main()
    {
        int n,a,x,b,i,e,S=0,c;
        printf("please input a suitable integer:");
        scanf("%d",&n);
        switch(n){
            case 3:a=100,b=999;break;
            case 4:a=1000,b=9999;break;
            case 5:a=10000,b=99999;break;
            case 6:a=100000,b=999999;break;
            case 7:a=1000000,b=9999999;break;
            default:printf("error\n");break;
        }
        for(x=a;x<=b;x++)
        {
            for(i=1;i<n;i++)
            { e=x/10%10;
                S=S+eee;
            }
            c=x%10;
            S=S+ccc;
            if(S==x)//这里本来是错的,前括号你用的是中文符号
                printf("%d\n",x);
        }
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月12日
  • 已采纳回答 3月4日
  • 创建了问题 11月6日

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭