【问题标题】:Not getting desired output. Is there something inherently wrong in the logic?没有得到想要的输出。逻辑中是否存在固有的错误?
【发布时间】:2016-08-26 23:30:16
【问题描述】:

输入描述没有测试,然后是输入数组的大小,然后是数组,输出给出 -1 或给定数组中最大素数的平方。 我提供了代码以及预期和实际输出以及使用的示例标准输入。

standard input:
3
5
1 4 6 8 10
3
2 2 9
2
156 13

expected output | getting
-1                -1
4                  4
169               -1


#include <stdio.h>
int main(){
int test,size;
int i,j;
scanf("%d\n",&test);
while(test>=1){
    scanf("%d\n",&size);
    int data[size],factors=0,max=0;
    for(i=0;i<size;i++){
        scanf("%d ",&data[i]);
        for(j=1;j<=data[i];j++){
            if(data[i]%j==0){
                factors+=1;
            }
        }
        if((factors==2) && (data[i]>max)){
            max=data[i];
        }
    }
    if(max>=2){
        printf("%d\n",max*max);
    }else{
        printf("%d\n",-1);
    }
    max=0;
    test-=1;
    }
}

【问题讨论】:

    标签: c arrays logic


    【解决方案1】:

    ok 调试并得到答案。因子需要在下一个 for 循环中初始化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多