|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
有两个数组a和b,各有10个元素,将它们对应地逐个相比(即a[0]与b[0]比,a[1]与b[1]比……)。如果a数组中的元素大于b数组中的相应元素的数目多于b数组中元素大于a数组中相应元素的数目(例如,a[i]>b[i]6次,b[i]>a[i]3次,其中i每次为不同的值),则认为a数组大于b数组,并分别统计出两个数组相应元素大于、等于、小于的次数。
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- //*****************比较两个数组内数的大小,并统计大小次数******************
- int main()
- {
- int compare(int a[],int b[]);
- int a[10],b[10],i,j,factors[3];
- for(i = 0;i < 10;i++)
- {
- printf("enter a[i],b[i]:");
- scanf("%d %d\n",a[i],b[i]);
- }
- factors = compare(a[10],b[10]);
- if(factors[0] >factors[1])
- {
- printf("a is bigger");
- printf("the times that a is bigger:",factors[0]);
- printf("the times that b is bigger:",factors[1]);
- }
- else if(factors[0] <factors[1])
- {
- printf("b is bigger");
- printf("the times that a is bigger:",factors[0]);
- printf("the times that b is bigger:",factors[1]);
- }
- else
- {
- printf("a,b is same");
- printf("the times that a is bigger:",factors[0]);
- printf("the times that b is bigger:",factors[1]);
- }
- return 0;
- }
- int compare(int a[],int b[])
- {
- int i,j,a_bigger = 0,b_bigger = 0,same = 0;
- int factors[3];
- for(i = 0;i < 10;i++)
- {
- if(a[i] > b[i])
- {
- a_bigger++;
- }
- else if (a[i] < b[i])
- {
- b_bigger++;
- }
- else
- {
- same ++;
- }
- }
- factors[0] = a_bigger;
- factors[1] = b_bigger;
- factors[2] = same;
- return factors;
- }
复制代码
本帖最后由 风过无痕1989 于 2020-10-25 18:26 编辑
已经通过 DEV_C++ 编译调试,请大家检验
- //*****************比较两个数组内数的大小,并统计大小次数******************
- #include <stdio.h>
- int main()
- {
- int compare(int a[],int b[],int n);
- int i,*p,*q;
- int x = 0;
- int y = 0;
- int z = 0;
- int a[10] = {0},b[10] = {0},factors[10] = {0};
- p = a;
- q = b;
- printf("enter a[i],b[i]:");
- for(i = 0;i < 10;i++)
- {
- scanf("%d %d\n",&a[i],&b[i]);
- }
-
- for(i = 0;i < 10;i++)
- {
- factors[i] = compare(p,q,i); 调用函数比较两数组
- }
- for(i = 0;i < 10;i++)
- {
- if(factors[i] == 1)
- {
- x += 1; // 统计 a[] > b[] 的次数
- }
- else if(factors[i] == -1)
- {
- y += 1; // 统计 a[] < b[] 的次数
- }
- else
- {
- z += 1; // 统计 a[] = b[] 的次数
- }
- }
- printf("a[] > b[] 的次数: %d\n",x);
- printf("a[] = b[] 的次数: %d\n",z);
- printf("a[] < b[] 的次数: %d\n",y);
- return 0;
- }
- int compare(int a[],int b[],int n)
- {
- int count = 0;
- if(a[n] > b[n])
- {
- count = 1;
- }
- else if (a[n] < b[n])
- {
- count = -1;
- }
- else
- {
- count = 0;
- }
- return count;
- }
复制代码
|
|