USACO 三道题目

    技术2022-05-20  36

    今天做题没状态……

     

    【subset sums】

    /* ID: wangqia6 TASK: subset LANG: C++ */ #include <fstream> #include <cstring> using namespace std; long long f[800]; int i,j,n,sum[40]; int main() { ifstream cin ("subset.in"); ofstream cout("subset.out"); cin >> n; memset(sum,0,sizeof(sum)); for (i = 1; i <= n; i++) sum[i] = sum[i - 1] + i; if (sum[n] & 1) { cout << 0 << endl; cin.close(); cout.close(); return 0; } memset(f,0,sizeof(f)); f[0] = 1; f[1] = 1; for (i = 2; i <= n; i++) for (j = sum[i]; j >=i; j--) f[j] += f[j - i]; cout << (f[sum[n] >> 1] >> 1) << endl; cin.close(); cout.close(); return 0; }

     

    【preface numbering】

    /* ID: wangqia6 TASK: preface LANG: C++ */ #include <fstream> #include <cstring> using namespace std; const string LIST = "IVXLCDM"; long counter[7]; void make(int x) { int t; t = x % 10; if ((t >= 1) && (t < 4)) counter[0] +=t; else if ((t >= 5) && (t < 9)) { counter[1]++; counter[0] += t - 5; } else if (t == 4) { counter[1]++; counter[0]++; } else if (t == 9) { counter[2]++; counter[0]++; } x /= 10; if (x == 0) return; t = x % 10; if ((t >= 1) && (t < 4)) counter[2] +=t; else if ((t >= 5) && (t < 9)) { counter[2] += t - 5; counter[3]++; } else if (t == 4) { counter[3]++; counter[2]++; } else if (t == 9) { counter[4]++; counter[2]++; } x /= 10; if (x == 0) return; t = x % 10; if ((t >= 1) && (t < 4)) counter[4] +=t; else if ((t >= 5) && (t < 9)) { counter[4] += t - 5; counter[5]++; } else if (t == 4) { counter[5]++; counter[4]++; } else if (t == 9) { counter[6]++; counter[4]++; } x /= 10; if (x == 0) return; t = x % 10; counter[6] +=t; return; } int main() { ifstream cin ("preface.in"); ofstream cout ("preface.out"); int n; cin >> n; memset(counter,0,sizeof(counter)); for (int i = 1; i <= n; i++) make(i); for (int i = 0; i < 7; i++) if (counter[i] != 0) cout << LIST[i] << ' ' << counter[i] << endl; cin.close(); cout.close(); return 0; }

     

    【runaround numbers】

    /* ID: wangqia6 TASK: runround LANG: C++ */ #include <fstream> #include <cstring> #include <cmath> using namespace std; const double ZERO = 1e-6; bool check(long x) { long t,i,dig = long (floor(log10(x + ZERO))) + 1,num[11]; bool vis[11]; memset(vis,0,sizeof(vis)); for (i = 1; i <= dig; i++) { t = x % 10; num[dig - i] = t; if ((vis[t]) || (t == 0)) return false; else vis[t] = true; x /= 10; } t = 0; memset(vis,0,sizeof(vis)); for (i = 1; i <= dig; i++) { if (vis[t]) return false; else vis[t] = true; t += num[t]; t %= dig; } if (t != 0) return false; return true; } int main() { ifstream cin ("runround.in"); ofstream cout ("runround.out"); long n; cin >> n; for (long i = n + 1; ; i++) if (check(i)) { cout << i << endl; break; } cin.close(); cout.close(); return 0; }

     


    最新回复(0)