在包含10个数的一维整数数组a中查找给定的数据num。 如果找到则返回1,未找到返回 0 .
#include <fstream.h>
#include <iostream.h>
int fun(int a[],int num)
{
/**********Program**********/
for(int i=0;i<10;i++)
{
if(a[i]==num)
return 1;
}
return 0;
/********** End **********/
}
void main()
{
int a[10]={54,256,563,754,34,56,345,543,45,65};
int num = 46;
if (fun(a,num)==1)
cout <<" 找到!" <<endl;
else
cout <<" 没有找到!" <<endl;
void wwjt();
wwjt();
}
void wwjt()
{
int a[10]={54,256,563,754,34,56,345,543,45,65};
fstream myfile;
myfile.open(" out.txt" ,ios::out);
myfile<<fun(a,46)<<endl;
myfile<<fun(a,345)<<endl;
myfile.close();
}
用“辗转相除方法”计算两个数 x,y 的最大公约数。
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
void writeinfile(int n);
void main()
{
int x,y,n;
x=25,y=10;
/**********Program**********/
n=x%y;
while(n)
{
x=y;
y=n;
n=x%y;
}
/********** End **********/
cout<<y;
writeinfile(y);
}
void writeinfile(int n)
{
fstream myfile;
myfile.open(" f.txt" ,ios::out);
myfile<<n<<endl;
myfile.close();
}