c語言練習篇(3)

#include <stdio.h>
#include <stdlib.h>
#include <cstdio>
#include <algorithm>
using namespace std;

1.
//就是存儲有關的題,對輸入的數組,存儲,加以一定的約束判斷,給出答案
    
const int maxn = 100010;
bool hashTable[maxn]={false};
int main(){
    int n, m, x;
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n;i++){
        scanf("%d",&x);
        hashTable[x] = true;     //直接把輸入的數作爲數組的下標來對這個數組的性質進行統計(這種方法非常的使用,要牢記)  
    }    
    
    for(int i = 0; i < m; i++){
        scanf("%d",&x);
        if(hashTable[x] == true){
           printf("YES\n");                
        }else{
           printf("NO\n");        
        }
    } 
    
    system("pause");
    return 0;
}

/*
const int maxn = 100010;
int hashTable[maxn]={0};
int main(){
    int n, m, x;
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n;i++){
        scanf("%d",&x);
        hashTable[x]++;     //直接把輸入的數作爲數組的下標來對這個數組的性質進行統計(這種方法非常的使用,要牢記)  
    }    
    
    for(int i = 0; i < m; i++){
        scanf("%d",&x);
        printf("%d\n",hashTable[x]);
    } 
    
    system("pause");
    return 0;
}


*/

2.
//盈利的那道題  與錢有關  

struct mooncake{
     double store; //庫存量
     double sell;  //總售價
     double price; //單價 
} cake[1010];

bool cmp(mooncake a, mooncake b){
     return a.price > b.price;     
} 

int main(){
    int n;              //月餅數量
    double D;           //訂單需求量
    scanf("%d%lf",&n,&D);
    for(int i = 0; i < n; i++){
        scanf("%lf",&cake[i].store);
    } 
    //printf("c");
    for(int i = 0; i < n; i++){
        scanf("%lf",&cake[i].sell);
        cake[i].price = cake[i].sell/cake[i].store;
    } 
    
    sort(cake,cake+n,cmp);
    
    double money = 0;    //收益; 
    for(int i = 0; i < n;i++){
        if(D >= cake[i].store){
           D = D - cake[i].store; 
           money = money + cake[i].sell;    
        }else {
           money = money + D * cake[i].price;    
           break;  
        }
    }
    
    printf("%.2f",money);  //.1f  表示保留小數點後面一位     .2f表示保留小數點後面兩位 
    system("pause");
    return 0; 
    
}

/*

int cmp(int a, int b){
    return a < b;        
} 

int main(){
    int arr[10];
    
    for(int i = 0; i < 10; i++){
        scanf("%d",&arr[i]);        
    }
    sort(arr,arr+10,cmp);
    
    if(arr[0] == 0){
       int temp = 0;
       for(int i = 1; i < 10; i++){
           if(arr[i]!=0){
              temp = arr[i];
              arr[0] = arr[i];
              arr[i] = temp;
              break;              
           }
       } 
    }
    
    
    for(int i = 0; i < 10; i++){
        printf("%d",arr[i]);        
    }
    system("pause");
    return 0;
} 
 

*/

3.
//二分查找 
    
int binarySearch(int A[], int left, int right, int x){
    int mid;
    while(left<=right){
          mid = (left + right) / 2;
          if(A[mid] == x) return mid;
          else if(A[mid] > x){
                right = right - 1;     
          } else {
                 left = mid + 1;
          }
    }    
    return -1;
}

int main(){
    const int n = 10;
    int A[n] = {1, 3, 4 ,6, 7, 8, 10, 12 ,15};
    printf("%d %d\n",binarySearch(A, 0, n-1, 6),binarySearch(A, 0, n-1, 9));
    system("pause");
    return 0;    
} 
                                         2020.5.2/週六
                                         by 922
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章