求c语言求最大公约数里文件的追加具体的解释

&&问题点数:0&&回复次数:4&&&
[求助]自己怎么添加库中没有的头文件?
关于一个求entropy的程序 我是用VC编译的 由于VC中没有以下两个头文件#include &unistd.h&#include &idc.h&我就直接拷贝这两个头文件到VC下的INCLUDE目录下,但是运行是出现“f:\program files\microsoft visual studio\vc98\include\sys\unistd.h( : fatal error C1083: Cannot open include file: '_ansi.h': No such file or directory”我查看了unistd.h头文件 里面有句#include &_ansi.h&,所以我就把_ansi.h放到相应的INCLUDE目录下。但是还是出现上面的错误。找不到答案,还请坛友指点指点。(代码是老师给的应该没问题,就是头文件调用可能出错)以下是我程序:#include &stdio.h&#include &math.h&#include &unistd.h&#include &idc.h&/***********************************************************************
File: entfile.c
computes zero order entropy assuming 256 letter alphabet.**
: K. Sayood
Last mod: 7/15/95
see usage(), for details see entfile.doc or man page
************************************************************************/
/********************************************************************************NOTICE:
**This code is believed by the author to be bug free.
You are free to use and
**modify this code with the understanding that any use, either direct or
**derivative, must contain acknowledgement of its author and source.
The author**makes no warranty of any kind, expressed or implied, of merchantability or
**fitness for a particular purpose.
The author shall not be held liable for any**incidental or consequential damages in connection with or arising out of the
**furnishing, performance, or use of this software.
This software is not
**authorized for use in life support devices or systems.
*********************************************************************************/
void usage(void);
main(int argc, char **argv){
int i, c, size, hist[256];
float prob[256], count,
char name1[50];
strcpy(name1,argv[1]);
if((ifp = fopen(argv[1],"r") == NULL)
fprintf(stderr,"Input open failed\n";
/* Compute the histogram
for(i=0; i&256; i++)
while((c=getc(ifp))!=EOF)
hist[c]++;
count = (float) (size);
Compute the zero order probability model
for(i=0; i& 256; i++)
prob= (float) hist/ /*
Compute the entropy
entropy = ent(prob,256);
fprintf(stderr,"Entropy of file is %5.2f\n",entropy);
void usage(void){
fprintf(stderr,"usage:\n \t entfile filename [-l size] [-h]\n";
fprintf(stderr,"\t\t filename: name of file for which zero order entropy is\n";
fprintf(stderr,"\t\t\t
to be calculated\n";
fprintf(stderr,"\t\t size
: Size of file in bytes\n";}
以下是头文件unistd.h的代码
#ifndef _SYS_UNISTD_H#define _SYS_UNISTD_H
#ifdef __cplusplusextern "C" {#endif
#include &_ansi.h&#include &sys/types.h&#include &sys/_types.h&#define __need_size_t#define __need_ptrdiff_t#include &stddef.h&
extern char **
_EXFUN(_exit, (int __status ) _ATTRIBUTE ((noreturn)));
_EXFUN(access,(const char *__path, int __amode ));unsigned
_EXFUN(alarm, (unsigned __secs ));int
_EXFUN(chdir, (const char *__path ));int
_EXFUN(chmod, (const char *__path, mode_t __mode ));#if !defined(__INSIDE_CYGWIN__)int
_EXFUN(chown, (const char *__path, uid_t __owner, gid_t __group ));#endif#if defined(__CYGWIN__) || defined(__rtems__)int
_EXFUN(chroot, (const char *__path ));#endifint
_EXFUN(close, (int __fildes ));char
_EXFUN(*ctermid, (char *__s ));char
_EXFUN(*cuserid, (char *__s ));#if defined(__CYGWIN__)int
_EXFUN(daemon, (int nochdir, int noclose));#endifint
_EXFUN(dup, (int __fildes ));int
_EXFUN(dup2, (int __fildes, int __fildes2 ));#if defined(__CYGWIN__)void
_EXFUN(endusershell, (void));#endifint
_EXFUN(execl, (const char *__path, const char *, ... ));int
_EXFUN(execle, (const char *__path, const char *, ... ));int
_EXFUN(execlp, (const char *__file, const char *, ... ));int
_EXFUN(execv, (const char *__path, char * const __argv[] ));int
_EXFUN(execve, (const char *__path, char * const __argv[], char * const __envp[] ));int
_EXFUN(execvp, (const char *__file, char * const __argv[] ));#if defined(__CYGWIN__) || defined(__rtems__)int
_EXFUN(fchdir, (int __fildes));#endifint
_EXFUN(fchmod, (int __fildes, mode_t __mode ));#if !defined(__INSIDE_CYGWIN__)int
_EXFUN(fchown, (int __fildes, uid_t __owner, gid_t __group ));#endifpid_t
_EXFUN(fork, (void ));long
_EXFUN(fpathconf, (int __fd, int __name ));int
_EXFUN(fsync, (int __fd));int
_EXFUN(fdatasync, (int __fd));char
_EXFUN(*getcwd, (char *__buf, size_t __size ));#if defined(__CYGWIN__)int
_EXFUN(getdomainname ,(char *__name, size_t __len));#endif#if !defined(__INSIDE_CYGWIN__)gid_t
_EXFUN(getegid, (void ));uid_t
_EXFUN(geteuid, (void ));gid_t
_EXFUN(getgid, (void ));#endifint
_EXFUN(getgroups, (int __gidsetsize, gid_t __grouplist[] ));#if defined(__CYGWIN__)long
_EXFUN(gethostid, (void));#endifchar
_EXFUN(*getlogin, (void ));#if defined(_POSIX_THREAD_SAFE_FUNCTIONS)int _EXFUN(getlogin_r, (char *name, size_t namesize) );#endifchar
_EXFUN(*getpass, (const char *__prompt));size_t
_EXFUN(getpagesize, (void));#if defined(__CYGWIN__)int
_EXFUN(getpeereid, (int, uid_t *, gid_t *));#endifpid_t
_EXFUN(getpgid, (pid_t));pid_t
_EXFUN(getpgrp, (void ));pid_t
_EXFUN(getpid, (void ));pid_t
_EXFUN(getppid, (void ));#ifdef __CYGWIN__pid_t
_EXFUN(getsid, (pid_t));#endif#if !defined(__INSIDE_CYGWIN__)uid_t
_EXFUN(getuid, (void ));#endif#ifdef __CYGWIN__char *
_EXFUN(getusershell, (void));char
_EXFUN(*getwd, (char *__buf ));int
_EXFUN(iruserok, (unsigned long raddr, int superuser, const char *ruser, const char *luser));#endifint
_EXFUN(isatty, (int __fildes ));#if !defined(__INSIDE_CYGWIN__)int
_EXFUN(lchown, (const char *__path, uid_t __owner, gid_t __group ));#endifint
_EXFUN(link, (const char *__path1, const char *__path2 ));int
_EXFUN(nice, (int __nice_value ));#if !defined(__INSIDE_CYGWIN__)off_t
_EXFUN(lseek, (int __fildes, off_t __offset, int __whence ));#endiflong
_EXFUN(pathconf, (const char *__path, int __name ));int
_EXFUN(pause, (void ));#ifdef __CYGWIN__int
_EXFUN(pthread_atfork, (void (*)(void), void (*)(void), void (*)(void)));#endifint
_EXFUN(pipe, (int __fildes[2] ));ssize_t _EXFUN(pread, (int __fd, void *__buf, size_t __nbytes, off_t __offset));ssize_t _EXFUN(pwrite, (int __fd, const void *__buf, size_t __nbytes, off_t __offset));_READ_WRITE_RETURN_TYPE _EXFUN(read, (int __fd, void *__buf, size_t __nbyte ));#if defined(__CYGWIN__)int
_EXFUN(rresvport, (int *__alport));int
_EXFUN(revoke, (char *__path));#endifint
_EXFUN(rmdir, (const char *__path ));#if defined(__CYGWIN__)int
_EXFUN(ruserok, (const char *rhost, int superuser, const char *ruser, const char *luser));#endifvoid *
_EXFUN(sbrk,
(ptrdiff_t __incr));#if !defined(__INSIDE_CYGWIN__)#if defined(__CYGWIN__)int
_EXFUN(setegid, (gid_t __gid ));int
_EXFUN(seteuid, (uid_t __uid ));#endifint
_EXFUN(setgid, (gid_t __gid ));#endif#if defined(__CYGWIN__)int
_EXFUN(setgroups, (int ngroups, const gid_t *grouplist ));#endifint
_EXFUN(setpgid, (pid_t __pid, pid_t __pgid ));int
_EXFUN(setpgrp, (void ));#if defined(__CYGWIN__) && !defined(__INSIDE_CYGWIN__)int
_EXFUN(setregid, (gid_t __rgid, gid_t __egid));int
_EXFUN(setreuid, (uid_t __ruid, uid_t __euid));#endifpid_t
_EXFUN(setsid, (void ));#if !defined(__INSIDE_CYGWIN__)int
_EXFUN(setuid, (uid_t __uid ));#endif#if defined(__CYGWIN__)void
_EXFUN(setusershell, (void));#endifunsigned _EXFUN(sleep, (unsigned int __seconds ));void
_EXFUN(swab, (const void *, void *, ssize_t));long
_EXFUN(sysconf, (int __name ));pid_t
_EXFUN(tcgetpgrp, (int __fildes ));int
_EXFUN(tcsetpgrp, (int __fildes, pid_t __pgrp_id ));char
_EXFUN(*ttyname, (int __fildes ));#if defined(__CYGWIN__)int
_EXFUN(ttyname_r, (int, char *, size_t)); #endifint
_EXFUN(unlink, (const char *__path ));int
_EXFUN(vhangup, (void ));_READ_WRITE_RETURN_TYPE _EXFUN(write, (int __fd, const void *__buf, size_t __nbyte ));
#ifdef __CYGWIN__# define __UNISTD_GETOPT__# include &getopt.h&# undef __UNISTD_GETOPT__#elseextern char *
/* getopt(3) external variables */extern int optind, opterr,int
getopt(int, char * const [], const char *);
/* getopt(3) external variable */#endif
_POSIX_SOURCEpid_t
_EXFUN(vfork, (void ));
extern char *
/* getsubopt(3) external variable */int
getsubopt(char **, char * const *, char **);#endif /* _POSIX_SOURCE */
#ifdef _COMPILING_NEWLIB/* Provide prototypes for most of the _&systemcall& names that are
provided in newlib for some compilers.
_EXFUN(_close, (int __fildes ));pid_t
_EXFUN(_fork, (void ));pid_t
_EXFUN(_getpid, (void ));int
_EXFUN(_link, (const char *__path1, const char *__path2 ));_off_t
_EXFUN(_lseek, (int __fildes, _off_t __offset, int __whence ));#ifdef __LARGE64_FILES_off64_t _EXFUN(_lseek64, (int __filedes, _off64_t __offset, int __whence ));#endif_READ_WRITE_RETURN_TYPE _EXFUN(_read, (int __fd, void *__buf, size_t __nbyte ));void *
_EXFUN(_sbrk,
(ptrdiff_t __incr));int
_EXFUN(_unlink, (const char *__path ));_READ_WRITE_RETURN_TYPE _EXFUN(_write, (int __fd, const void *__buf, size_t __nbyte ));int
_EXFUN(_execve, (const char *__path, char * const __argv[], char * const __envp[] ));#endif
#if defined(__CYGWIN__) || defined(__rtems__) || defined(__sh__)#if !defined(__INSIDE_CYGWIN__)int
_EXFUN(ftruncate, (int __fd, off_t __length));int
_EXFUN(truncate, (const char *, off_t __length));#endif#endif#if defined(__CYGWIN__) || defined(__rtems__)int
_EXFUN(getdtablesize, (void));int
_EXFUN(setdtablesize, (int));useconds_t _EXFUN(ualarm, (useconds_t __useconds, useconds_t __interval));unsigned _EXFUN(usleep, (unsigned int __useconds));#if !(defined
(_WINSOCK_H) || defined (__USE_W32_SOCKETS))/* winsock[2].h defines as __stdcall, and with int as 2nd arg */int
_EXFUN(gethostname, (char *__name, size_t __len));#endifchar *
_EXFUN(mktemp, (char *));#if defined(__CYGWIN__)void
_EXFUN(sync, (void));#else /* defined(__rtems__) */int
_EXFUN(sync, (void));#endif#endifint
_EXFUN(readlink, (const char *__path, char *__buf, int __buflen));int
_EXFUN(symlink, (const char *__name1, const char *__name2));
#include &sys/features.h&
#define STDIN_FILENO
/* standard input file descriptor */#define STDOUT_FILENO
/* standard output file descriptor */#define STDERR_FILENO
/* standard error file descriptor */
4.8.1 Get Configurable System Variables, P93, p. 96**
NOTE: Table 4-2, Configurable System Variables, p. 96*/
#define _SC_ARG_MAX
0#define _SC_CHILD_MAX
1#define _SC_CLK_TCK
2#define _SC_NGROUPS_MAX
3#define _SC_OPEN_MAX
/* no _SC_STREAM_MAX */#define _SC_JOB_CONTROL
5#define _SC_SAVED_IDS
6#define _SC_VERSION
7#define _SC_PAGESIZE
8/* CYGWIN-specific values .. do not touch */#define _SC_NPROCESSORS_CONF
9#define _SC_NPROCESSORS_ONLN
10#define _SC_PHYS_PAGES
11#define _SC_AVPHYS_PAGES
12/* end of CYGWIN-specific values */#define _SC_MQ_OPEN_MAX
13#define _SC_MQ_PRIO_MAX
14#define _SC_RTSIG_MAX
15#define _SC_SEM_NSEMS_MAX
16#define _SC_SEM_VALUE_MAX
17#define _SC_SIGQUEUE_MAX
18#define _SC_TIMER_MAX
19#define _SC_TZNAME_MAX
#define _SC_ASYNCHRONOUS_IO
21#define _SC_FSYNC
22#define _SC_MAPPED_FILES
23#define _SC_MEMLOCK
24#define _SC_MEMLOCK_RANGE
25#define _SC_MEMORY_PROTECTION
26#define _SC_MESSAGE_PASSING
27#define _SC_PRIORITIZED_IO
28#define _SC_REALTIME_SIGNALS
29#define _SC_SEMAPHORES
30#define _SC_SHARED_MEMORY_OBJECTS
31#define _SC_SYNCHRONIZED_IO
32#define _SC_TIMERS
33#define _SC_AIO_LISTIO_MAX
34#define _SC_AIO_MAX
35#define _SC_AIO_PRIO_DELTA_MAX
36#define _SC_DELAYTIMER_MAX
P1003.1c/D10, p. 52 adds the following.*/
#define _SC_THREAD_KEYS_MAX
38#define _SC_THREAD_STACK_MIN
39#define _SC_THREAD_THREADS_MAX
40#define _SC_TTY_NAME_MAX
#define _SC_THREADS
42#define _SC_THREAD_ATTR_STACKADDR
43#define _SC_THREAD_ATTR_STACKSIZE
44#define _SC_THREAD_PRIORITY_SCHEDULING
45#define _SC_THREAD_PRIO_INHERIT
46/* _SC_THREAD_PRIO_PROTECT was _SC_THREAD_PRIO_CEILING in early drafts */#define _SC_THREAD_PRIO_PROTECT
47#define _SC_THREAD_PRIO_CEILING
_SC_THREAD_PRIO_PROTECT#define _SC_THREAD_PROCESS_SHARED
48#define _SC_THREAD_SAFE_FUNCTIONS
49#define _SC_GETGR_R_SIZE_MAX
50#define _SC_GETPW_R_SIZE_MAX
51#define _SC_LOGIN_NAME_MAX
52#define _SC_THREAD_DESTRUCTOR_ITERATIONS 53
#if !defined(__CYGWIN__) && !defined(__rtems__)#define _SC_STREAM_MAX
100#define _SC_PRIORITY_SCHEDULING
_PC_LINK_MAX
_PC_MAX_CANON
_PC_MAX_INPUT
_PC_NAME_MAX
_PC_PATH_MAX
_PC_PIPE_BUF
_PC_CHOWN_RESTRICTED
_PC_NO_TRUNC
_PC_VDISABLE
_PC_ASYNC_IO
_PC_PRIO_IO
10# define
_PC_SYNC_IO
11#ifdef __CYGWIN__/* Ask for POSIX permission bits support. */# define
_PC_POSIX_PERMISSIONS
90/* Ask for full POSIX permission support including uid/gid settings. */# define
_PC_POSIX_SECURITY
/* FIXME: This is temporary until winsup gets sorted out.
*/#ifdef __CYGWIN__#define MAXPATHLEN (260 - 1 /* NUL */)#else# define
MAXPATHLEN
1024#endif
#ifdef __cplusplus}#endif#endif /* _SYS_UNISTD_H */
[此贴子已经被作者于 23:09:55编辑过]
搜索更多相关主题的帖子:
等 级:新手上路
帖 子:43
因为不同的编译器自带的库不一样的....换个环境!
#include &unistd.h&#include &idc.h&
这两个头文件是那个编译器环境的,你就换那个编译器..
等 级:新手上路
帖 子:565
好长啊,懒得看了呵。自己写一个.h的头文件,里面编写你自己需要的函数。然后在主程序文件中包含你的头文件,就可以在程序中调用你的自定义函数和常量了。当然,这个头文件需要在系统指定的目录里,比如include文件夹
努力成为菜鸟!
等 级:新手上路
谢谢你们的回答!头文件我找到了也放到相应的目录下了,是不是还要找到这些头文件对应的LIB文件然后在添加到相应的目录下才可以啊?
等 级:新手上路
帖 子:36
这个当然啦.
版权所有,并保留所有权利。
Powered by , Processed in 0.020225 second(s), 8 queries.
Copyright&, BCCN.NET, All Rights Reserved求C语言经典编程282例,求这本书的PDF或是其他文档的文件,我邮箱zhouqg__百度知道
求C语言经典编程282例,求这本书的PDF或是其他文档的文件,我邮箱zhouqg_
提问者采纳
【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去      掉不满足条件的排列。2.程序源代码:main(){int i,j,k;printf(&\n&);for(i=1;i&5;i++) for(j=1;j&5;j++)  for (k=1;k&5;k++)   {    if (i!=k&&i!=j&&j!=k)     printf(&%d,%d,%d\n&,i,j,k);   }}【程序2】题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高   于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提   成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于   40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于   100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。      2.程序源代码:main(){int bonus1,bonus2,bonus4,bonus6,bonus10,scanf(&%ld&,&i);bonus1=.1;bonus2=bonus1+.75;bonus4=bonus2+.5;bonus6=bonus4+.3;bonus10=bonus6+.15; if(i&=100000)  bonus=i*0.1; else if(i&=200000)     bonus=bonus1+(i-.075;    else if(i&=400000)        bonus=bonus2+(i-.05;       else if(i&=600000)           bonus=bonus4+(i-.03;          else if(i&=1000000)              bonus=bonus6+(i-.015;             else              bonus=bonus10+(i-.01;printf(&bonus=%d&,bonus);}【程序3】题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后      的结果满足如下条件,即是结果。请看具体分析:2.程序源代码:#i nclude &math.h&main(){long int i,x,y,z;for (i=1;i&100000;i++) { x=sqrt(i+100); /*x为加上100后开方后的结果*/  y=sqrt(i+268); /*y为再加上168后开方后的结果*/   if(x*x==i+100&&y*y==i+268)/*如果一个数的平方根的平方等于该数,这说明此数是完全平方数*/    printf(&\n%ld\n&,i); }}【程序4】题目:输入某年某月某日,判断这一天是这一年的第几天?1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊      情况,闰年且输入月份大于3时需考虑多加一天。2.程序源代码:main(){int day,month,year,sum,printf(&\nplease input year,month,day\n&);scanf(&%d,%d,%d&,&year,&month,&day);switch(month)/*先计算某月以前月份的总天数*/{ case 1:sum=0; case 2:sum=31; case 3:sum=59; case 4:sum=90; case 5:sum=120; case 6:sum=151; case 7:sum=181; case 8:sum=212; case 9:sum=243; case 10:sum=273; case 11:sum=304; case 12:sum=334; default: printf(&data error&);}sum=sum+ /*再加上某天的天数*/ if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/  leap=1; else  leap=0;if(leap==1&&month&2)/*如果是闰年且月份大于2,总天数应该加一天*/sum++;printf(&It is the %dth day.&,sum);}【程序5】题目:输入三个整数x,y,z,请把这三个数由小到大输出。1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x&y则将x与y的值进行交换,      然后再用x与z进行比较,如果x&z则将x与z的值进行交换,这样能使x最小。2.程序源代码:main(){int x,y,z,t;scanf(&%d%d%d&,&x,&y,&z);if (x&y){t=x;x=y;y=t;} /*交换x,y的值*/if(x&z){t=z;z=x;x=t;}/*交换x,z的值*/if(y&z){t=y;y=z;z=t;}/*交换z,y的值*/printf(&small to big: %d %d %d\n&,x,y,z);}【程序6】题目:用*号输出字母C的图案。1.程序分析:可先用'*'号在纸上写出字母C,再分行输出。2.程序源代码:#i nclude &stdio.h&main(){printf(&Hello C-world!\n&);printf(& ****\n&);printf(& *\n&);printf(& * \n&);printf(& ****\n&);}【程序7】题目:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!1.程序分析:字符共有256个。不同字符,图形不一样。      2.程序源代码:#i nclude &stdio.h&main(){char a=176,b=219;printf(&%c%c%c%c%c\n&,b,a,a,a,b);printf(&%c%c%c%c%c\n&,a,b,a,b,a);printf(&%c%c%c%c%c\n&,a,a,b,a,a);printf(&%c%c%c%c%c\n&,a,b,a,b,a);printf(&%c%c%c%c%c\n&,b,a,a,a,b);}【程序8】题目:输出9*9口诀。1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。2.程序源代码:#i nclude &stdio.h&main(){ int i,j, printf(&\n&); for (i=1;i&10;i++)  { for(j=1;j&10;j++)    {     result=i*j;     printf(&%d*%d=%-3d&,i,j,result);/*-3d表示左对齐,占3位*/    }   printf(&\n&);/*每一行后换行*/  }}【程序9】题目:要求输出国际象棋棋盘。1.程序分析:用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格。2.程序源代码:#i nclude &stdio.h&main(){int i,j;for(i=0;i&8;i++) {  for(j=0;j&8;j++)   if((i+j)%2==0)    printf(&%c%c&,219,219);   else    printf(& &);   printf(&\n&); }}【程序10】题目:打印楼梯,同时在楼梯上方打印两个笑脸。1.程序分析:用i控制行,j来控制列,j根据i的变化来控制输出黑方格的个数。2.程序源代码:#i nclude &stdio.h&main(){int i,j;printf(&\1\1\n&);/*输出两个笑脸*/for(i=1;i&11;i++) { for(j=1;j&=i;j++)   printf(&%c%c&,219,219); printf(&\n&); }}【程序11】题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月   后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....2.程序源代码:main(){long f1,f2;f1=f2=1;for(i=1;i&=20;i++) { printf(&%12ld %12ld&,f1,f2);   if(i%2==0) printf(&\n&);/*控制输出,每行四个*/   f1=f1+f2; /*前两个月加起来赋值给第三个月*/   f2=f1+f2; /*前两个月加起来赋值给第三个月*/ }}【程序12】题目:判断101-200之间有多少个素数,并输出所有素数。1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,      则表明此数不是素数,反之是素数。       2.程序源代码:#i nclude &math.h&main(){ int m,i,k,h=0,leap=1; printf(&\n&); for(m=101;m&=200;m++)  { k=sqrt(m+1);   for(i=2;i&=k;i++)     if(m%i==0)      {leap=0;}   if(leap) {printf(&%-4d&,m);h++;        if(h%10==0)        printf(&\n&);        }   leap=1;  } printf(&\nThe total is %d&,h);}【程序13】题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数   本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。2.程序源代码:main(){int i,j,k,n;printf(&'water flower'number is:&); for(n=100;n&1000;n++) {  i=n/100;/*分解出百位*/  j=n/10%10;/*分解出十位*/  k=n%10;/*分解出个位*/  if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)   {   printf(&%-5d&,n);   } }printf(&\n&);}【程序14】题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。(2)如果n&&k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n, 重复执行第一步。(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。2.程序源代码:/* zheng int is divided yinshu*/main(){int n,i;printf(&\nplease input a number:\n&);scanf(&%d&,&n);printf(&%d=&,n);for(i=2;i&=n;i++) {  while(n!=i)  {   if(n%i==0)   { printf(&%d*&,i);    n=n/i;   }   else      }}printf(&%d&,n);}【程序15】题目:利用条件运算符的嵌套来完成此题:学习成绩&=90分的同学用A表示,60-89分之间的用B表示,   60分以下的用C表示。1.程序分析:(a&b)?a:b这是条件运算符的基本例子。2.程序源代码:main(){   printf(&please input a score\n&); scanf(&%d&,&score); grade=score&=90? 'A': (score&=60? 'B':'C'); printf(&%d belongs to %c&,score,grade);}【程序16】题目:输入两个正整数m和n,求其最大公约数和最小公倍数。1.程序分析:利用辗除法。2.程序源代码:main(){ int a,b,num1,num2, printf(&please input two numbers:\n&); scanf(&%d,%d&,&num1,&num2); if(num1 { temp=num1;  num1=num2;  num2= }a=num1;b=num2;while(b!=0)/*利用辗除法,直到b为0为止*/ {  temp=a%b;  a=b;  b= }printf(&gongyueshu:%d\n&,a);printf(&gongbeishu:%d\n&,num1*num2/a);}【程序17】题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。1.程序分析:利用while语句,条件为输入的字符不为'\n'.      2.程序源代码:#i nclude &stdio.h&main(){ int letters=0,space=0,digit=0,others=0; printf(&please input some characters\n&); while((c=getchar())!='\n') { if(c&='a'&&c&='z'||c&='A'&&c&='Z')  letters++; else if(c==' ')  space++;   else if(c&='0'&&c&='9')       digit++;     else       others++;}printf(&all in all:char=%d space=%d digit=%d others=%d\n&,letters,space,digit,others);}【程序18】题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+(此时   共有5个数相加),几个数相加有键盘控制。1.程序分析:关键是计算出每一项的值。2.程序源代码:main(){ int a,n,count=1; long int sn=0,tn=0; printf(&please input a and n\n&); scanf(&%d,%d&,&a,&n); printf(&a=%d,n=%d\n&,a,n); while(count&=n) {  tn=tn+a;  sn=sn+  a=a*10;  ++ }printf(&a+aa+...=%ld\n&,sn);}【程序19】题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程   找出1000以内的所有完数。1. 程序分析:请参照程序&--程序14.2.程序源代码:main(){static int k[10];int i,j,n,s;for(j=2;j&1000;j++) { n=-1; s=j;  for(i=1;i {   if((j%i)==0)   {n++;    s=s-i;    k[n]=i;   }  } if(s==0) { printf(&%d is a wanshu&,j); for(i=0;i printf(&%d,&,k); printf(&%d\n&,k[n]); }}}【程序20】题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在   第10次落地时,共经过多少米?第10次反弹多高?1.程序分析:见下面注释2.程序源代码:main(){float sn=100.0,hn=sn/2;for(n=2;n&=10;n++) {  sn=sn+2*/*第n次落地时共经过的米数*/  hn=hn/2; /*第n次反跳高度*/ }printf(&the total of road is %f\n&,sn);printf(&the tenth is %f meter\n&,hn);}其他的等整理出来再发! :) :)【程序21】题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个   第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下   的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。1.程序分析:采取逆向思维的方法,从后往前推断。2.程序源代码:main(){int day,x1,x2;day=9;x2=1;while(day&0) {x1=(x2+1)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/ x2=x1; day--; }printf(&the total is %d\n&,x1);}==============================================================【程序22】题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定   比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出   三队赛手的名单。1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,      则表明此数不是素数,反之是素数。       2.程序源代码:main(){char i,j,k;/*i是a的对手,j是b的对手,k是c的对手*/for(i='x';i&='z';i++) for(j='x';j&='z';j++) { if(i!=j)  for(k='x';k&='z';k++)  { if(i!=k&&j!=k)   { if(i!='x'&&k!='x'&&k!='z')   printf(&order is a--%c\tb--%c\tc--%c\n&,i,j,k);   }  } }}==============================================================【程序23】题目:打印出如下图案(菱形)****************************1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重      for循环,第一层控制行,第二层控制列。2.程序源代码:main(){int i,j,k;for(i=0;i&=3;i++) { for(j=0;j&=2-i;j++)  printf(& &); for(k=0;k&=2*i;k++)  printf(&*&); printf(&\n&); }for(i=0;i&=2;i++) { for(j=0;j&=i;j++)  printf(& &); for(k=0;k&=4-2*i;k++)  printf(&*&); printf(&\n&); }}==============================================================【程序24】题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。1.程序分析:请抓住分子与分母的变化规律。2.程序源代码:main(){int n,t,number=20;float a=2,b=1,s=0;for(n=1;n&=n++) { s=s+a/b; t=a;a=a+b;b=t;/*这部分是程序的关键,请读者猜猜t的作用*/ }printf(&sum is %9.6f\n&,s);}==============================================================【程序25】题目:求1+2!+3!+...+20!的和1.程序分析:此程序只是把累加变成了累乘。2.程序源代码:main(){float n,s=0,t=1;for(n=1;n&=20;n++) { t*=n; s+=t; }printf(&1+2!+3!...+20!=%e\n&,s);}==============================================================【程序26】题目:利用递归方法求5!。1.程序分析:递归公式:fn=fn_1*4!2.程序源代码:#i nclude &stdio.h&main(){int fact();for(i=0;i&5;i++) printf(&\40:%d!=%d\n&,i,fact(i));}int fact(j){if(j==0) sum=1;else sum=j*fact(j-1);}==============================================================【程序27】题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。1.程序分析:2.程序源代码:#i nclude &stdio.h&main(){int i=5;void palin(int n);printf(&\40:&);palin(i);printf(&\n&);}void palin(n){if(n&=1) { next=getchar(); printf(&\n\0:&); putchar(next); }else { next=getchar(); palin(n-1); putchar(next); }}==============================================================【程序28】题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第   3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后   问第一个人,他说是10岁。请问第五个人多大?1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道      第四人的岁数,依次类推,推到第一人(10岁),再往回推。2.程序源代码:age(n){if(n==1) c=10;else c=age(n-1)+2;return(c);}main(){ printf(&%d&,age(5));}==============================================================【程序29】题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。1. 程序分析:学会分解出每一位数,如下解释:(这里是一种简单的算法,师专数002班赵鑫提供)2.程序源代码:main( ){long a,b,c,d,e,x;scanf(&%ld&,&x);a=x/10000;/*分解出万位*/b=x%;/*分解出千位*/c=x%;/*分解出百位*/d=x%100/10;/*分解出十位*/e=x%10;/*分解出个位*/if (a!=0) printf(&there are 5, %ld %ld %ld %ld %ld\n&,e,d,c,b,a);else if (b!=0) printf(&there are 4, %ld %ld %ld %ld\n&,e,d,c,b);  else if (c!=0) printf(& there are 3,%ld %ld %ld\n&,e,d,c);    else if (d!=0) printf(&there are 2, %ld %ld\n&,e,d);      else if (e!=0) printf(& there are 1,%ld\n&,e);}==============================================================【程序30】题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。   1.程序分析:同29例2.程序源代码:main( ){long ge,shi,qian,wan,x;scanf(&%ld&,&x);wan=x/10000;qian=x%;shi=x%100/10;ge=x%10;if (ge==wan&&shi==qian)/*个位等于万位并且十位等于千位*/ printf(&this number is a huiwen\n&);else printf(&this number is not a huiwen\n&);}【程序31】题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续   判断第二个字母。1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。2.程序源代码:#i nclude &stdio.h&void main(){printf(&please input the first letter of someday\n&);while ((letter=getch())!='Y')/*当所按字母为Y时才结束*/{ switch (letter){case 'S':printf(&please input second letter\n&);     if((letter=getch())=='a')      printf(&saturday\n&);     else if ((letter=getch())=='u')         printf(&sunday\n&);       else printf(&data error\n&);     case 'F':printf(&friday\n&);case 'M':printf(&monday\n&);case 'T':printf(&please input second letter\n&);     if((letter=getch())=='u')      printf(&tuesday\n&);     else if ((letter=getch())=='h')         printf(&thursday\n&);       else printf(&data error\n&);     case 'W':printf(&wednesday\n&);default: printf(&data error\n&);  } }}==============================================================【程序32】题目:Press any key to change color, do you want to try it. Please hurry up!1.程序分析:            2.程序源代码:#i nclude &conio.h&void main(void){for (color = 0; color & 8; color++) { textbackground(color);/*设置文本的背景颜色*/ cprintf(&This is color %d\r\n&, color); cprintf(&Press any key to continue\r\n&); getch();/*输入字符看不见*/ }}==============================================================【程序33】题目:学习gotoxy()与clrscr()函数   1.程序分析:2.程序源代码:#i nclude &conio.h&void main(void){clrscr();/*清屏函数*/textbackground(2);gotoxy(1, 5);/*定位函数*/cprintf(&Output at row 5 column 1\n&);textbackground(3);gotoxy(20, 10);cprintf(&Output at row 10 column 20\n&);}==============================================================【程序34】题目:练习函数调用1. 程序分析:2.程序源代码:#i nclude &stdio.h&void hello_world(void){printf(&Hello, world!\n&);}void three_hellos(void){for (counter = 1; counter &= 3; counter++)hello_world();/*调用此函数*/}void main(void){three_hellos();/*调用此函数*/}
提问者评价
原来是这样,感谢!
来自:求助得到的回答
其他类似问题
c语言的相关知识
您可能关注的推广回答者:回答者:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 c语言求补码 的文章

 

随机推荐