标签:
小技巧:本文之前由csdn自动生成了一个目录,不必下拉一个一个去找,可通过目录标题直接定位。
本文转载自本人的csdn博客,复制过来的,排版就不弄了,欢迎转载。
声明:
题目部分皆为南阳OJ题目。
代码部分包含AC代码(可能不止一个)和最优代码,大部分都是本人写的,并且大部分为c代码和少部分c++代码and极少java代码,但基本都是c语言知识点,没有太多差别,可能代码有的写的比较丑,毕竟知识有限。
语言入门部分题基本都较为简单,是学习编程入门的很好练习,也是ACM的第一步,入门的最佳方法,望认真对待。
本文由csdn-jtahstu原创,转载请注明出处,欢迎志同道合的朋友一起交流学习。本人QQ:1373758426和csdn博客地址,链接:http://blog.csdn.net/jtahstu
该分类南阳OJ地址:我是地址,链接:http://acm.nyist.edu.cn/JudgeOnline/problemset.php?typeid=1
Now begin
1、
2 3
5
#include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); printf("%d\n",a+b); }
#include<iostream> using namespace std; int main() { int a,b; cin>>a>>b; cout<<a+b<<endl; }
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) throws Exception { Scanner cin=new Scanner(System.in); int a=cin.nextInt(),b=cin.nextInt(); System.out.println(a+b); } }
import java.io.*; import java.util.*; public class Main { public static void main (String args[]) throws Exception { BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); String line = stdin.readLine(); StringTokenizer st = new StringTokenizer(line); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); System.out.println(a+b); } }
#include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); printf("%d\n",a+b); } #include<iostream> main(){std::cout<<(1<<31)-1;}
#include<stdio.h> #define MAX 3 char a[MAX]; int main() { int n; scanf("%d\n", &n); while (n--) { char x, y, z; scanf("%s", a); x = a[0]; if (a[1] < x) x = a[1]; if (a[2] < x) x = a[2]; z = a[2]; if (a[1] > z) z = a[1]; if (a[0] > z) z = a[0]; y = a[0] + a[1] + a[2] - x - z; printf("%c %c %c\n", x, y, z); } return 0; } #include "stdio.h"//最优程序 main() { char a,b,c,d; int i; scanf("%d",&i); getchar(); while(i--) { scanf("%c%c%c",&a,&b,&c); getchar(); if (a>b) {d=a;a=b;b=d;} if (a>c) {d=a;a=c;c=d;} if (b>c) {d=b;b=c;c=d;} printf("%c %c %c\n",a,b,c); } }
11、
# include"stdio.h" int main() { int i, b, c, d, j, k; scanf("%d", &i); for (d = 0; d < i; d++) { scanf("%d", &b); for (k = 1; k <= b; k++) { if ((k % 2) == 1) printf("%d ", k); } printf("\n"); for (j = 1; j <= b; j++) { if ((j % 2) == 0) printf("%d ", j); } printf("\n"); } return 0; } #include<stdio.h>//最优程序 int main() { int n; scanf("%d", &n); int a; while (n--) { scanf("%d", &a); for (int i = 1; i <= a; i += 2) printf("%d ", i); puts(""); for (int i = 2; i <= a; i += 2) printf("%d ", i); puts(""); } }
13、
#include <stdio.h> int F(int n) { if (n == 1 || n == 2) { return 1; } else { return F(n - 1) + F(n - 2); } } int main() { int i, n; scanf("%d", &i); while (i--) { scanf("%d", &n); printf("%d\n", F(n)); } return 0; } #include<stdio.h>//最优程序 main() { int m,n,i,s1,s2; scanf("%d",&m); while(m--) { scanf("%d",&n); for(i=3,s1=s2=1;i<=n;i++) { s1=s1+s2;s2=s1-s2; } printf("%d\n",s1); } }
22、
3 5 1 2 3 4 5 8 11 12 13 14 15 16 17 18 10 21 22 23 24 25 26 27 28 29 30
10 41 52
#include<stdio.h> #include<math.h> int main() { int i, j, k, a, n, m, sum = 0; scanf("%d", &a); for (i = 0; i < a; i++) { scanf("%d", &n); for (j = 0; j < n; j++) { scanf("%d", &m); for (k = 2; k <= sqrt(m); k++) { if (m % k == 0) break; } if (k > sqrt(m) && m != 1) sum = sum + m; } printf("%d\n", sum); sum = 0; } return 0; } #include<stdio.h>//最优程序 #include <math.h> int main() { int m,n,i,j,a[1000],flag=0; long s; scanf("%d",&m); while(m--) { s=0; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { if(a[i]==1) continue; flag=0; for(j=2;j<=sqrt(a[i]);j++) { if(a[i]%j==0) {flag=1;break;} } if(flag==0) s+=a[i]; } printf("%d\n",s); } return 0; }
24、
3 6 8 10
5 1 7 1 11 1
#include<stdio.h> #include<math.h> bool judge(int m); int main() { int N,n,i; scanf("%d",&N); while(N--) { scanf("%d",&n); if(judge(n)) { printf("%d 0\n",n); continue; } for(i=1; n-i!=-1; i++) { if(judge(n-i)) { printf("%d %d\n",n-i,i); break; } if(judge(n+i)) { printf("%d %d\n",n+i,i); break; } } } return 0; } bool judge(int m) { if(m==0||m==1) return false; int i; for(i=2; i<=sqrt(m); i++) { if(m%i==0)break; } if(i>sqrt(m)) return true; return false; } #include<iostream>//最优程序 #include<cmath> using namespace std; bool isprime(int n) { for(int k=2;k<=sqrt((double)n);k++) if((n%k)==0) return false; return true; } int main() { int n; cin>>n; while(n--) { int num,i,j; cin>>num; if(num==1) { cout<<"2 1"<<endl; continue; } for(i=num;!isprime(i);i--); for(j=num;!isprime(j);j++); if((num-i)<(j-num)) cout<<i<<‘ ‘<<(num-i)<<endl; else if((num-i)>(j-num)) cout<<j<<‘ ‘<<(j-num)<<endl; else if((num-i)==(j-num)) cout<<i<<‘ ‘<<(num-i)<<endl; } }
25、
A | A#=Bb | B | C | C#=Db | D | D#=Eb | E | F | F#=Gb | G | G#=Ab |
Ab minor | A# major | A# minor | C# major | Db minor |
D# major | D# minor | Gb major | Gb minor | G# major |
Ab minor D# major G minor
Case 1: G# minor Case 2: Eb major Case 3: UNIQUE
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; char str[100]; int cas = 1; int main() { while(gets(str)) { printf("Case %d: ",cas++); int i,j,len; len = strlen(str); if(str[1] == ‘ ‘) printf("UNIQUE\n"); else { if(str[1] == ‘#‘) { if(str[0] == ‘G‘) printf("Ab"); else printf("%cb",str[0]+1); } else if(str[1] == ‘b‘) { if(str[0] == ‘A‘) printf("G#"); else printf("%c#",str[0]-1); } for(i = 2;i<len;i++) printf("%c",str[i]); printf("\n"); } } return 0; } #include<iostream>//最优程序 #include<string> using namespace std; string trans(string a){ string b=""; if(a[1]==‘#‘){ b+=char((a[0]-‘A‘+1)%7+‘A‘); b+=‘b‘; }else{ b+=char((a[0]-‘A‘+6)%7+‘A‘); b+=‘#‘; } return b; } int main(){ string a,b; for(int t=1; cin>>a>>b; t++){ cout<<"Case "<<t<<": "; if(a.length()==1) cout<<"UNIQUE"<<endl; else cout<<trans(a)<<" "<<b<<endl; } return 0; }
31、
1 2 3 4 5
1 5
#include<stdio.h> int main() { int a[5], temp, i, j; for (i = 0; i < 5; i++) scanf("%d", &a[i]); for (j = 0; j < 5; j++) for (i = 0; i < 4; i++) if (a[i] > a[i + 1]) { temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } printf("%d %d", a[0], a[4]); return 0; } #include<iostream>//最优程序 #include<iterator> #include<algorithm> using namespace std; int main() { int a[5]; copy(istream_iterator<int>(cin),istream_iterator<int>(),a); cout<<*min_element(a,a+5)<<" "<<*max_element(a,a+5)<<endl; }
33、
#include"stdio.h" #include<stdlib.h> int main() { int a[109][109]= {0},i,j,k,n,m,top,x,y; top=1; scanf("%d",&n); a[x=0][y=n-1]=1; while(top<n*n) { while(x+1<n&&!a[x+1][y]) a[++x][y]=++top; while(y-1>=0&&!a[x][y-1]) a[x][--y]=++top; while(x-1>=0&&!a[x-1][y]) a[--x][y]=++top; while(y+1<n&&!a[x][y+1]) a[x][++y]=++top; } for(i=0; i<n; i++) { for(j=0; j<n; j++) printf("%d ",a[i][j]); printf("\n"); } //system("33.exe\n"); } #include<stdio.h>//最优程序 int main() { int a,b,c,d,n,sum=1; int yi[101][101]; scanf("%d",&n); for(a=0;a<=(n-1)/2;a++) { for(b=a;b<=n-a-1;b++) yi[b][n-a-1]=sum++; for(b=n-2-a;b>=a;b--) yi[n-a-1][b]=sum++; for(b=n-a-2;b>=a;b--) yi[b][a]=sum++; for(b=a+1;b<n-a-1;b++) yi[a][b]=sum++; } for(c=0;c<n;c++) { for(d=0;d<n;d++) printf("%d ",yi[c][d]); printf("\n"); } }
34、
#include<iostream> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; int n=(a*70+b*21+c*15)%105; if(n>100||n<10) cout<<"No answer"<<endl; else cout<<n<<endl; }
39、
#include "stdio.h" int main(int argc, char const *argv[]) { int n=0; //freopen("input.txt","r",stdin); while(scanf("%d",&n)==1) { if(n!=0) { if (n==153||n==370||n==371||n==407) printf("Yes\n"); else printf("No\n"); } } return 0; } #include<iostream>//最优程序 using namespace std; int main() { int a; while(1) { cin>>a; if(a==0) break; cout<<((a==153||a==370||a==371||a==407)?"Yes":"No")<<endl; } }
40、
#include<stdio.h> int main() { int a, b, c, n, k; scanf("%d", &n); //输入一个整数n(0<n<=10000),表示有n组测试数据 while (n--) { scanf("%d %d", &a, &b); //输入两个整数 k = a * b; while (b != 0) { c = a % b; a = b; b = c; } printf("%d %d\n", a, k / a); } return 0; } import java.io.*; import java.util.*; public class Main { public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static Scanner count = new Scanner(System.in); public static void main(String[] arges) { int n = count.nextInt(); while ((n--) > 0) { int a = count.nextInt(), b = count.nextInt(); System.out.println(gcd(a, b) + " " + a * b / gcd(a, b)); } count.close(); } } #include<stdio.h> int main() { unsigned int u,v,r,s,i,d; scanf("%u",&s); for(i=1;i<=s;i++) { scanf("%u%u",&u,&v); d=u*v; while(v!=0) { r=u%v; u=v; v=r; } printf("%u %u\n",u,d/u); } return 0; }
41、
import java.util.Scanner; public class Main { public static void main(String[] args) { int a,b,c,n; Scanner num=new Scanner(System.in); System.out.print(""); a=num.nextInt(); System.out.print(""); b=num.nextInt(); System.out.print(""); c=num.nextInt(); if(a>b) { n=a;a=b;b=n; } if(b>c) { n=b;b=c;c=n; } if(a>c) { n=a;a=c;c=n; } System.out.println(a+" "+b+" "+c); } } #include <stdio.h>//最优程序 int main() { int a,b,c,an[3],i,t,j,max,flag; scanf ("%d %d %d",&an[0],&an[1],&an[2]); for (i=0;i<3;i++) { t=max=an[i]; flag=i; for (j=i;j<3;j++) if (an[j]>t) { max=an[j]; flag=j; }; t=an[i]; an[i]=max; an[flag]=t; } for (i=2;i>=0;i--) printf ("%d ",an[i]); return 0; }
56、
2
100 5
16 2
24
15
#include <stdio.h>
int main(void) {
int N, n, m, count;
scanf("%d", &N);
while (N--) {
count = 0;
scanf("%d%d", &n, &m);
while (n) {
n = n / m;
count = count + n;
}
printf("%d\n", count);
}
return 0;
}
#include<iostream>//最优程序
using namespace std;
int get(int n,int num)
{
if(n==0) return 0;
else return get(n/num,num)+n/num;
}
int main()
{
int n;
cin>>n;
while(n--)
{
int a,b;
cin>>a>>b;
cout<<get(a,b)<<endl;
}
}
57、
假 设你有一个各位数字互不相同的四位数,把所有的数字从大到小排序后得到a,从小到大后得到b,然后用a-b替换原来这个数,并且继续操作。例如,从 1234出发,依次可以得到4321-1234=3087、8730-378=8352、8532-2358=6174,又回到了它自己!现在要你写一个 程序来判断一个四位数经过多少次这样的操作能出现循环,并且求出操作的次数
比如输入1234执行顺序是1234->3087->8352->6174->6174,输出是4
#include<stdio.h> int main() { int n,m,a[4],i,j,count,max,min,t; scanf("%d",&n); while(n--) { count=1; scanf("%d",&m); while(m!=6174) { for(i=0; i<4; i++) { a[i]=m%10; m=m/10; } for(i=0; i<4; i++) for(j=i+1; j<4; j++) if(a[i]<a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } max=a[0]*1000+a[1]*100+a[2]*10+a[3]; min=a[3]*1000+a[2]*100+a[1]*10+a[0]; m=max-min; count++; } printf("%d\n",count); } return 0; } #include<iostream>//最优程序 #include<algorithm> #include<stdio.h> using namespace std; int main() { //freopen("1.txt","r",stdin); int k; cin>>k; while(k--) { int n,a[4],n1,n2; scanf("%d",&n); int s=1; while(n!=6174) { a[0]=n%10; a[3]=n/1000; a[1]=n/10%10; a[2]=n/100%10; sort(a,a+4); n1=1000*a[3]+100*a[2]+10*a[1]+a[0]; n2=1000*a[0]+100*a[1]+10*a[2]+a[3]; n=n1-n2; s++; } printf("%d\n",s); } }
60、
1 4 YaoLin 87 82 Y N 0 ChenRuiyi 88 78 N Y 1 LiXin 92 88 N N 0 ZhangQin 83 87 Y N 1
ChenRuiyi 9000 28700
#include <iostream> using namespace std; int main(void) { string str,_str; char ca,cb; int n,m,x,y,z,s,sum,max; cin>>n; while(n--) { cin>>m; max = sum = 0; while(m--) { s = 0; cin>>str>>x>>y>>ca>>cb>>z; if(x>80&&z>0) { s += 8000; sum += 8000; } if(x>85&&y>80) { s += 4000; sum += 4000; } if(x>90) { s += 2000; sum += 2000; } if(x>85&&cb == ‘Y‘) { s += 1000; sum += 1000; } if(y>80&&ca == ‘Y‘) { s += 850; sum += 850; } if(max < s) { max = s; _str = str; } } cout<<_str<<endl<<max<<endl<<sum<<endl; } } #include<iostream>//最优程序 #include<string> #include<algorithm> #include<numeric> using namespace std; int calc(int qm,int py,bool gb,bool xb,bool lw) { int all=0; if(qm>80 && lw) all+=8000; if(qm>85&& py>80) all+=4000; if(qm>90) all+=2000; if(xb&&qm>85) all+=1000; if(gb&&py>80) all+=850; return all; } int main() { int n; cin>>n; while(n--) { int m; cin>>m; int max_num=0,all=0; string max_stu; while(m--) { int qm,py,lw; string xm,gbs,xbs; cin>>xm>>qm>>py>>gbs>>xbs>>lw; bool gb=gbs=="Y",xb=xbs=="Y"; int num=calc(qm,py,gb,xb,lw>0); all+=num; if(num>max_num) {max_num=num;max_stu=xm;} } cout<<max_stu<<endl<<max_num<<endl<<all<<endl; } }
62、
笨小熊的词汇量很小,所以每次做英语选择题的时候都很头疼。但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大!
这种方法的具体描述如下:假设maxn是单词中出现次数最多的字母的出现次数,minn是单词中出现次数最少的字母的出现次数,如果maxn-minn是一个质数,那么笨小熊就认为这是个Lucky Word,这样的单词很可能就是正确的答案。
2 error olympic
Lucky Word 2 No Answer 0
#include<iostream> #include<string> using namespace std; int prime_number(int n) { if(n==1||n==0)return 0; if(n==2)return 1; else for(int i=2;i*i<=n;i++) { if(n%i==0)return 0; } return 1; } int maxn_minn(string a) { int b[101]={0}; for(int i=0; i<a.size(); i++) for(int j=0; j<a.size(); j++) { if(a[i]==a[j]) b[i]++; } int maxn=b[0]; int minn=b[0]; for(int i=0;i<a.size();i++) { if(b[i]>maxn)maxn=b[i]; if(b[i]<minn)minn=b[i]; } return maxn-minn; } int main() { int n; cin>>n; while(n--) { string a; cin>>a; prime_number(maxn_minn(a))==1?cout<<"Lucky Word\n"<<maxn_minn(a)<<endl:cout<<"No Answer\n"<<"0"<<endl; } return 0; } #include<iostream>//最优程序 #include<string> #include<algorithm> #include<numeric> using namespace std; bool isPrime(int n) { if(n==0) return false; if(n==1) return false; if(n==2) return true; for(int i=2;i*i<=n;i++) { if(n%i==0) return false; } return true; } int min_e(int *p,int *q) { int m=1000; for(int* i=p;i!=q;i++) { if(*i<m && *i!=0) m=*i; } return m; } int main() { int n; string str; cin>>n; while(n--) { int count[26]={0}; cin>>str; for(int i=0;i!=str.size();++i) { ++count[str[i]-‘a‘]; } int nn=*max_element(count,count+26)-min_e(count,count+26); if(isPrime(nn)) cout<<"Lucky Word"<<endl<<nn<<endl; else cout<<"No Answer"<<endl<<0<<endl; } }
64、
#include "stdio.h" int main(int argc, char const *argv[]) { int i,m,n,N; scanf("%d",&N); while(N--) { int x,y,a=0; scanf("%d%d",&n,&m); for(i=0;i<=n;i++) { if(i*2+(n-i)*4==m) { a=1; printf("%d %d\n",i,n-i);break; } } if(a==0)printf("No answer\n"); } return 0; } #include<iostream>//最优程序 using namespace std; int main() { int n,a,b,p,q; cin>>n; while(n--) { cin>>a>>b; q=(b-2*a)/2; p=a-q; if(p<0 ||q<0 || b%2) cout<<"No answer"<<endl; else cout<<p<<" "<<q<<endl; } }
65、
大家都知道阶乘这个概念,举个简单的例子:5!=1*2*3*4*5.现在我们引入一种新的阶乘概念,将原来的每个数相乘变为i不大于n的所有奇数相乘例如:5!!=1*3*5.现在明白现在这种阶乘的意思了吧!
现在你的任务是求出1!!+2!!......+n!!的正确值(n<=20)
#include <stdio.h> int f(int n) { int i, s = 1; for (i = 1; i <= n; i += 2) s *= i; return s; } int main() { int a, n, i, s; scanf("%d", &a); while (a--) { s = 0; scanf("%d", &n); for (i = 1; i <= n; i++) s += f(i); printf("%d\n", s); } return 0; } /* #include<iostream> using namespace std; int f(int n) { if(n%2) return n==1?1:n*f(n-2); return f(n-1); } int g(int n) { return n?g(n-1)+f(n):0; } int main() { int n,m; cin>>n; while(n--) { cin>>m; cout<<g(m)<<endl; } }*/ #include<iostream>//最优程序 using namespace std; int main() { int n,m,r[]={0,1,2,5,8,23,38,143,248,1193,2138,12533,22928,158063,293198,2320223,4347248,38806673,73266098,727995173,1382724248}; cin>>n; while(n--) { cin>>m; cout<<r[m]<<endl; } }
72、
100.00 489.12 12454.12 1234.10 823.05 109.20 5.27 1542.25 839.18 83.99 1295.01 1.75
1581.42
#include "stdio.h" int main(int argc, char const *argv[]) { double sum=0,a; int n=12; // freopen("input.txt","r",stdin); while(n--) { scanf("%lf",&a); sum+=a; } printf("%.2lf\n",sum/12); return 0; } #include<iostream>//最优程序 #include<iomanip> using namespace std; int main() { double sum=0,a; for(int i=0;i<12;i++) { cin>>a; sum+=a; } cout<<fixed<<setprecision(2)<<sum/12.0<<endl; }
74、
#include <stdio.h> int main() { int m,n,i,j; int a[3],b[3]; while(scanf("%d%d",&m,&n),m!=0||n!=0) { a[0]=m/100; a[1]=(m-100*a[0])/10; a[2]=m%10; b[0]=n/100; b[1]=(n-100*b[0])/10; b[2]=n%10; j=0; for(i=2; i>=0; i--) { if(a[i]+b[i]>=10) { j++; a[i-1]+=1;//要考虑进位问题 } } printf("%d\n",j); } return 0; } #include<stdio.h> int main() { int a,b,c,d,e,f,n,m,i; for(;;) { scanf("%d%d",&n,&m); if(n==0&&m==0) return 0; else { i=0; a=n/100;b=n%100/10;c=n%10; d=m/100;e=m%100/10;f=m%10; if(c+f>=10) {i+=1;b+=1;} if(b+e>=10) {i+=1;a+=1;} if(a+d>=10) {i+=1;} printf("%d\n",i); } } return 0; }
75、
3 2000 4 5 2001 5 4 2010 10 24
96 124 297
#include <iostream> using namespace std; int leap(int a) { if(a%4==0&&a%100!=0||a%400==0) return 1; else return 0; } int number(int year,int m,int d) { int sum=0,i,j,k,a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int b[12]={31,29,31,30,31,30,31,31,30,31,30,31}; if(leap(year)==1) for(i=0;i<m-1;i++) sum+=b[i]; else for(i=0;i<m-1;i++) sum+=a[i]; sum+=d; return sum; } int main(int argc, char const *argv[]) { int n; cin>>n; while(n--) { int year,month,day,n; cin>>year>>month>>day; n=number(year,month,day); cout<<n<<endl; } return 0; } #include<stdio.h>//最优程序 int main() { int a,b=0,c,y,m,d,fib; scanf("%d",&a); while(a--) { scanf("%d %d %d",&y,&m,&d); if(y%400==0||y%100!=0&&y%4==0) fib=29; else fib=28; for(c=1;c<=m;c++) switch(c-1) { case 1: case 3: case 5: case 7: case 8: case 10:b+=31;break; case 2:b+=fib;break; case 4: case 6: case 9: case 11:b+=30;break; } b+=d; printf("%d\n",b); b=0; } return 0; }
77、
有 n盏灯,编号为1~n,第1个人把所有灯打开,第2个人按下所有编号为2 的倍数的开关(这些灯将被关掉),第3 个人按下所有编号为3的倍数的开关(其中关掉的灯将被打开,开着的灯将被关闭),依此类推。一共有k个人,问最后有哪些灯开着?输入:n和k,输出开着的 灯编号。k≤n≤1000
#include<stdio.h> int main( ) { int n, k, j, open,i; scanf("%d%d", &n, &k ); for(i = 1 ; i <= n ; i++ ) { open = 1; for( j = 2 ; j <= k ; j++ ) { if( i % j == 0 ) open = (open+1) % 2; } if( open ) printf("%d ", i ); } return 0; } #include <iostream>//最优程序 using namespace std; int main() { int n,k,a[1000],i; cin>>n>>k; for(i=0;i<n;i++) a[i]=1; for(i=2;i<=k;i++) for(int j=0;j<n;j++) { if((j+1)%i==0) if(a[j]==0)a[j]=1; else a[j]=0; } for(i=0;i<n;i++) if(a[i]==1)cout<<i+1<<" "; cout<<endl; return 0; }
94、
Tom has many cigarettes. We hypothesized that he has n cigarettes and smokes them
one by one keeping all the butts. Out of k > 1 butts he can roll a new cigarette.
Now,do you know how many cigarettes can Tom has?
3 4 3 10 3 100 5
5 14 124
#include<iostream> using namespace std; int main() { int n;cin>>n; while(n--) { int a,b; cin>>a>>b; int sum=0; sum+=a; while(a>=b) { a-=b;//只要a-b还大于b , 就相当于a会多一个 a++; sum++; } cout<<sum<<endl; } return 0; } #include "stdio.h" #include<fstream> int main() { //freopen("d:\\1.txt","r",stdin); //freopen("d:\\2.txt","w",stdout); int m; scanf("%d",&m); while(m--) { int n,k,sum; scanf("%d%d",&n,&k); sum=n; while(n/k) { sum+=n/k; n=n/k+n%k; } printf("%d\n",sum); } return 0; }
96、
已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则求出w的后n-1位的数。
#include<stdio.h><pre name="code" class="cpp">//较简单的做法 int main() { int num; int M; scanf("%d",&num); while(num--) { scanf("%d",&M); int i=10; while(M/i>=10) { i=i*10; } printf( "%d\n" ,M%i); } } #include<cstdio>//最优程序 int main() { int n,m; scanf("%d",&n); while(n--) { scanf("\n%*c%d",&m); printf("%d\n",m); } }
97、
#include "stdio.h" int main() { int n; scanf("%d",&n); while(n--) { double m,x,y,z,s; scanf("%lf%lf%lf%lf",&m,&x,&y,&z); s=x*m/(y-x)*z; printf("%.2lf\n",s); } return 0; } #include<iostream>//最优程序 #include<stdio.h> using namespace std; int main() { int n; cin>>n; while(n--) { int s,a,b,c; cin>>s>>a>>b>>c; printf("%.2lf\n",s*a/(double)(b-a)*c); } }
98、
#include "stdio.h" int main(int argc, char const *argv[]) { int n,a; scanf("%d",&n); while(n--) { scanf("%d",&a); if(90<=a&&a<=100) printf("A\n"); if(80<=a&&a<=89) printf("B\n"); if(70<=a&&a<=79) printf("C\n"); if(60<=a&&a<=69) printf("D\n"); if(0<=a&&a<=59) printf("E\n"); } return 0; } #include<iostream>//最优程序 using namespace std; int main() { int n,s; cin>>n; while(n--) { cin>>s; switch(s/10) { case 10: case 9:cout<<"A"<<endl;break; case 8:cout<<"B"<<endl;break; case 7:cout<<"C"<<endl;break; case 6:cout<<"D"<<endl;break; default:cout<<"E"<<endl;break; } } }
100、
#include <iostream> #include <stdio.h> using namespace std; int main(int argc, char const *argv[]) { int n; cin>>n; while(n--) { int a,i,count=0; cin>>a; while(a) { if(a%2==1) count++; a/=2; } cout<<count<<endl; } return 0; } #include<stdio.h>//最优程序 main(){int n,m,s;scanf("%d",&n);while(n--){scanf("%d",&m);s=0;while(m)m&=m-1,s++;printf("%d\n",s);}}
101、
#include<math.h> #include<stdio.h> int main() { int n; double l,x1,x2,y1,y2; // freopen("input.txt","r",stdin); scanf("%d",&n); while(n--) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); l=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); printf("%.2lf\n",l); } return 0; } #include<iostream>//最优程序 #include<math.h> #include<iomanip> using namespace std; int main() { /*freopen("1.txt","r",stdin); freopen("2.txt","w",stdout);*/ double x1,x2,y1,y2,m; double a; cin>>m; while(m--) { cin>>x1>>y1>>x2>>y2; a=sqrt((double)((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))); cout.setf(ios::fixed); cout<<setprecision(2)<<a<<endl; } return 0; }
111、
1/8+3/8 1/4-1/2 1/3-1/3
1/2 -1/4 0
#include <iostream> #include<cstdlib> using namespace std; int yinshu(int x,int y) { if(x<y) { int tmp; tmp = x; x = y; y = tmp; } if(x%y == 0) return abs(y); else return yinshu(y,x%y); } int main(void)//最优程序 { int a,b,c,d; char optr,num; while(cin >> a >> num >> b >> optr >> c >> num >> d) { int x,y; switch(optr) { case ‘+‘: { x = a*d+b*c; y = b*d; break; } case ‘-‘: { x = a*d-b*c; y = b*d; break; } } if(x == 0) cout << 0 << endl; else if(y/yinshu(x,y) == 1) cout << x/yinshu(x,y) << endl; else cout << x/yinshu(x,y) << "/" << y/yinshu(x,y) << endl; } return 0; }
113、
you are what you do
we are what we do
#include "iostream" #include "cstring" #include "cstdio" using namespace std; int main(int argc, char const *argv[]) { char a[1001]; while(gets(a)!=NULL) { int i,len=0; len=strlen(a); for(i=0; i<len; i++) { if(a[i]==‘y‘&&a[i+1]==‘o‘&&a[i+2]==‘u‘) { cout<<"we"; i+=2; } else cout<<a[i]; } cout<<endl; } return 0; } #include<algorithm>//最优程序 #include<iostream> #include<string> using namespace std; int main() { string s, s1, s2; while(getline(cin,s)) { int flag; s1 = "you"; s2 = "we"; flag = s.find(s1,0); while(flag != string::npos) { s.replace(flag, 3, s2); flag = s.find(s1, flag + 1); } cout << s << endl; } return 0; }
122、
The nth Triangular number, T(n) = 1 + … + n, is the sum of the first n integers. It is the number of points in a triangular array with n points on side. For example T(4):
Write a program to compute the weighted sum of triangular numbers:
W(n) = SUM[k = 1…n; k * T(k + 1)]
4 3 4 5 10
1 3 45 2 4 105 3 5 210 4 10 2145
#include<iostream> using namespace std; int main() { int n,j=1; cin>>n; while(n--) { int a,sum=0; cin>>a; for(int i=1;i<=a;i++) sum+=(i*(i+1)*(i+2))/2; cout<<j++<<" "<<a<<" "<<sum<<endl; } return 0; } #include<iostream>//最优程序 using namespace std; const int M=310; int W[M]; int main() { for(int i=1;i!=M;i++) W[i]=W[i-1]+i*(i+1)*(i+2)/2; int m,n; cin>>n; for(int i=1;i<=n;i++) { cin>>m; cout<<i<<" "<<m<<" "<<W[m]<<endl; } }
125、
《盗梦空间》是一部精彩的影片,在这部电影里,Cobb等人可以进入梦境之中,梦境里的时间会比现实中的时间过得快得多,这里假设现实中的3分钟,在梦里就是1小时。
然而,Cobb他们利用强效镇静剂,可以从第一层梦境进入第二层梦境,甚至进入三层,四层梦境,每层梦境都会产生同样的时间加速效果。那么现在给你Cobb在各层梦境中经历的时间,你能算出现实世界过了多长时间吗?
比如,Cobb先在第一层梦境待了1个小时,又在第二层梦境里待了1天,之后,返回第一层梦境之后立刻返回了现实。
那么在现实世界里,其实过了396秒(6.6分钟)
1 6 IN STAY 60 IN STAY 1440 OUT OUT
396
#include <iostream> #include <string> using namespace std; int main() { int n, m; cin >> n; while (n--) { double jt = 0, min, tt = 1; cin >> m; while (m--) { string s; cin >> s; if (s == "IN") { tt *= 20; continue; } if (s == "OUT") { tt /= 20; continue; } cin >> min; jt += min / tt; } cout << jt * 60 << endl; } return 0; } #include<iostream>//最优程序 #include<string> using namespace std; int main() { int m; string s; cin>>m; while(m--) { int p=1,n,t=0,tt; cin>>n; while(n--) { cin>>s; if(s=="STAY") { cin>>tt; t+=tt*60/p; } else if(s=="IN") p*=20; else p/=20; } cout<<t<<endl; } }
168、
2010 年上海世界博览会(Expo2010),是第41届世界博览会。于2010年5月1日至10月31日期间,在中国上海市举行。本次世博会也是由中国举办的 首届世界博览会。上海世博会以“城市,让生活更美好”(Better City,Better Life)为主题,将充分探索21世纪城市生活。
这次世博会总投资达450亿人民币,创造了世界博览会史上的最大规模记录。吸引200个国家和国际组织参展。预计有7000万人次的参观者。
为 了更好地接待在这期间来自世界各地的参观者,如何合理安排各宾馆的住房问题提到了日程。组委会已接到了大量的客户住宿定单,每张定单的内容包括要住宿的房 间数,开始住宿时间和要住的天数。为了便于整个城市各宾馆的管理,组委会希望对这些定单进行安排,目的是用尽可能少的房间来满足这些定单,以便空出更多的 房间用于安排流动游客。
组委会请求DR.Kong来完成这个任务,对这些定单进行合理安排,使得满足这些定单要求的房间数最少。
假设:某个定单上的游客一旦被安排到某房间,在他预定住宿的期间内是不换房间的。为了简化描述,定单上的开始住宿时间为距离现在的第几天。例如,定单为(10,30,5)表示游客要求使用10个房间,第30天开始连住5天。
1 3 3 10 4 4 9 3 3 12 6
7
#include <iostream> using namespace std; int main() { int n; cin>>n; while(n--){ int m,a[200]={0},b,c,d;//计算出每天的最大人数,即房间数,保存 cin>>m; while(m--){ cin>>b>>c>>d; for(int i=c;i<c+d;i++)//是<,第c+d天不住的 a[i]+=b; } int max=a[1]; for(int i=2;i<181;i++) if(a[i]>max) max=a[i]; cout<<max<<endl; } return 0; } #include <stdio.h>//最优程序 #include <string.h> #define MAX 200 int Scan(){ int res=0 , ch; while(!((ch=getchar()) >= ‘0‘ && ch <= ‘9‘)) if(ch==EOF) return EOF; res=ch-‘0‘; while((ch=getchar()) >= ‘0‘ && ch <= ‘9‘) res = res*10 + (ch-‘0‘); return res; } int main() { int Ncase,d[MAX]; scanf("%d",&Ncase); while(Ncase--){ memset(d,0,sizeof(d)); int max = -1, n, a, b, c; n = Scan(); while(n--){ a = Scan(); b = Scan(); c = Scan(); d[b] += a; d[b+c] -= a; } for (int i = 1 ; i < MAX ; i++){ d[i] = d[i-1] + d[i]; if (max < d[i]){ max = d[i]; } } printf("%d\n",max); } return 0; }
169、
走进世博园某信息通信馆,参观者将获得前所未有的尖端互动体验,一场充满创想和喜悦的信息通信互动体验秀将以全新形式呈现,从观众踏入展馆的第一步起,就将与手持终端密不可分,人类未来梦想的惊喜从参观者的掌上展开。
在 等候区的梦想花园中,参观者便开始了他们奇妙的体验之旅,等待中的游客可利用手机等终端参与互动小游戏,与梦想剧场内的虚拟人物Kr. Kong 进行猜数比赛。当屏幕出现一个整数X时,若你能比Kr. Kong更快的发出最接近它的素数答案,你将会获得一个意想不到的礼物。
例如:当屏幕出现22时,你的回答应是23;当屏幕出现8时,你的回答应是7;若X本身是素数,则回答X;若最接近X的素数有两个时,则回答大于它的素数。
4 22 5 18 8
23 5 19 7
#include<stdio.h> int main() { int a[1010]= {1,1}; int i,j; for(i=2; i<32; i++) { if(a[i]==0) for(j=i*i; j<1010; j+=i) a[j]=1; } int N; scanf("%d",&N); while(N--) { int n; scanf("%d",&n); for(i=n; a[i]==1; i--); for(j=n; a[j]==1; j++); if((j-n)<=(n-i)) printf("%d\n",j); else printf("%d\n",i); } } #include<iostream>//最优程序 #include<algorithm> using namespace std; int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181, 191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409, 419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647, 653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911, 919,929,937,941,947,953,967,971,977,983,991,997,1009}; int main() { int n,m; cin>>n; while(n--) { cin>>m; if(m==1) {cout<<2<<endl;continue;} int* l=lower_bound(prime,prime+169,m); if(*l-m<=m-*(l-1)) cout<<*l<<endl; else cout<<*(l-1)<<endl; } }
198、
我们平时数数都是喜欢从左向右数的,但是我们的小白同学最近听说德国人数数和我们有些不同,他们正好和我们相反,是从右向左数的。因此当他看到123时会说“321”。
现在有一位德国来的教授在郑州大学进行关于ACM的讲座。现在他聘请你来担任他的助理,他给你一些资料让你找到这些资料在书中的页数。现在你已经找到了对应的页码,要用英文把页码告诉他。
为了简化我们的问题,你只需要返回单词的大写的首字母。(数字0读成字母O)
注意:每个数字式单独读取的,因此不会出现11读成double one的情况。
#include<iostream> #include<string> using namespace std; int main ( ) { int n; cin >> n; while ( n-- ) { string s; cin>>s; for ( int i = s.size()-1; i >=0; i-- ) { switch ( s[i] ) { case ‘0‘: case ‘1‘: cout << "O"; break; case ‘2‘: case ‘3‘: cout << "T"; break; case ‘4‘: case ‘5‘: cout << "F"; break; case ‘6‘: case ‘7‘: cout << "S"; break; case ‘8‘: cout << "E"; break; case ‘9‘: cout << "N"; break; } } cout << endl; } //system ( "PAUSE" ); return 0; } #include<cstdio>//最优程序 char str[]="OOTTFFSSENT"; void show(int t) { if(t){putchar(*(str+t%10));show(t/10);} } int main() { int n,t; scanf("%d",&t); while(t--) { scanf("%d",&n); show(n);puts(""); } }
199、
我们的乐乐同学对于网络可算得上是情有独钟,他有一个计划,那就是用无线网覆盖郑州大学。
现 在学校给了他一个机会,因此他要购买很多的无线路由。现在他正在部署某条大道的网络,而学校只允许把他的无线路由器放在路的正中间。我们默认这条大道是笔 直的并且它在任何地方的宽度都一样。并且所有的路由器的覆盖面积是相同的。现在乐乐计算出这条大道的长和宽,以及路由器的覆盖半径,想请你帮忙,帮他计算 出他最少要购买的路由器的数量。
注意:为了防止某种干扰,两台无线路由之间的最小距离不能小于1米
图1中为一条矩形的道路,中间的虚线代表中线。图2为最小覆盖的示意图。
/* * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=199 * by jtahstu on 2015/2/11 16:00 */ #include <iostream> #include <cmath> using namespace std; int main() { int r, d, l, n; cin >> n; while (n--) { cin >> l >> d >> r; if (4 * r * r - d * d < 1) { cout << "impossible" << endl; } else { double a = sqrt((2 * r) * (2 * r) * 1.0 - (d * d)); if ((int) (l / a) == l / a) cout << (int) (l / a) << endl; else cout << (int) (l / a) + 1 << endl; } } return 0; } #include<stdio.h>//最优程序 #include<math.h> int main(){int T;double L,D,R,a; scanf("%d",&T);while(T--) {scanf("%lf%lf%lf",&L,&D,&R); a=4*R*R-D*D;if(a>0)printf("%.0lf\n",ceil(L/sqrt(a))); else puts("impossible");}}
204、
As is known to all,if you throw a coin up and let it droped on the desk there are usually three results. Yes,just believe what I say ~it can be the right side or the other side or standing on the desk, If you don‘t believe this,just try In the past there were some famous mathematicians working on this .They repeat the throwing job once again. But jacmy is a lazy boy.He is busy with dating or playing games.He have no time to throw a single coin for 100000 times. Here comes his idea,He just go bank and exchange thousands of dollars into coins and then throw then on the desk only once. The only job left for him is to count the number of coins with three conditions.
He will show you the coins on the desk to you one by one. Please tell him the possiblility of the coin on the right side as a fractional number if the possiblity between the result and 0.5 is no larger than 0.003. BE CAREFUL that even 1/2,50/100,33/66 are equal only 1/2 is accepted ! if the difference between the result and 0.5 is larger than 0.003,Please tell him "Fail".Or if you see one coin standing on the desk,just say "Bingo" any way.
6 UUUDDD
1/2
#include<stdio.h> int divisor( int n, int m ) { if( n % m == 0 ) return m; else divisor( m, n % m ); } int main( ) { int n, n1 = 0, n2 = 0, m1, m2; char ch[65537]; bool stand = false; scanf("%d", &n ); scanf("%s", ch ); for( int i = 0 ; i < n ; i++ ) { if( ch[i] == ‘U‘ ) n1++; else if( ch[i] == ‘D‘ ) n2++; else if( ch[i] == ‘S‘ ) stand = true; } if( stand ) printf("Bingo\n"); else if( (1.0*n1/n) > (0.5+0.003) || (1.0*n1/n) < (0.5-0.003) ) printf("Fail\n"); else printf("%d/%d\n", n1/divisor( n, n1 ), n/divisor( n, n1 ) ); return 0; } #include<cstdio>//最优程序 int u,d; int gcd(int a,int b) { if(a==0) return b; else return gcd(b%a,a); } int main() { int n; char c; scanf("%d",&n); getchar(); for(int i=0;i!=n;i++) { c=getchar(); if(c==‘S‘) {puts("Bingo");return 0;} if(c == ‘U‘) ++u; else ++d; } int g=gcd(u,u+d); if((double)u/(u+d)-0.5>0.003 ||(double)u/(u+d)-0.5<-0.003) puts("Fail"); else printf("%d/%d",u/g,(u+d)/g); }
206、
1 2 3 2
3 18
#include "stdio.h" int main(int argc, char const *argv[]) { int a,b; while(scanf("%d%d",&a,&b)!=EOF) { printf("%.0lf\n",(double)a*(a+1)*b*(b+1)/4); } return 0; } #include <stdio.h>//最优程序 main(){float a,b;while(scanf("%f%f",&a,&b)+1)printf("%.0f\n",(a+1)*a*(b+1)*b/4);}
216、
2 1 3
0 1
#include<iostream> using namespace std; int main() { int n; cin>>n; while(n--) { int a,count=0; cin>>a; for(int i=1; (i+1)*(i+1)<=(a+1); i++) if((a+1)%(i+1)==0) count++; cout<<count<<endl; } return 0; } #include<cstring> #include<cstdio> #include<map> #include<string> #include<algorithm> #include<vector> #include<iostream> #include<cmath> using namespace std; #define CLR(arr,val) memset(arr,val,sizeof(arr)) int main() { int t,n,cnt=0; //long long num; int num; scanf("%d",&t); while(t--) { // scanf("%lld",&num); scanf("%d",&num); int nn=(int)(sqrt(num+1.0)+0.5); num++; cnt=0; for(int i=2;i<=nn;i++) if(num%i==0) cnt++; printf("%d\n",cnt); } }
217、
6 R 1 P 2 G 3 r 1 p 2 g 3
19 18 10 -17 -14 -4
#include "stdio.h" int main(int argc, char const *argv[]) { int n; //freopen("input.txt","r",stdin); scanf("%d",&n); while(n--) { char a; int b,c,d; char ch=getchar(); scanf("%c%d",&a,&b); c=a; if(a>=‘A‘&&a<=‘Z‘) d=c-64; if(a>=‘a‘&&a<=‘z‘) d=96-c; printf("%d\n",b+d); } return 0; } #include<stdio.h>//最优程序 main() { int x,n; scanf("%d",&x); while(x--) { getchar(); char c; scanf("%c %d",&c,&n); if(c>=65&&c<=90) c=c-64; else c=0-(c-96); n=c+n; printf("%d\n",n); } }
241、
#include <stdio.h> #include "string.h" #include <iostream> using namespace std; int main(int argc, char const *argv[]) { int n,i,len; char a[1011]; cin>>n; getchar(); while(n--) { char b[1011]={0}; int fla=0,max=0; cin>>a; len=strlen(a); for(i=0; i<len; i++) { b[a[i]-‘a‘]++; } max=b[0]; for(i=0; i<26; i++) { if(b[i]>max)//||(b[i]==max&&a[i]-‘a‘<a[fla]-‘a‘) { max=b[i]; fla=i; } } cout<<(char)(fla+‘a‘)<<endl; } return 0; } #include<stdio.h>//最优程序 #include<string.h> main() { int x,i,max,q; char a[1011]; scanf("%d",&x); getchar(); while(x--) { int s[26]={0}; gets(a); for(i=strlen(a)-1;i>=0;i--) s[a[i]-97]++; max=0; for(i=0;i<26;i++) if(max<s[i]) max=s[i],q=i; printf("%c\n",q+97); } }
242、
#include<stdio.h> int main() { double n,pi=3.1415926; while(scanf("%lf",&n)==1) { printf("%.0lf\n",4*pi*n*n*n/3); } return 0; }
243、
#include<stdio.h> #include<iostream> using namespace std; int main() { int n, i, a[100], t, j; while (cin >> n && n) { j = 0; for (i = 0; i < n; i++) cin >> a[i]; t = a[0]; for (i = 0; i < n; i++) { if (a[0] > a[i]) { a[0] = a[i]; j = i; } } a[j] = t; for (i = 0; i < n; i++) printf("%d ", a[i]); printf("\n"); } return 0; } #include<iostream>//最优程序 #include<iterator> #include<algorithm> using namespace std; int data[110]; int main() { int n; while(cin>>n && n) { for(int i=0;i!=n;i++) cin>>data[i]; iter_swap(data,min_element(data,data+n)); copy(data,data+n,ostream_iterator<int>(cout," ")); cout<<endl; } }
244、
#include"stdio.h" int main() { int n, a, b; while (scanf("%d", &n) != EOF) { while (n--) { scanf("%x %x", &a, &b); printf("%o\n", a + b); } } return 0; } #include<stdio.h>//最优程序 int main() { int T; scanf("%d",&T); while(T--) { int a,b,d; char c; scanf("%x%c%x",&a,&c,&b); if(c==‘+‘) d=a+b; else d=a-b; if(d>=0) printf("%o\n",d); else printf("-%o\n",-d); } }
255、
#include<iostream> #include<algorithm> using namespace std; int main(int argc, char const *argv[]) { int n; cin>>n; while(n--) { int m,i,a[110],count; count=0; cin>>m; for(i=0; i<m; i++) cin>>a[i]; sort(a,a+m); for(i=0; i<m; i++) { if(a[i]!=a[i+1]) count++; else if(a[i]==a[i+1]) a[i]=-1; } cout<<count<<endl; for(i=0; i<m; i++) { if(a[i]!=-1) cout<<a[i]<<" "; } cout<<endl; } return 0; } #include<iostream>//最优程序 #include<map> #include<cstdio> #include<algorithm> #include<iterator> using namespace std; const int maxn=110; int tab[maxn]; int main() { int t;cin>>t; while(t--){ int n;cin>>n; for(int i=0;i<n;i++)scanf("%d",&tab[i]); sort(tab,tab+n); cout<<(n=distance(tab,unique(tab,tab+n)))<<endl; copy(tab,tab+n,ostream_iterator<int>(cout," "));cout<<endl; } }
259、
茵茵今年已经六年级了,爸爸给她报了一个学习程序设计的班。
第一节课上,老师讲的就是如何输入一个数,再原样输出出来。
以现在的你看来,挺容易的是不?
那么,就请你也写出一个一样的程序吧
2 3.5 5
3.5 5
#include<stdio.h> #include<string.h> int main() { int n; char a[20]; scanf("%d",&n); while(n--) { scanf("%s",a); printf("%s\n",a); } return 0; }
260、
在墙角堆放着一堆完全相同的正方体小木块,如下图所示:
因为木块堆得实在是太有规律了,你只要知道它的层数就可以计算所有木块的数量了。
现在请你写个程序 给你任一堆木块的层数,求出这堆木块的数量.
215
135
#include "iostream" using namespace std; int main() { int n,i,a,b[10001],sum; cin>>n; b[0]=0; for(i=1;i<=10001;i++) {b[i]=b[i-1]+i;} while(n--) { sum=0; cin>>a; for(i=1;i<=a;i++) {sum+=b[i];} cout<<sum<<endl; } return 0; } #include <stdio.h>//最优程序 int main() { int n,t; scanf("%d",&t); while(t--) { scanf("%d",&n); printf("%d\n",n*(n+1)*(n+2)/6); } }
263、
222000 30 1234567892000 20 98765432143000 50 8721984423000 45 7524981242000 60 7651287423000 45 652278122
987654321752498124
#include<stdio.h> int main() { int n; scanf("%d",&n); while(n--) { int m,a,b,c,a1,b1,c1; scanf("%d",&m); scanf("%d%d%d",&a,&b,&c); for(int i=1; i<m; i++) { scanf("%d%d%d",&a1,&b1,&c1); if(a1>a||(a1==a&&b1<b)||(a1==a&&b1==b&&c1>c)) a=a1,b=b1,c=c1; } printf("%d\n",c); } return 0; } #include<stdio.h>//最优程序 int main() { int n,m,i,a,b,c,x,y,z; scanf("%d",&n); while(n--) { scanf("%d",&m); a=0;b=0;c=0; for(i=0;i<m;i++) { scanf("%d%d%d",&x,&y,&z); if(x>a||x==a&&y<b||x==a&&y==b&&z>c) { a=x;b=y;c=z; } } printf("%d\n",c); } return 0; }
264、
比如一条项链,我们用AB来表示,不同的字母表示不同颜色的珍珠。如果把B端接触镜面的话,魔镜会把这条项链变为ABBA。如果再用一端接触的话,则会变成ABBAABBA(假定国王只用项链的某一端接触魔镜)。
给定最终的项链,请编写程序输出国王没使用魔镜之前,最初的项链可能的最小长度。
2ABBAABBAA
21
#include<cstdio> #include<cstring> #include<iostream> using namespace std; int main() { int temp,k,i; char s[100]; scanf("%d",&temp); getchar(); while(temp--) { gets(s); k=strlen(s); while(k) { for(i=0; i<k; ++i) if(s[i]!=s[k-1-i]||k==1||k%2) { cout<<k<<endl; k=0; } k=k/2; } } } #include<stdio.h>//最优程序 #include<string.h> main() { int z,x,q,i; char a[101],t[51]; scanf("%d",&z); getchar(); while(z--) { gets(a); do { x=strlen(a); if(x%2) break; for(i=0;i<x/2;i++) t[i]=a[x-1-i]; t[i]=‘\0‘; a[x/2]=‘\0‘; q=strcmp(a,t); }while(q==0); printf("%d\n",x); } }
266、
3 abc 123de abc 123 abc d
edcba cba dcba
#include <stdio.h> #include <string.h> int main(void) { int n,k,i; char a[100]; //freopen("input.txt", "r", stdin); scanf("%d",&n); getchar(); while(n--) { gets(a); k=strlen(a); for(i=k-1; i>=0; i--) if(a[i]>=‘a‘&&a[i]<=‘z‘) printf("%c",a[i]); printf("\n"); } return 0; } #include <stdio.h> void revers() { char c; if((c = getchar()) != ‘\n‘) revers(); if(c != ‘\n‘&&c>=‘a‘&&c<=‘z‘) putchar(c); } int main() { int a; scanf("%d\n",&a); while(a--) { revers(); printf("\n"); } return 0; }
268、
#include "iostream" #include "cstdio" #include "cstring" using namespace std; int main(int argc, char const *argv[]) { int n,i; char a[1001]; cin>>n; getchar(); while(n--) { int x=0,y=0,z=0,m=0; //把getchar();放这里了,我去 gets(a); m=strlen(a); for(i=0;i<m;i++) { if(a[i]==‘R‘)x++; if(a[i]==‘W‘)y++; if(a[i]==‘B‘)z++; } for (i=0;i<x;i++) cout<<‘R‘; for(i=0;i<y;i++) cout<<‘W‘; for(i=0;i<z;i++) cout<<‘B‘; cout<<endl; } return 0; } #include<stdio.h>//最优程序 int main() { int n; scanf("%d",&n); getchar(); while(n--) { int w=0,b=0; char c; while((c=getchar())!=10) c==‘R‘?printf("R"):(c==‘W‘?w++:b++); while(w--) putchar(‘W‘); while(b--) putchar(‘B‘); printf("\n"); } }
273、
2 asdasl+%$^&ksdhkjhjksd adklf&(%^(alkha
q j
#include "stdio.h" #include "string.h" int main(int argc, char const *argv[]) { int n; char a; //freopen("input.txt","r",stdin); scanf("%d",&n); getchar(); while(n--) { int number=0; while(scanf("%c",&a),a!=‘\n‘) { if (a>=‘a‘&&a<=‘z‘) number++; } if (number%26==0) printf("z\n"); else printf("%c\n",‘a‘+number%26-1); } return 0; } #include <cstdio>//最优程序 #include <cctype> #include <cstring> int main(){ int n,i; char arr[201]; scanf("%d",&n); while(n--){ scanf("%s",arr); int l=strlen(arr),r=0; for(int i=0;i!=l;i++) if(islower(arr[i])) r++; r%=26; printf("%c\n",r==0?‘z‘:96+r); } }
274、
5 1 13 22 62 155
1.05 176.98 506.84 4025.43 25158.92
#include<stdio.h> #define pi 3.1415926 int main() { double a,s=0; int n; scanf("%d",&n); while(n--) { scanf("%lf",&a); s=a*a*pi/3; printf("%.2lf\n",s); } return 0; } #include<stdio.h>//最优程序 int main() { int n; double m,pi=3.1415926; scanf("%d",&n); while(n--) { scanf("%lf",&m); printf("%.2lf\n",pi*m*m/3); } return 0; }
275、
ACM队的队花C小+经常抱怨:“C语言中的格式输出中有十六、十、八进制输出,然而却没有二进制输出,哎,真遗憾!谁能帮我写一个程序实现输入一个十进制数n,输出它的二进制数呀?”
难道你不想帮帮她吗?^_^
#include "iostream"//十进制转换成二进制 using namespace std; void dec_bin(const int x) { if (x/2>0) { dec_bin(x/2); cout<<x%2; } else cout<<x; } int main(int argc, char const *argv[]) { int data=0; while(cin>>data) { dec_bin(data); cout<<endl; } return 0; }
276、
任意给出两个英文字母,比较它们的大小,规定26个英文字母A,B,C.....Z依次从大到小。
#include<stdio.h> int main() { int i; char a, b; scanf("%d", &i); while (i--) { getchar(); scanf("%c %c", &a, &b); if (a > b) printf("%c<%c\n", a, b); else if (a == b) printf("%c=%c\n", a, b); else if (a < b) printf("%c>%c\n", a, b); } return 0; }
277、
#include "iostream" #include "string.h" #include "cstdio" using namespace std; int main(int argc, char const *argv[]) { int n; cin>>n; while(n--) { int m,i; char a[6]="zzzzz",str[6]; cin>>m; getchar(); for(i=0; i<m; i++) { scanf("%s",str); if(strcmp(a,str)>0) strcpy(a,str); } printf("%s\n",a); } return 0; } #include<stdio.h>//最优程序 #include<string.h> main(){int n;scanf("%d",&n);while(n--) {int m;char a[6],c[6];scanf("%d",&m);getchar();gets(c); while(--m){gets(a);if(strcmp(a,c)<0)strcpy(c,a);}puts(c);}}
283、
7 Bo Pat Jean Kevin Claude William Marybeth 6 Jim Ben Zoe Joey Frederick Annabelle 5 John Bill Fran Stan Cece 0
SET 1 Bo Jean Claude Marybeth William Kevin Pat SET 2 Jim Zoe Frederick Annabelle Joey Ben SET 3 John Fran Cece Stan Bill
#include<string> #include<iostream> #include<algorithm> using namespace std; bool cmp(string a, string b) { return a.size() < b.size(); } int main() { int n; int count=0; string a[10001]; while(cin>>n) { if(n==0)break; for(int i=0;i<n;i++) { cin>>a[i]; } sort(a,a+n,cmp);//sort cout<<"SET "<<++count<<endl; for(int i=0;i<n;i+=2) cout<<a[i]<<endl; if(n%2==0) { for(int i=n-1;i>0;i-=2) cout<<a[i]<<endl; } else for(int i=n-2;i>0;i-=2) cout<<a[i]<<endl; } return 0; } #include<stdio.h>//最优程序 #include<string.h> main() { int x,i,j,q=0; char s[16][26],t[26],z[16][26]; while(1) { scanf("%d",&x); if(x==0) break; q++; getchar(); for(i=0;i<x;i++) gets(s[i]); for(i=1;i<x;i++) for(j=0;j<x-i;j++) if(strlen(s[j])>strlen(s[j+1])) {strcpy(t,s[j]);strcpy(s[j],s[j+1]);strcpy(s[j+1],t);} for(i=0,j=0;i<x;i++,j++) { strcpy(z[j],s[i]); if(i!=x-1) strcpy(z[x-j-1],s[++i]); } printf("SET %d\n",q); for(i=0;i<x;i++) puts(z[i]); } }
324、
#include "stdio.h" int f(int m) { if(m==0) return 1; else return 2*(f(m-1)+1); } int main() { int n,m; //freopen("input.txt","r",stdin); scanf("%d",&n); while(n--) { scanf("%d",&m); printf("%d\n",f(m)); } return 0; } #include<stdio.h>//最优程序 int main() { int n,m; scanf("%d",&n); while(n--) { scanf("%d",&m); printf("%d\n",(3<<m)-2); } return 0; }
399、
#include<stdio.h> int main() { int n,b; while(scanf("%d%d",&n,&b)!=EOF) { int count; count=n/b; printf("%d\n",count); } return 0; } #include <iostream>//最优程序 using namespace std; int main() { int n,r; while(cin>>n>>r) cout<<n/r<<endl; }
455、
#include<stdio.h> int main() { int k; scanf("%d",&k); while(k--) { int n; scanf("%d",&n); printf("%d\n",n); } return 0; } #include<cstdio>//最优程序 main(){char _[15];gets(_);while(gets(_))puts(_);}
457、
#include<stdio.h> #include<string.h> int main() { int n, i, len; char a[100]; scanf("%d", &n); while (n--) { scanf("%s", a); len = strlen(a); for (i = 0; i < len; i++) { if (a[i] <= ‘z‘ && a[i] >= ‘a‘) a[i] = a[i] - 32; else a[i] = a[i] + 32; } printf("%s\n", a); } return 0; } #include<stdio.h>//最优程序 int main() { int a,b,c,n; char x; scanf("%d",&n); getchar(); while(n--) { while(scanf("%c",&x)&&x!=‘\n‘) { if(x>=97&&x<=122) printf("%c",x-32); else if(x<=90&&x>=64) printf("%c",x+32); } printf("\n"); } }
458、
#include "iostream" using namespace std; int main(int argc, char const *argv[]) { int n; cin>>n; while(n--) { long long m; cin>>m; cout<<(m-1)*1000+471<<endl; } return 0; } #include<stdio.h>//最优程序 int main() { long long a,b,c,d,e; scanf("%lld",&a); while(a--) { scanf("%lld",&b); printf("%lld\n",(b-1)*1000+471); } return 0; }
463、
小时候学过的九九乘法表也许将会扎根于我们一生的记忆,现在让我们重温那些温暖的记忆,请编程输出九九乘法表.
是那种反过来的三角形啦,具体如下图:
每两个式子之前用一个空格 隔开。。。
3 2 1 5
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45
#include "iostream" using namespace std; int main(int argc, char const *argv[]) { int n,i,j,m; cin>>n; while(n--) { cin>>m; for(i=1; i<=m; i++) { for(j=i; j<=9; j++) cout<<i<<‘*‘<<j<<‘=‘<<i*j<<‘ ‘; cout<<endl; } } return 0; } #include<stdio.h>//最优程序 int main() { int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1;i<=n;i++){ for(int j=i;j<10;j++) printf("%d*%d=%d ",i,j,i*j); printf("\n"); } } }
477、
3 -11.1 +11.1 0 11 -11.25 -0.25 1 2 +4
Yes Yes No
#include "iostream" using namespace std; int main(int argc, char const *argv[]) { int n; cin>>n; while(n--) { double a,b,c; cin>>a>>b>>c; if((a+b-c>-0.0001)&&(a+b-c<0.0001)) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; } #include <cstdio>//最优程序 #include <math.h> double a,b,c; main() { for(scanf("%lf",&a);~scanf("%lf%lf%lf",&a,&b,&c);puts(fabs(c-(a+b))<1e-6?"Yes":"No")); }
484、
Mr. B, Mr. G and Mr. M are now in Warsaw, Poland, for the 2012’s ACM-ICPC World Finals Contest. They’ve decided to take a 5 hours training every day before the contest. Also, they plan to start training at 10:00 each day since the World Final Contest will do so. The scenery in Warsaw is so attractive that Mr. B would always like to take a walk outside for a while after breakfast. However, Mr. B have to go back before training starts, otherwise his teammates will be annoyed. Here is a problem: Mr. B does not have a watch. In order to know the exact time, he has bought a new watch in Warsaw, but all the numbers on that watch are represented in Roman Numerals. Mr. B cannot understand such kind of numbers. Can you translate for him?
I II III IV V VI VII VIII IX X XI XII
Case 1: 1 Case 2: 2 Case 3: 3 Case 4: 4 Case 5: 5 Case 6: 6 Case 7: 7 Case 8: 8 Case 9: 9 Case 10: 10 Case 11: 11 Case 12: 12
#include<iostream> #include<string> using namespace std; int main() { string a; int count=1; while(cin>>a) { if(a=="I") { cout<<"Case "<<count<<": 1"<<endl; count++; } if(a=="II") { cout<<"Case "<<count<<": 2"<<endl; count++; } if(a=="III") { cout<<"Case "<<count<<": 3"<<endl; count++; } if(a=="IV") { cout<<"Case "<<count<<": 4"<<endl; count++; } if(a=="V") { cout<<"Case "<<count<<": 5"<<endl; count++; } if(a=="VI") { cout<<"Case "<<count<<": 6"<<endl; count++; } if(a=="VII") { cout<<"Case "<<count<<": 7"<<endl; count++; } if(a=="VIII") { cout<<"Case "<<count<<": 8"<<endl; count++; } if(a=="IX") { cout<<"Case "<<count<<": 9"<<endl; count++; } if(a=="X") { cout<<"Case "<<count<<": 10"<<endl; count++; } if(a=="XI") { cout<<"Case "<<count<<": 11"<<endl; count++; } if(a=="XII") { cout<<"Case "<<count<<": 12"<<endl; count++; } } return 0; } #include<map>//最优程序 #include<iostream> #include<string> using namespace std; map<string,int>m; int main() { m["I"]=1; m["II"]=2; m["III"]=3; m["IV"]=4; m["V"]=5; m["VI"]=6; m["VII"]=7; m["VIII"]=8; m["IX"]=9; m["X"]=10; m["XI"]=11; m["XII"]=12; string s; int c=0; while(cin>>s) cout<<"Case "<<++c<<": "<<m[s]<<endl; return 0; }
596、
5 9 10 7 11 1 6 5 7 3 5 2 7 3 7 6 0
19 13
#include "stdio.h" int main() { int n,max=0,a,b,i; // freopen("input.txt","r",stdin); while(scanf("%d",&n),n) { max=0; for(i=0; i<n; i++) { scanf("%d%d",&a,&b); if((a+b)>max)max=(a+b); } printf("%d\n",max); } return 0; } #include<stdio.h>//最优程序 main(){int n;while(scanf("%d",&n),n){int a,b,s=0;while(n--){scanf("%d%d",&a,&b);if(a+b>s) s=a+b;}printf("%d\n",s);}}
599、
2 1 5
1 1
#include<stdio.h> int main(void) { int H,i,t; scanf("%d",&t); for(i=1; i<=t; i++) { int day=1,h=0; scanf("%d",&H); while(h+10<H) { h+=5; day++; } printf("%d\n",day); } return 0; }
733、
万圣节有一个Party,XadillaX显然也要去凑热闹了。因为去凑热闹的人数非常庞大,几十W的数量级吧,自然要进场就需要有门票了。很幸运的,XadillaX竟然拿到了一张真·门票!这真·门票的排列规则有些奇怪:
门票号是由0~6组成的六位数(0~6这几个数字可重用)
每一个门票号的每一位不能有三个连续相同的数字(如123335是不行的)
每一个门票号相邻的两位相差必须在四以下(≤4)(如016245是不行的)
#include<stdio.h> #include<math.h> int main() { int n; scanf("%d",&n); while(n--) { int ticket1, ticket2, i, j; int flag1, flag2, flag3; char x[6]; scanf("%d %d",&ticket1, &ticket2); for (i = ticket1; i <= ticket2; i++) { flag1 = 1; flag2 = 1; flag3=1; x[0] = i / 100000 + ‘0‘; x[1] = i /10000 % 10 + ‘0‘; x[2] = i / 1000 % 10 + ‘0‘; x[3] = i / 100 % 10 + ‘0‘; x[4] = i / 10 % 10 + ‘0‘; x[5] = i % 10 + ‘0‘; for (j = 0; j < 6; j++) if (x[j] - ‘0‘ > 6) { flag3 = 0; break; } for (j = 0; j < 5; j++) if (fabs(x[j] - x[j+1]) >4) { flag1 = 0; break; } for (j = 0; j < 4; j++) if (x[j] - x[j+1] == 0 && x[j] - x[j+2] == 0) { flag2 = 0; break; } if (flag1 == 1 && flag2 == 1 && flag3 == 1) { for (j = 0; j < 6; j++) printf("%c",x[j]); printf("\n"); } } printf("\n"); } return 0; }
779、
3 2
3
#include<stdio.h> int main() { int n, k; while (scanf("%d%d", &n, &k) != EOF) { if (n < k) printf("2\n"); else { if (2 * n % k == 0) printf("%d\n", 2 * n / k); else printf("%d\n", 2 * n / k + 1); } } return 0; }
811、
Yougth讲课的时候考察了一下求三个数最大值这个问题,没想到大家掌握的这么烂,幸好在他的帮助下大家算是解决了这个问题,但是问题又来了。
他想在一组数中找一个数,这个数可以不是这组数中的最大的,但是要是相对比较大的,但是满足这个条件的数太多了,怎么办呢?他想到了一个办法,把这一组数从开始把每相邻三个数分成一组(组数是从1开始),奇数组的求最大值,偶数组的求最小值,然后找出这些值中的最大值。
#include "iostream" using namespace std; int max1(int a,int b,int c)//我去,这里用的max,尼玛 { int t; if(a>b){t=a;a=b;b=t;} if(a>c){t=a;a=c;c=t;} if(b>c){t=b;b=c;c=t;} return c; } int min1(int x,int y,int z) { int m; if(x>y){m=x;x=y;y=m;} if(x>z){m=x;x=z;z=m;} if(y>z){m=y;y=z;z=m;} return x; } int main(int argc, char const *argv[]) { int n,i,d[10001],e[4000]; while(cin>>n) { int max=0; for(i=1;i<=n;i++) { cin>>d[i]; } for(i=3;i<=n;i+=3) { if((i/3)%2==1) { e[i/3]=max1(d[i-2],d[i-1],d[i]); } if((i/3)%2==0) { e[i/3]=min1(d[i-2],d[i-1],d[i]); } } max=e[1]; for(i=2;i<=n/3;i++) if(e[i]>max) max=e[i]; cout<<max<<endl; } return 0; } #include <cstdio>//最优程序 #define Max(a,b,c) a>(b>c?b:c)?a:(b>c?b:c) #define Min(a,b,c) a>(b>c?c:b)?(b>c?c:b):a int main() { int a[10005]; int n,i,j,h; while(~scanf("%d",&n)) { int max=1<<32; for(i=0;i<n&&scanf("%d",&a[i]);i++){} for(i=0;i<n;i+=3){ h=i%2==0?Max(a[i],a[i+1],a[i+2]):Min(a[i],a[i+1],a[i+2]); max=h>max?h:max; } printf("%d\n",max); } return 0; }
813、
Topcoder 招进来了 n 个新同学,Yougth计划把这个n个同学分成两组,要求每组中每个人必须跟另一组中每个同学进行一次算法对决,问存不存在一种分组方式在k场完成对决。(两组中每一组中人数都要大于0)
#include "stdio.h" int main(int argc, char const *argv[]) { int i,n,k; while(scanf("%d%d",&n,&k),n,k) { int jt=0; for(i=0; i<n; i++) { if(k==i*(n-i)) { printf("YES\n"); jt=1; break; } } if(jt==0) printf("NO\n"); } return 0; } #include <cstdio>//最优程序 int main() { int n,k; while(scanf("%d%d",&n,&k)&&(n||k)) { int ok=0; for(int i=1;i<=n/2;i++) { if(i*(n-i)==k) ok=1;continue; } if(ok) printf("YES\n"); else printf("NO\n"); } return 0; }
822、
4
**** **** **** ****
#include "stdio.h" int main(int argc, char const *argv[]) { int n,i,j; scanf("%d",&n); for (i=0; i<n; i++) { for(j=0; j<n; j++) { printf("*"); } printf("\n"); } return 0; }
833、
Yougth和Hrdv玩一个游戏,拿出n个石子摆成一圈,Yougth和Hrdv分别从其中取石子,谁先取完者胜,每次可以从中取一个或者相邻两个,Hrdv先取,输出胜利着的名字。
#include <stdio.h> int main(void) { int n; while (scanf("%d", &n) != EOF) { if (n < 3) printf("Hrdv\n"); else printf("Yougth\n"); } } #include<cstdio> int n; int main() { while(~scanf("%d",&n)) printf(n>=3?"Yougth\n":"Hrdv\n"); return 0; }
844、
#include <stdio.h> /* 整数反转 */ long long rev(int a) { long long s = 0; while (a) { s = s * 10 + a % 10; a /= 10; } return s; } int main(void) { int m, n; long long M, N; while (1) { scanf("%d%d", &m, &n); if (m == 0 && n == 0) break; printf("%lld\n", rev(m) + rev(n)); } return 0; } #include <iostream>//最优程序 #include <string> using namespace std; int main() { string s,t; int x,y; while(cin>>s>>t) { int x=0,y=0; if(s[0]==‘0‘&&t[0]==‘0‘) break; for(int i=s.size()-1;i>=0;i--) x=x*10+(s[i]-‘0‘); for(int i=t.size()-1;i>=0;i--) y=y*10+(t[i]-‘0‘); cout<<x+y<<endl; } return 0; }
845、
#include <stdio.h> int main() { int m,n,i,j; int a[100]= {0}; int c[100]= {0},k=0; while(scanf("%d %d",&m,&n)&&m&&n) { a[m]+=n; c[k++]=m; } for(i=0; i<k-1; i++) { for(j=i+1; j<k; j++) { if(c[i]==c[j]) c[j]=0; } } for(i=0; i<k; i++) { if(c[i]!=0) { printf("%d %d\n",c[i],a[c[i]]); c[i]=0; } } return 0; }
975、
Acm队的流年对数学的研究不是很透彻,但是固执的他还是想一头扎进去。
浏 览网页的流年忽然看到了网上有人用玫瑰花瓣拼成了521三个数字,顿时觉得好浪漫,因为每个男生都会不经意的成为浪漫的制造者。此后,流年走到哪里都能看 到5、2、1三个数字,他怒了,现在他想知道在连续的数中有多少数全部包含了这三个数字。例如12356就算一个,而5111就不算。特别的,如果他看到 了521三个数连续出现,会特别的愤怒。例如35210。
#include <iostream> #include <string.h> using namespace std; int array[2][1000010]; int main() { int a, b, c; int cnt = 0, cntp = 0; memset(array, 0, sizeof(array)); for (int i = 1; i <= 1000000; i++) { a = b = c = 0; if ((i/100000)%10 == 1 || (i/10000)%10 == 1 || (i/1000)%10 == 1 || (i/100)%10 == 1 || (i/10)%10 == 1 || i%10 == 1) a = 1; if ((i/100000)%10 == 2 || (i/10000)%10 == 2 || (i/1000)%10 == 2 || (i/100)%10 == 2 || (i/10)%10 == 2 || i%10 == 2) b = 1; if ((i/100000)%10 == 5 || (i/10000)%10 == 5 || (i/1000)%10 == 5 || (i/100)%10 == 5 || (i/10)%10 == 5 || i%10 == 5) c = 1; if (a && b && c) ++cnt; if ((i/1000)%1000 == 521 || (i/100)%1000 == 521 || (i/10)%1000 == 521 || (i%1000) == 521) ++cntp; array[0][i] += cnt; array[1][i] += cntp; } int x, y, ca = 0; while (cin >> x >> y) { cout << "Case " << ++ca << ":"; cout << array[0][y] - array[0][x-1] << " " << array[1][y] - array[1][x-1] << endl; } return 0; } #include<stdio.h>//最优程序 int a[2][1000001]={0}; int main() { int k=0,i,sum=0; for(i=1;i<=1000000;i++) { if((i%10==5||(i/10)%10==5||(i/100)%10==5||(i/1000)%10==5||(i/10000)%10==5||(i/100000)%10==5)&&(i%10==2||(i/10)%10==2||(i/100)%10==2||(i/1000)%10==2 ||(i/10000)%10==2||(i/100000)%10==2)&&(i%10==1||(i/10)%10==1||(i/100)%10==1||(i/1000)%10==1||(i/10000)%10==1||(i/100000)%10==1)) {sum++; if(i/1000==521||(i/100)%1000==521||(i/10)%1000==521||i%1000==521)k++;} a[0][i]+=sum; a[1][i]+=k; } int m,n,w=0; while(scanf("%d%d",&n,&m)!=EOF) printf("Case %d:%d %d\n",++w,a[0][m]-a[0][n-1],a[1][m]-a[1][n-1]); }
1071、
判断:两个数x、y的正负性。
要求:不可以使用比较运算符,即"<",">","<=",">=","==","!="。
/* * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1071 * by jtahstu on 2015/2/11 15:00 * copy csdn 听自己心跳的声音 * http://blog.csdn.net/u013634213/article/details/40055329 */ #include<cstdio> #include<cstring> int main() { int a, b; while (scanf("%d%d", &a, &b) != EOF) { if (!(a * b)) { puts("Signs can‘t be sure"); continue; } if ((a >> 31) ^ (b >> 31)) puts("Signs are opposite"); else puts("Signs are not opposot"); } return 0; } #include<stdio.h>//最优程序 int main() { int a,b,c; while(~scanf("%d%d",&a,&b)) { c=((a>>31)&1)+((b>>31)&1); if((!a)||(!b))printf("Signs can‘t be sure\n"); else printf("Signs are %s\n",c&1?"opposite":"not opposot"); } }
1092、
在一个遥远的国家,银行为了更快更好的处理用户的订单,决定将一整串的数字按照一定的规则分隔开来,分隔规则如下:
1、实数的整数部分按照每三个数字用逗号分隔开(整数部分的高位有多余的0时,需先将多余的0过滤后,再进行数字分隔,如:0001234567 输出结果为1,234,567.00)
2、小数部分保留两位小数(四舍五入)
3、如果该数是负的,则在输出时需用括号将分隔后的数字括起来,例如:-10005.1645的输出结果为(10,005.16)
00012345670.0000-10005.1645
1,234,567.000.00(10,005.16)
#include <iostream> #include <cstring> using namespace std; int main() { char s[110],t[110],r[110]; while(cin>>s) { memset(r,‘\0‘,sizeof(r)); memset(t,‘\0‘,sizeof(t)); int i,l,poi=0,dis,car,k=0,f=1,j=0; l=strlen(s); for(i=s[0]==‘-‘?1:0; i<l; i++)//处理原字符串 { if(s[i]!=‘0‘&&f) { if(s[i]==‘.‘) t[j++]=‘0‘; f=0; } if(s[i]==‘0‘&&f) continue; else t[j++]=s[i]; } if(!j) { t[j++]=‘0‘;f=0;//判断当原字符串为0时的情况 } for(i=0; i<j; i++) { if(t[i]==‘.‘) { poi=i;//找出小数点的位置 f=1;//如果有小数点,则f置1 break; } } if(f)//当字符串有小数点时 { dis=j-1-poi;//计算小数点到字符串末尾的距离 if(dis==2) for(i=j-1; i>=0; i--)r[k++]=t[i];//如果只有两位小数,原样输出 else if(dis==1)//如果有一位小数,则最后一位补0 { r[k++]=‘0‘; for(i=j-1; i>=0; i--) r[k++]=t[i]; } else//如果距离大于2 { if(t[poi+3]<‘5‘) for(i=poi+2; i>=0; i--)r[k++]=t[i];//如果第三位小数小于5 else//如果第三位小数大于5 { car=1; for(i=poi+2; i>=0; i--) { if(t[i]==‘.‘) { r[k++]=t[i]; continue; } if(car) t[i]+=1; if(t[i]>‘9‘) { t[i]=‘0‘; car=1; } else car=0; r[k++]=t[i]; } if(t[0]==‘0‘) r[k++]=‘1‘; } } } else for(i=j-1; i>=0; i--)r[k++]=t[i];//没有小数点,原样输出 if(s[0]==‘-‘)//如果是负数 { if(!f) { cout<<‘(‘;//负数需要在字符串前后加上括号 for(i=k-1; i>=0; i--) { cout<<r[i]; if(i%3==0&&i) cout<<‘,‘;//控制‘,‘的位置 } cout<<".00"<<‘)‘<<endl;//没有小数点,补上小数点和两位零 } else { cout<<‘(‘; for(i=k-1; i>=0; i--) { cout<<r[i]; if(i%3==0&&i>3) cout<<‘,‘; } cout<<‘)‘<<endl; } } else { if(!f) { for(i=k-1; i>=0; i--) { cout<<r[i]; if(i%3==0&&i) cout<<‘,‘; } cout<<".00"<<endl; } else { for(i=k-1; i>=0; i--) { cout<<r[i]; if(i%3==0&&i>3) cout<<‘,‘; } cout<<endl; } } } }
1104、
as we all know ,就我不坑,呵呵,当然,这次我还是不坑,我只让你帮我翻译一下数字即可。
所谓翻译,就是将一个数字用中文读出来,很简单吧,快快AC吧。
数字的中文表示分别为:零、壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿.
这题换题了,之前不是这题目,所以就粘贴了一份来 #include<stdio.h> #include<string.h> int main() { char ch[20]; int len,i,k,t,p; while(scanf("%s",ch)!=EOF) { k=0;p=1; len=strlen(ch); for(i=0;i<len;i++) { t=len-k; if(ch[i]==‘0‘&&(t-1)%4!=0&&ch[i+1]!=‘0‘||ch[0]==‘0‘) printf("零"); if(ch[i]==‘1‘) printf("壹"); if(ch[i]==‘2‘) printf("贰"); if(ch[i]==‘3‘) printf("叁"); if(ch[i]==‘4‘) printf("肆"); if(ch[i]==‘5‘) printf("伍"); if(ch[i]==‘6‘) printf("陆"); if(ch[i]==‘7‘) printf("柒"); if(ch[i]==‘8‘) printf("捌"); if(ch[i]==‘9‘) printf("玖"); if(ch[i]!=‘0‘||(t-1)%4==0) { if(t==10) printf("拾"); if(t==9) printf("亿"); if(t==8) printf("仟"); if(t==7) printf("佰"); if(t==6) printf("拾"); if(t==5&&(ch[k]!=‘0‘||ch[k-1]!=‘0‘||ch[k-2]!=‘0‘||ch[k-3]!=‘0‘)) printf("万"); if(t==4) printf("仟"); if(t==3) printf("佰"); if(t==2) printf("拾"); } k++; } printf("\n"); } return 0; }
OK,本文到此也就结束了,也花了本人好几个小时的时间,但AC这些题却用了好几个月,花了相当长的时间,也是边刷边学习,不断改进,下次再写汇总也不知何年何月了,但还会写其他的,马上学java了,又有的写了,慢慢来。
标签:
原文地址:http://www.cnblogs.com/jtahstu/p/4288547.html