2020年3月3日PAT

13598 ワード

文書ディレクトリ
  • A 1020 25'欲張り`easy`
  • B 1023 20'欲張り`easy`
  • A 1020 25’欲張りeasy
    #include 
    #include 
    #include 
    using namespace std;
    struct cake{
    	double store;
    	double sell;
    	double price;
    };
    bool cmp(cake a, cake b)
    {
    	return a.price > b.price;
    }
    vector<cake> cakes;
    void p(vector<cake> cakes)
    {
    	for (auto i : cakes)
    	{
    		printf("%f %f %f
    "
    , i.store, i.sell, i.price); } } void test() { int n; double m; cin >> n >> m; cake a; while (n--) { cin >> a.store; cakes.push_back(a); } for (int i = 0; i < cakes.size(); ++i) { cin >> a.sell; cakes[i].sell = a.sell; cakes[i].price = cakes[i].sell / cakes[i].store; } sort(cakes.begin(), cakes.end(),cmp); double sum = 0; for (auto i : cakes) { if (i.store <= m) { sum += i.sell; m -= i.store; } else { sum = sum + i.price*m; break; } } printf("%.2lf", sum); } int main() { test(); system("pause"); return 0; }

    B 1023 20’貪欲easy
    #include 
    #include 
    #include 
    using namespace std;
    
    void test()
    {
    	int a[10];
    	for (int i = 0; i < 10; ++i)
    		scanf_s("%d", &a[i]);
    	for (int i = 1; i < 10; ++i)
    	{
    		if (a[i] > 0)
    		{
    			cout << i;
    			a[i]--;
    			break;
    		}
    	}
    	for (int i = 0; i < 10; ++i)
    	{
    		for (int j = 0; j < a[i]; ++j)
    		{
    			cout << i;
    		}
    	}
    }
    int main()
    {
    	test();
    	system("pause");
    	return 0;
    }