C中級(jí)實(shí)戰(zhàn)題目解答_第1頁(yè)
C中級(jí)實(shí)戰(zhàn)題目解答_第2頁(yè)
C中級(jí)實(shí)戰(zhàn)題目解答_第3頁(yè)
C中級(jí)實(shí)戰(zhàn)題目解答_第4頁(yè)
已閱讀5頁(yè),還剩47頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

C語(yǔ)言程序設(shè)計(jì)中級(jí)實(shí)戰(zhàn)題目解答馬俊編寫(xiě)和整理計(jì)算太陽(yáng)和地球之間的萬(wàn)有引力。解答:F=G(ml*m2)/R2 G=6.67259*10"m3s2kg1ml=l.987E30(kg)m2=5.975E24(kg)R=1.495E11(m)/* 程序grav.c計(jì)算太陽(yáng)和地球之間的萬(wàn)有引力floatgrav(floatml,floatm2,floatdistance){floatg,G=6.67259E-11;g=G*ml*m2/(distance*distance);returng;}main(){floatg,Msun=L987E30,Mearth=5.975E24;g=grav(Msun,Mearth,1.495E11);printf〈Thegravitationbetweensunandearthis%gNewton.\n,g);)2、試用計(jì)算機(jī)驗(yàn)證歌德巴赫猜想,即任何ー個(gè)偶數(shù)可分解為兩個(gè)素?cái)?shù)之和1+1=2分析:假設(shè)我們要驗(yàn)證10000以?xún)?nèi)的偶數(shù),首先將10000個(gè)數(shù)放在ー個(gè)數(shù)組中,然后將所有的合數(shù)去除掉(用零代替),循環(huán)從4到10000開(kāi)始遍歷所有的偶數(shù),測(cè)試毎ー個(gè)偶數(shù)是否可以分解為這個(gè)數(shù)組中兩個(gè)非零數(shù)相加。File:goldbuch.cAuthor:馬俊Time:2006-10-19function:TotestthesupposeofGOLDBUCH#include<stdio.h>ttdefineM10001voidCreatePrimeList(intPrimeList[])(inti,j;for(i=0;i<M;i++)PrimeList[i]=i;i=2;while(i<M/2){for(j=i+l;j<M;j++)if(PrimeList[j]!=0&&PrimeList[j]%PrimeList[i]==0)PrimeList[j]=0;i=NextPrimeNumber(i,PrimeList);intNextPrimeNumber(intp,intPrimeList[]){p++;while(PrimeList[p]==0)p++;returnPrimeList[p];)voidtest(intPrimeList[])(intx,p;x=4;while(x<M)(p=PrimeList[2];while(pくM/2&&PrimeList[x-p]ニニ〇)p=NextPrimeNumber(p,PrimeList);if(p>M/2)printf(^Greatdiscovery:Goldbachiswrong!\n");elseprintf("Theevennumber%d=%d+%d\n",x,p,x-p);x=x+2;}}voidmain()(intPrimeList[M];CreatePrimeList(PrimeList);test(PrimeList);getchO;}3、輸入一個(gè)年份,打印年歷分析:1)我們?nèi)绻滥骋荒暝┦切瞧趲?則這一年每一天是星期幾就定下來(lái)了。2)我們知道1900年的元旦是星期ー。3)每年是365天=52*7+1,閏年多一天4)4年中會(huì)有一個(gè)閏年,閏年的判斷條件是:year%4==0&&year%100!=01|year%400==05)4、6、9、11月30天,3、5、7、8、10、12月31天,2月來(lái)處理閏月File:Calender,cAuthor:馬俊Time:2006-10-19Function:PrinttheCalender#include<stdio.h>SdefineYES1#defineNO0intisleap(intyear){intleap=N0;if(year%4=;=0&&year%100!=01Iyear%400==0)leap=YES;returnleap;)intweek_of_newyears_day(intyear)(intn=year-1900;n=n+(n-l)/4+1;n=n%7;returnn;)voidprint(intyear){intmonth,day,weekday,len_of_month,i;printf("ヽn\n Thecalenderoftheyear%d\n〃,year);weekday=week_of_newyears_day(year);for(month=l;month<=12;month++)(printf(*\nThemonthof%d\n*,month);printf(* \n〃);printfCSUNMONTUEWEDTHUFRISAT\n〃);printf(" \n");for(i=0;i<weekday;i++)printf("つ;if(month二二41Imonthニニ61Imonthニニ91Imonth=ll)len_of_month=30;elseif(month==2)(if(isleap(year))1en_of_month=29;elselen_of_month=28;}elselen—Of_inonth^^l;for(day=l;day<=len_of_month;day++)(printf(*%2d”,day);weekday++;if(weekday==7)weekday=O;printf(*\n*);)}printf("\n\n");}}main(){intyear;printf('\nPleaseinputyear:;scanf&year);print(year);getchO;}4、數(shù)學(xué)類(lèi)函數(shù)、常數(shù)的計(jì)算及矩陣運(yùn)算。a)計(jì)算e,“的值b)編寫(xiě)sin(x),sqrt(x),cos(x),arcsin(x),ln(x)的計(jì)算程序c)編寫(xiě)矩陣運(yùn)算程序分析:1)運(yùn)用我們學(xué)過(guò)的數(shù)學(xué)知識(shí),我們可以得到計(jì)算e和n的近似計(jì)算公式,(泰勒級(jí)數(shù)Pn(x)=f(xo)+f'(x(>)(x-xo)+f''(x())/2!(x-xo”+…,其中x(>=0為麥克勞林級(jí)數(shù))n/4^1-l/3+l/5-l/7+—e=l+l/l!+1/2!+1/3!+1/4!+-+l/n!+-2)Xn+1=(Xn+a/xn)/2(牛頓迭代法求sqrt(a))sin(x)=x-x3/3!+x5/5!-,,,+(-l)nx2n+1/(2n+l)!+,,,cos(x)=l-x2/2!+x4/4!~?,,+(-1)"x2"/(2n)!+…arctg(x)=x-x3/3+x5/5--arcsin(x)=x+l/2*(x3/3)+(1*3)/(2*4)*x'/5+…In(1+x)=x-x2/2+x>3-…+(T)IrixI7n+…3)矩陣對(duì)應(yīng)于二維數(shù)組,根據(jù)數(shù)學(xué)理論矩陣A=B+C為對(duì)應(yīng)元素之和,A=B*C,A的每個(gè)元素等于B的行?C的列之和。pi.c求pi的值^include<stdio.h>#include<math.h>main(){ints;floatn,t,pi;t=l;pi=0;n=l.0;s=l;while(fabs(t)>le-6)|pi=pi+t;n=n+2;s=~s;t=s/n;)pi=pi*4;printf(*pi=%10.6f\n",pi);getchO;}e.c求自然數(shù)e的近似值#include<stdio.h>#include<math.h>mainO(doublee=l.0;doubleu=l.0;intn=l;while(u>l.0e-6){u=u/n;e=e+u;n=n+l;)printf("\ne二%If(n二%d)\n”,e,n);getchO;}Filename:mysqrt.cauthor:majuntime:2008-1-21#include<stdio.h>#include<math.h>voidmain()floatxl,x2,a;printf(''pleaseinputanumber:");scanf("%f",&a);x2=a;do{xl=x2;x2=(xl+a/xl)/2;}while(fabs(x2-xl)>1.0e-5);printf("mysqrt(%f)=%f\n",a,x2);printf("sqrt(%f)=%f\n",a,sqrt(a));getchO;)Filename:mysin.cauthor:majuntime:2008-1-21#include<stdio.h>^include<math.h>voidmain()(floatx,sinx,t;intn=l,jd;/*printf("pleaseinputx:");scanf("%f",&x);*/printf("pleaseinputaangle:");scanf("%d",&jd);x=jd*3.1415926/180.0;sinx=0.0;t=x;do(sinx=sinx+t;t=(-1)*t*x*x/(n+1)/(n+2);n=n+2;}while(fabs(t)>l.0e-6);printf("mysin(%d)=%f\n",jd,sinx);printf(*sin(%d)=%f\n",jd,sin(x));getchO;其他省略!Filename:matrix.cauthor:majuntime:2008-1-21#include<stdio.h>#include<math.h>voidmatrix_add(doublea[],doubleb[],doublec[],intm,intn)(inti,j;for(i=0;i<m;i++){for(j=0;j<n;j++)(a[i*n+j]=b[i*n+j]+c[i*n+j];voidmatrixdisp(doubleaロ,intm,intn){inti,j;for(i=0;i<m;i++)(for(j=0;j<n;j++)(printfC%6.2f*,a[i*n+j]);)printf("\n");voidmatrix_multi(doublea[],doubleb[],doublec[],int1,intm,intn)(inti,j,k;for(i=0;i<l;i++)for(j=0;j<n;j++)(c[i*n+j]=0;for(k=0;k<m;k++)c[i*n+j]+=a[i*m+k]*b[k*n+j];voidmain()(doublea[4][5]={1.0,3.0,~2.0,0.0,4.0),{-2.0,-1.0,5.0,_7.0,2.0},{0.0,8.0,4.0,1.0,-5.0},{3.0,-3.0,2.0,-4.0,1.0});doubleb[5][3]={{4.0,5.0,-1.0),{2.0,-2.0,6.0},{7.0,8.0,1.0},{0.0,3.0,-5.0},{9.0,8.0,-6.0}};doublec[4][3];inti,j;matrixmulti(*a,*b,*c,4,5,3);printfC"TheresultisC=\n");matrix_disp(*a,4,5);matrixdisp(*b,5,3);matrix_disp(*c,4,3);getchO;}其他省略!5、排序算法1)、冒泡排序2)、插入排序3)、選擇排序4)、希爾排序Filename:mysort.cAuthor:馬俊#include<stdio.h>#include<stdlib.h>voidbubble_sort(intlist[],intcount){inti,j,tmp;for(i=0;i<count;i++){for(j=count-l;j>i;j=j-l)(if(list[j-l]>list[j])(tmp=list[j-1];list[j-l]=list[j];voidshell_sort(intlist[],intcount)(intexchange,i,tmp,gap=count;while(gap>l)/?每循環(huán)一次,步長(zhǎng)減為原來(lái)的一半?/{gap=gap/2;do/?循環(huán)至在當(dāng)前步長(zhǎng)時(shí)無(wú)需再交換?/{exchange=O;for(i=0;i<count-gap;i=i+l){if(list[i]>list[i+gap])(tmp=list[i];list[i]=list[i+gap];list[i+gap]=tmp;exchange=l;)}}while(exchange!=0);voidselect_sort(intlist[],intcount){inti,j,k,tmp;for(i=0;iくcountT;i++){k=i;for(j=i+l;j<count;j++)if(list[j]<list[k])k=j;if(k!=i)tmp=list[i];list[i]=list[k];list[k]=tmp;voidinsert_sort(intlist[],intcount)inti,j,tmp;for(i=l;i<count;i++){tmp=list[i];for(j=i-l;tmp<list j—)list[j+l]=list[j];list[j+l]=tmp;voidmain(){inti,a[10];randomize();for(i=0;i<10;i++)a[i]=random(100);for(i=0;i<10;i++)printf("%4d",a[i]);insert_sort(a,10);printf('\n");for(i=0;iく10;i++)printf("%4d",a[i]);getchO;)6、文件操作(顯示、拷貝、加密、解密)/*mytype.c*/#include<stdio.h>voidmain(intargc,char*argv[])(FILE*fin,*fout;charch;if(argc!=2){printf("Usage:mytypefilelfile2\n");exit(0);}if((fin=fopen(argv[1],"r"))ニニNULL){printf("Openinputfileerror!\n");exit(0);)while((ch=fgetc(fin))!=-l)putchar(ch);fclose(fin);)/*mycopy.c*/#include<stdio.h>voidmain(intargc,char*argv[])(FILE*fin,*fout;charch;if(argc!=3){printf(*Usage:mycopyfilelfile2\n");exit(0);}if((fin=fopen(argv[l],"r"))==NULL){printf(*0peninputfileerror!\n");exit(0);)if((fout=fopen(argv[2],"w"))ニニNULL){printf("Openoutputfileerror!\n");exit(0);}ch二fgetc(fin);while(!feof(fin)){fputc(ch+10,fout);ch二fgetc(fin);)fclose(fin);fclose(fout);}其他省略7、基于菜單的小型職エ檔案管理系統(tǒng)employee.h職エ檔案管理的數(shù)據(jù)類(lèi)型定義^include<dos.h>#defineMANAGER0#defineTECHNICIAN1^defineWORKER2#defineMALE0#defineFEMALE1#defineEMPLOYEE_TYPEstructemployee_type#defineMAX_EMPLOYEE100structmanager_type(charpositional]; /?職務(wù)?/charrank[21]; /*級(jí)別?/};structtechnician_type(chartech_post[11];/*職稱(chēng)*/chardegree[11]; /?學(xué)位?/charmajor[11]; /?專(zhuān)業(yè)?/);structworkertype{charspeciality[ll]; /?工種?/intrank; /*級(jí)別?/chareducation[11]; /?文化程度?/};unionsorttypestructmanager_typem;/*管理人員數(shù)據(jù)?/structtechniciantypet;/?技術(shù)人員的數(shù)據(jù)?/structworker_typew;/?工人的數(shù)據(jù)?/};structemployee_type{charname[9];intsex;structdatebirthdate;inttype;unionsorttyped; /?不同類(lèi)型職エ的分類(lèi)數(shù)據(jù)?/);intcounts;EMPLOYEETYPE*pemp[MAXEMPLOYEE];/*empmenu,c*/#include<conio.h>#include<stdio.h>#include<bios.h>#include<dos.h>#include''employee,h”#defineKEY_ESC27#defineKEY_Enter13#defineKEY_Up0x4800#defineKEY_Down0x5000#defineKEY_Left0x4b00#defineKEY_Right0x4d00#defineKEY_PGUP0x4900#defineKEY_PGDN0x5100#defineKEY_INS0x5200ftdefineKEY_DEL0x5300#defineKEY_F10x3b00EMPLOYEE_TYPEempl;char*filename="emp.dat”;voidreadfromfileO;voidwritetofileO;intputinfo(intcol,introw,charinfo[])(structtext_infor;charblock[1000];intkey;gettextinfo(&r);gettext(col,row,col+41,row+4,block);textcolor(YELLOW);textbackground(RED);window(col+l,row+1,col+40,row+3);clrscr();textbackground(CYAN);window(col,row,col+39,row+2);clrscr();puts(〃! 1つ;gotoxy(1,2);puts("I");gotoxy(39,2);puts(*I*);gotoxy(1,3);putsC' 1");gotoxy(3,2);textcolor(WHITE);cputs(info);key=getch();window(r.winleft,r.wintop,r.winright,r.winbottom);puttext(col,row,col+41,row+4,block);returnkey;voidpop_up_win(intx,inty,intwidth,intheight,intcolor,intbk)(inti;textbackground(BLACK);window(x+1,y+1,x+width,y+height);clrscr();textbackground(bk);window(x,y,x+width-1,y+height-1);clrscr();window(l,1,80,25);textcolor(color);gotoxy(x,y);cputs(〃「“);for(i=0;i<(width-4)/2;i++)cputsC—");cputs("n");for(i=0;i<height-2;i++){gotoxy(x,y+l+i);cputs("|");gotoxy(x+width-2,y+l+i);cputs("|");};gotoxy(x,y+2);cputs(?卜”);for(i=0;iく(width-4)/2;i++)cputs("一つ;cputs(〃T〃);gotoxy(x,y+height-1);cputsCし);for(i=0;i<(width-4)/2;i++)cputs(z/—*);cputs(〃」〃);)unionkeycodetype{intword;charbyte;};typedefstruct|chartitle[41];}MENUITEM;intget_key(void)(unionkeycode_typekey;key.word=bioskey(0);returnkey.byte?key.byte:key.word;)voiddisp_item(intx,inty,inti,chartitle[],intbk)(textcolor(YELLOW);textbackground(bk);gotoxy(x+2,y+3+i);cputs(title);}voidprint_info(EMPLOYEE_TYPE*emp)printf(*\n \n");printf("姓名:%s\n",emp->name);printf("性別:%s\n”,(empー)sex==0?"男":"女"));printf(" 出 生 日 期%4d-%02d-%02d\n",emp->birthdate.dayear,emp->birthdate.da_mon,emp->birthdate.da_day);switch(emp->type)(caseMANAGER:printf("職務(wù):%s\n",emp->d.m.position);printf("級(jí)別:%s\n",emp->d.m.rank);break;caseTECHNICIAN:printf("職稱(chēng):%s\n*,emp->d.t.tech_post);printf("學(xué)位:%s\n",emp->d.t.degree);printf("專(zhuān)業(yè):%s\n",emp->d.t.major);break;caseWORKER:printf("エ種:%s\n",emp->d.w.speciality);printf(“級(jí)別:%d\n",emp->d.w.rank);printf("文化程度:%s\n",emp->cation);break;default:printf("職エ類(lèi)型不明確,無(wú)法顯示分類(lèi)數(shù)據(jù)!'n");)printf(* \n");)EMPLOYEETYPEinput_info(void){charblock[8000];/*保存屏幕區(qū)域(25,80)的數(shù)組*/EMPLOYEE_TYPEemp;intx=10,y=5,w=60,h=15;gettext(x,y,x+w+1,y+h+1,block);pop_up_win(x,y,w,h,WHITE,MAGENTA);textcolor(YELLOW);gotoxy(x+(w-16)/2,y+1);cputs("職エ檔案錄入模塊"ンgotoxy(x+2,y+3);cputs("姓名:*);gotoxy(x+24,y+3);cputs("性別(0一男,1一女):");gotoxy(x+2,y+4);cputs("出生日期:“);gotoxy(x+24,y+4);cputs("類(lèi)別(0ー管理,1ー技術(shù),2ー工人):");gotoxy(x+12,y+3);scanf("%8s",);gotoxy(x+41,y+3);scanf("%ld",&(emp.sex));gotoxy(x+12,y+4);scanf("%4d-%2d-%2d",&(emp.birthdate,dayear),&(emp.birthdate,da_mon),&(emp.birthdate.da_day));gotoxy(x+52,y+4);scanf("%ld",&(emp.type));switch(emp.type)(caseMANAGER:gotoxy(x+2,y+6);cputs("職務(wù):");gotoxy(x+2,y+7);cputs("級(jí)別:");gotoxy(x+12,y+6);cscanf("%10s",emp.d.m.position);gotoxy(x+12,y+7);cscanf("%10s",emp.d.m.rank);break;caseTECHNICIAN:gotoxy(x+2,y+6);cputs("職稱(chēng):");gotoxy(x+2,y+7);cputs("學(xué)位:");gotoxy(x+2,y+8);cputs("專(zhuān)業(yè):");gotoxy(x+12,y+6)jcscanf("%10s^,emp.d.t.tech_post);gotoxy(x+12,y+7);cscanfemp.d.t.degree);gotoxy(x+12,y+8);cscanf(%10s",emp.d.t.major);break;caseWORKER:gotoxy(x+2,y+6);cputs("エ種:");gotoxy(x+2,y+7);cputs("級(jí)別:");gotoxy(x+2,y+8);cputs("文化程度:");gotoxy(x+12,y+6);cscanf(〃%10s,emp.d.w.speciality);gotoxy(x+12,y+7);cscanfし%Id”,emp.d.w.rank);gotoxy(x+12,y+8);cscanf("%10s",cation);break;)puttext(x,y,x+w+1,y+h+1,block);counts++;returnemp;)voidprint_all()(inti;for(i=0;iくcounts;i++)(if(pemp[i]!=NULL)print_info(pemp[i]);(getchO;)intmenu(intx,inty,intwidth,chartopic[],MENU_ITEMitem[],intn)(charblock[4000];intheight=n+4;inti,key;gettext(x,y,x+width+1,y+height+1,block);popupwin(x,y,width,height,WHITE,CYAN);textcolor(YELLOW);gotoxy(x+(width-strlen(topic))/2,y+1);cputs(topic);for(i=0;i<n;i++)disp_item(x,y,i,item[i].title,CYAN);i=0:

disp_item(x,y,i,item[i].title,MAGENTA);while((key=get_key())!=KEY_ESC)(disp_item(x,y,i,item[i].title,CYAN);switch(key)(caseKEY_Enter:switch(i)(case0:putinfo(20,15,empl=input_info();printinfo(&empl);writetofile(empl);readfromfile();getchO;break;putinfo(20,15,readfromfileO;print_all();break;putinfo(20,15,”/z);break;putinfo(20,15,*putinfo(20,15,〃");clrscr();exit(0);}break;caseKEY_Up:i=i-0?n-l:i-l;break;caseKEY_Down:i=i==n-l?0:i+1;break;}disp_item(x,y,i,item[i].title,MAGENTA);}puttext(x,y,x+width+1,y+height+1,block);)voidreadfromfile(){FILE*fp;inti;if((fp=fopen(filename,"rb"))!=NULL)(fread(ftcounts,sizeof(int),1,fp);新職工檔案入庫(kù)”);職エ情況統(tǒng)計(jì)分析表”);按姓名查詢(xún)職エ檔案打新職工檔案入庫(kù)”);職エ情況統(tǒng)計(jì)分析表”);按姓名查詢(xún)職エ檔案打印某職エ檔案”);break;退出pemp[i]=(EMPLOYEE_TYPE*)malloc(counts*sizeof(EMPLOYEETYPE));if(pemp[i]!=NULL)(fread(pemp[i],sizeof(EMPLOYEE_TYPE),1,fp);}else(printf("內(nèi)存分配失敗!\n");exit(1);)}}else(counts-0;)fclose(fp):)voidwritetofile(EMPLOYEETYPEemp)(FILE*fp;inti;if((fp=fopen(filename,"wb"))!=NULL)(fwrite(&counts,sizeof(int),1,fp):if(counts==l)(fwrite(&emp,sizeof(EMPLOYEE_TYPE),1,fp);)else(for(i=0;i<counts;i++)(if(pemp[i]!=NULL)(if(strcmp(pemp[i]->name,)<0){fwrite(pemp[i],sizeof(EMPLOYEETYPE),1,fp);}}else{fwrite(&emp,sizeof(EMPLOYEE_TYPE),1,fp);else(printf("文件打開(kāi)失敗!\n");exit(3);}fclose(fp);)voidmain(void){staticMENU_ITEMmenu_item[]={TOC\o"1-5"\h\z" 新職エ檔案入庫(kù) "," 職エ情況統(tǒng)計(jì)分析表 "," 按姓名查詢(xún)職エ檔案 "," 打印某職エ檔案 "," [ESC]退出本系統(tǒng) ",);textbackground(BLUE);readfromfileO;clrscr();menu(20,3,40,”職工人事檔案管理系統(tǒng)",menu_item,5);if(pemp!=NULL)free(pemp);)其他程序請(qǐng)同學(xué)們補(bǔ)充完整!8、找瓶子問(wèn)題:有十二個(gè)從同一個(gè)模子里生產(chǎn)出來(lái)的瓶子,但發(fā)現(xiàn)有1個(gè)瓶子的質(zhì)量和其他十一個(gè)不同,現(xiàn)給你ー個(gè)天平,有求只使用3次,找到那只次瓶子,并盡可能告訴瓶子的輕重。分析建模與思路:分三組:每組四個(gè),第一組編號(hào)14,第二組5-8,第三組9-12.第一次稱(chēng):天平左邊放第一組,右邊放第二組。A第一種可能:平衡。則不同的在第三組。接下來(lái)可以在左邊放第9、10、11號(hào),右邊放1、2、3號(hào)三個(gè)正常的。a.如果平衡,則12號(hào)是不同的;再找ー個(gè)正常瓶子與12號(hào)相比,可判斷出12號(hào)瓶子的輕重b.如果左重右輕,則不同的在9、10、11號(hào)中,而且比正常球重。再稱(chēng)一次:9放左邊,10放右邊,如果平衡,則11號(hào)是不同的;如果左重右輕,則9號(hào)是不同的,如果右重左輕,則10號(hào)是不同的。c.如果左輕右重,道理同bB第二種可能:左重右輕,則不同的在1-8號(hào)中。第二次稱(chēng):左邊放1、2、5號(hào),右邊放6、9、3號(hào)。a.如果平衡。則不同的在4、7、8中。可以稱(chēng)第三次:左邊放4、7,右邊放9、10。如果平衡,則8是不同,8是輕的;如果左重右輕,則4是不同,4是重的;如果左輕右重,則7是不同,7是輕的。b.仍然左重右輕。則不同的在位置沒(méi)有改變的1、2、6中??梢苑Q(chēng)第三次:左邊放1、6,右邊放9、10。如果平衡,則2是不同,2是重的;如果左重右輕,則1是不同,1是重的;如果左輕右重,則6是不同,6是輕的。c:左輕右重。則不同的在5、3、中,因?yàn)橹挥兴鼈兏淖兞嗽瓉?lái)的位置??梢苑Q(chēng)第三次:左放5,3,右放9,10。如果左輕右重,貝リ5是不同,5是輕的,如果左重右輕,則3是不同,3是重的。C第三種可能:左輕右重,道理同B用ー個(gè)ー維數(shù)組來(lái)模擬瓶子,然后用隨機(jī)數(shù)產(chǎn)生次瓶子。#include<stdio.h>#include<stdlib.h>#include<math.h>#include<time.h>intmain()(intb,i,a[13];inty0,yl;a[0]=0;randomize();y0=random(20);yl=(y0<10)?(y0-l0):(y0-9);for(i=l;i<=12;i++)a[i]=0;do{i=random(l3);}while(i==0);/*i=(int)rand()*l1/RAND_MAX;b=rand()%13;*/a[i]=yl;/?第一種可能:平衡。則不同的在第三組ワif(a[1]+a[2]+a[3]+a[4]=a[5]+a[6]+a[7]+a[8]){if(a[9]+a[l0]+a[11J==a[1]+a[2]+a[3])(if(a[1]>a[12])printf(nThedifferentbottleisnum12!num12islight!\nu);elseprintf(uThedifferentbottleisnum12!num12isheavy!\nn);)printf(nThedifferentbottleisnum12!\nM);if(a[9]+a[l0]+a[ll]>a[l]+a[2]+a[3]){if(a[9]==a[10])printf("Thedifferentbottleisnumll!num11isheavy!\nn);heavy!\nH);heavy!\nn);if(a[9]>a[10])printf("Thedifferentif(a[9]<a[10])printf(MThedifferent)if(a[9]+a[l0]+a[ll]<a[l]+a[2]+a[3])bottleisbottleisnum9!num9isnum10!num10islight!\nn);{if(a[9]==a[10])printf(MThedifferentbottleisnum11!num11islight!\nn);if(a[9]>a[10])printf("Thedifferentbottleisnum10!num10islight!1");if(a[9]<a[10])}printf("Thedifferentbottleisnum9!num9is/*第二種可能:左重右輕,則不同的在1?8號(hào)中,但不知比正常的輕還是重?/if(a[l]+a[2]+a[3]+a[4]>a[5]+a[6]+a[7]+a[8]){if(a[l]+a[2]+a[5]=a[6]+a[9]+a[3])(if(a[4]+a[7]=a[9]+a[10])printf(MThedifferentbottleisnum8!num8islight!\nn);if(a[4]+a[7]>a[9]+a[10])printf(MThedifferentbottleisnum4!num4isheavy!\nn);if(a[4]+a[7]<a[9]+a[10])printf(,,Thedifferentbottleisnum7num7islight!\n!H);1if(a[1]+a[2]+a[5]>a[6]+a[9]+a[3]){if(a[1]+a[6]==a[9]+a[10])printf("Thedifferentbottleisnum2!num2isheavy!\n");if(a[l]+a[6]>a[9]+a[10])printf(nThedifferentbottleisnum1!num1isheavy!\nM);if(a[1]+a[6]<a[9]+a[10])printf(nThedifferentbottleisnum6!num6islight!\nM);}if(a[1]+a[2]+a[5]<a[6]+a[9]+a[3])(if(a[5]+a[3]>a[9]+a[10])printf(nThedifferentbottleisnum3!num3isheavy!\nM);if(a[5]+a[3]<a[9]+a[10])printf(,,Thedifferentbottleisnum5!num5islight!\n");)}/*第三種可能:左輕右重,則不同的在1-8號(hào)中,但不知比正常的輕還是重?/if(a[1]+a[2]+a[3]+a[4]<a[5]+a[6]+a[7]+a[8]){if(a[1]+a[2]+a[5]=a[6]+a[9]+a[3])if(a[4]+a[7J==a[9]+a[10])printf("Thedifferentbottleisnum8!num8islighten");if(a[4]+a[7]>a[9]+a[10])printf(wThedifferentbottleisnum4!num4isheavy!\nM);if(a[4]+a[7]<a[9]+a[10])printf(MThedifferentbottleisnum7!num7islighten");)if(a[l]+a[2]+a[5]>a[6]+a[9]+a[3])(if(a[1]+a[6]==a[9]+a[10])printf("Thedifferentbottleisnum2!num2isheavy!\nH);if(a[l]+a[6]>a[9]+a[10])printf("Thedifferentbottleisnuml!num1isheavy!\nM);if(a[1]+a[6]<a[9]+a[10])printf(MThedifferentbottleisnum6!num6islight!\nM);)if(a[1]+a[2]+a[5]<a[6]+a[9]+a[3])(if(a[5]+a[3]>a[9]+a[10])printf(nThedifferentbottleisnum3!num3isheavy!\nH);if(a[5]+a[3]<a[9]+a[10])printf(nThedifferentbottleisnum5!num5islight!\n");))getchar();)9、分析:定義ー個(gè)結(jié)構(gòu)體來(lái)保存員エ信息,使用結(jié)構(gòu)的指針來(lái)傳遞結(jié)構(gòu)體變量的地址。注意輸入數(shù)據(jù)格式的控制!/?頭文件?/#include<stdio.h>#defineN3/?結(jié)構(gòu)定義★/typedefstruct{intdd;intmm;intyy;}DATE;typedefstruct(intidno;charname[40];DATEdob;floatsalary;intdependents;}EMPLOYEE;voiddisplay__employee_details(EMPLOYEEemployee){printf(HName:吿s\n”,);printf(nIdNo:%d\nn,employee.idno);printf(nDateOfBirth:%d/%d/%d\nH,employee.dob.dd,employee.dob.mm,employee.dob.yy);printf(nSalary:%.2f\nn,employee.salary);printf(nDependents:%d\n\nH,employee.dependents);voidread_employee_details(EMPLOYEE*employee){if(employee==NULL)return;printf(n\tEnterIDNo:");scanf("%dn,&employee->idno);printf(n\tEnterName:H);scanf("%s'?zemployee->name);do{printf(n\tEnterDateofbirth:\n");printf(n\t\tDay[1-31]:?');scanf(%d"zaemployee->dob.dd);}while(employee->dob.dd<l||employee->dob.dd>31);do{printf(n\t\tMonth[1-12]:");scanf("%d",&employee->dob.mm);}while(employee->dob.mm<l||employee->dob.mm>12);do{printf(n\t\tYear[1850-2002]:n);scanf("%dn,&employee->dob.yy);}while(employee->dob.yy<1850||employee->dob.yy>2002);do{printf(n\tEnterSalary:n);scanf("%f",&employee->salary);}while(employee->salary<0);do{printf(n\tEnterNoofdependents:");scanf('*%d"z&employee->dependents);}while(employee->dependents<0);}voidincrease_salary(EMPLOYEE*employee,intempID,floatpercentIncrease){employee->salary=employee->salary+employee->salary*percentIncrease/100;)main(){EMPLOYEEemployee[N];inti,id=0;floatpercent=0.Of;intfound=0;for(i=0;i<N;i++)(printf(nEnterdetailsforemployee%d\n”,(i+1));read_employee_details(&employee[i]);)printf("DisplayingEmployeeDetails:\n");for(i=0;i<N;i++)display_employee_details(employee[i]);printf("IncreaseinSalary:\n");printf("EnteremployeeID:");scanf("%d",&id);printf("Enterpercentincrease:");scanf("%f"z&percent);found=0;for(i=0;i<N;i++)(if(employee[i].idno==id)(found=1;increase_salary(&emp1oyee[i],id,percent);printf("Newsalaryfor%sis%.2f\n",\employee[i].name,employee[i].salary);})if(found==0)printf("NoemployeewithID%dfound\n",id);getch();}I。、(順序執(zhí)行各種任務(wù)的完整C語(yǔ)言程序)/?假設(shè):.有效的字母和數(shù)字,以及、」、、?,、、!,、、$,、、%,、'*,、、&,、、(,、、)‘、、0’、、+‘、'['、'],、'{’、'}‘、ッ'、'I、和',,均被視作字符。其它特殊字符不可視作字符。.數(shù)據(jù)的格式應(yīng)該是dd/mm/yyyy.如果兩個(gè)相同單詞中的字符大小寫(xiě)不同,那么應(yīng)該視為不同的單詞.句子的結(jié)束可以用'.,、'\n,、'\L或'?,表示..輸入文件是input.txt.輸出文件是output.txt.文本文件包含的單詞數(shù)少于1000個(gè)*//?頭文件?/#include<stdio.h>#include<stdlib.h>#include<string.h>#include<ctype.h>/?全局變量?/intcharCount=0,wordCount=0,sentenceCount=0;/?函數(shù)原型?/intcountcharacters(charch);intcountwords(charch);intcountsentences(charch);intcountcharacters(charch){if(isalnum(ch)||ch==?.'||ch==*?1||\ch==?!?IIch==,$*IIch=='%>IIch=='*'\II ch==1&? I I ch==?(? I I ch==1) ? I I ch==1-'\TOC\o"1-5"\h\zII ch==1+' I I ch=='[' I I ch==1] 1 I I ch==1{, \II ch=='}1 I I ch=='/' I I ch=='\\' I Ich==?|' \II ch==,\?? I Ich==';' I Ich==*:' I Ich==*,' \IIch==,\'?){charCount++;return(1);}return(0);intcountwords(charch){staticcharprevChar;staticintprevValidChar=O;if(prevValidChar==l&&(ch=='*||ch==?\t'\IIch==1\n'IIch=='\r'||ch==1.?||ch==???\IIch=='!')){prevValidChar=O;wordCount++;prevChar=ch;return(1);}if(isalnum(ch)||ch=='$'||ch=='%'||ch=='*'\II ch==?&' I I ch==*(' I I ch==*) * I I ch=='-'\TOC\o"1-5"\h\zII ch==1+* I I ch=='[' I I ch==*] * I I ch=='{' \II ch=='}* I I ch=='/? I I ch==*\\* I Ich==*|1 \II ch==>\"? I Ich==*;* I Ich==':? I Ich==*,* \IIch=='\''){prevChar=ch;prevValidChar=l;}return(0);intcountsentences(charch){staticcharprevValidChar=?a';staticcharprevChar=?a?;iff!(prevValidChar==*.*||prevValidChar==*?*||\prevChar=='\n')&&(ch=='.'||ch=='\n'||\ch=='\r?IIch==1?')){prevValidChar=ch;prevChar=ch;sentenceCount++;return(1);}if(isalnum(ch)||ch==*.*||ch=='?'||\ch==?!*IIch=='$'IIch==*%'IIch==**'\II ch=='&? I I ch==?(' I I ch==1) * I I ch=='-'\TOC\o"1-5"\h\zII ch=='+* I I ch==?[? I I ch=='] ? I I ch=='{? \II ch==*}' I I ch=='/' I I ch==?\\' I Ich==?|' \II ch=='\n1 I Ich==,;* I Ich==':1 I Ich==',' \IIch==?\??){prevValidChar=ch;}prevChar=ch;return(0);}main(){char*inp_file=*?input.txt,,;char*out_file=Houtput.txt";FILE*instream,*outstream;char*wordlist[1000]={0};char*sentence[1000]={0};chartempstr[10]={0};chartempword3[50]={0};intsentenceEnd;char*word=NULL;char*tempword=NULL;char*tempword2=NULL;char*tempString=NULL;charch;intwordEnd=0,validChar=0,wordCounter=0,begin=l;intsentenceWords=0,i,len=0;/?打開(kāi)文件供輸入/輸出?/if((instream=fopen(inp_file,"r"))==NULL)(fprintf(stderr,uError:Unabletoopenfile'%s'forreading:-(\nn,inp_file);exit(0);}if((outstream=fopen(out_file, ))==NULL)(fclose(instream);fprintf(stderr,HError:Unabletoopenfile'%s'forwriting:-(\nH,out_file);exit(0);)ch=fgetc(instream);while(!feof(instream))(validChar=countcharacters(ch);sentenceEnd=countSentences(ch);wordEnd=countWords(ch);if(begin==l&&validChar==l){ch=toupper(ch);begin=0;}if(sentenceEnd==l){begin=l;}if(ch=='\n*){ch=?\;}if(word==NULL&&validChar==l){tempstr[0]=ch;tempstr[1]=*\01;word=(char*)malloc(strlen(tempstr)+1);strcpy(word,tempstr);}else{if(validChar==l){tempstr[0]=ch;tempstr[1]=*\0';len=strlen(word);strcpy(tempword3,word);word=(char*)malloc(len+\strlen(tempstr)+1);strcat(tempword3,tempstr);strcpy(word,tempword3);}if((wordEnd==l)&&word!=NULL){wordlist[wordCounter++]=word;sentence[sentenceWords++]=word;tempword=word;word=NULL;))/?打印至Ijoutput.txt文件?/if(sentenceEnd==l){for(i=0;i<sentencewords;i++){fprintf(outstream,,'%sH,sentence[i]);fprintf(outstream,^^%s,^," );}if(ch==1\n'){fprintf(outstream,"%cH,*\n*);}for(i=0;i<sentencewords;i++){sentence[i]=NULL;}sentenceWords=0;}ch=fgetc(instream);if(ch==1\n*&&sentenceEnd==l){fprintf(outstream,“3c”,1\n*);)if(feof(instream)&&sentenceEnd==O){sentencecount++;)}/?將最后一行打印到output.txt文件?/if(feof(instream)){for(i=0;i<sentencewords;i++){fprintf(outstream,"%s",sentence[i]);fprintf(outstream,H%c",1');}}fprintf(outstream,H%c",'\n');fflush(outstream);fprintf(outstream,n\n Summary M);fprintf(outstream,"\nNumberofcharacters:%d",charCount);fprintf(outstream,H\nNumberofWords:%dH,wordCount);fprintf(outstream,H\nNumberofSentences:%d",\sentencecount);fprintf(outstream,"\n \n”);fclose(instream);fclose(outstream);getch();}11.電話程控交換機(jī),IM左右的存儲(chǔ)空間,響應(yīng)時(shí)間不得超過(guò)0.5秒。分析:要將近千萬(wàn)個(gè)電話號(hào)碼存儲(chǔ)起來(lái),還要完成排序和快速查找定位,使用其他的數(shù)據(jù)格式都耗時(shí)和耗空間,選擇位集的方式存儲(chǔ)最為合適。#include<stdio.h>defineBITSPERWORD32defineSHIFT5defineMASK0X1F#defineN10000000inta[1+N/BITSPERWORD];voidset(inti){a[i>>SHIFT]|=(1<<(i&MASK));}voidclr(inti){a[i>>SHIFT]&=(1<<(i&MASK));}inttest(inti){returna[i>>SHIFT]&(1<<(i&MASK));}voidmain(void)(inti;for(i=0;i<N;i++)clr(i);while(scanf(H%dn,&i)!=EOF)set(i);for(i=0;i<N;i++)if(test(i))printf("%d\nHri);getchO;}12、有兩種解決方案。1)隨機(jī)生成運(yùn)算符以及數(shù)字,把他們的運(yùn)算結(jié)果求出,之后判斷是不是得到2、4、6、8這四個(gè)結(jié)果,如果得到,輸出結(jié)果,否則繼續(xù)。2)把運(yùn)算結(jié)果為2、4、6、8的全都保存在ー個(gè)結(jié)構(gòu)體之中,然后再判斷是不是有數(shù)字或者運(yùn)算符重復(fù)。如果有,進(jìn)行下一次循環(huán),否則輸出結(jié)果。^include<stdio.h>^include<stdlib.h>typedefstruct{intfirstnumber,secondnumber;charoperator;intresult;}Person;voidinit(Personperson[],intaロ)inti;for(i=0;i<10;i++)a[i]=i;for(i=0;i<4;i++)person[i].result=2*(i+l);)}voiddistribute_op(Personperson[],charop[]){inti,t,j;for(i=0;i<4;i++)(person[i].operator=op[j=random(4-i)];t=op[j];op[j]=op[4-i-l];op[4-i-l]=t;))voiddistribute_number(Personperson[],inta口)(inti,t,j,k;for(i=0;i<4;i++)(person[i].first_number=a[j=random(9-2*i)]+l;t=a[j];a[j]=a[8-2*i];a[8-2*i]=t;person[i].second_number=a[k=random(9-2*i-1)]+l;t=a[k];a[k]=a[8-2*i-l];a[8-2*i-l]=t;}}voiddisplay(Personperson[]){inti;for(i=0;iく4;i++){switch(person[i].operator){case'+':person[i].result=person[i]?first_number+person[i].second_number;break;case‘一':person[i].result=person[i].firstnumber-person[i].secondnumber;break;case'*':person[i].result=person[i]?first_number*person[i].second_number;break;case'/':person[i].result=person[i],first_number/person[i].second_number;break;voidmain(void){charc;Personperson[4];inta[10],i,j,ok=0;charop[4]={'init(person,a);do{ok=0;do{distributeop(person,op);distribute_number(person,a);display(person);for(i=0;iく4;i++)if(person[i].result=2)break;if(i<4){for(i=0;i<4;i++)if(person[i].result==4)break;if(i<4){for(i=0;i<4;i++)if(person[i].result==6)break;if(i<4){for(i=0;i<4;i++)if(person[i].result==8)break;if(i<4)ok=l;})}}while(!ok);for(i=0;i<4;i++){if(person[i].operator!=,/*IIperson[i].first_number%person[i].second_number!=0)continue;{for(i二〇;iく4;i++){printf(〃、nperson跳d]'sresultis:%d=%d%c%dz/,i,person[i].result,person[i].firstnumber,person[i].operator,person[i].secondnumber);)printfC\nln9number,%disnotin4people*shand”,a[0]+l);}}c=getch();}while(c!=,q');13、隨機(jī)散步?建立一個(gè)二維數(shù)組,模擬地面瓷磚.從終端上獲得起始位置,用隨機(jī)函數(shù)獲得跳蚤的下ー跳的方向。隨后對(duì)每種情況分別處理,設(shè)置觸發(fā)器,統(tǒng)計(jì)是否全部遍歷,同時(shí)設(shè)置ー個(gè)計(jì)數(shù)器,統(tǒng)計(jì)跳蚤總的跳數(shù)和跳蚤在每塊瓷磚跳的次數(shù)!^include<stdio.h>^include<stdlib.h>#defineM10#defineN20voidmain()(inta[M][N]={0};inti,j,k,total,tol;printfC"gaiebegin!\n");printf("whichnubersdoyouwanttobegin!\n");scanf("%d%d",&i,&j);a[i][j]=l;total=l;do{switch(random(4))(/************[n]期K********************/case0:if((j-l)<0){break;}(j=j-l;a[i][j]++;if(a[i][j]==D{total++;}break;}/************向目k********************/if((j+l)>19){break;}{j=j+1;a[i][j]++;if(a[i][j]=l){total.;}}break;/************向左跳**********************/if((iT)く0){break;}{i=i-l;a[i][j]++;i

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論