解答一个C语言程序。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解答一个C语言程序。相关的知识,希望对你有一定的参考价值。

源程序如下:
main()

int num1,num2;
int *num1_p=&num1,*num2_p=&num2,*pointer;
printf("input two number:");
scanf("%d%d\n",num1_p,num2_p);
printf("num1=%d,num2=%d\n",num1,num2);
if(*num1_p>*num2_p)

pointer=num1_p;
num1_p=num2_p;
num2_p=pointer;

printf("min=%d,max=%d\n",*num1_p,*num2_p);
getch();

各位大侠看看这个程序有没有错误的地方。
另外,我用的是TC2.0,在软件里面运行这个程序的时候,必须要输入三个数才能往下进行,我题目设定的是两个数。运行其他有关指针的程序也是这个情况,这是什么缘故,高手解答一下啊。

你的代码scanf里面有一个\n你把那个删掉就不需要输入3次,至于为什么有\n还要输入一次字符才可以显示我也不是很清楚 参考技术A main()

int num1,num2;
int *num1_p=&num1,*num2_p=&num2,*pointer;
printf("input two number:");
scanf("%d%d",num1_p,num2_p); 这里把\n拿掉
printf("num1=%d,num2=%d\n",num1,num2);
if(*num1_p>*num2_p)

pointer=num1_p;
num1_p=num2_p;
num2_p=pointer;

printf("min=%d,max=%d\n",*num1_p,*num2_p);
getch();

程序没什么错误
参考技术B SCANF里面去掉\N

c语言啊,求大神您解答

用100元人民币兑换10元、5员和1元的纸币,(每一种都要有)公50张,请用穷举法编程计算共有集中兑换发难,每种方案各兑换多少张纸币? 请大神们解答,谢谢,拜过
问题补充:
速度阿,在线等大神执教,回答正确有意想不到的惊喜哦

给楼主个思路吧。

每种都要有,就是说10,5,1至少有一张,那至少有16块是定的,组合的只是84块。

写个循环
int shik = 0;
int wuk = 0;
int yik = 0;
for (shik = 0; shik * 10 < 84 ;shik++)

for (wuk = 0; 84 - shik * 10 - wuk * 5 > 0; wuk++)

for (yik = 1; yik < 84 - 84 - shik * 10 - wuk * 5; yik++)

if (shik * 10 + wuk * 5 + yik == 84)
printf("%d %d %d, shik, wuk, yik);



主体都差不多了。
惊喜呢?
参考技术A 如果分别设X,Y,Z代表10元,五元,一元则
10X+5Y+Z==100;
X+Y+Z==50; //LZ的意思是不是三种加起来要50张还是别的什么意思??
容易得到。。。9X+4Y==50;
X==(50-4Y)/9;则Y只能取1,2,3,4,5,6,7,8,9,10,11,12,13,(这个用程序很容易实现)
可以得出Y==8,X==2,Z==40(唯一解)也就是说2张10元,八张五元,和四十张1元,一楼的程序测试过没结果输出,pascal,没学过
参考技术B 太简单了……给你个pascal版的先看看
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
Button10: TButton;
Button11: TButton;
Button12: TButton;
Button13: TButton;
Button14: TButton;
Button15: TButton;
Button17: TButton;
Button18: TButton;
Button19: TButton;
Button20: TButton;
Panel1: TPanel;
procedure Button17Click(Sender: TObject);
procedure Button13Click(Sender: TObject);
procedure Button14Click(Sender: TObject);
procedure Button15Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button12Click(Sender: TObject);
procedure Button18Click(Sender: TObject);
procedure Button19Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button20Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
Private declarations
public
Public declarations
end;
var
Form1: TForm1;
a,b,c,d:real;
operator,i,flag:integer;
implementation
$R *.dfm
procedure TForm1.Button17Click(Sender: TObject);
begin
panel1.caption:=panel1.caption+(sender as tbutton).Caption;
end;
procedure TForm1.Button13Click(Sender: TObject);
begin
panel1.caption:=panel1.caption+(sender as tbutton).Caption;
end;
procedure TForm1.Button14Click(Sender: TObject);
begin
panel1.caption:=panel1.caption+(sender as tbutton).Caption;
end;
procedure TForm1.Button15Click(Sender: TObject);
begin
panel1.caption:=panel1.caption+(sender as tbutton).Caption;
end;
procedure TForm1.Button11Click(Sender: TObject);
begin
panel1.caption:=panel1.caption+(sender as tbutton).Caption;
end;
procedure TForm1.Button10Click(Sender: TObject);
begin
panel1.caption:=panel1.caption+(sender as tbutton).Caption;
end;
procedure TForm1.Button9Click(Sender: TObject);
begin
panel1.caption:=panel1.caption+(sender as tbutton).Caption;
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
panel1.caption:=panel1.caption+(sender as tbutton).Caption;
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
panel1.caption:=panel1.caption+(sender as tbutton).Caption;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
panel1.caption:=panel1.caption+(sender as tbutton).Caption;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
panel1.caption:='';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if (panel1.caption<>'') or (panel1.Caption<>'-')or(panel1.Caption<>'.') then
a:=strtofloat(panel1.caption);
operator:= (Sender as tbutton).Tag;
panel1.caption:='';
flag:=flag+1;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if (panel1.caption<>'') or (panel1.Caption<>'-')or(panel1.Caption<>'.') then
a:=strtofloat(panel1.caption);
operator:= (Sender as tbutton).Tag;
panel1.caption:='';
flag:=flag+1;
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
if (panel1.caption<>'') or (panel1.Caption<>'-')or(panel1.Caption<>'.') then
a:=strtofloat(panel1.caption);
operator:= (Sender as tbutton).Tag;
panel1.caption:='';
flag:=flag+1;
end;
procedure TForm1.Button12Click(Sender: TObject);
begin
if (panel1.caption<>'') or (panel1.Caption<>'-')or(panel1.Caption<>'.') then
a:=strtofloat(panel1.caption);
operator:= (Sender as tbutton).Tag;
panel1.caption:='';
flag:=flag+1;
end;
procedure TForm1.Button18Click(Sender: TObject);
begin
if i mod 2=0 then
panel1.caption:='-'+panel1.caption
else
begin
panel1.caption:=copy(panel1.Caption,2,length(panel1.Caption));
end;
i:=i+1;
end;
procedure TForm1.Button19Click(Sender: TObject);
begin
panel1.caption:=panel1.caption+(sender as tbutton).Caption;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
i:=0 ;
operator:=0;
flag:=0;
end;
procedure TForm1.Button20Click(Sender: TObject);
begin
if (panel1.caption<>'') or (panel1.Caption<>'-')or(panel1.Caption<>'.') then
begin
b:=strtofloat(panel1.caption);
if (c=b)and(flag=0)then
begin
a:=c;
b:=d;
end;
case operator of
1:begin panel1.caption:=floattostr(a+b);c:=a+b; end;
2:begin panel1.caption:=floattostr(a-b);c:=a-b; end;
3:begin panel1.caption:=floattostr(a*b);c:=a*b; end;
4:
begin
if a=0 then
begin
if b=0 then
panel1.caption:='NAN'
else
panel1.caption:='INF';
end
else
begin
panel1.caption:=floattostr(a/b);
c:=a/b;
end;
end;
end;
end;
d:=b;
flag:=0;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
panel1.caption:=copy(panel1.caption,0,length(panel1.caption)-1) ;
end;
end.追问

这是啥啊?用啥运行?能用codeblocks运行吗

参考技术C 其实就是一个2重循环,
第一层循环遍历10元张数
第二层循环遍历5元张数
10元、5元张数确定后,1元的也知道了
然后判断总数是不是100,如果是则输出或者存储到一个数组中

int num10, num5, num1, num = 0;

for(num10 = 1; num10 < 49; num10 ++)

for(num5 = 1; num5 < 50 - num10; num5 ++)


num1 = 50 - num10 - num5;

if(100 == (num10 * 10 + num5 * 5 + num1))


num ++;

printf("Solution %d, need %d $10, %d $5, %d $1\n", num, num10, num5, num1);







printf("Total %d solutions.\n", num);

以上是关于解答一个C语言程序。的主要内容,如果未能解决你的问题,请参考以下文章

c语言数据类型异常,请牛人解答

c语言程序解答(在线等)?

c语言中求交集的一个函数,不知道哪里错了,求高手解答。。。

求解答C语言“指针赋值”?

c语言背包问题,求高手解答

求解答c语言结构体定义中typedef的作用