1. 程式人生 > >程式設計與演算法 第十週測驗

程式設計與演算法 第十週測驗

1:成績排序

#include <iostream>
#include <cstring>
using namespace std;
#define MAX_NUM 20
#define MAX_NAME 20
struct Student {
  char name[MAX_NAME+1];
  int score;
};
int main(void)
{
  Student student[MAX_NUM] = {{'\0',-1}};
  int n = 0;
  cin >> n;
  for(int c=0; c<n; c++)
  {
    Student s = {{'\0'
,-1}}; cin >> s.name >> s.score; int p = c; for(int i=0; i<c; i++) { if(student[i].score < s.score || (student[i].score == s.score && strcmp(student[i].name, s.name)>0)) { p = i; for(int j=c; j>i; j--) student[j] = student[j-1
]; break; } } student[p] = s; } for(int c=0; c<n; c++) cout << student[c].name << " " << student[c].score << endl; return 0; }

2:分數線劃定

#include <iostream>
using namespace std;
struct Competitor {
  int k;
  int s;
};
#define MAX_NUM 5000
int main(void) { int n = 0; int m = 0; Competitor competitor[MAX_NUM] = {{0,0}}; cin >> n >> m; for(int c=0; c<n; c++) { Competitor comp = {0,0}; cin >> comp.k >> comp.s; int p = c; for(int i=0; i<c; i++) { if(competitor[i].s < comp.s || (competitor[i].s == comp.s && competitor[i].k > comp.k)) { p = i; for(int j=c; j>i; j--) competitor[j] = competitor[j-1]; break; } } competitor[p] = comp; } int score = competitor[m*150/100 - 1].s; int cnt = 0; for(cnt=0;cnt<n && competitor[cnt].s>=score;cnt++); cout << score << " " << cnt << endl; for(int i=0;i<cnt;i++) cout << competitor[i].k << " " << competitor[i].s << endl; return 0; }

3:病人排隊

#include <iostream>
using namespace std;
#define MAX_NAME 10
#define MAX_NUM 100
struct Sick {
  char id[MAX_NAME];
  int age;
};
int main(void)
{
  int n = 0;
  int m = 0;
  Sick sick_old[MAX_NUM] = {{'\0',0}};
  Sick sick_young[MAX_NUM] = {{'\0',0}};
  int yi = 0;
  int oi = 0;
  cin >> n;
  for(int c=0; c<n; c++)
  {
    Sick sk = {'\0',0};
    cin >> sk.id >> sk.age;
    if (sk.age < 60)
    {
      sick_young[yi++] = sk;
    }
    else
    {
      oi++;
      int p = c;
      for(int i=0; i<c; i++)
      {
        if(sick_old[i].age < sk.age)
        {
          p = i;
          for(int j=c; j>i; j--)
            sick_old[j] = sick_old[j-1];
          break;
        }
      }
      sick_old[p] = sk;
    }
  }
  for(int i=0;i<oi;i++)
    cout << sick_old[i].id << endl;
  for(int i=0;i<yi;i++)
    cout << sick_young[i].id << endl;
  return 0;
}

4:mysort

#include <iostream>
using namespace std;
struct A {
    int nouse1;
    int nouse2;
    int n;
};
// 在此處補充你的程式碼
void mysort(void *list_in, int len, int size, int (*func)(const void *,const void *))
{
  char *list = (char *)list_in;
  for (int i = len-1; i > 0; --i)
  {
    for(int j = 0; j < i; ++j)
    {
      if(func(list+j*size, list+(j+1)*size)>0)
      {
        for(int c=0; c<size; c++)
        {
          char temp = *(list+j*size+c);
          *(list+j*size+c) = *(list+(j+1)*size+c);
          *(list+(j+1)*size+c) = temp;
        }
      }
    }
  }
}
int MyCompare1( const void * e1,const void * e2) 
{
    int * p1 = (int * ) e1;
    int * p2 = (int * ) e2;
    return * p1 - * p2;
}
int MyCompare2( const void * e1,const void * e2) 
{
    int * p1 = (int * ) e1;
    int * p2 = (int * ) e2;
    if( (* p1 %10) - (* p2 % 10))
        return (* p1 %10) - (* p2 % 10);
    else
        return * p1 - * p2;
}
int MyCompare3( const void * e1,const void * e2) 
{
    A * p1 = (A*) e1;
    A * p2 = (A*) e2;
    return p1->n - p2->n;
}
int a[20];
A b[20];
int main ()
{   
    int n;
    while(cin >> n) {
        for(int i = 0;i < n; ++i) {
            cin >> a[i];
            b[i].n = a[i];
        }
        mysort(a,n,sizeof(int),MyCompare1);
        for(int i = 0;i < n; ++i) 
            cout << a[i] << "," ;
        cout << endl;
        mysort(a,n,sizeof(int),MyCompare2);
        for(int i = 0;i < n; ++i) 
            cout << a[i] << "," ;
        cout << endl;
        mysort(b,n,sizeof(A),MyCompare3);
        for(int i = 0;i < n; ++i) 
            cout << b[i].n << "," ;
        cout << endl;
    }
    return 0;
}

5:從字串中取數

#include <iostream>
#include <iomanip>
using namespace std;
double GetDoubleFromString(char * str)
{
// 在此處補充你的程式碼
  static char *s = NULL;
  if(str != NULL)
  {
    s = str;
  }
  int f = 1;
  double t = 0;
  double n = 0;
  for(;*s!='\0';s++)
  {
    if(*s!='.'&&(*s<'0'||'9'<*s) )
    {
      if(f==0)
        break;
    }
    else if (*s=='.'&&*(s+1)=='.')
    {
      s++;
      if(f==0)
        break;
    }
    else if(*s=='.')
    {
      t=1;
    }
    else
    {
      f = 0;
      if(t==0)
      {
        n = n*10 + (*s-'0');
      }
      else
      {
        t *= 10;
        n += (*s-'0')/t;
      }
    }
  }
  if(f==1)
  {
    return -1;
  }
  else
  {
    return n;
  }
}

int main()
{
    char line[300];
    while(cin.getline(line,280)) {
        double n;
        n = GetDoubleFromString(line);
        while( n > 0) {
            cout << fixed << setprecision(6) << n << endl;
            n = GetDoubleFromString(NULL);
        }
    }
    return 0;
}

-eof-

相關推薦

程式設計演算法 測驗

1:成績排序 #include <iostream> #include <cstring> using namespace std; #define MAX_NUM 20 #define MAX_NAME 20 struct Stu

程式設計演算法 測驗

1:Pell數列 #include <iostream> using namespace std; int main() { int n; long k; long long P[1000000+1]={0}; P[1]=1;

演算法設計分析Leetcode作業

Edit Distance Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You hav

201771010123汪慧和《面向物件程式設計Java》實驗總結

一、理論部分 1、泛型:也稱引數化型別。就是定義類、介面和方法時,通過型別引數指示將要處理的物件型別。 2、泛型程式設計:編寫程式碼可以被很多不同型別的物件所重用。 3、泛型方法: a.除了泛型類外,還可以只單獨定義一個方法作為泛型方法,用於指定方法引數或者返回值為泛型型別,留待方法呼叫時確定。 b

201771010134楊其菊《面向物件程式設計java》學習總結

         第8章泛型程式設計學習總結  第一部分:理論知識  主要內容:   什麼是泛型程式設計             &

Coursera-吳恩達-機器學習--測驗-Large Scale Machine Learning

本片文章內容: Coursera吳恩達機器學習課程,第十週 Large Scale Machine Learning 部分的測驗,題目及答案截圖。 1.cost increase ,說明資料diverge。減小learning rate。 stochastic不需要每步都是減

coursera Machine Learning 測驗quiz答案解析Large Scale Machine Learning

1.選擇:D 解析:由於代價函式上升了,所以應該減少學習速率,選擇D 2.選擇:BC 解析:A並不需要代價函式總是減少,可能會降低故錯誤。B在執行隨機梯度下降演算法前最好將樣本打亂隨機化,正確。C也就隨機的優點正確。D並行可不是隨機的優點,是對映約減的優點,故錯

程式設計演算法(二)測驗 3:棋盤問題

3:棋盤問題 檢視 提交 統計 提問 總時間限制:  1000ms   記憶體限制:  65536kB 描述 在一個給定形狀的棋盤(形狀可能是不規則的)上面擺放棋子,棋子沒有區別。要求擺放時任意的兩個棋子不能放

程式設計演算法(二)測驗 2:A Knight's Journey

2:A Knight's Journey 檢視 提交 統計 提問 總時間限制:  1000ms   記憶體限制:  65536kB 描述 Background The knight is getting bor

程式設計演算法(二)測驗 1:紅

1:紅與黑 檢視 提交 統計 提問 總時間限制:  1000ms   記憶體限制:  65536kB 描述 有一間長方形的房子,地上鋪了紅色、黑色兩種顏色的正方形瓷磚。你站在其中一塊黑色的瓷磚上,只能向相鄰的黑

程式設計演算法(一)測驗

8:奧運獎牌計數 描述 2008年北京奧運會,A國的運動員參與了n天的決賽專案(1≤n≤17)。現在要統計一下A國所獲得的金、銀、銅牌數目及總獎牌數。2008年北京奧運會,A國的運動員參與了n天的決賽專案(1≤n≤17)。現在要統計一下A國所獲得的金、銀、銅牌

程式設計演算法(三) c++新特性和c++高階主題(3)

強制型別轉換 :static_cast、interpret_cast、const_cast、dynamic_cast 1、static_cast:static_cast 用來進行比較“自然”和低風險的轉

北京大學MOOC C++程式設計 程式設計演算法(三)測驗

1:返回什麼才好呢 程式填空,使其按要求輸出 #include <iostream> using namespace std; class A { public: int val; A(int // 在此處補充你的程式碼 }; int main()

北京大學MOOC C++程式設計 程式設計演算法(三)測驗

1:簡單的SumArray 描述 填寫模板 PrintArray,使得程式輸出結果是: TomJackMaryJohn 10 不得編寫SumArray函式 #include <iostream> #include <string> using n

程式設計演算法(三) c++新特性和c++高階主題 (2)

無序容器(雜湊表) //雜湊表插入和查詢的時間複雜度幾乎是常數 #include <iostream> #include <string> #include <unordered_map> using namespace std; int main

程式設計演算法(三) c++新特性和c++高階主題 (1)

#include <iostream> using namespace std; class A{}; A operator+(int n, const A& a) { return a; } template<class T1, class

程式設計演算法測驗finish

1:全面的MyString 檢視 提交 #include <cstdlib> #include <iostream> using namespace std; int strlen(const char * s) { int i = 0; fo

馬凱軍201771010116《面向物件程式設計Java》學習總結

一.理論知識部分 第九章  集合 1.資料結構介紹:線性結構:線性表,棧,佇列,串,陣列,檔案。非線性結構:樹,圖。 散列表:又稱為雜湊表。 散列表演算法的基本思想是:以結點的關鍵字為自變數,通過一定的函式關係(雜湊函式)計算出對應的函式值,以這個值作為該結點儲存在散列表中的地址。當散列表中

馬凱軍201771010116《面向物件程式設計Java》學習總結

一、理論與知識學習部分 Java的抽象視窗工具箱(Abstract Window Toolkit, AWT)包含在java.awt包中,它提供了許多用來設計GUI的元件類和容器類。 大部分AWT元件都有其Swing的等價元件,Swing元件的名字一般是在AWT元件名前面新增一個字母“J”。 通常把由Co

演算法設計分析》作業

《演算法設計與分析》第十週作業 標籤(空格分隔): 課堂作業 文章目錄 《演算法設計與分析》第十週作業 @[toc] 題目概要 思路 具體實現 心得 原始碼: