顯示具有 ACM 標籤的文章。 顯示所有文章
顯示具有 ACM 標籤的文章。 顯示所有文章

2016/07/11

[ACM] Q10008

#include 
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <algorithm>

struct item{
 char chr;
 int time;
};

using namespace std;

int main(){

 int n;
 item data[26];
 char str[1024]="", line[256];
 
 for(int i=0; i<26; i++){ 
  data[i].chr = 'A'+i;
  data[i].time=0;
 }
 
 scanf("%d", &n);
 getchar();
 
 while(n){
  gets(line);
  strcat(str, line);
  n--;
 }
 
 for(int i=0; i<strlen(str); i++){
  
  if(isalpha(str[i])){
   
   if(isupper(str[i])){
    //upper letter
    data[str[i]-'A'].time++;
    
   }else{
    //lower letter
    data[str[i]-'a'].time++;
   }
  }
  
 }
 
 //sort
 for(int i=0; i<26; i++){
  
  for(int j=i+1; j<26; j++){
   
   if(data[j].time > data[i].time){
    swap(data[i], data[j]);
    
   }else if(data[i].time == data[j].time){
    
    if(toupper(data[i].chr) > toupper(data[j].chr)){
     swap(data[i], data[j]);
    }
   }
  }
 }
 
 //display result
 for(int i=0; i<26; i++){
  if(data[i].time > 0){
   printf("%c %d\n", data[i].chr, data[i].time);
  }
 }
 
 return 0;

2014/07/29

[ACM] Q490 Rotating Sentences

輸出的地方看起來是正確了,不過Online Judge給Wrong Answer 不知道是不是因為沒有對輸入資料合法性作確認的緣故,待修正。

[ACM] Q10409 Die Game

交換兩個面後,利用骰子對面點數和為7的特性,求出剩餘兩個面的值。

2014/06/29

[ACM] Q263 Number Chains

發現用atoi()轉換的話, "00135"可以正確轉為135
這樣就可省去自己判斷產生出來最小值的位數問題
另外在C中可用atoi()、atol()將字串(char [])轉為整數
要將整數轉回字串的話,可以用sprintf(string, "%d", int);
P.S 有個itoa()是Windows底下的整數轉字串函數,要跨平台的話必須用sprintf()
itoa()
sprintf()

2013/07/26

[ACM] Q483 Word Scramble

#include <iostream>

using namespace std;

string ReverseString(string str)
{
    int Length = str.length();
    string Temp;

    for(int i=Length-1; i>=0; i--)
    {
        Temp += str[i];
    }

    return Temp;
}

int main()
{
    string Message;
    string GetVolcabulary;

    while(true)
    {
        cout << "Please input message:" <<endl;
        getline(cin,Message);
        Message += " ";

        for(int i=0; i<Message.length(); i++)
        {
            if(Message[i] != ' ')
            {
                GetVolcabulary += Message[i];
            }

            else
            {
                cout << ReverseString(GetVolcabulary) << " ";
                GetVolcabulary = "";
            }
        }

        cout << endl;
    }
    cin.get();
    return 0;
}

2013/05/15

[ACM]Q10929 : You can say 11


#include <cstdlib>
#include <iostream>
#include <sstream>

using namespace std;

int CharToInt(char chr)
{
    stringstream ss;
    int N;
    ss << chr;
    ss >> N;
    return N;
}

int main(int argc, char *argv[])
{
    int Sum = 0;
    string str;
 
    while(true)
    {
        cout << "Please input a number: ";
        getline(cin,str);
     
        if(str == "0")
        {
            break;
        }
     
        for(int i=0; i<str.length(); i++)
        {
            if(i % 2 == 0)
            {
                Sum = Sum + CharToInt(str[i]);
            }
            else
            {
                Sum = Sum - CharToInt(str[i]);
            }
        }
     
        if(Sum == 0)
        {
            cout << str << " is a multiple of 11..." << endl;
        }
        else
        {
            cout << str << " is not a multiple of 11..." << endl;
        }
     
        Sum = 0;
    }
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

2013/02/08

[ACM]Q445 Marvelous Mazes


題目 http://luckycat.kshs.kh.edu.tw/homework/q445.htm

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int Sum(string str)
{
    stringstream ss;
    int A=0;
    int Sum=0;
 
    for(int i=0; i<str.length(); i++)
    {
        ss << str[i];
        ss >> A;
        Sum = Sum + A;
        ss.clear();
    }
 
    return Sum;
}

int main(int argc, char *argv[])
{
    //cout << Sum("1234") << endl;
    string GetNum = "";
    string Line = "";
    string Target = "0123456789";
    ifstream infile("input.txt",ios::in);
    int Time = 0;
 
    while(!infile.eof())
    {
        getline(infile,Line);
        Line = Line + "!";
        for(int i=0; i<Line.length(); i++)
        {
            if(Target.find(Line[i]) != -1)
            {
                GetNum = GetNum + Line[i];
            }
            else
            {
                if(Line[i] == '!')
                {
                    cout << endl;
                }
             
                else
                {
                    Time = Sum(GetNum);
                    for(int j=0; j<Time; j++)
                    {
                        if(Line[i] == 'b')
                        {
                            cout << " ";
                        }
                     
                        else
                        {
                            cout << Line[i];
                        }
                    }
                    GetNum = "";
                }
            }
        }
    }
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

2011/08/17

[ACM]Q10310 Dog and Gopher

嗯 首先呢 這是Dog&Gopher進階版(其實是誤解題意之下的產物)
上週卡到暑輔考試
 一直沒碰的Dog&Gopher
週末完成結果卡在pow()的用法上 compiler一直不給過...
昨天到學校待了一天 跟著老師Debug 修修補補之後才把原本的bug處理完...
重點是
後來才發現這題其實是不需要讀檔 用cin來寫就可以了(悲劇~~往好處想是多練習了一次開檔案處理???)

 不過這段Code應該還可以更精簡...

原題:http://luckycat.kshs.kh.edu.tw/homework/q10310.htm
測資:下載