{
int i,j
for (i=0,j=0(i<length)&&(j<length))
{
while (*a <*b)//有序排列,挨个比较
{
a++
i++
}
while (*a >逗衫 *b)
{
b++
j++
}
if (*a == *b) return *a
}
}
int main(void)
{
int a[ ]={1,3,5,7,9,13,15,27,29,37}
int b[ ]={2,4,6,8,10,13,14,27,29,37}
int c
c = FindSamdData(a, b ,sizeof(a))
}
增加出错的程序。出错的最好使用一个单独参数,我为了方便没有增加参数。
//有序数组查找相同数据,数组等长
//*length为0未找到相同数据,
int FindSamdData(int *a, int*b, int *length)
{
int i,j
for (i=0,j=0(i<*length)&&(j<*length))
{
while (*a <*b)//有序排列,挨个比较
{
a++
i++
if (i>=*length)break
}
while (*a >高搏 *b)
{
b++
j++
if (j>=*length)break
}
if (*a == *b)
{
*length = 1
return *a
}
}
*length = 0
return 0//
}
//数组不等长
//*al或*bl超出数组长表示出错,
int FindSamdData2(int *a, int*b, int *al, int *bl)
{
int i,j
for (i=0,j=0(i<*al)&&(j<*bl))
{
while (*a <*b)//有序排列,挨个比较
{
a++
i++
if (i>=*al) break
}
while (*a >*b)
{
b++
j++
if (j>=*bl) break
}
if (*a == *b)
{
*al = i
*bl = j
return *a
}
}
*al = i
*bl = j
return 0
}
void main(void)
{
int a[]={1,3,5,7,9,13,15,27,29,37}
int b[]={2,4,6,8,10,13,14,27,29,37}
int la = sizeof(a)
int lb = sizeof(b)
int c
c = FindSamdData(a, b ,&la)
if (la == 0) printf("山念腔No same!")
la = sizeof(a)//la会被函数 FindSamdData 修改.
c = FindSamdData2(a, b ,&la, &lb)
if ((la >=sizeof(a)) || (la >=sizeof(a))) printf("No same!")
}
// 记得多给点分哦,很难写的。// C
#include <stdio.h>
int strnmerge(char*str,int n,char const*s1,char const*s2){
while(n > 0) {
if(!(*s1) && !(*s2)) break
if(*s1 && n > 0) {
*str++ = *s1++
--n
}
if(*s2 && n > 0) {
*str++ = *s2++
--n
}
}
*str = '\0'
}
int main() {
char s1[] 悔早= "aaaa"
char s2[] = "bbbbbbbbb"
char str[100]
strnmerge(str, 10, s1, s2)
printf("s1 = %s\n", s1)
printf("s2 = %s\n", s2)
printf("str = %s\n", str)
return 0
}
// C++
#include <iostream>
using namespace std
int strnmerge(char*str,int 腔汪n,char const*s1,char const*s2){
while(n > 0) {
if(!(*s1) && !(*s2)) break
if(*s1 && n > 0) {
*str++ = *s1++
--n
}
if(*s2 && n > 0) {
*str++ = *s2++
--n
}
}
*str = '\0'
}
int main() {
char s1[] = "aaaa"
char s2[] = "bbbbbbbbb"
char str[100]
strnmerge(str, 10, s1, s2)
cout << "s1 = " 伍前仔<< s1 << endl
cout << "s2 = " << s2 << endl
cout << "str = " << str << endl
return 0
}
#include <stdio.h>#define Pi 3.1416
int main()
{float r,h,c,s1,s2,v1,v2
printf("请输入圆半径和圆柱高:")
scanf("%f%f",&r,&h)
c=2*Pi*r
s1=0.5*c*r
s2=4*s1
v1=3*s1*r/4
v2=s1*h
printf("圆周长=%f\n圆面积=%f\n圆搜此球表面积=%f\n",c,s1,s2)
世闹迅弯袜printf("圆球体积=%f\n圆柱体积=%f\n",v1,v2)
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)