【PTA练习遇到的问题】warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
2018-09-26 12:43
1596 查看
版权声明:本文为博主原创文章,转载请注明:面框背打技术_ https://blog.csdn.net/yhtEric/article/details/82851645
最近在做PTA的题,遇到了下面这个问题,是不是遇到了边界问题? 大神们帮忙解答下咯? 注:查到一些相关的解答,说加if,或者用void等等,但是这些办法 都是需要对main函数进行修改的。由于PTA特殊性,这里不允许对main函数进行修改。
问题:a.c: In function ‘main’:
a.c:13:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
^~~~~~~~~~~~~~~
a.c:15:9: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%f", &A[i]);
题目:求自定类型元素序列的中位数
本题要求实现一个函数,求N个集合元素A[]的中位数,即序列中第⌊N/2+1⌋大的元素。其中集合元素的类型为自定义的ElementType。
函数接口定义:ElementType Median( ElementType A[], int N );
其中给定集合元素存放在数组A[]中,正整数N是数组元素个数。该函数须返回N个A[]元素的中位数,其值也必须是ElementType类型。
输入样例:
3
12.3 34 -5
输出样例:
12.30
代码如下:
#include <stdio.h> #define MAXN 10 typedef float ElementType; ElementType Median( ElementType A[], int N ); void quick_sort ( ElementType A[], int left, int right ); int Partition(ElementType A[], int left, int right); int main () { ElementType A[MAXN]; int N, i; scanf("%d", &N); for ( i=0; i<N; i++ ) scanf("%f", &A[i]); printf("%.2f\n", Median(A, N)); return 0; } ElementType Median( ElementType A[], int N ) { if (N>MAXN) { N=MAXN; } int left=0, right=N-1; int m=0; quick_sort (A,left,right);//快速排序调用 m = N/2; return A[m];//返回数组的中位数值 } void quick_sort (ElementType A[], int left, int right) { if (left>=right)//递归跳出条件 { return; } else { int pivotpos=Partition(A,left,right);//Partition()为划分操作 quick_sort (A, left, pivotpos-1);//左侧字表递归 quick_sort (A, pivotpos+1, right);//右侧字表递归 return; } } int Partition(ElementType A[], int left, int right) { int i,j; ElementType pivot=A[left];//将当前表中的左边第一个元素设为枢轴值,pivot为哨兵 i=left; j=right; while (i<j)//循环跳出条件 { while (i<j&&A[j]>=pivot) {j--;} A[i]=A[j];//将比枢轴值小的元素移动到左边 while (i<j&&A[i]<=pivot) {i++;} A[j]=A[i];//将比枢轴值大的元素移动到右边 } A[i]=pivot;//枢轴元素存放在最终位置 return i;//返回存放枢轴元素的最终位置 }
PS:初学者一个,有哪些写得不恰当的,欢迎大家给我意见,谢谢。
相关文章推荐
- [绍棠] ReactNative : ignoring return value of function declared with warn_unused_result attribute
- ReactNative : ignoring return value of function declared with warn_unused_result attribute
- ReactNative : ignoring return value of function declared with warn_unused_result attribute
- react native 打包Ignoring return value of function declared with warn_unused_result attribute
- The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one
- Warning 534: Ignoring return value of function 'printf
- SSH搭建遇到的问题---There is no result type defined for type 'json' mapped with name 'success'.
- IOS __attribute__ ((warn_unused_result))的含义
- BUG6:Cannot return a value from a method with void result type 返回值错误
- 运行时候出现问题Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
- AS编译问题:Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
- PHP 解决版本问题:"Assigning the return value of new by reference is deprecated"
- 问题解决: Attribute value request.getParameter("id") is quoted with " which must be escaped when used wi
- [WARNING ] Key 'file_ignore_glob' with value None has an invalid type of NoneType
- 在使用JavaBean时遇到的问题:The value for the useBean class attribute is invalid
- 简单的问题,用类创建一个对象error C2380: type(s) preceding 'Complex' (constructor with return type, or illegal redefinition of current class-n
- 【iOS遇到的问题】switch控件--conflicting return type in implementation of 'auroLogin':'uiswitch' vs 'void';
- [Objective-C] NSArray initWithObjects "Expression result unused" warning
- Attribute value is quoted with " which must be escaped when used within the value 问题解决
- 问题解决: Attribute value request.getParameter("id") is quoted with " which must be escaped when used within the value