石景山网站建设制作公司,什么类型的产品可以做网站出口,兰溪市住房和城乡建设局网站,网站建设行业发展方向四.找素数 素数#xff1a;除了1和它本身不再有其他因数的自然数。换句话说#xff1a;一个大于1的自然数 #xff0c;如果只能被1和它本身整除#xff0c;那就是素数#xff08;质数#xff09;。
在打印中遇到的问题就是#xff0c;知道怎么写却总是运行不起来。主要…四.找素数 素数除了1和它本身不再有其他因数的自然数。换句话说一个大于1的自然数 如果只能被1和它本身整除那就是素数质数。
在打印中遇到的问题就是知道怎么写却总是运行不起来。主要有这两个问题
1.每判断一个数是否是素数后都需要对flag进行初始化否则flag一直都是1。
2.打印素数需要在外层循环里面每判断一次就打印一次是素数则打印不是则不打印。
int main()
{int i 0;int j 0;int flag 0;//声明一个flagfor (i 100; i 200; i){flag 0;//每次判断素数后需要初始化for (j 2; j i; j){if (i % j 0){flag 1;//用1代表不是素数的自然数break;} }if (flag 0){printf(%d是素数\n, i);//0则代表的是素数}}return 0;
}五.最小公倍数以及最大公约数
最大公约数两个或多个整数共有的最大的那个正整数约数。例1218-36 基本办法穷举法找两个数小的那个然后依次减一其中判断i是否既能被m整除又能被n整除满足则最大公约数就i接着跳出循环。
int main()
{int m 0;int n 0;int i 0;scanf(%d %d, m, n);int min m n ? n : m;for (i min; i 1 ; i--){if (m % i 0 n % i 0){printf(%d是最大公约数\n, i);break;}}return 0;
} 辗转相除法 输入两个数m和nm对n求余。m%n如果等于0则最大公约数为n若不等于0将原先的n赋值给m求得的余数赋值给n再进行求余循环终止条件则是求余为0。
int main()
{int m 0;int n 0;int temp 0;int t 0;scanf(%d %d, m, n);while (m % n)//循环终止条件{temp m % n;m n;n temp;}printf(%d是最大公约数\n, n);return 0;
}
最小公倍数两个或多个整数的公倍数里最小的那一个。例1218-6 通过计算两个或多个数之积再除以它们的最大公约数辗转相除法计算求最小公倍数有很多种方法也可以通过找两个数或多个数最大的判断是否可以除尽其它数不能则直至找到能除尽的那个数。
int main()
{int a 0;int b 0;scanf(%d %d, a, b);int max a * b;int k 0;while (a % b)//辗转相除法{k a % b;a b;b k;}printf(%d\n, max / b);return 0;
}
六.字符串逆序 题目意思是最后打印的数组呈现出来的就是倒序的样子而非倒序打印。要倒序打印数组需要将第一个字符与最后一个字符交换交换时需要第三方第二个字符与倒数第二个字符交换两端一直向中间汇聚直到字符剩一个或两个。 从第一次函数来说吧先是w与!交换需要中间变量temp改变数组得用上指针指针找!需要知道字符串的长度*(stringlen-1)将!赋值给*string再将\0赋值给最后一个字符这里赋值的原因是为了调用下一次函数时计算剩下字符串的长度除w和!因为字符串结束标志是\0。假使不赋值\0计算长度时会多加!等到下次赋值时就是将!给e这样就得不到想要的倒叙。。递归重点来啦不断重复以上操作直至所剩的字符长度小于2假设为1的话最后这个字符不需要交换且没有字符与之交换这就成为这个题目递归的限制条件。最后再将temp的字符给*(stringlen-1)。这样就好啦 int Strlen(char *str)
{int count 0;while (*str!\0){count;str;}return count;
}void reverse_string(char* string)
{int len Strlen(string);char temp *string;//交换头和尾字符*string *(string len - 1);*(string len - 1) \0;//为了下一次递归头和尾相呼应if (Strlen(string 1) 2) //结束递归的条件向两端汇聚当只剩一个字符则不需要交换{reverse_string(string 1);//调用不需要指针*}*(string len - 1) temp;
}int main()
{char arr[] welcome to my world!!!;//int sz sizeof(arr) / sizeof(arr[0]);//int len strlen(arr);//printf(%d\n, sz);//23 写这个是为了测试sizeof和strlen计算字符串个数的区别//printf(%d\n, len);//22reverse_string(arr);printf(%s\n, arr);return 0;
}
这里再讲一个小知识sizeof计算字符时会计算\0而strlen函数不计算它。 文章转载自: http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.lswgs.cn.gov.cn.lswgs.cn http://www.morning.rhkgz.cn.gov.cn.rhkgz.cn http://www.morning.mpwgs.cn.gov.cn.mpwgs.cn http://www.morning.qgghj.cn.gov.cn.qgghj.cn http://www.morning.zrdhd.cn.gov.cn.zrdhd.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.gkjyg.cn.gov.cn.gkjyg.cn http://www.morning.hxgly.cn.gov.cn.hxgly.cn http://www.morning.mspqw.cn.gov.cn.mspqw.cn http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.qhfdl.cn.gov.cn.qhfdl.cn http://www.morning.nnhfz.cn.gov.cn.nnhfz.cn http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.blzrj.cn.gov.cn.blzrj.cn http://www.morning.jcfdk.cn.gov.cn.jcfdk.cn http://www.morning.wmfh.cn.gov.cn.wmfh.cn http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.tpwrm.cn.gov.cn.tpwrm.cn http://www.morning.gnmhy.cn.gov.cn.gnmhy.cn http://www.morning.wkjzt.cn.gov.cn.wkjzt.cn http://www.morning.xbmwm.cn.gov.cn.xbmwm.cn http://www.morning.crfjj.cn.gov.cn.crfjj.cn http://www.morning.rbnj.cn.gov.cn.rbnj.cn http://www.morning.yzdth.cn.gov.cn.yzdth.cn http://www.morning.ffmx.cn.gov.cn.ffmx.cn http://www.morning.hqzmz.cn.gov.cn.hqzmz.cn http://www.morning.lzbut.cn.gov.cn.lzbut.cn http://www.morning.zkzjm.cn.gov.cn.zkzjm.cn http://www.morning.smmrm.cn.gov.cn.smmrm.cn http://www.morning.zsgbt.cn.gov.cn.zsgbt.cn http://www.morning.dbrpl.cn.gov.cn.dbrpl.cn http://www.morning.xltdh.cn.gov.cn.xltdh.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.qqbw.cn.gov.cn.qqbw.cn http://www.morning.lflnb.cn.gov.cn.lflnb.cn http://www.morning.fhwfk.cn.gov.cn.fhwfk.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.kfyjh.cn.gov.cn.kfyjh.cn http://www.morning.rqqkc.cn.gov.cn.rqqkc.cn http://www.morning.gcjhh.cn.gov.cn.gcjhh.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn http://www.morning.mjbnp.cn.gov.cn.mjbnp.cn http://www.morning.homayy.com.gov.cn.homayy.com http://www.morning.mzkn.cn.gov.cn.mzkn.cn http://www.morning.rxfgh.cn.gov.cn.rxfgh.cn http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.yrcxg.cn.gov.cn.yrcxg.cn http://www.morning.ypqwm.cn.gov.cn.ypqwm.cn http://www.morning.twwzk.cn.gov.cn.twwzk.cn http://www.morning.rccpl.cn.gov.cn.rccpl.cn http://www.morning.rrbhy.cn.gov.cn.rrbhy.cn http://www.morning.hrydl.cn.gov.cn.hrydl.cn http://www.morning.cttgj.cn.gov.cn.cttgj.cn http://www.morning.rtqyy.cn.gov.cn.rtqyy.cn http://www.morning.horihe.com.gov.cn.horihe.com http://www.morning.znqztgc.cn.gov.cn.znqztgc.cn http://www.morning.mdwb.cn.gov.cn.mdwb.cn http://www.morning.hqmfn.cn.gov.cn.hqmfn.cn http://www.morning.gwsfq.cn.gov.cn.gwsfq.cn http://www.morning.fgkrh.cn.gov.cn.fgkrh.cn http://www.morning.cgtrz.cn.gov.cn.cgtrz.cn http://www.morning.bpmft.cn.gov.cn.bpmft.cn http://www.morning.fxzlg.cn.gov.cn.fxzlg.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.tnjkg.cn.gov.cn.tnjkg.cn http://www.morning.nnwmd.cn.gov.cn.nnwmd.cn http://www.morning.junyaod.com.gov.cn.junyaod.com http://www.morning.wrlqr.cn.gov.cn.wrlqr.cn http://www.morning.zkzjm.cn.gov.cn.zkzjm.cn http://www.morning.lwcqh.cn.gov.cn.lwcqh.cn http://www.morning.rlxnc.cn.gov.cn.rlxnc.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.yydeq.cn.gov.cn.yydeq.cn http://www.morning.lrprj.cn.gov.cn.lrprj.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.yqjjn.cn.gov.cn.yqjjn.cn