三、给出下列程序运行后的输出结果
1.# include <iostream.h>
void main( ) {
int s1 = 0 , s2 = 0 ;
for (int i = 0; i<12 ; i++)
if (i%2) s1 += i ;
else s2 += i ;
cout <<s1<<’ ’<<s2<<endl;
}
输出结果:
36 30
2. #include<iostream.h>
void main() {
char ch='*';
int n=5;
while(n) {
for(int i=0;i<n;i++) cout<<ch;
cout<<endl;
n--;
}
}
输出结果:
*****
****
***
**
*
3. #include<iostream.h>
void main() {
int a=10, b=15;
cout<<a<<' '<<b<<endl;
{ a*=3;
int b=a+20;
cout<<a<<' '<<b<<endl;
}
cout<<a<<' '<<b<<endl;
}
输出结果:
10 15
30 50
30 15
4.# include <iostream.h>
void main( ) {
int a[10] = {8 , 19 , 26 , 15 , 6 , 24 , 27 , 18 , 20 , 53};
int c1 = 0 , c2 = 0;
for ( int i=0; i<10; i++ )
{if ( a[i] >= 20 ) c1++ ;
if (a[i] >=10 && a[i] <= 30 ) c2++;}
cout <<c1<<’ ‘<<c2<<endl;
}
输出结果:
5 7
5. #include<iostream.h>
void main() {
int s=0;
for(int i=1;i<=8;i=+2)
s+=i*i;
cout<<"s="<<s<<endl;
}
输出结果:
s=84
6. #include<iostream.h>
void main() {
int s=0;
for(int i=1;i<=5; i++)
s+=i*i;
cout<<"s="<<s<<endl;
}
输出结果:
s=55
7. #include<iostream.h>
void main() {
int i=1,s=0;
while(s<20) {
if(i%2==0 && i!=6) s+=i;
i++;
}
cout<<i<<’,’<<s<<endl;
}
输出结果:
11,24
8.# include <iostream.h>
void main( ) {
int s=0 ;
for (int i=1 ; ; i++) {
if (s>50) break;
if (i%2==0) s += i;
}
cout <<"i,s="<<i<<","<<s<<endl;
}
输出结果:
i,s=15,56
9.# include <iostream.h>
void main( ) {
int a[10] = {36 , 25 , 48 , 14 , 55 , 40 , 72 , 40 , 86 , 27};
int b1 , b2;
b1 = b2 = a[0];
for (int i=1; i<10; i++)
if (a[i]>b1) {
if (b1>b2) b2 = b1;
b1 = a[i];
}
cout <<b1<<‘ ’<<b2<<endl;
}
输出结果:
86 72
10.# include <iostream.h>
void main( ) {
int a[8]={36,25,48,14,55,40,72,40};
int b1 , b2;
b1=b2=a[0];
for (int i=1 ; i<8 ; i++)
if (a[i]>b1) {
if (b1>b2) b2=b1;
b1=a[i];
}
cout <<b1<<’ ‘<<b2<<endl;
}
输出结果:
72 55
11.# include <iostream.h>
void main( ) {
int a[8]={36,25,48,14,55,40,32,66};
int b1 , b2;
b1=b2=a[0];
for (int i=1 ; i<8 ; i++)
if (a[i]>b1) {
if (b1>b2) b2=b1;
b1=a[i];
}
cout <<b1<<’ ‘<<b2<<endl;
}
输出结果:
66 55
12.# include <iostream.h>
void main( ) {
int a[8]={36,25,48,14,55,40,32,66};
int b1 , b2;
b1=b2=a[0];
for (int i=1 ; i<8 ; i++)
if (a[i]<b1) {
if (b1<b2) b2=b1;
b1=a[i];
}
cout <<b1<<’ ‘<<b2<<endl;
}
输出结果:
14 25
13.#include <iomanip.h>
void main( ) {
int a[8] = {3 , 5 , 7 , 9 , 11 , 13 , 15 , 17};
int * p = a;
for (int i=0; i<8; i++) {
* p += 10;
cout <<setw(5)<<*p++;
if ((i+1) % 3 == 0) cout <<endl;
}
}
输出结果:
13 15 17
19 21 23
25 27
14. #include<iomanip.h>
void main() {
int a[9]={2,4,6,8,10,12,14,16,18};
for(int i=0;i<9;i++) {
cout<<setw(5)<<*(a+i);
if((i+1)%3==0) cout<<endl;
}
}
输出结果:
2 4 6
8 10 12
14 16 18
15. #include<iostream.h>
const int n=10;
void main() {
int a[n]={76,83,54,62,40,65,80,92,67,88};
int c1=70, c2=85;
for(int i=0;i<n;i++)
if(a[i]>=c1 && a[i]<=c2) cout<<a[i]<<’ ’;
cout<<endl;
}
输出结果:
76 83 80
16. #include<iostream.h>
void main() {
char a[]="abcdabcabfgacd";
int i1=0, i2=0, i=0;
while(a[i]) {
if(a[i]=='a') i1++;
if(a[i]=='b') i2++;
i++;
}
cout<<i1<<' '<<i2<<endl;
}
输出结果:
4 3
17. #include<iomanip.h>
void main() {
int a[8]={4,8,15,16,20,30,48,62};
int *p=a;
do {
cout<<*p<<' ';
p+=2;
} while(p<a+8);
cout<<endl;
}
输出结果:
4 15 20 48
18. #include<iomanip.h>
void main() {
int a[8]={4,8,12,16,20,24,28,32};
int *p=a;
do {
cout<<*p<<' ';
p+=2;
} while(p<a+8);
cout<<endl;
}
输出结果:
4 12 20 28
19.# include <iostream.h>
void main( ) {
int a[3][4] = { {1 , 2 , 7 , 8} , {5 , 6 , 11 , 31} , {9 , 20 , 3 , 4} };
int m = a[0][0] ;
int ii = 0 , jj = 0 ;
for ( int i=0; i<3; i++ )
for ( int j=0; j<4; j++ )
if (a[i][j] > m) { m = a[i][j] ; ii = i ; jj = j ; }
cout <<ii<<’ ‘<<jj<<’ ‘<<a[ii][jj]<<endl;
}
输出结果:
1 3 31
20.
输出结果:
2 1 6 1
21.# include <iostream.h>
void main( ) {
int a[10] = {68 , 79 , 86 , 65 , 46 , 94 , 37 , 78 , 60 , 53};
int c = 0;
for ( int i=0; i<10; i++ )
if ( a[i] >= 60 ) c++ ;
cout<<’c=‘<<c<<endl;
}
输出结果:
c=7
22.# include <iomanip.h>
void LB(int * a , int n) {
int s = 1;
for ( int i=0; i<n; i++ ) s *= *a++;
return s ;
}
void main( ) {
int a[ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8};
cout <<LB(a , 4)<<’ ‘<<LB(&a[3] , 2)<<’ ‘;
cout <<LB(a+5 , 3)<<endl;
}
输出结果:
24 20 336
23.# include <iomanip.h>
void LB(int * a , int n) {
int s = 1;
for ( int i=0; i<n; i++ ) s *= *a++;
return s ;
}
void main( ) {
int a[ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8};
cout <<LB(a , 5)<<’ ‘<<LB(&a[3] , 3)<<’ ‘;
cout <<LB(a+2 , 4)<<endl;
}
输出结果:
120 120 360
24.#include <iostream.h>
struct Worker {
char name[15]; // 姓名
int age; // 年龄
float pay; // 工资
};
void main( ) {
Worker x = {“wangfong” , 46 , 1640} ;
Worker y , * p ;
y =x ; p = &x;
cout <<y.name<<’ ‘<<y.age<<’ ‘<<y.pay<<endl;
cout <<p->name<<‘ ’<< p->age+2<<‘ ’<< p->pay-100<<endl;
}
输出结果:
wangfong 46 1640
wangfong 48 1540
25.#include <iostream.h>
struct Worker {
char name[15]; // 姓名
int age; // 年龄
float pay; // 工资
};
void main( ) {
Worker x = {“wangfong” , 43 , 640} ;
Worker y , * p ;
y =x ; p = &x;
cout <<y.name<<’ ‘<<y.age<<’ ‘<<y.pay<<endl;
cout <<p->name<<‘ ’<< p->age*2<<‘ ’<< p->pay+100<<endl;
}
输出结果:
wangfong 43 640
wangfong 86 740
26.# include <iostream.h>
# include <stdlib.h>
double SD(int a , int b , char op) {
double x ;
switch ( op ) {
case ‘+’ : x = double (a) + b ; break;
case ‘-’ : x = double (a) - b ; break;
case ‘*’ : x = double (a) * b ; break;
case ‘/’ : if (b) x = double (a) / b ;
else exit(1);
break;
default : exit(1);
}
return x;
}
void main( ) {
int x = 20 , y = 5 ;
cout <<SD(x , y ,’+’)<<’ ‘;
cout <<SD(x , y ,’*’)<<’ ‘;
cout <<SD(x-y , y ,’/’)<<endl;
}
输出结果:
25 100 3
27.# include <iostream.h>
# include <stdlib.h>
double SD(int a , int b , char op) {
double x ;
switch ( op ) {
case ‘+’ : x = double (a) + b ; break;
case ‘-’ : x = double (a) - b ; break;
case ‘*’ : x = double (a) * b ; break;
case ‘/’ : if (b) x = double (a) / b ;
else exit(1);
break;
default : exit(1);
}
return x;
}
void main( ) {
int x = 25 , y = 8 ;
cout <<SD(x , y ,’-’)<<’ ‘;
cout <<SD(x , y ,’*’)<<’ ‘;
cout <<SD(x+y , y ,’/’)<<endl;
}
输出结果:
17 200 4.125
28.# include <iostream.h>
# include <string.h>
void main( ) {
char * a[5] = {“student” , “worker” , “cadre” , “soldier” , “apen”};
char * p1 , * p2;
p1 = p2 = a[0];
for ( int i=1; i<5; i++ ) {
if ( strcmp(a[i] , p1)>0 ) p1 = a[i];
if ( strcmp(a[i] , p2)<0 ) p2 = a[i];
}
cout <<p1<<’ ‘<<p2<<endl;
}
输出结果:
worker apen
29.# include <iostream.h>
void WF(int x , int y) {
x = x+y;
y = x+y;
cout <<”subs : “<<”x , y = “<<x<<’,’<<y<<endl;
}
void main( ) {
int x =8 , y = 15;
cout <<”main : “<<”x , y = “<<x<<’,’<<y<<endl;
WF(x , y);
x = 2*x;
cout <<”main : “<<”x , y = “<<x<<’,’<<y<<endl;
}
输出结果:
main : x , y = 8 , 15
subs : x , y =23 ,38
main : x , y = 16 , 15
30.
输出结果:
main x , y = 8 , 15
subs x , y =23, 38
main x , y =16, 15
subs x , y =31, 46
main x , y =16, 15
31.# include <iomanip.h>
void LF(int & x , int y) {
x = x+y;
y = x+y;
cout <<”x=”<<x<<”,y=”<<y<<endl;
}
void main( ) {
int x =5 , y = 8;
cout <<”x=”<<x<<”,y=”<<y<<endl;
LF(x , y);
cout <<”x=”<<x<<”,y=”<<y<<endl;
}
输出结果:
x = 5 , y = 8
x =13 , y = 21
x =13 , y = 8
32.# include <iostream.h>
void LG(int * & a , int & m) {
a = new int[m];
int * p = a;
for ( int i=0; i<m; i++ )
* p ++ = i*i+1;
}
void main( ) {
int * b , n =5;
LG(b , n);
for (int i=0; i<n; i++)
cout <<b[i]<<’ ‘;
cout <<endl;
delete [ ]b;
}
输出结果:
1 2 5 10 17
33.#include <iostream.h>
# include <string.h>
struct Worker {
char name[15]; // 姓名
int age; // 年龄
float pay; // 工资
};
void main( ) {
Worker x;
char * t = “WeiRong”;
int d =45 ; float f = 1235;
strcpy(x.name , t);
x.age = d; x.pay = f;
cout <<x.name<<’ ‘<<x.age<<’ ‘<<x.pay<<endl;
}
输出结果:
WeiRong 45 1235
34.# include <iostream.h>
void SB(char ch) {
switch (ch) {
case ‘A’ : case ‘a’ :
cout <<”well!”; break;
case ‘B’ : case ‘b’ :
cout <<”good!”; break;
case ‘C’ : case ‘c’ :
cout <<”pass!”; break;
default:
cout <<”bad!”; break;
}
cout <<endl;
}
void main( ) {
char a[6] = “Abcaf”;
for (int i=0; a[i]; i++) SB(a[i]);
}
输出结果:
well!
good!
pass!
well!
bad!
35.# include <iomanip.h>
void main( ) {
int x = 25 , y = 120;
cout <<”dec:”<<dec<<setw(10)<<x<<setw(10)<<y<<endl;
cout <<”oct:”<<oct<<setw(10)<<x<<setw(10)<<y<<endl;
cout <<”hex:”<<hex<<setw(10)<<x<<setw(10)<<y<<endl;
}
输出结果:
dec: 25 120
oct: 31 170
hex: 19 78
36. #include<iomanip.h>
void LE(int* a, int* b) {
int x=*a;
*a=*b; *b=x;
cout<<*a<<' '<<*b<<endl;
}
void main() {
int x=10, y=25;
LE(&x,&y); cout<<x<<' '<<y<<endl;
}
输出结果:
25 10
25 10
37.
输出结果:
y=81
38.
输出结果:
2 3 3 2
39.
输出结果:
3 6 9 12 15
40.
输出结果:
b=25
41.
输出结果:
20 40
30 40
42. #include<iostream.h>
class A {
int a,b;
public:
A() {a=b=0;}
A(int aa, int bb) {
a=aa; b=bb;
cout<<a<<' '<<b<<endl;
}
};
void main() {
A x,y(2,3),z(4,5);
}
输出结果:
2 3
4 5
43.#include<iostream.h>
template<class TT>
class FF {
TT a1, a2, a3;
public:
FF(TT b1, TT b2, TT b3) {
a1=b1; a2=b2; a3=b3;
}
TT Sum() {return a1+a2+a3;}
};
void main() {
FF<int> x(2,3,4),y(5,7,9);
cout<<x.Sum()<<' '<<y.Sum()<<endl;
}
输出结果:
9 21
44. # include <iomanip.h>
void LC( int a,int b) {
int x=a;
a=b;b=x;
cout<<a<’ ‘<<b<<endl;
}
void main( ) {
int x=10; y=25;
LC(x,y); cout<<x<’ ‘<<y<<endl;
}
输出结果:
25 10
10 25
45.
输出结果:
a=31
46.
}
输出结果:
ghi 49 560
版权声明
声明:有的资源均来自网络转载,版权归原作者所有,如有侵犯到您的权益
请联系本站我们将配合处理!