您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 程序设计入门-C语言-翁恺
第1页共22页程序设计入门-C语言浙大翁恺循环类型编程源代码1、#includestdio.hintmain(){inta,b;intmin;scanf(%d%d,&a,&b);if(ab){min=a;}else{min=b;}intret=0;inti;for(i=1;imin;i++){if(a%i==0){if(b%i==0){ret=i;}}}printf(%d和%d的最大公约数是%d.\n,a,b,ret);return0;}2、#includestdio.hintmain(){inta,b;intt;scanf(%d%d,&a,&b);intoriga=a;intorigb=b;while(b!=0){t=a%b;a=b;b=t;第2页共22页}printf(%d和%d的最大公约数是%d.\n,origa,origb,a);return0;}3、#includestdio.hintmain(){intx;scanf(%d,&x);intmask=1;intt=x;while(t9){t/=10;mask*=10;}printf(x=%d,mask=%d\n,x,mask);do{intd=x/mask;printf(%d,d);if(mask9){printf();}x%=mask;mask/=10;}while(mask0);printf(\n);return0;}4、#includestdio.hintmain(){intn;inti;doubleret=0.0;scanf(%d,&n);for(i=1;i=n;i++){第3页共22页ret+=1.0/i;}printf(%f\n,ret);return0;}5、#includestdio.hintmain(){intn;inti;doubleret=0.0;intsign=1;scanf(%d,&n);for(i=1;i=n;i++){ret+=1.0*sign/i;sign=-sign;}printf(%f\n,ret);return0;}6、#includestdio.hintmain(){intx;intone,two,five;scanf(%d,&x);for(one=1;onex*10;one++){for(two=1;twox*10/2;two++){for(five=1;fivex*10/5;five++){if(one+two*2+five*5==x*10){printf(可以用%d个1角加%d个2角加%d个5角得到%d元\n,one,two,five,x);break;}}}第4页共22页}return0;}7、#includestdio.hintmain(){intx;intone,two,five;intexit=0;scanf(%d,&x);for(one=1;onex*10;one++){for(two=1;twox*10/2;two++){for(five=1;fivex*10/5;five++){if(one+two*2+five*5==x*10){printf(可以用%d个1角加%d个2角加%d个5角得到%d元\n,one,two,five,x);exit=1;break;}}if(exit==1)break;}if(exit==1)break;}return0;}8、#includestdio.hintmain(){intx;intone,two,five;scanf(%d,&x);for(one=1;onex*10;one++){for(two=1;twox*10/2;two++){for(five=1;fivex*10/5;five++){if(one+two*2+five*5==x*10){第5页共22页printf(可以用%d个1角加%d个2角加%d个5角得到%d元\n,one,two,five,x);gotoout;}}}}out:return0;}9、#includestdio.hintmain(){intx;scanf(%d,&x);inti;intisPrime=1;//x是素数for(i=2;ix;i++){if(x%i==0){isPrime=0;break;}}if(isPrime==1){printf(是素数\n);}else{printf(不是素数\n);}return0;}10、#includestdio.hintmain(){intx;for(x=1;x=100;x++){inti;intisPrime=1;//x是素数第6页共22页for(i=2;ix;i++){if(x%i==0){isPrime=0;break;}}if(isPrime==1){printf(%d,x);}}printf(\n);return0;}11、#includestdio.hintmain(){intx;intcnt=0;x=1;while(cnt50){inti;intisPrime=1;//x是素数for(i=2;ix;i++){if(x%i==0){isPrime=0;break;}}if(isPrime==1){cnt++;printf(%d\t,x);if(cnt%5==0){printf(\n);}}x++;}return0;}12、#includestdio.h第7页共22页intmain(){intsum=0;intcount=0;intnumber;scanf(%d,&number);while(number!=-1){sum+=number;count++;scanf(%d,&number);}doubledsum=sum;printf(Theaverageis%f.\n,dsum/count);return0;}13、#includestdio.hintmain(){intn=3;while(n=0){printf(%d,n);n--;}printf(发射\n);return0;}14、#includestdio.hintmain(){intnumber=rand()%100+1;intcount=0;inta=0;printf(我已经想好了一个1到100之间的数。);do{第8页共22页printf(请猜这个1到100之间数:);scanf(%d,&a);if(anumber){printf(你猜的数大了。);}elseif(anumber){printf(你猜的数小了。);}count++;}while(a!=number);printf(太好了,你用了%d次就猜到了答案。\n,count);return0;}15、//log2x#includestdio.hintmain(){intx;intret=0;scanf(%d,&x);intt=x;while(x1){x/=2;ret++;}printf(log2of%dis%d.,t,ret);return0;}16、#includestdio.h#includestdlib.h#includetime.hintmain(){srand(time(0));inta=rand();第9页共22页printf(%d\n,a%100);return0;}17、#includestdio.hintmain(){intx;scanf(%d,&x);intdigit;intret=0;while(x0){digit=x%10;printf(%d,digit);ret=ret*10+digit;//printf(x=%d,digit=%d,ret=%d\n,x,digit,ret);x/=10;}//printf(%d,ret);return0;}每周编程题目源代码18、#includestdio.hintmain(){inti=0;scanf(%d,&i);printf(%d\n,(i%10)*100+(i%100/10)*10+i/100);return0;}19、#includestdio.hintmain(){inth,m,input,b;scanf(%d,&input);h=input/100;m=input%100;第10页共22页{if(h=8){printf(%d,b=(h-8)*100+m);}else{printf(%d,b=(h+24-8)*100+m);}}return0;}20、#includestdio.hintmain(){intx;scanf(%d,&x);intm,n;m=x/10;n=x%10;switch(n){case1:printf(Faintsignals,barelyperceptible);break;case2:printf(Veryweaksignals);break;case3:printf(Weaksignals);break;case4:printf(Fairsignals);break;case5:printf(Fairlygoodsignals);break;case6:printf(Goodsignals);break;case7:printf(Moderatelystrongsignals);break;case8:printf(Strongsignals);break;case9:printf(Extremelystrongsignals);break;default:printf(worng);break;}printf(,);switch(m){case1:printf(unreadable);break;case2:printf(barelyreadable,occasionalwordsdistinguishable);break;case3:printf(readablewithconsiderabledifficulty);break;case4:printf(readablewithpracticallynodifficulty);break;case5:printf(perfectlyreadable);break;default:printf(worng);break;}printf(.);return0;}21、#includestdio.hintmain(){第11页共22页intx;inta=0,b=0;while(scanf(%d,&x)){if(x==-1)break;if(x%2==0)a++;elseb++;}printf(%d%d,b,a);return0;}22、#includestdio.hintmain(){intx;scanf(%d,&x);intans=0,k=1,cnt=1;while(x){inttemp;if(x%2==k)temp=1;elsetemp=0;//printf(%d,temp);ans=ans+temp*cnt;k=1-k;x/=10;cnt*=2;}printf(%d\n,ans);return0;}23、#includestdio.h#includemath.hinta[100000];intisprime(intx){if(x==2||x==3||x==5||x==7)return1;for(inti=2;i*i=x;i++)if(x%i==0)return0;return1;}intmain(){intn,m;scanf(%d%d,&n,&m);intk=0;第12页共22页for(inti=2;k=m;i++){if(isprime(i)==1)a[k++]=i;}intsum=0;for(inti=n-1;im;i++){sum+=a[i];}printf(%d\n,sum);return0;}24、#includestdio.
本文标题:程序设计入门-C语言-翁恺
链接地址:https://www.777doc.com/doc-5611048 .html