辽宁师范大学 • 张大为@https://daweizh.github.io/noip/
#include <iostream> using namespace std; int r1,r2; string s; int cton(char c){ int num=0; if(c>='A' && c<='Z') num = c-'A'+10; if(c>='a' && c<='z') num = c-'a'+10; if(c>='0' && c<='9') num = c-'0'; return num; } char ntoc(int n){ char c; if(n>=0 && n<=9) c = '0'+n; if(n>=10 && n<=15) c = 'A'+ n -10; return c; } long long toTen(string sn,int radix){ long long num = 0,t=1; int len = sn.size(); for(int i=len-1;i>=0;i--){ num = num + t*cton(sn[i]); t = t * radix; } return num; } string toRadix(long long n,int radix){ string s=""; if (n==0) s="0"; while(n>0){ int r=n%radix; n = n/radix; s = ntoc(r)+s; } return s; } int main(){ cin >> r1 >> s >> r2; long long temp = toTen(s,r1); s = toRadix(temp,r2); cout << s << endl; return 0; }
#include <iostream> using namespace std; int m=1,d=1,w,month[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int main(){ m=1; d=0; cin >> w; for(int i=1;i<=365;i++){ d++; if(d==13 && w==5){ cout << m << endl; } if(d==month[m]){ m++; d=0; } w++; w=w%7; if(w==0) w=7; } return 0; }
#include <iostream> #include <cmath> #include <cstdio> #include <string> using namespace std; int cnt=0; string s; double n[20],ans=0; int main(){ cin >> s; int x=0; int len = s.size(); for(int i=2;i<len;i++){ n[++x] = s[i]-'0'; } ans = 0; for(int i=1;i<=x;i++){ ans = ans + n[i]*pow(0.125,i); } cout << s << " [8] = "; printf("%.45g [10]\n",ans); return 0; }
#include <iostream> #include <string> using namespace std; int a[30],mx=0; string s; int main(){ for(int i=0;i<4;i++){ getline(cin,s); int len=s.size(); for(int i=0;i<len;i++){ if(s[i]>='A' && s[i]<='Z'){ int t = s[i]-'A'; a[t]++; if(a[t]>mx) mx = a[t]; } } } for(int l=mx;l>=1;l--){ for(int i=0;i<26;i++){ if(a[i]>=l) cout << "* "; else cout << " "; } cout << endl; } for(int i=0;i<26;i++) cout << char('A'+i) << " "; cout << endl; return 0; }
#include <iostream> using namespace std; const int maxn = 1005; int n,used[maxn],number[maxn],tot=0; int prime(int bnd){ int cnt=0; for(int i=2;i<=n;i++){ if(!used[i]){ number[++cnt]=i; } for(int j=1;(j<=cnt)&&(i*number[j]<=n);j++){ used[i*number[j]]=true; if(i%number[j]==0) break; } } return cnt; } bool palindrome(int n){ int s=n,x=0; while(s>0){ x = x*10+s%10; s=s/10; } return x==n; } int main(){ cin >> n; int len = prime(n); for(int i=5;i<=len;i++) if(palindrome(number[i])) tot++; cout << tot << endl; return 0; }
#include <iostream> #include <string> using namespace std; string src; bool judge(string s,int n){ int len = s.size(); int x =0; for(int i=len-1;i>=0;i--){ int t = s[i]-'0'; int a = t * n+x;; if(a>=10){ s[i]='0'+ a%10; x = a /10; }else{ x=0; s[i]='0'+a; } } string ls = s + s; string::size_type st=ls.find(src); if(st!=s.npos) return true; return false; } int main(){ cin >> src; int ln = src.size(),flag=1; for(int i=1;i<=ln;i++) if(!judge(src,i)){ flag = 0; break; } if(flag) cout << 1 << endl; else cout << 0 << endl; return 0; }
#include <iostream> #include <string> #include <map> using namespace std; string haab[]={"pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin", "mol", "chen", "yax", "zac", "ceh", "mac", "kankin", "muan", "pax", "koyab", "cumhu", "uayet"}; string holly[]={"imix", "ik", "akbal", "kan", "chicchan", "cimi", "manik", "lamat", "muluk", "ok", "chuen", "eb", "ben", "ix", "mem", "cib", "caban", "eznab", "canac", "ahau"}; map<string,int> hab; map<int,string> hly; int haabDays(int day,string month,int year){ return year*365 + hab[month]*20 + day; } int n,d,y; string m; char ch[100]; void hollyDate(int dys){ y = dys / 260; d = dys % 13+1; m = hly[dys % 20]; } int main(){ for(int i=0;i<19;i++) hab[haab[i]]=i; for(int i=0;i<20;i++) hly[i]=holly[i]; cin >> n; cout << n << endl; for(int i=0;i<n;i++){ scanf("%d. %s%d",&d,ch,&y); m = ch; int days = haabDays(d,m,y); hollyDate(days); cout << d << " " << m << " " << y << endl; } return 0; }
#include <iostream> using namespace std; int n,year,month,day,hour,minute,second,days,secs; int rnn(int y){ int cnt =0; for(int i=2000;i<=y;i++){ if(i%400==0 || (i%4==0 && i%100!=0)) cnt ++; } return cnt; } int rnd(int y){ int cnt = 0; if(y%400==0 || (y%4==0 && y%100!=0)) cnt ++; return cnt; } int m1[]={0,31,59,90,120,151,181,212,243,273,304,334,365}; int main(){ cin >> n; for(int i=0;i<n;i++){ scanf("%d:%d:%d%d.%d.%d",&hour,&minute,&second,&day,&month,&year); days = (year-2000) * 365 + rnn(year-1) + m1[month-1] + day -1; if(month>2) days = days + rnd(year); year = days /1000; days = days % 1000; month = days / 100 +1; day = days % 100+1; secs = (hour * 3600+minute*60+second)*125/108; hour = secs / (100*100); secs = secs % (100*100); minute = secs / 100; second = secs % 100; cout << hour << ":" << minute << ":" << second << " "; cout << day << "." << month << "." << year << endl; } return 0; }
#include <iostream> #include <string> using namespace std; const int maxn=500; string sa,sb; int a[maxn],b[maxn],c[maxn],la,lb,lc,x; int main(){ cin >> sa; cin >> sb; la = sa.size(); lb = sb.size(); for(int i=la-1;i>=0;i--) a[la-i] = sa[i] - 48; for(int i=lb-1;i>=0;i--) b[lb-i] = sb[i] - 48; for(int i=1;i<=la;i++){ x = 0; for(int j=1;j<=lb;j++){ c[i+j-1] +=(a[i]*b[j]+x); x = c[i+j-1]/10; c[i+j-1]%=10; } c[i+lb] = x; } lc = la+lb; while(c[lc]==0 && lc>1) lc --; for(int i=lc;i>=1;i--) cout << c[i]; cout << endl; return 0; }
#include <iostream> using namespace std; const int maxn = 1000005; int x,y,used[maxn],number[maxn],tt=0; int prime(int n){ int cnt = 0; for(int i=2;i<=n;i++){ if(!used[i]){ number[++cnt]=i; } for(int j=1;(j<=cnt)&&(i*number[j]<=n);j++){ used[i*number[j]]=true; if(i%number[j]==0) break; } } return cnt; } int main(){ cin >> x >> y; if(x>y) swap(x,y); int len = prime(y); for(int i=1;i<=len;i++){ if(number[i]>=x && number[i]<=y) tt++; } cout << tt << endl; return 0; }
#include <iostream> #include <cmath> #include <set> using namespace std; int x; set<int> a; bool isprime(int n){ if(n==1) return false; if(n<=3) return true; if(n%6!=1 && n%6!=5) return false; int rt = sqrt(n); for(int i=5;i<=rt;i+=6){ if(n%i==0 || n%(i+2)==0) return false; } return true; } int palindrome(int n){ int ans=n; n = n/10; while(n>0){ ans = ans*10 + n%10; n = n/10; } return ans; } int main(){ cin >> x; if(x==1){ cout << 4 << endl; cout << "2 3 5 7" << endl; }else if(x==2){ cout << 1 << endl; cout << 11 << endl; }else if(x%2==0){ cout << 0 << endl; }else{ x = (x+1)/2; int l = pow(10,x-1); int r = pow(10,x)-1; for(int i=l;i<=r;i++){ int t = palindrome(i); if(isprime(t)) a.insert(t); } cout << a.size() << endl;; for(set<int>::iterator it=a.begin();it!=a.end();it++) cout << *it << " "; cout << endl; } return 0; }
#include <iostream> using namespace std; int n,p,q,x,y; char c; int gcd(int a,int b){ int r = a%b; while(r){ a=b; b=r; r=a%b; } return b; } int main(){ cin >> n; cin >> x >> c >> y; for(int i=1;i<n;i++){ cin >> p >> c >> q; int x1 = (x*q+y*p); int y1 = y*q; int gd = gcd(x1,y1); x = x1 / gd; y = y1 / gd; } if(y==1) cout << x << endl; else cout << x << "/" << y << endl; return 0; }
#include <iostream> using namespace std; int n,t; int main(){ cin >> n; t = n/100; cout << t << endl; n = n-t*100; t = n/50; cout << t << endl; n = n-t*50; t = n/20; cout << t << endl; n = n-t*20; t = n/10; cout << t << endl; n = n-t*10; t = n/5; cout << t << endl; n = n-t*5; cout << n << endl; return 0; }
#include <iostream> #include <cstring> using namespace std; int n,p,i,a[10]; bool dbl(int x){ memset(a,0,sizeof(a)); while(x){ int r = x%10; a[r]++; x = x/10; } for(int i=0;i<10;i++) if(a[i]>1) return true; return false; } int main(){ cin >> n; for(i=10;i<=31;i++){ if(dbl(i*i)){ p++; if(p==n) break; } } cout << i*i << endl; return 0; }
#include <iostream> #include <string> #include <cstdio> using namespace std; string s[130],x; int n,p,cnt[130]; int main(){ cin >> n; for(int i=0;i<n;i++){ cin >> x; int len = x.size(); p=0; if(x[p]=='-'){ s[i] +=x[p]; p++; }else if(x[p]=='+'){ p++; } for(;p<len;p++){ if(x[p]!='0') break; } for(;p<len;p++) s[i]+=x[p]; if(s[i].size()==0||s[i]=="-") s[i]="0"; } int flag = 1; for(int i=0;i<n;i++){ cnt[i]=1; for(int j=0;j<n;j++){ if(i==j) continue; if(s[i].compare(s[j])==0){ cnt[i]++; }else{ flag=0; } } } if(flag) cout << "no" << endl; else{ p=0; int mx = cnt[0]; for(int i=1;i<n;i++){ if(cnt[i]>mx){ mx = cnt[i]; p = i; } } cout << s[p] << endl; } return 0; }
#include <iostream> #include <string> #include <algorithm> using namespace std; struct wrd{ int len; string wd; } w[500]; string s; int p=0; bool comp(wrd a,wrd b){ return b.len < a.len; } int main(){ while(cin>>s){ p++; w[p].len = s.length(); w[p].wd = s; } w[p].len--; w[p].wd = w[p].wd.substr(0,w[p].len); stable_sort(w+1,w+p+1,comp); cout << w[1].wd << endl; return 0; }
#include <iostream> #include <string> using namespace std; int n,cnt,k; int main(){ cin >> n; string p[n+1]; for(int i=0;i<n;i++) cin >> p[i]; cnt = 0; k=0; do{ if(cnt==0){ cout << p[k]; cnt +=p[k++].size(); }else{ if(cnt+p[k].size()+1>80){ cout << endl; cnt = 0; }else{ cout << " " << p[k]; cnt +=p[k++].size()+1; } } }while(k<=n); cout << endl; return 0; }
#include <iostream> using namespace std; int md[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int year,month,day; int main(){ scanf("%d-%d-%d",&year,&month,&day); if((year%4==0&&year%100!=0)||year%400==0) md[2]++; if(month==12 && day==31){ year++; month=1; day=1; }else if(md[month]==day){ month++; day=1; }else{ day++; } printf("%4d-%02d-%02d",year,month,day); return 0; }
#include <iostream> #include <cmath> using namespace std; const int maxn=10005; int n; //居民点个数 int barrel[maxn]; //每个居民点需要啤酒的桶数 int distan[maxn]; //距离前缀和 int sum; //环岛路程之和 int mins=0x7fffffff;//设点的最小值 int pos; //记录设点位置 /* 1 2 3 4 5 6 0 居民点编号 | 10 | 30 | 25 | 60 | 28 | 35 | 到下一个居民点的距离 500 300 350 400 700 200 500 本居民点需要啤酒的桶数 10 40 65 125 153 188 */ int main(){ cin >> n; for(int i=1;i<=n;i++){ cin >> barrel[i] >> distan[i]; sum = sum + distan[i]; distan[i] = distan[i] + distan[i-1]; } for(int i=1;i<=n;i++){ //设点位置 int tmp = 0; //i到其他点的最小花费之和 for(int j=1;j<=n;j++){ //distance[j]存的是j到j+1的长度 int t1 = abs(distan[j-1]-distan[i-1]); //i到j的距离(j-1到i-1) int t2 = sum - t1; //i反方向到j的距离 tmp = tmp + min(t1,t2) * barrel[j]; //累加到j点的最小值 } if(mins>tmp){ mins = tmp; pos = i-1; } } cout << pos << "," << mins << endl; return 0; }
#include <iostream> #include <cstdio> #include <map> #include <set> #include <vector> using namespace std; map<int,vector<int> > ms; int n,k,x,p; int main(){ map<int,vector<int> >::iterator pos; cin >> n; for(int i=0;i<n;i++){ cin >> p >> k; for(int j=0;j<k;j++){ cin >> x; pos = ms.find(x); if(pos==ms.end()){ vector<int> v; v.push_back(p); ms[x]=v; }else{ ms[x].push_back(p); } } } int mx=0; for(map<int,vector<int> >::iterator it=ms.begin();it!=ms.end();it++){ vector<int> v = it->second; if(v.size()>mx){ mx = v.size(); pos = it; } } cout << pos->first << endl; vector<int> v = pos->second; set<int> s; for(int i=0;i<v.size();i++){ s.insert(v[i]); } int flag = 0; for(set<int>::iterator it=s.begin();it!=s.end();it++){ if(flag) cout << " "; else flag = 1; cout << *it; } cout << endl; return 0; }
#include <iostream> #include <cmath> using namespace std; bool isprime(int a){ int t = sqrt(a); for(int i=2;i<=t;i++){ if(a%i==0) return false; } return true; } int maxprime(int x){ int mx=0; for(int i=2;i<=x;i++){ while(x%i==0) x/=i; if(isprime(i)&&i>mx){ mx = i; } } return mx; } int m,n; int main(){ cin >> m >> n; cout << maxprime(m); for(int i=m+1;i<=n;i++){ cout << "," << maxprime(i); } cout << endl; return 0; }
#include <iostream> using namespace std; const int maxn = 100; int n,used[maxn],numb[maxn],pw[maxn]; int primes(int n){ int cnt = 0; for(int i=2;i<=n;i++){ if(used[i]==0){ numb[++cnt]=i; } for(int j=1;(j<=cnt)&&(i*numb[j]<=n);j++){ used[i*numb[j]]=1; if(i%numb[j]==0) break; } } return cnt; } int main(){ cin >> n; int pn = primes(n); for(int i=1;i<=pn;i++){ int c = 0; while(n%numb[i]==0){ c++; n=n/numb[i]; } pw[i]=c; } int flag = 0; for(int i=1;i<=pn;i++){ if(pw[i]>0 && flag) cout << "*"; else if(pw[i]>0) flag = 1; if(pw[i]>1) cout << numb[i] << "^" << pw[i]; else if(pw[i]==1) cout << numb[i]; } return 0; }
#include <iostream> #include <cmath> #include <set> using namespace std; const int maxn = 100005; int m,n,num[maxn],flag=0; bool used[maxn]; set<int> vs; int prime(int x){ int cnt=0; for(int i=2;i<=x;i++){ if(!used[i]){ num[++cnt]=i; } for(int j=1;(j<=cnt)&&(i*num[j]<=x);j++){ used[i*num[j]] = true; if(i%num[j]==0) break; } } return cnt; } int revs(int a){ int ans=0; while(a){ ans = ans*10 + a%10; a = a/10; } return ans; } bool isprime(int x){ if(x<=3) return true; if(x%6!=1 && x%6!=5) return false; int rt = sqrt(x); for(int i=5;i<=rt;i+=6){ if(x%i==0 || x%(i+2)==0) return false; } return true; } int main(){ cin >> m >> n; int len = prime(n); if(m==1) vs.insert(1); for(int i=1;i<=len;i++){ if(num[i]>=m && num[i]<=n){ int t = revs(num[i]); if(isprime(t)) vs.insert(num[i]); } } if(vs.size()==0) cout << "No"; else{ for(set<int>::iterator it=vs.begin();it!=vs.end();it++){ if(flag) cout <<","; else flag = 1; cout << *it ; } } cout << endl; return 0; }
#include <iostream> using namespace std; int rns(int y){ int cnt=0; for(int i=1900;i<y;i++){ if((i%4==0&&i%100!=0)||i%400==0) cnt++; } return cnt; } int rny(int y){ int cnt=0; if((y%4==0&&y%100!=0)||y%400==0) cnt++; return cnt; } int year,month,days; int m[]={0,31,59,90,120,151,181,212,243,273,304,334,365}; int d[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int main(){ cin >> year >> month; days = (year-1900)*365+rns(year)+m[month-1]; int r = rny(year); if(month>2) days = days + r; int t = d[month]; if(month==2) t = t+r; r = days % 7; printf("Sun Mon Tue Wed Thu Fri Sat\n"); for(int j=0;j<=r;j++) printf(" "); for(int i=1;i<=t;i++){ if((r+i)%7==0) printf("\n"); printf("%3d ",i); } return 0; }
#include <iostream> #include <cstdio> using namespace std; int rns(int s,int e){ int cnt=0; for(int i=s;i<e;i++){ if((i%4==0&&i%100!=0)||i%400==0) cnt++; } return cnt; } int rny(int y){ int cnt=0; if((y%4==0&&y%100!=0)||y%400==0) cnt++; return cnt; } int y1,m1,d1,y2,m2,d2,days; int m[]={0,31,59,90,120,151,181,212,243,273,304,334,365,0}; int main(){ cin >> y1 >> m1 >> d1; cin >> y2 >> m2 >> d2; if(y1==y2){ days = m[m2-1]-m[m1-1]-d1+d2; if(m1<=2 && m2>2) days +=rny(y1); }else if(y2-y1==1){ days=m[12]-m[m1-1] -d1+m[m2-1]+d2; if(m1<=2) days +=rny(y1); if(m2>2) days +=rny(y2); }else if(y2-y1>1){ days=(y2-y1-1)*365+rns(y1+1,y2); days=days+m[12]-m[m1-1] -d1+m[m2-1]+d2; if(m1<=2) days +=rny(y1); if(m2>2) days +=rny(y2); } cout << days << endl; return 0; }
#include <iostream> #include <string> #include <vector> #include <map> using namespace std; string s,t; int n; map<string,int> mp; vector<string> vs; int main(){ cin >> n >> s; int sl = s.size(); for(int i=0;i<=sl-n;i++){ t = s.substr(i,n); mp[t]++; vs.push_back(t); } int mx = 0; for(map<string,int>::iterator it=mp.begin();it!=mp.end();it++){ //cout << it->first << "=" << it->second << endl; if(it->second>mx){ mx = it->second; } } if(mx>1){ cout << mx << endl; for(int i=0;i<vs.size();i++){ if(mp[vs[i]]==mx){ mp.erase(vs[i]); cout << vs[i] << endl; } } }else{ cout << "NO" << endl; } return 0; }
#include <iostream> #include <string> using namespace std; const int maxn = 100; string s; int a[maxn],c[maxn],x=0; int main(){ cin >> s; int len = s.size(); for(int i=0;i<len;i++) a[i+1] = s[i]-48; for(int i=1;i<=len;i++){ int t = x*10+a[i]; c[i] = t / 13; x = t % 13; } int p=1; while(c[p]==0 && p<len) p++; for(int i=p;i<=len;i++) cout << c[i]; cout << endl; cout << x << endl; return 0; }
#include <iostream> #include <algorithm> using namespace std; int a[1005],b[1005],n,ans=10000; int main(){ cin >> n; for(int i=0;i<n;i++){ cin >> a[i]; b[a[i]+50] ++; } for(int i=0;i<=100;i++){ if(b[i]>n/2){ ans = i-50; } } if(ans<10000) cout << ans << endl; else cout << "no" << endl; return 0; }
#include <iostream> #include <string> using namespace std; string s; int n,a[1005],maxc=0,ans; int main(){ cin >> s; n = s.size(); for(int i=0;i<n;i++){ a[s[i]-'a']++; } for(int i=0;i<30;i++){ if(a[i]>maxc){ maxc = a[i]; ans = i; } } cout << char(ans+'a') << " " << maxc << endl; return 0; }
#include <iostream> using namespace std; int n,ans=0; int main(){ cin >> n; while(n){ int r=n%2; n = n/2; if(r) ans ++; } cout << ans << endl; return 0; }
#include <iostream> #include <vector> #include <string> using namespace std; struct node{ char c; int n; node(char a,int b){ c=a; n=b; } }; vector<node> v; char f='*',cnt; string s; int main(){ cin >> s; int len=s.size(); for(int i=0;i<len;i++){ if(s[i]!=f){ node cn(f,cnt); v.push_back(cn); f = s[i]; cnt = 1; }else{ cnt++; } } node cn(f,cnt); v.push_back(cn); len = v.size(); int mx=0,ps=0; for(int i=0;i<len;i++) if(v[i].n>mx){ mx = v[i].n; ps = i; } cout << v[ps].c << " " << v[ps].n << endl; return 0; }
#include <iostream> #include <string> using namespace std; int rny(int y){ int cnt=0; if((y%4==0&&y%100!=0)||y%400==0) cnt++; return cnt; } int year,month,day,days; int m[]={0,31,59,90,120,151,181,212,243,273,304,334,365}; int d[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; string wk[]={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"}; int main(){ year = 2000; month = 1; day = 1; cin >> days; int wm = days % 7; days++; while(days>=365){ if(rny(year)) days --; if(days>=365){ year++; days=days-365; } } if(days>0) for(int i=1;i<=12;i++){ if(days-d[i]>=0){ month = (i+1)%12; days = days - d[i]; }else{ break; } } if(rny(year)&&month>2&&days>1) days --; if(days==0) day = 1; else day = days; printf("%d-%02d-%02d ",year,month,day); cout << wk[wm] << endl; return 0; }
#include <iostream> #include <string> using namespace std; const int N=200; string s1,s2; int a1[N],a2[N],b1[N],b2[N],c1[N],c2[N],x,p; int main(){ cin >> s1 >> s2; int l1 = s1.size(); for(int i=0;i<l1;i++){ if(s1[i]=='.'){ p=i; break; } } for(int i=p-1;i>=0;i--){ a1[p-i] = s1[i]-48; } int p1 = p; int l2 = s2.size(); for(int i=0;i<l2;i++){ if(s2[i]=='.'){ p=i; break; } } for(int i=p-1;i>=0;i--){ a2[p-i] = s2[i]-48; } int p2 = p; if(l1-p1>l2-p2){ for(p=1;p<=l1-p1-l2+p2;p++) b2[p]=0; for(int i=l2-1;i>p2;i--){ b2[p++]=s2[i]-48; } // for(int i=1;i<p;i++) // cout << b2[i] ; // cout << endl; p=1; for(int i=l1-1;i>p1;i--) b1[p++]=s1[i]-48; // for(int i=1;i<p;i++) // cout << b1[i] ; // cout << endl; }else if(l1-p1<l2-p2){ for(p=1;p<=l2-p2-l1+p1;p++) b1[p]=0; for(int i=l1-1;i>p1;i--){ b1[p++]=s1[i]-48; } // for(int i=1;i<p;i++) // cout << b1[i] ; // cout << endl; p=1; for(int i=l2-1;i>p2;i--) b2[p++]=s2[i]-48; // for(int i=1;i<p;i++) // cout << b2[i] ; // cout << endl; }else{ // cout << "hello"<< endl; p=1; for(int i=l1-1;i>p1;i--) b1[p++]=s1[i]-48; // for(int i=1;i<p;i++) // cout << b1[i] ; // cout << endl; p=1; for(int i=l2-1;i>p2;i--) b2[p++]=s2[i]-48; // for(int i=1;i<p;i++) // cout << b2[i] ; // cout << endl; } // cout << p << endl; int pp = p; x=0; for(int i=1;i<p;i++){ c2[i] = b1[i]+b2[i]+x; x = c2[i]/10; c2[i]=c2[i]%10; } // for(int i=1;i<p;i++) // cout << c2[i] ; // cout << endl; p=max(p1,p2)+3; for(int i=1;i<=p;i++){ c1[i] = a1[i]+a2[i]+x; x = c1[i]/10; c1[i]=c1[i]%10; } //c1[p+1]=x; p=l1+l2; while(c1[p]==0 && p>1) p--; for(int i=p;i>=1;i--) cout << c1[i]; int flag =0; for(int i=pp-1;i>=1;i--) if(c2[i]!=0){ flag=1; break; } if(flag){ cout << "."; p=1; while(c2[p]==0 && p<pp) p++; for(int i=pp-1;i>=p;i--) cout << c2[i]; cout << endl; } return 0; }
#include <iostream> using namespace std; int todec(int a,int b){ int ans=0,pct=1; while(a){ int r = a%10; if(r>=b){ ans = -1; break; } a = a/10; ans = ans + r*pct; pct = pct * b; } return ans; } int p,q,r,f=1; int main(){ cin >> p >> q >> r; // for(int i=2;i<=16;i++) // cout << i << "=" << todec(8,i) << endl; for(int i=2;i<=16;i++){ int a1 = todec(p,i); int a2 = todec(q,i); int a3 = todec(r,i); if(a1>0 && a2>0 && a3>0 && a1*a2==a3){ cout << i << endl; f = 0; break; } } if(f) cout << 0 << endl; return 0; }
#include <iostream> using namespace std; int n; int main(){ cin >> n; for(int i=31;i>=0;i--) cout << ((n>>i)&1); cout << endl; return 0; }
#include <iostream> using namespace std; bool tobin(int a){ int one=0,zer=0; while(a){ int r = a%2; a = a/2; if(r) one++; else zer++; } return one>zer; } int main(){ int a=0,b=0; for(int i=1;i<=1000;i++) if(tobin(i)) a++; else b++; cout << a << " " << b << endl; return 0; }
#include <iostream> #include <string> #include <cmath> using namespace std; string s,r=""; int flag = 0; int main(){ while(cin>>s){ r+=s; } int len = r.size(); int w=0,l=0; for(int i=0;i<len;i++){ if(r[i]=='E') break; if(r[i]=='W') w++; if(r[i]=='L') l++; if(abs(w-l)>=2 && (w>=11 || l>=11)){ cout << w << ":" << l << endl; w=0; l=0; } } cout << w << ":" << l << endl; cout << endl; w=0,l=0; for(int i=0;i<len;i++){ if(r[i]=='E') break; if(r[i]=='W') w++; if(r[i]=='L') l++; if(abs(w-l)>=2 && (w>=21 || l>=21)){ cout << w << ":" << l << endl; w=0; l=0; } } cout << w << ":" << l << endl; return 0; }
#include <iostream> #include <algorithm> #include <cmath> using namespace std; const int N = 1000; struct peanuts{ int x; //x坐标 int y; //y坐标 int t; //时间 int n; //数量 } p[N]; bool comp(peanuts p1,peanuts p2){ return p1.n > p2.n; } int row,col,k,a,b,d,ans; int main(){ cin >> row >> col >> k; b=1; for(int i=1;i<=row;i++){ for(int j=1;j<=col;j++){ cin >> a; if(a>0){ p[b].x = i; p[b].y = j; p[b].n = a; b++; } } } sort(p+1,p+b,comp); ans = 0; for(int i=1;i<b;i++){ d=p[i].x; //当前点跳回公路的距离 if(i==1) p[i].t = p[i].x + 1; else p[i].t = p[i-1].t + abs(p[i].x-p[i-1].x)+abs(p[i].y-p[i-1].y)+1; if(p[i].t+d<=k) ans = ans + p[i].n; else break; } cout << ans << endl; return 0; }
#include <iostream> #include <string> using namespace std; string s[105]; int n; int main(){ cin >> n; for(int i=0;i<=n;i++) cin >> s[i]; if(s[0]=="-1") cout << "-x^" << n; else if(s[0]=="1") cout << "x^" << n; else if(s[0][0]=='+') cout << s[0].substr(1) << "x^" << n; else cout << s[0] << "x^" << n; for(int i=1;i<n;i++){ if(s[i]=="-1"){ cout << "-x"; if(n-i>1) cout << "^" << n-i; }else if(s[i]=="1" || s[i]=="+1"){ cout << "+x"; if(n-i>1) cout << "^" << n-i; }else if(s[i][0]=='-'){ cout << s[i] << "x"; if(n-i>1) cout << "^" << n-i; }else if(s[i][0]=='+'){ cout << s[i].substr(1) << "x"; if(n-i>1) cout << "^" << n-i; }else if(s[i]!="0"){ cout << "+" << s[i] << "x"; if(n-i>1) cout << "^" << n-i; } } if(s[n][0]=='-'||s[n][0]=='+') cout << s[n] << endl; else if(s[n][0]!='0') cout << "+" << s[n] << endl; return 0; }
#include <iostream> #include <string> #include <cmath> #include <vector> #include <algorithm> using namespace std; vector<int> v; string s,n; int main(){ getline(cin,s); int len = s.size(),flag=0; n=""; for(int i=0;i<len;i++){ if(s[i]>='0' && s[i]<='9'){ n = n+ s[i]; }else{ if(n.size()>0){ v.push_back(atoi(n.c_str())); } n=""; } } if(n.size()>0){ v.push_back(atoi(n.c_str())); } if(v.size()>0){ sort(v.begin(),v.end()); for(int i=0;i<v.size();i++){ if(flag) cout << ","; else flag = 1; cout << v[i]; } }else{ cout << 0 ; } cout << endl; return 0; }
#include <iostream> using namespace std; int k,x,ans; bool f(int a){ if(a==x){ cout << "YES" << endl; return true; }else if(a<x){ if(f(2*a+1)) return true; else if(f(3*a+1)) return true; } return false; } int main(){ scanf("%d,%d",&k,&x); if(!f(k)) cout << "NO" << endl; return 0; }
#include <iostream> #include <string> #include <map> #include <vector> using namespace std; int n,no,mx=0; string s; map<char,vector<int> > bs; int main(){ cin >> n; for(int i=0;i<n;i++){ cin >> no >> s; int len = s.size(); for(int i=0;i<len;i++){ bs[s[i]].push_back(no); } } map<char,vector<int> >::iterator pos; for(map<char,vector<int> >::iterator it=bs.begin();it!=bs.end();it++){ if(it->second.size()>mx){ mx = it->second.size(); pos = it; } } cout << pos->first << endl; cout << mx << endl; for(int i=0;i<pos->second.size();i++){ cout << pos->second[i] << endl; } return 0; }
#include <iostream> using namespace std; int n,y,m1,m2,days; int r(int x){ int cnt=0; if((x%4==0&&x%100!=0)||x%400==0) cnt++; return cnt; } int m[]={0,31,59,90,120,151,181,212,243,273,304,334,365}; int main(){ cin >> n; for(int i=0;i<n;i++){ cin >> y >> m1 >> m2; if(m1>m2) swap(m1,m2); days = m[m2-1]-m[m1-1]; if(m1<=2 && m2>=2) days = days + r(y); if(days%7==0) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
#include <iostream> #include <string> #include <cstdlib> using namespace std; int m,p,q; string s,n,idx="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int toten(char ch){ if(ch>='0'&&ch<='9') return ch - '0'; else if (ch>='A'&&ch<='Z') return ch - 'A' + 10; } string trans(int p,string n,int q){ string res=""; int len = n.size(); int sum = 1,r,g,t; while(sum!=0){ r = sum = 0; for(int i=0;i<len;i++){ int t = toten(n[i]); sum += t; g = p*r+t; n[i]=idx[g/q]; r = g % q; } if(sum>0) res = idx[r]+res; } if(res=="") res = "0"; return res; } int main(){ //freopen("44.in","r",stdin); cin >> m; for(int i=0;i<m;i++){ cin >> s; int len = s.size(); int dot1 = s.find(','); p = atoi(s.substr(0,dot1).c_str()); int dot2 = s.rfind(','); q = atoi(s.substr(dot2+1,len-dot2+1).c_str()); n = s.substr(dot1+1,dot2-dot1-1); if(p==q) cout << n << endl; else cout << trans(p,n,q) << endl; } return 0; }
#include <iostream> #include <string> using namespace std; int n; string s=""; int main(){ cin >> n; while(n){ int r = n%8; s = char('0'+r) + s; n=n/8; } cout << s << endl; return 0; }
#include <iostream> using namespace std; int n,ans=0,p=1; int main(){ cin >> n; while(n){ int r = n%10; ans = ans + p*r; p=p*8; n=n/10; } cout << ans << endl; return 0; }
#include <cstdio> #include <cstring> #include <iostream> using namespace std; #define tail head+len2-1//与head联动的tail 记录除数的尾部指针 #define Init len1=strlen(s1+1),len2=strlen(s2+1);//初始化 const int Maxn=10001; int len1,len2,cur=1; char s1[Maxn],s2[Maxn]; int a[Maxn],b[Maxn],c[Maxn],ans[Maxn]; bool compare(int head) { cur=1;while(a[cur]==0)cur++;//去除a前面的0 便于计算 if(tail-cur+1 > len2)return 1;//tail-cur+1即是当前串的长度 if(tail-cur+1 < len2)return 0; string str1="",str2="";//比大小 for(int i=cur;i<=tail;i++){ str1 += a[i]+48; str2 += b[i-head+1]+48; } if(str1>=str2)return 1;//使用重载过的String类比较 else return 0;//如果大于等于就减 小于则不能减 } int sub(int head) { for(int i=tail;i>=tail-len2+1;i--){//从后往前减 a[i]=a[i]-b[i-head+1];//减法 if(a[i]<0){//如果不够减借一位 a[i]+=10; a[i-1]--; } } ans[tail]++;//统计答案 } int main() { scanf("%s%s",s1+1,s2+1);Init//读入并处理出长度 for(int i=1;i<=len1;i++)a[i] = s1[i]-'0';//读入 for(int i=1;i<=len2;i++)b[i] = s2[i]-'0';//读入 for(int head=1;tail<=len1;head++){//查看该数放在哪一个位置 if( !compare(head) )continue;//如果a小于b 则继续下一层 即把b数往后挪一位 else while( compare(head) ) sub(head);//只要能减就一直减 直到a小于b 同时在tail处统计答案 } cur=1;while( ans[cur]==0 && cur!=len1 )cur++;//去除前面的0 for(int i=cur;i<=len1;i++)printf("%d",ans[i]);//输出 }
#include <iostream> #include <string> using namespace std; const int N=200; string s1,s2; int a1[N],a2[N],b1[N],b2[N],c1[N],c2[N],x,p; int main(){ cin >> s1 >> s2; int l1 = s1.size(); for(int i=0;i<l1;i++){ if(s1[i]=='.'){ p=i; break; } } for(int i=p-1;i>=0;i--){ a1[p-i] = s1[i]-48; } int p1 = p; int l2 = s2.size(); for(int i=0;i<l2;i++){ if(s2[i]=='.'){ p=i; break; } } for(int i=p-1;i>=0;i--){ a2[p-i] = s2[i]-48; } int p2 = p; if(l1-p1>l2-p2){ for(p=1;p<=l1-p1-l2+p2;p++) b2[p]=0; for(int i=l2-1;i>p2;i--){ b2[p++]=s2[i]-48; } // for(int i=1;i<p;i++) // cout << b2[i] ; // cout << endl; p=1; for(int i=l1-1;i>p1;i--) b1[p++]=s1[i]-48; // for(int i=1;i<p;i++) // cout << b1[i] ; // cout << endl; }else if(l1-p1<l2-p2){ for(p=1;p<=l2-p2-l1+p1;p++) b1[p]=0; for(int i=l1-1;i>p1;i--){ b1[p++]=s1[i]-48; } // for(int i=1;i<p;i++) // cout << b1[i] ; // cout << endl; p=1; for(int i=l2-1;i>p2;i--) b2[p++]=s2[i]-48; // for(int i=1;i<p;i++) // cout << b2[i] ; // cout << endl; }else{ // cout << "hello"<< endl; p=1; for(int i=l1-1;i>p1;i--) b1[p++]=s1[i]-48; // for(int i=1;i<p;i++) // cout << b1[i] ; // cout << endl; p=1; for(int i=l2-1;i>p2;i--) b2[p++]=s2[i]-48; // for(int i=1;i<p;i++) // cout << b2[i] ; // cout << endl; } // cout << p << endl; int pp = p; x=0; for(int i=1;i<p;i++){ c2[i] = b1[i]+b2[i]+x; x = c2[i]/10; c2[i]=c2[i]%10; } // for(int i=1;i<p;i++) // cout << c2[i] ; // cout << endl; p=max(p1,p2)+3; for(int i=1;i<=p;i++){ c1[i] = a1[i]+a2[i]+x; x = c1[i]/10; c1[i]=c1[i]%10; } //c1[p+1]=x; p=l1+l2; while(c1[p]==0 && p>1) p--; for(int i=p;i>=1;i--) cout << c1[i]; int flag =0; for(int i=pp-1;i>=1;i--) if(c2[i]!=0){ flag=1; break; } if(flag){ cout << "."; p=1; while(c2[p]==0 && p<pp) p++; for(int i=pp-1;i>=p;i--) cout << c2[i]; cout << endl; } return 0; }
#include <iostream> #include <cmath> using namespace std; long double a,b; int r; int main(){ cin >> a >> b; r = log10(b)/log10(a); cout << r << endl; return 0; }
#include <iostream> #include <string> #include <cstring> using namespace std; int a[2][1005],p,len; string s; void addb(int n){ int x=0,i=0; a[1][0]+=n; x = a[1][0]/10; a[1][0]%=10; while(x){ i++; a[1][i]+=x; x = a[1][i]/10; a[1][i]%=10; } return; } int main(){ cin >> s; len=s.size(); for(int i=0;i<len;i++) a[0][i] = s[i]-'0'; while(1){ memset(a[1],0,sizeof(a[1])); for(int i=0;i<len;i++) addb(a[0][i]); memset(a[0],0,sizeof(a[0])); int p=len; while(a[1][p]==0 && p>0) p--; for(int i=p;i>=0;i--) a[0][p-i]=a[1][i]; if(p==0) break; } cout << a[0][0] << endl; return 0; }
#include <iostream> #include <algorithm> #include <string> using namespace std; string s1,s2; int a[26],b[26]; int main(){ cin >> s1 >> s2; int len = s1.size(); for(int i=0;i<len;i++) a[s1[i]-'A']++; len = s2.size(); for(int i=0;i<len;i++) b[s2[i]-'A']++; sort(a,a+26); sort(b,b+26); int flag = 1; for(int i=0;i<26;i++){ if(a[i]!=b[i]){ flag=0; break; } } if(flag) cout << "YES" << endl; else cout << "NO" << endl; return 0; }