#include<iostream>usingnamespace std;typedeflonglong LL;LL gcd(LL a, LL b){return b ?gcd(b, a % b): a;}intmain(){int n;cin >> n;LL a =0, b =1;for(int i =0; i < n; i ++){LL c, d;scanf("%lld/%lld",&c,&d);LL t =gcd(c, d);c /= t, d /= t;t =gcd(b, d);a = d / t * a + b / t * c;b = b / t * d;t =gcd(a, b);a /= t, b /= t;}if(b ==1) cout << a;else{if(a > b)printf("%lld ", a / b), a %= b;printf("%lld/%lld", a, b);}return0;}
#include<iostream>#include<vector>usingnamespace std;intmain(){int n;cin >> n;vector<int> res;for(int i =2; i * i <= n; i ++)if(n % i ==0){vector<int> seq;for(int m = n, j = i; m % j ==0; j ++){seq.push_back(j);m /= j;}if(seq.size()> res.size()) res = seq;}if(res.empty()) res.push_back(n);cout << res.size()<< endl;cout << res[0];for(int i =1; i < res.size(); i ++) cout <<"*"<< res[i];return0;}
1103 Integer Factorization (30 分)
題意 :
正整數(shù) N 的 K?P 分解,是將 N 寫為 K 個(gè)正整數(shù)的 P 次冪的和。
#include<iostream>usingnamespace std;intmain(){int n;cin >> n;longdouble res =0;for(int i =1; i <= n; i ++){longdouble x;cin >> x;res += x * i *(n - i +1);}printf("%.2Lf", res);return0;}#include<iostream>usingnamespace std;typedeflonglong ll;intmain(){int n;cin >> n;ll res =0;for(int i =1; i <= n; i ++){double x;cin >> x;res +=(ll)(1000* x)* i *(n - i +1);// 乘1000放大數(shù)字,避免精度的影響,注意括號(hào)}printf("%.2lf", res /1000.0);// 加上.0可以轉(zhuǎn)化成小數(shù)return0;}
#include<iostream>usingnamespace std;constint N =200;int st[N];// 1沒壞,0壞了,2輸出過intmain(){string s;int k;cin >> k >> s;for(int i =0; i < s.size(); i ++){int j = i +1;while(j < s.size()&& s[j]== s[i]) j ++;int len = j -1- i +1;if(len % k) st[s[i]]=1;i = j -1;// 因?yàn)檫€要 ++}string res;for(int i =0; i < s.size(); i ++){if(!st[s[i]]) cout << s[i], st[s[i]]=2;if(st[s[i]]==1) res += s[i];else{res += s[i];i += k -1;}}cout << endl << res << endl;return0;}
#include<iostream>usingnamespace std;constint N =4e4;int primes[N], cnt;bool st[N];voidinit(){for(int i =2; i < N; i ++)if(!st[i]){primes[cnt ++]= i;for(int j = i *2; j < N; j += i)st[j]=true;}}boolcheck(string s){int x =stoi(s);for(int i =0; primes[i]<= x / primes[i]; i ++)if(x % primes[i]==0)returnfalse;returntrue;}intmain(){init();int l, k;string s;cin >> l >> k >> s;for(int i =0; i + k <= l; i ++){string ss = s.substr(i, k);if(check(ss)){cout << ss;return0;}}cout <<404;return0;}