很多程序都是非常简单的:
1.输入2个正整数m和n,求其最大公约数和最小公倍数
#include
#include
int main()
{
int m,n,p,q,s,r;
printf("请输入两个正整数;m,n\n");
scanf("%d,%d",&m,&n);
s=m*n;
if(m
p=m;
m=n;
n=p;
}
printf("%d\t%d\n",m,n);/*按从大到小输出m,n */
while(n!=0)
{
q=m%n;
m=n;
n=q;
}
/*是不是m才是最大公约数啊*/
r=s/m;
printf("m和n最大公约数为 %d\n",m);
printf("m和n最小公倍数为 %d\n",r);/*m和n的积除以m和n的最大公约数即为最小公倍数*/
return 0;
}
2.输出100以内能被3整除且个位数为6的所有整数。
#include
int main(void)
{
int i;
for(i=1;i<=100;i++)
if(i%3==0&&i%10==6)printf("%d ",i);
return 0;
}
3. 编程计算从1到10各数阶乘的和,即1! + 2! + 3! + …… + 9! + 10! 的和
#include
int main(void)
{
int sum,i,term;
sum=0;
term=1;
for(i=1;i<=10;i++)
{
term=term*i;
sum=sum+term;
printf("%d的阶乘 %d\n",i,term);
}
printf("1到10各数阶乘的和 %d\n",sum);
return 0;
}
4.使用嵌套循环产生下列由大写字母组成的图
#include
int main(void)
{
char ch1,ch2;
for(ch1='A';ch1<='Z';ch1++)
{
for(ch2='A';ch2<=ch1;ch2++)
printf("%c",ch2);
printf("\n");
}
return 0;
}
5. 编程输出所有三位数中的素数。
#include
int main(void)
{
int x,i;
for(x=100;x<=999;x++)
{
for(i=2;i
if(i==x/2)printf("%d ",x);
}
return 0;
}
6. 定义一个函数even(),判断一个整数是否是偶数。如果是偶数返回1,否则返回0。(要求包括能使程序正常运行的主函数)
#include
int main(void)
{
int even(int);
int x,y;
scanf("%d",&x);
y=even(x);
if(y==1)
printf("%d 是偶数\n",x);
else
printf("%d 是奇数\n",x);
return 0;
}
int even(int x)
{
if(x%2==0)
return 1;
else
return 0;
}
7. 编写函数mypow,求整型变量x的y次方。(要求包括能使程序正常运行的主函数)
#include
int main(void)
{
int mypow(int,int);
int x,y,s;
scanf("%d%d",&x,&y);
s=mypow(x,y);
printf("%d的%d次方是 %d\n",x,y,s);
return 0;
}
int mypow(int x,int y)
{
int sum,i;
sum=1;
for(i=1;i<=y;i++)
sum=sum*x;
return sum;
}
8.输入一个3位整数,输出它的逆序数。例如,输入127,输出应该是721。
#include
int main(void)
{
int x,y;
scanf("%d",&x);
y=x/100+x/10%10*10+x%10*100;
printf("%d的逆序数 %d\n",x,y);
return 0;
}
#include
int main()
{
int a,b,c,i=1;
char d;
while(i<=20)
{
printf("请输入两个数字,写明加(+)减(-)乘(*)除(/)!");
scanf("%d%c%d",&a,&d,&b);
if(d=='+')c=a+b;
else if(d=='-')c=a-b;
else if(d=='*')c=a*b;
else if(d=='/')c=a/b;
else {printf("输入错误,重来!");continue;}
printf("答案是%d\n",c);
i++;
}
return 0;
}
你的那几个if判断条件里的等号写错了,改成这样“==”;另外,不要这样写d==75,直接d=='+'就好了,就这么几个错,改过来就行了,下面的是改过来的,已调,没问题!
#include
int main()
{
int a,b,c,i=1;
char d;
printf("请输入两个数字,写明加减乘除!\n");
scanf("%d%c%d",&a,&d,&b);
while(i<=20)
{
if(d=='+')c=a+b;
else if(d=='-')c=a-b;
else if(d=='*')c=a*b;
else if(d=='/')c=a/b;
printf("答案是%d\n",c);
printf("请继续输入\n");
scanf("%d%c%d",&a,&d,&b);
i++;
}
return 0;
}
1
#include
main()
{ int i,j,k;
for(i=0;i<=20;i++)
for(j=0;j<=33;j++)
{k=100-i-j;
if((k%3==0)&&(i*5+j*3+k/3==100))printf("%d %d %d\n",i,j,k);}
system("PAUSE");
}
2
#include
main()
{ int n,m,s,f,i;
int a[18]={0};
f=0;s=0;
while(f!=17)
for(i=1;i<=17;i++)
{if(a[i]==0)s=s+1;
if(s==7){s=0;printf("%d ",i);a[i]=1;f=f+1;if(f==17)break;}
}
system("PAUSE");
}
unit Unit4;
interface
uses
Windows,Sysutils;
type
PTTreeData = ^TTreeData;
TTreeData = packed record
NodeValue:Integer;
AHead : THandle;
StockID:string[10];
StockName:string[20];
NextPtr:PTTreeData ;
PrevPtr:PTTreeData ;
end;
TTreeClass = class(TObject)
private
Findex,FCount:Integer ;
HeaderNode:BOOL;
function newData:PTTreeData;
procedure IsBlankHeadLast(list:TTreeData);
function GetCount: Integer;
public
constructor Create;
function ADDData(s,str:string): PTTreeData;
procedure DelData(Cindnal:PTTreeData);
procedure PreverFindData;
procedure BackFindData;
function GetFirst:PTTreeData;
function GetLast:PTTreeData;
function FindData(S:string):PTTreeData;
property FindexData: integer read Findex write Findex;
property Count:Integer read GetCount;
end;
var
AHandle,OleHandle,NewHandle:THandle;
First,Last,Header,BlanK :PTTreeData;
implementation
function TTreeClass.ADDData(s,str:string): PTTreeData;
begin
try
begin
Result := newData;
Result.AHead := LongInt(Result);
AHandle := Result.AHead ;
if not HeaderNode then
begin
IsBlankHeadLast(Result^);
Result.NextPtr := nil ;
Result.StockID := str;
Result.StockName := str;
Result.PrevPtr := Result;
First := Result ;
Last := Result ;
Header := Result ;
Result.NodeValue := FCount;
Inc(Findex);
Inc(FCount);
end
else
begin
Result.NextPtr := nil;
Result.StockID := str;
Result.StockName := str;
PTTreeData(OleHandle).NextPtr := Result;
Result.PrevPtr := PTTreeData(OleHandle);
Inc(Findex);
Inc(FCount);
end;
OleHandle := result.AHead ;
end;
except
Result := nil;
end;
end;
procedure TTreeClass.IsBlankHeadLast(list:TTreeData);
begin
HeaderNode := true ;
end;
procedure TTreeClass.BackFindData;
begin
//
end;
constructor TTreeClass.Create;
begin
Findex:= 0;
FCount:= 0;
HeaderNode := False;
end;
procedure TTreeClass.DelData(Cindnal:PTTreeData);
var
midHand,NextHand,PrevHand:PTTreeData;
begin
midHand:= cindnal;
if HeaderNode then
begin
if FCount = 1 then
begin
Dispose(Header);
Findex := 0 ;
FCount := 0 ;
HeaderNode := False;
end
else
begin
if midHand<>nil then
begin
if midHand.NextPtr = nil then
begin
midHand.PrevPtr.NextPtr := nil;
Dispose(midHand);
Dec(FCount);
end
else
begin
NextHand := midHand.NextPtr ;
PrevHand := midHand.PrevPtr ;
PrevHand.NextPtr := NextHand ;
NextHand.PrevPtr := PrevHand ;
Dispose(midHand);
Dec(FCount);
end;
end;
end;
end;
end;
function TTreeClass.GetFirst:PTTreeData;
begin
Result := Header ;
end;
function TTreeClass.GetLast:PTTreeData;
begin
Result := nil ;
if PTTreeData(AHandle).NextPtr = nil then
begin
Result := PTTreeData(AHandle) ;
end;
end;
function TTreeClass.newData: PTTreeData;
var
Node:PTTreeData;
begin
try
begin
New(Node);
result := PTTreeData(Node);
end;
except
result := nil;
end;
end;
procedure TTreeClass.PreverFindData;
begin
end;
function TTreeClass.FindData(S:string):PTTreeData;
var
nodehd:Pttreedata;
begin
Result := nil ;
nodehd := GetLast ;
if nodehd.StockID = s then Result := nodehd
else
begin
nodehd := Header ;
while nodehd.NextPtr <> nil do
begin
if nodehd.StockID <> s then
begin
nodehd:= nodehd.NextPtr ;
end
else
begin
Result := nodehd ;
Break;
end;
end;
end;
end;
function TTreeClass.GetCount: Integer;
begin
Result := FCount ;
end;
end.