npsc2012
2016年1月17日 星期日
2014年9月27日 星期六
csv to token "1,2,3" => [1,2,3]
std::string input = "abc,def,ghi";
std::istringstream ss(input);
std::string token;
while(std::getline(ss, token, ',')) {
std::cout << token << '\n';
}
source:
http://stackoverflow.com/questions/11719538/how-to-use-stringstream-to-separate-comma-separated-strings
std::strtok
2014年9月8日 星期一
跑者的修練
#include <iostream>
using namespace std;
int main()
{
int T;
cin>>T;
long long M,N;
while(T--)
{
cin>>M>>N;
long long ans=(1+N)*N/2;
if(N>M)
ans+=(((N-M)+(N%M))*(int)(N/M))/2;
cout<<ans<<endl;
}
return 0;
}
2014年8月23日 星期六
b024: 指南宮的階梯 (luke)
- /**********************************************************************************/
- /* Problem: b024 "指南宮的階梯" from 動態規劃-爬樓梯問題 */
- /* Language: C++ */
- /* Result: AC (4ms, 184KB) on ZeroJudge */
- /* Author: luke at 2014-08-23 15:38:35 */
- /**********************************************************************************/
- #include <iostream>
- #include <iomanip>
- #include <stdio.h>
- using namespace std;
- long long int a[100];
- long long int solve( int number){
- if(a[number]!=0){
- return a[number];
- }
- if(number==1){
- a[1]=1;
- return 1;
- }
- if(number==2){
- a[2]=2;
- return 2;
- }
- else{a[number]=solve(number-1)+solve(number-2);
- return solve(number-1)+solve(number-2);}
- }
- int main () {
- int m;
- cin>>m;
- cout<<solve(m)<<" ";
- long long int k=solve(m);
- int v=k%m;
- cout<<solve(v);
- return 0;
- }
2014年8月21日 星期四
b021 "指考分發" (skipper)
- /**********************************************************************************/
- /* Problem: b021 "指考分發" from 排序-複合欄位 */
- /* Language: C++ */
- /* Result: AC (4ms, 200KB) on ZeroJudge */
- /* Author: skipper at 2014-08-22 10:30:20 */
- /**********************************************************************************/
- #include <iostream>
- #include <algorithm>
- #include <numeric>
- using namespace std;
- struct Score {
- int no,subj[5],total;
- };
- bool compare (Score a,Score b) {
- if (a.total>b.total) return true;
- if (a.total==b.total) return a.subj[2]>=b.subj[2];
- return false;
- }
- int main ()
- {
- int N; cin >> N;
- Score *s;
- s= new Score[N];
- for (int i=0;i<N;i++){
- cin >> s[i].no; s[i].total = 0;
- for (int j=0;j<5;j++) { cin >> s[i].subj[j];}
- s[i].total=accumulate(s[i].subj,s[i].subj+5,0);
- }
- sort(s,s+N,compare);
- for (int i=0;i<N;i++){
- cout << s[i].no << endl;
- }
- }
執行時間測量(gcd)
#include <time.h>
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
int gcd1(int a, int b){
for (int d = 1; d <= min(a, b); d++){
if (a%d == 0 && b%d == 0) return d;
}
}
int gcd2(int a, int b){
if (a<b) swap(a, b);
if (a%b)
return b;
else
return gcd2(a - b, b);
}
// source:
http://www.math.wustl.edu/~victor/mfmm/compaa/gcd.c
/* Standard C
Function: Greatest Common Divisor */
int gcd(int a, int b)
{
int c;
while (a != 0) {
c = a; a = b%a;
b = c;
}
return b;
}
/* Recursive
Standard C Function: Greatest Common Divisor */
int gcdr(int a, int b)
{
if (a == 0) return b;
return gcdr(b%a, a);
}
void main(){
clock_t begin = clock();
// your CODE
srand(clock());
int x = rand();
int y = rand();
for (int i = 0; i < 100000000; i++){
gcd1(x, y);
}
clock_t end = clock();
cout << end - begin;
//system("pause");
}
訂閱:
意見 (Atom)