2023.4.19
1 //1.7最佳存款方案 2 //假设银行一年整存零取的月息为0.63%。现在某人手中有一笔钱,他打算在今后的 3 //五年中的每年年底取出1000元,到第五年时正好取完,请算出他存钱时应存入多少 4 #include<iostream> 5 using namespace std; 6 int main() 7 { 8 float m,n; 9 m = 1000/(1+12*0.0063); 10 for(int i = 0;i<4;i++) 11 { 12 n = (m + 1000)/(1+12*0.0063); 13 m = n; 14 } 15 cout<<n; 16 return 0; 17 }
1 //1.8冒泡排序 2 //对N个整数(数据由键盘输入)进行升序排列 3 #include<iostream> 4 using namespace std; 5 int main() 6 { 7 int N; 8 cout<<"请输入整数N:"<<endl; 9 cin>>N; 10 int arr[100]; 11 for(int i = 0;i<N;i++) 12 { 13 cin>>arr[i]; 14 } 15 for(int j = 0;j<N-1;j++) 16 { 17 for(int k = 0;k<N-j-1;k++) 18 { 19 if(arr[k]>arr[k+1]) 20 { 21 int temp = arr[k]; 22 arr[k] = arr[k+1]; 23 arr[k+1] = temp; 24 } 25 } 26 } 27 for(int i = 0;i<N;i++) 28 { 29 cout<<arr[i]<<" "; 30 } 31 return 0; 32 }
1 //1.9折半查找 2 //N个有序整数数列已放在一维数组中,利用二分查找法查找整数m在数组中的位置。 3 //若找到,则输出其下标值;反之,则输出“Not be found!” 4 #include<iostream> 5 using namespace std; 6 int main() 7 { 8 int arr[10] = {1,2,3,4,5,6,7,8,9,10}; 9 int low = 0; 10 int high = 9; 11 int mid = (low+high)/2; 12 int m; 13 cout<<"请输入要查找的值:"<<endl; 14 cin>>m; 15 while(low<mid) 16 { 17 if(m == arr[mid]) 18 { 19 cout<<"下标为:"<<mid<<endl; 20 break; 21 } 22 else if(m>arr[mid]) 23 { 24 low = mid; 25 mid = (low+high)/2; 26 } 27 else if(m<arr[mid]) 28 { 29 high = mid; 30 mid = (low+high)/2; 31 } 32 } 33 return 0; 34 }