c語言練習篇(5)

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

1.
// 沒有sort沒有cmp時本身默認是從小到大,所以這個地方寫了一個從大到下的cmp 
    
bool cmp(char a,char b){    參數類型看情況寫  
      return a>b; //可以理解爲 a > b 時把a放在b前面 
}

using namespace std;
int main(){
    //char c[] = {'a',  'n', 'c', 'd'};
    char c[] = {'T',  'W', 'A', 'K'};
    sort(c, c + 4,cmp);
    for(int i = 0; i < 4; i++){
        printf("%c",c[i]);        
    }   
    
    system("pause");
    return 0;
}
*/



struct Student{
     char id[15];    //准考證號
     int score;      //分數
     int location_number;   //考場號
     int local_rank; //考場內排名 
}stu[30010];

bool cmp(Student a, Student b){
   if(a.score != b.score)  return a.score > b.score;     //先按分數從高到底排序
   else return a.id < b.id; 
} 

int main(){
    int n,k,num=0;       //num 爲總考生數
    scanf("%d",&n);      //n爲考場數
    for(int i = 1; i <= n; i++){
       scanf("%d",&k);   //k爲幾號考場的人數 
       //printf("ss") ;
       for(int j = 0; j < k; j++){
           scanf("%s %d", stu[num].id, &stu[num].score);
           stu[num].location_number = i;           //該考生的考場號爲i
           num++;                                  //總考生數加1 
       }        
       //printf("sseeee\n") ;
       sort(stu + num - k, stu + num, cmp);        //該考場的考生排序
       
      
       stu[num - k].local_rank = 1;                //該 考場第一名的 locat_rank 爲1
       //printf("sseeeerree\n") ;
       for(int j = num - k + 1; j < num; j++){       //對該考場剩餘的考生
           //printf("sseeeeoooooo") ;
           if(stu[j].score == stu[j - 1].score){
                           
              //則j - 1的local_rank 與它前面的相同
              stu[j].local_rank = stu[j - 1].local_rank;            
           }else{    //如果與前一位考生的分不同
               stu[j].local_rank = j + 1 - (num - k);  
           }
       } 
    } 
    printf("%d\n", num);              //輸出總考生數
    sort(stu, stu + num,cmp);         //將所有考生排序
    int r = 1;                        //當前考生的排名
    for(int i = 0; i < num; i++){
        if(i > 0 && stu[i].score != stu[i - 1].score){
            r = r + 1;      //當前考生與上一個考生分數不同時,讓r更新爲人數+1 
        } 
        printf("%s ",stu[i].id);
        printf("%d %d %d\n", r, stu[i].location_number,stu[i].local_rank);
    } 
    
    system("pause");
    return 0;
    
}

2.
//計算n!中有多少個質因子 
    
int cal(int n, int p){
    int count = 0;
    for(int i = 2; i <= n; i++){
        int temp = i;
        while(temp % p == 0){
              count++;
              temp = temp / p;           
        }        
    }    
    return count;
}

int main(){
    int n, p;
    scanf("%d%d", &n, &p);
    int count = cal(n,p);
    printf("%d",count);
    
    system("pause");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

3.
    
int main(){    
    double n;
    scanf("%lf",&n); //輸入的是浮點數   lf  不是 數字1 1f         
    //printf("%.1f\n",n);
    if(n <= 3){
       printf("10.0");     
    }
    if(n>3 && n<=10){
       printf("%.1f",10 + (n-3)*2);       
    }
    if(n>10){
       printf("%.1f",10 + 7*2 + (n-10)*3);    //輸出保留一位小數   .1f  裏面的是1  不是小寫的L 
    }
    
    system("pause");
    return 0;
}

4.
//pow函數的使用   網信院的題  求 2.7的7次方

int main(){
    double x = 2.7;
    double y = 7;
    printf("%f\n",pow(x,y));   
    
    system("pause");
    return 0;
} 

5.
//輸入n個數,要求0到9的整數,統計每個數出現的次數,按照數字順序輸出 

int main(){
    int n;
    scanf("%d",&n);
    int num[n];
    
    for(int i = 0; i < 10; i++){
        num[i] = 0;        
    }
    
    int x = 0;
    for(int i = 0; i < n; i++){
        scanf("%d",&x);
        num[x]++; 
    }    
    
    for(int i = 0; i < 10; i++){
        if(num[i] != 0){    
           printf("%d %d\n",i,num[i]);  
        }      
    }
    
    system("pause");
    return 0;
}

6.
//輸入數字,計算每個數組出現的次數(期數用傳統字符數組存也行)
    
int main()
  {
      char c;//表示一個字符
      int shu[10]={0},i;
      while((c=getchar())!='\n')
      {
        if(c>='0'&&c<='9')
             shu[c-'0']++;    // 字符0  `0`的ASCII碼等於48  字符`1`的ASCII碼爲49  所以 字符數字變成 常數字  可用ASCII碼相減來實現 
      }
         for(i=0;i<=9;i++){
         printf("%d有%d個\n",i,shu[i]);
         }
         system("pause");
         return 0;
}
/*
如樓上所說while((c=getchar())!= '\n ') cout < <c < <endl; 
將是: 
輸入: abc回車 
輸出: a 
b 
c 
*/

                                         2020.5.2/週六
                                         by 922
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章