當前位置:才華齋>IT認證>計算機等級>

浙江省計算機二級上機考試試題庫

計算機等級 閱讀(1.39W)

2017年3月計算機等級考試就要開考了,同學們準備好了嗎?下面跟yjbys小編一起來看看最新的計算機二級試題吧!

浙江省計算機二級上機考試試題庫

  1. 數字處理(切割技術)

1-1找Armstrong(水仙花)數:371=3*3*3+7*7*7+1*1*1 【這是一個三位的自然數,要點是三位分離】

#include

#include

void main()

{ int i,a,b,c;

for(i=100;i<=999;i++)

{ a=i/100;

_______1_______ // b=i0/10;

c=i;

if (________2________) // a*a*a+b*b*b+c*c*c = = i

printf("%d is a Armstrong number!n",i);

}

}

1-2輸入1個整數後,輸出該數的位數。(例:輸入3214則輸出4,輸入-23156則輸出5)。【逐位剝離】

#include

void main()

{ int n,k=0;

scanf("%d",&n);

while( _____1_____ ){ // n!=0

k++;

_____2_____; // n=n/10

}

printf("%dn",k);

}

1-3求輸入的整數各位數字之和,如輸入234則輸出9,輸入-312則輸出6。【逐位剝離】

#include

#include

void main()

{

int n,s=0;

scanf("%d",&n);

______ 1 ______ // if (n<0) n=-n;【符號預處理】

while(n!=0) {

______ 2 ______ // s+=n;

n=n/10;

}

printf("%dn",s);

}

1-4呼叫函式f,將一個整數首尾倒置。例如:若程式輸入12345,則輸出54321;若程式輸入-34567,則輸出-76543。【逐位剝離+逆置技術y=y*10+m 】

#include

#include

long f(long n)

{ long m,y=0; m=fabs(n);

while(m!=0) {

y=y*10+m;

____1____ // m=m/10 ;

}

if(n>=0) return y;

else _____2_____ // return -y ;

}

void main()

{

printf("%ldt",f(12345)); printf("%ldn",f(-34567));

}

1-5尋找並輸出11至999之間的數m,它滿足m、m*m、m*m*m均為迴文數。說明:所謂迴文數是指各位數字左右對稱,例如121、676、94249等。滿足上述條件的數如m=11,m^2=121,m^3=1331皆為迴文數。

請編制函式int JSValue(long m)實現此功能,如果是迴文數,則函式返回1,反之則返回0。最後把結果寫入到考生資料夾中Paper子資料夾下的新建檔案。【本題目的演算法與 1-4題相同】

#include

#include

#include

int JSValue(long m)

{

long i,n;

n=m; i=0; // i中存放的是m的倒置數

while(n>0)

{ i=i*10+n; n=n/10; }

if (m = = i) return 1;

else return 0;

}

void main()

{

FILE *p;long m;

p=fopen("","w");

for(m=11;m<1000;m++)

{ if(JSValue(m)&&JSValue(m*m)&&JSValue(m*m*m))

fprintf(p,"%ld ",m); }

fclose(p); }

  2. 字串操作(特別,單字元刪除的兩種演算法)

2-1輸入一個小寫字母,將字母迴圈後移5個位置後輸出。例如:"a"變成"f","w"變成"b"。

#include

void main()

{ char c;

c=getchar();

if(______1______) // c>='a'&&c<='u'

c=c+5;

else

if (c>='v' && c<='z')

______2______ // c=(c-'a'+5)&+'a'; 或 c=c-21; 或 c=c+5-26;

putchar(c);

}

2-2輸入一個字串,將組成字串的所有字元先按順序存放到字串t中,再將字串中的字元按逆序連線到字串t後面。例如:輸入"ABCD",則字串t為"ABCDDCBA"。PP2

#include

#include

void fun(char *s,char *t)

{ int i,sl;

sl=strlen(s);

for(i=0;i

t[i]=s[i];

for(i=0;i

t[sl+i]=s[sl-i]; // t[sl+i]=s[sl-1-i];

t[sl]=" "; // t[2*sl]=’ ’;

}

void main()

{ char s[100],t[100];

scanf("%s",s);

fun(s,t);

printf("%s",t);

}

2-3設計程式:計算字串s中每個字元的權重值,所謂權重值就是字元在字串中的位置值與該字元的ASCII碼值的乘積。位置值從1開始依此遞增。將每個字元的權重值,以格式"%d "寫入到源程式目錄中Paper子目錄下的新建檔案中。

#include

#include

void main()

{ FILE *p; int i,w;

char *s="we45*&y3r#$1";

p=fopen("","w");

for (i=0;s[i]!=' ';i++)

{ w=(i+1)*s[i];

fprintf( p,"%d ",w);

}

fclose(p);

}

2-4呼叫find函式在輸入的字串中查詢是否出現"the"這個單詞。如果查到返回出現的次數,如果未找到返回0。【本題解在判斷源串裡當前連續三個字元是否為"the"這個單詞采用了查詢演算法】

#include

int find(char *str)

{ char *fstr="the";

int i=0,j,n=0;

while (str[i]!=' ') 【注:while (str[i+2]!=' ') 更佳】

{

for(______1______) // j=0; j<3; j++

if (str[j+i]!=fstr[j]) break;

if (______2______) n++; // j>=3 或者 j = = 3

i++;

}

return n;

}

void main()

{ char a[80];

gets(a);

printf("%d",find(a));

}

【注:以下為單字元刪除。出現兩種演算法。一是使用strcpy做子串覆蓋,二是逐個保留新串的'字元】

2-5呼叫函式f,從字串中刪除所有的數字字元。

#include

#include

#include

void f(char *s)

{ int i=0;

while(s[i]!=' '){

if(isdigit(s[i])) ____1____(s+i,s+i+1); // strcpy

___2___ i++;} // else

}

void main()

{ char str[80];

gets(str); f(str); puts(str);

}

2-6將字串s中所有的字元'c'刪除。

#include

void main()

{ char s[80];

int i,j;

gets(s);

for(i=j=0; ______1______; i++) // s[i] != ' '

if(s[i] != 'c')

{ s[j]=s[i];

______2______ // j++;

}

s[j]=' ';

puts(s);

}

2-7輸入一個字串,將組成字串的所有非英文字母的字元刪除後輸出。

#include

#include

void main()

{ char str[256];

int i,j,k=0,n;

gets(str);

n=strlen(str);

for(i=0;i

if (tolower(str[i])<'a' || tolower(str[i])>'z') // if (tolower(str[i])>='a' && tolower(str[i])<='z')

{

str[n]=str[i]; n++; // str[k]=str[i]; k++;

}

str[k]=' ';

printf("%sn",str);

  3. 最大(小)值

3-1執行時輸入10個數,然後分別輸出其中的最大值、最小值。PP11

#include

void main()

{ float x,max,min; int i;

for(i=0;i<=10;i++) { // for(i=1; i<=10; i++) {

scanf("%f",&x);

if(i=1) { max=x;min=x;} // if(i==1) { max=x;min=x;}

if(x>max) max=x;

if(x

}

printf("%f,%fn",max,min);

}

3-2 對x=1,2,……,10,求f(x)=x*x-5*x+sin(x)的最大值。P3

#include

#include

#define f(x) x*x-5*x+sin(x)

void main()

{ int x; float max;

______1______ // max=f(1);

for(x=2;x<=10;x++)

______2______ // if (f(x)>max) max=f(x);

printf("%fn",max);

}

3-3對x=1,2,…10,求函式f(x)=x-10*cos(x)-5*sin(x)的最大值,並將該數以格式".3f"寫入到考生資料夾中Paper子資料夾下的新建檔案。

#include

#include

void main()

{ FILE *p; float f(float),max,x;

int i; max=f(1);

for (i=2;i<=10;i++)

{ x=f(i);

if (max

}

p=fopen("","w");

fprintf(p,"%.3f",max);

fclose(p);

}

float f(float x)

{ float t;

t=x-10*cos(x)-5*sin(x);

return t;

}

3-4 z=f(x,y)=(3.14*x-y)/(x+y),若x、y取值為區間[1,6]的整數,找出使z取最小值的x1、y1,並將x1、y1以格式"%d,%d"寫入到考生資料夾中Paper子資料夾下的新建檔案。P6