经典算法(31~60)
【程序31】
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续
判断第二个字母。
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 char c; 6 cin>>c; 7 switch(c) 8 { 9 case 'M': 10 cout<<"Monday"<<endl; 11 break; 12 case 'T': 13 cout<<"input another letter: "; 14 cin>>c; 15 if(c=='u') cout<<"Tuesday"<<endl; 16 else if(c=='h') cout<<"Thursday"<<endl; 17 break; 18 case 'W': 19 cout<<"Wendesday"<<endl; 20 break; 21 case 'F': 22 cout<<"Friday"<<endl; 23 break; 24 case 'S': 25 cout<<"input another letter: "; 26 cin>>c; 27 if( c=='a') cout<<"Saturday"<<endl; 28 else if( c=='u') cout<<"Sunday"<<endl; 29 break; 30 default: 31 cout<<"input error"<<endl; 32 break; 33 } 34 system("pause"); 35 return 0; 36 }
==============================================================
【程序32】(TC中可以添加头文件“conio.h”,但VC中没有,只能手动添加textbackground())
题目:Press any key to change color, do you want to try it. Please hurry up!
1 #include<iostream> 2 #include "windows.h" 3 using namespace std; 4 int textbackground(short iColor) 5 { 6 HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE); 7 CONSOLE_SCREEN_BUFFER_INFO csbInfo; 8 GetConsoleScreenBufferInfo(hd, &csbInfo); 9 return SetConsoleTextAttribute(hd, (iColor<<4)|(csbInfo.wAttributes&~0xF0)); 10 } 11 int main() 12 { 13 int color; 14 char c; 15 for( color=0; color<8; color++) 16 { 17 textbackground(color); 18 cout<<"This is color "<<color<<endl; 19 cout<<"Please press any key to continue."<<endl; 20 cin>>c; 21 } 22 system("pause"); 23 return 0; 24 }
==============================================================
【程序33】(略)
题目:学习gotoxy()与clrscr()函数
==============================================================
【程序34】
题目:练习函数调用
1 #include<iostream> 2 using namespace std; 3 void hello_world(); 4 void triple_hello_world(); 5 int main() 6 { 7 triple_hello_world(); 8 system("pause"); 9 return 0; 10 } 11 void hello_world() 12 { 13 cout<<"Hello World!"<<endl; 14 } 15 void triple_hello_world() 16 { 17 for(int i=0; i<3; i++) 18 hello_world(); 19 }
==============================================================
【程序35】(略)
题目:文本颜色设置
==============================================================
【程序36】
题目:求100之内的素数
1 #include<iostream> 2 #include"math.h" 3 using namespace std; 4 int test(int i); 5 int main() 6 { 7 for( int i=2; i<100; i++) 8 { 9 if(test(i)) 10 cout<<i<<" "; 11 } 12 system("pause"); 13 return 0; 14 } 15 int test(int i) 16 { 17 int max=(int)sqrt(i+1); 18 for(int j=2; j<=max; j++) 19 { 20 if(i%j==0) 21 return 0; 22 } 23 return 1; 24 }
==============================================================
【程序37】(冒泡排序)
题目:对10个数进行排序
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int a[10]; 6 for(int i=0; i<10; i++) 7 cin>>a[i]; 8 for(int i=0; i<9; i++) 9 { 10 for(int j=i+1; j<10; j++) 11 { 12 if(a[i]>a[j]) 13 { 14 a[i]=a[i]^a[j]; 15 a[j]=a[i]^a[j]; 16 a[i]=a[i]^a[j]; 17 } 18 } 19 cout<<a[i]<<" "; 20 } 21 cout<<a[9]; 22 system("pause"); 23 return 0; 24 }
==============================================================
【程序38】
题目:求一个3*3矩阵对角线元素之和
1 #include<iostream> 2 #include<math.h> 3 using namespace std; 4 int main() 5 { 6 int a[3][3]; 7 int sum=0; 8 for(int i=0; i<3; i++) 9 { 10 for(int j=0; j<3; j++) 11 { 12 cin>>a[i][j]; 13 if(i==j) sum+=a[i][j]; 14 } 15 } 16 cout<<"sum="<<sum; 17 system("pause"); 18 return 0; 19 }
==============================================================
【程序39】(复习数组及数组长度的获取)
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
1 //加深对数组的理解 2 #include<iostream> 3 #include<math.h> 4 using namespace std; 5 int main() 6 { 7 int a[20]={1, 2, 3, 5, 8, 13, 21, 34, 55, 89}; //如果不指定大小,数组长度可以通过sizeof(a)/sizeof(a[0])得到,但最后添加后会溢出 8 int insert; 9 int length=10; //sizeof(a)/sizeof(a[0]); 10 cin>>insert; 11 //cout<<sizeof(a)<<endl; 12 if(insert>a[length-1]) a[length]=insert; 13 else if(insert<a[0]) 14 { 15 for( int j=length; j>0; j--) 16 { 17 a[j]=a[j-1]; 18 } 19 a[0]=insert; 20 } 21 else 22 { 23 for(int i=0; i<length-1; i++) 24 { 25 if(insert>=a[i] && insert<=a[i+1]) 26 { //插入,依次后移 27 for( int j=length; j>i+1; j--) 28 { a[j]=a[j-1]; 29 //cout<<"a["<<j<<"]="<<a[j]<<endl; 30 } 31 a[i+1]=insert; 32 break; 33 } 34 } 35 } 36 length++; 37 for(int i=0; i<length; i++) 38 cout<<a[i]<<" "; 39 system("pause"); 40 return 0; 41 }
==============================================================
【程序40】(逗比了)
题目:将一个数组逆序输出。
1 //逗比算法 2 #include<iostream> 3 #include<math.h> 4 using namespace std; 5 int main() 6 { 7 int a[]={1,2,3,5,8,13,21,34,55,89}; 8 int length=sizeof(a)/sizeof(a[0]); 9 cout<<"顺序: "; 10 for(int i=0; i<length; i++) 11 cout<<a[i]<<" "; 12 for(int i=0; i<length/2; i++) 13 { 14 a[i]=a[i]^a[length-1-i]; 15 a[length-1-i]=a[i]^a[length-1-i]; 16 a[i]=a[i]^a[length-1-i]; 17 } 18 cout<<endl<<"逆序: "; 19 for( int i=0; i<length; i++) 20 cout<<a[i]<<" "; 21 system("pause"); 22 return 0; 23 }
1 //倒着输出不就行了 2 #include<iostream> 3 #include<math.h> 4 using namespace std; 5 int main() 6 { 7 int a[]={1,2,3,5,8,13,21,34,55,89}; 8 int length=sizeof(a)/sizeof(a[0]); 9 cout<<"顺序: "; 10 for(int i=0; i<length; i++) 11 cout<<a[i]<<" "; 12 cout<<endl<<"逆序: "; 13 for( int i=0; i<length; i++) 14 cout<<a[length-1-i]<<" "; 15 system("pause"); 16 return 0; 17 }
==============================================================
【程序41】(复习static,静态局部变量,输出0 0 0,0 1 2)
题目:学习static定义静态变量的用法
1 #include<stdio.h> 2 void varfunc() 3 { 4 int var=0; 5 static int static_var=0; 6 printf("\40:var equal %d \n",var); 7 printf("\40:static var equal %d \n",static_var); 8 printf("\n"); 9 var++; 10 static_var++; 11 } 12 void main() 13 {int i; 14 for(i=0;i<3;i++) 15 varfunc(); 16 system("pause"); 17 }
==============================================================
【程序42】 (auto用于声明变量的生存期为自动,在函数中定义的变量视为局部变量。这个关键字不怎么多写,因为所有的变量默认就是auto的。输出2 3 4,1 1 1)
题目:学习使用auto定义变量的用法
1 #include "stdio.h" 2 void main() 3 { 4 int i,num; 5 num=2; 6 for (i=0;i<3;i++) 7 { 8 printf("\40: The num equal %d \n",num); 9 num++; 10 { 11 int num=1; 12 printf("\40: The internal block num equal %d \n",num); 13 num++; 14 } 15 } 16 system("pause"); 17 }
==============================================================
【程序43】 (static,输出 2 3 4,1 2 3)
题目:学习使用static的另一用法。
1 #include "stdio.h" 2 void main() 3 { 4 int i,num; 5 num=2; 6 for(i=0;i<3;i++) 7 { 8 printf("\40: The num equal %d \n",num); 9 num++; 10 { 11 static int num=1; 12 printf("\40:The internal block num equal %d\n",num); 13 num++; 14 } 15 } 16 system("pause"); 17 }
==============================================================
【程序44】 (输出c=7)
题目:学习使用external的用法。
1 #include "stdio.h" 2 int a,b,c; 3 void add() 4 { int a; 5 a=3; 6 c=a+b; 7 } 8 void main() 9 { a=b=4; 10 add(); 11 printf("The value of c is equal to %d\n",c); 12 }
==============================================================
【程序45】 (输出5050,和不加register没区别)
题目:学习使用register定义变量的方法。
1 #include "stdio.h" 2 void main() 3 { 4 register int i; 5 int tmp=0; 6 for(i=1;i<=100;i++) 7 tmp+=i; 8 printf("The sum is %d\n",tmp); 9 system("pause"); 10 }
==============================================================
【程序46】
题目:宏#define命令练习(1)
1 #include "stdio.h" 2 #define TRUE 1 3 #define FALSE 0 4 #define SQ(x) (x)*(x) 5 void main() 6 { 7 int num; 8 int again=1; 9 printf("\40: Program will stop if input value less than 50.\n"); 10 while(again) 11 { 12 printf("\40:Please input number==>"); 13 scanf("%d",&num); 14 printf("\40:The square for this number is %d \n",SQ(num)); 15 if(num>=50) 16 again=TRUE; 17 else 18 again=FALSE; 19 } 20 system("pause"); 21 }
==============================================================
【程序47】(不明白此处#define用法)
题目:宏#define命令练习(2)
1 #include "stdio.h" 2 #define exchange(a,b) { \ /*宏定义中允许包含两道以上命令的情形,此时必须在最右边加上"\"*/ 3 int t;\ 4 t=a;\ 5 a=b;\ 6 b=t;\ 7 } 8 void main(void) 9 { 10 int x=10; 11 int y=20; 12 printf("x=%d; y=%d\n",x,y); 13 exchange(x,y); 14 printf("x=%d; y=%d\n",x,y); 15 }
==============================================================
【程序48】
题目:宏#define命令练习(3)
1 #include<stdio.h> 2 #define LAG > 3 #define SMA < 4 #define EQ == 5 int main() 6 { 7 int i=10; 8 int j=20; 9 if(i LAG j) 10 printf("\40: %d larger than %d \n",i,j); 11 else if(i EQ j) 12 printf("\40: %d equal to %d \n",i,j); 13 else if(i SMA j) 14 printf("\40:%d smaller than %d \n",i,j); 15 else 16 printf("\40: No such value.\n"); 17 system("pause"); 18 return 0; 19 }
==============================================================
【程序49】(关于本题的知识点亟待补充)
题目:#if #ifdef和#ifndef的综合应用。
1 #include "stdio.h" 2 #define MAX 3 #define MAXIMUM(x,y) (x>y)?x:y 4 #define MINIMUM(x,y) (x>y)?y:x 5 void main() 6 { int a=10,b=20; 7 #ifdef MAX 8 printf("\40: The larger one is %d\n",MAXIMUM(a,b)); 9 #else 10 printf("\40: The lower one is %d\n",MINIMUM(a,b)); 11 #endif 12 #ifndef MIN 13 printf("\40: The lower one is %d\n",MINIMUM(a,b)); 14 #else 15 printf("\40: The larger one is %d\n",MAXIMUM(a,b)); 16 #endif 17 #undef MAX 18 #ifdef MAX 19 printf("\40: The larger one is %d\n",MAXIMUM(a,b)); 20 #else 21 printf("\40: The lower one is %d\n",MINIMUM(a,b)); 22 #endif 23 #define MIN 24 #ifndef MIN 25 printf("\40: The lower one is %d\n",MINIMUM(a,b)); 26 #else 27 printf("\40: The larger one is %d\n",MAXIMUM(a,b)); 28 #endif 29 }
==============================================================
【程序50】 (略,以后补充)
题目:#include 的应用练习
==============================================================
【程序51】 (输出为3 3,看注释)
题目:学习使用按位与 & 。
1 #include "stdio.h" 2 main() 3 { 4 int a,b; 5 a=077; //八进制077对应二进制是111111 6 b=a&3; 7 printf("\40: The a & b(decimal) is %d \n",b); 8 b&=7; 9 printf("\40: The a & b(decimal) is %d \n",b); 10 system("pause"); 11 }
==============================================================
【程序52】 (输出63 63)
题目:学习使用按位或 | 。
1 #include "stdio.h" 2 main() 3 { 4 int a,b; 5 a=077; 6 b=a|3; 7 printf("\40: The a & b(decimal) is %d \n",b); 8 b|=7; 9 printf("\40: The a & b(decimal) is %d \n",b); 10 system("pause"); 11 }
==============================================================
【程序53】 (输出60 59)
题目:学习使用按位异或 ^ 。
1 #include "stdio.h" 2 main() 3 { 4 int a,b; 5 a=077; 6 b=a^3; 7 printf("\40: The a & b(decimal) is %d \n",b); 8 b^=7; 9 printf("\40: The a & b(decimal) is %d \n",b); 10 system("pause"); 11 }
==============================================================
【程序54】 (理解分析,学会构造,移位补充位知识不足)
题目:取一个整数a从右端开始的4~7位。
程序分析:可以这样考虑:
(1)先使a右移4位。
(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
(3)将上面二者进行&运算。
1 #include "stdio.h" 2 main() 3 { 4 int a,b; 5 a=0x9c; //a的值为十进制的156,十六进制9c 6 //printf("%d\n",a); 7 b=0xf; //b的值为十进制的15,十六进制f,低四位全为1 8 //printf("%d\n",b); 9 a=a>>4; 10 b=a&b; 11 printf("%d\n",b); 12 system("pause"); 13 }
参考代码:
1 #include<stdio.h> 2 main() 3 { 4 unsigned a,b,c,d; 5 scanf("%o",&a); 6 b=a>>4; 7 c=~(0<<4); 8 printf("c=%x\n",c); 9 d=b&c; 10 printf("%o\n%o\n",a,d); 11 system("pause"); 12 }
==============================================================
【程序55】 (输出-235 ffffff15)(值得思考)
题目:学习使用按位取反~。
1 #include "stdio.h" 2 main() 3 { 4 int a,b; 5 a=234; 6 b=~a; 7 printf("\40: The a's 1 complement(decimal) is %d \n",b); 8 a=~a; 9 printf("\40: The a's 1 complement(hexidecimal) is %x \n",a); 10 system("pause"); 11 }
(之后绘图的部分省略)