声明:
题目部分皆为南阳OJ题目。
代码部分包含AC代码(可能不止一个)和最优代码,大部分都是本人写的,并且大部分为c代码和少部分c++代码and极少java代码,但基本都是c语言知识点,没有太多差别,可能代码有的写的比较丑,毕竟知识有限。
语言入门部分题基本都较为简单,是学习编程入门的很好练习,也是ACM的第一步,入门的最佳方法,望认真对待。
本文由csdn-jtahstu原创,转载请注明出处,欢迎志同道合的朋友一起交流学习。本人QQ:1373758426和csdn博客地址。
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、给定两个数m,n,其中m是一个素数。
将n(0<=n<=10000)的阶乘分解质因数,求其中有多少个m。
#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);}}
原文地址:http://blog.csdn.net/jtahstu/article/details/43742793