L1-001 Hello World (5 分)
#include
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
system("pause");
}
L1-002 打印沙漏 (20 分)
#include
using namespace std;
int main()
{
int N,n=0,i=1,r=0;
char c;
cin>>N>>c;
while(n<=N)
{
n=2*i*i-1;
i++;
}
i=i-2;
r=N-2*i*i+1;
int j,k;
for(j=0;j<i-1;j++)
{
for(k=0;k<j;k++)
cout<<" ";
for(k=0;k<2*(i-1-j)+1;k++)
cout<<c;
cout<<endl;
}
for(j=0;j<i;j++)
{
for(k=0;k<i-j-1;k++)
cout<<" ";
for(k=0;k<2*j+1;k++)
cout<<c;
cout<<endl;
}
cout<<r;
return 0;
}
L1-003 个位数统计 (15 分)
#include
#include
using namespace std;
int main()
{
string number;
int a[10]={0};
int i;
cin>>number;
for(i=0;i<number.size();i++)
a[number[i]-'0']++;
for(i=0;i<10;i++)
if(a[i]) cout<<i<<":"<<a[i]<<endl;
return 0;
}
L1-004 计算摄氏温度 (5 分)
#include
using namespace std;
int main()
{
int C;
int F;
cin>>F;
cout<<"Celsius = "<<5*(F-32)/9;
return 0;
}
L1-005 考试座位号 (15 分)
#include
using namespace std;
struct person{
long long int ip;
int num;
int sit;
}a[1025];
int main()
{
int N,i,j;
cin>>N;
for(i=0;i<N;i++)
{
cin>>a[i].ip>>a[i].num>>a[i].sit;
}
int M,d;
cin>>M;
for(i=0;i<M;i++)
{
cin>>d;
for(j=0;j<N;j++)
{
if(a[j].num==d)
cout<<a[j].ip<<" "<<a[j].sit<<endl;
}
}
}
L1-006 连续因子 (20 分)
#include
#include
using namespace std;
int IsPrime(int number)
{
int i;
if (number == 2) return 1;
else
{
for (i = 2; i <= sqrt(number); i++)
if (number%i == 0) return 0;
return 1;
}
}
int main()
{
int fact = 0, i, j, N,len=0,start=0;
cin >> N;
if (IsPrime(N))
{
cout << "1" << endl;
cout << N;
}
else
{
for (i = 2; i <= sqrt(N); i++)
{
fact = 1;
for (j = i; j*fact <= N; j++)
{
fact *= j;
if (N%fact == 0 && j - i + 1 > len)
{
len = j - i + 1;
start = i;
}
}
}
cout << len<< endl;
for (i = start; i < start + len; i++)
{
cout << i;
if (i < start + len - 1)
cout << "*";
}
}
}
L1-007 念数字 (10 分)
#include
#include
#include
int main()
{
int number,n=0,k;
char a[10][5];
strcpy(a[0],"ling");
strcpy(a[1],"yi");
strcpy(a[2],"er");
strcpy(a[3],"san");
strcpy(a[4],"si");
strcpy(a[5],"wu");
strcpy(a[6],"liu");
strcpy(a[7],"qi");
strcpy(a[8],"ba");
strcpy(a[9],"jiu");
scanf("%d",&number);
k=number;
if(number<0){
number=-number;
printf("fu");}
while(number)
{
number/=10;
n++;
}
for(int i=n-1;i>=0;i--){
if(k<10) printf("%s",a[k]);
else{
printf("%s",a[k/(int)pow(10,i)]);
k=k-(k/(int)pow(10,i))*(int)pow(10,i);}
}
return 0;
}
L1-008 求整数段和 (10 分)
#include
int main()
{
int A,B,i,count=0,Sum=0;
scanf("%d%d",&A,&B);
for(i=A;i<=B;i++)
{
printf("%5d",i);
Sum+=i;
count++;
if(count%5==0||i==B) printf("\n");
}
printf("Sum = %d",Sum);
return 0;
}
L1-009 N个数求和 (20 分)
#include
using namespace std;
int gcd(int q,int p)
{
if (q%p == 0) return p;
else
return gcd(p, q%p);
}
int main()
{
int sum1 = 0, sum2 = 1,m=0;
int a, b;
char c = '/';
int N;
cin >> N;
while (N--)
{
cin >> a >>c>> b;
sum1 *= b;
sum1 += a * sum2;
sum2 = sum2 * b;
m = gcd(sum1, sum2);
sum1 /= m;
sum2 /= m;
}
if (sum1 > sum2&&sum1%sum2!=0) cout << sum1 / sum2 << " " << sum1 % sum2 << c << sum2;
else if (sum1%sum2 == 0)cout << sum1 / sum2;
else if (sum1 < sum2) cout<< sum1 << c << sum2;
}
L1-010 比较大小 (10 分)
#include
using namespace std;
int main()
{
int a[3],temp;
int min = 0;
int i, j;
for (i = 0; i < 3; i++)
cin >> a[i];
for (i = 0; i < 2; i++)
{
min = i;
for (j = i+1; j < 3; j++)
{
if (a[min]> a[j])
min = j;
}
if (min != i)
{
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
for (i = 0; i < 3; i++)
{
cout << a[i];
if (i < 2)cout << "->";
}
}
L1-011 A-B (20 分)
#include
#include
using namespace std;
int main()
{
string a;
string b;
getline(cin, a);
getline(cin, b);
int i, j,flag=0;
for (i = 0; i < a.size(); i++)
{
flag = 0;
for (j = 0; j < b.size(); j++)
{
if (a[i] == b[j])
flag = 1;
}
if (flag == 0)cout << a[i];
}
}
L1-012 计算指数 (5 分)
#include
#include
int main()
{
int n,sum=0;
scanf("%d",&n);
sum=pow(2,n);
printf("2^%d = %d",n,sum);
return 0;
}
L1-013 计算阶乘和 (10 分)
#include
int main()
{
int n,N=0,sum=1,i,j,add=0;
scanf("%d",&N);
for(i=1;i<=N;i++)
{
sum=1;
for(j=1;j<=i;j++)
sum*=j;
add+=sum;
}
printf("%d",add);
}
L1-014 简单题 (5 分)
#include
int main()
{
printf("This is a simple problem.");
}
L1-015 跟奥巴马一起画方块 (15 分)
#include
int main()
{
int N,i,j,t;
char a;
scanf("%d %c",&N,&a);
if(N%2) t=N/2+1;
else t=N/2;
for(i=0;i<t;i++)
{
for(j=0;j<N;j++)
printf("%c",a);
printf("\n");
}
return 0;
}
L1-016 查验身份证 (15 分)
#include
#include
using namespace std;
int main()
{
int power[20] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };
string check = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
string str;
int N,i,j,flag=1;
cin >> N;
for (i = 0; i < N; i++)
{
cin >> str;
int sum = 0, sign = 1;
for (j = 0; j < 17; j++) {
if (str[j] >= '0'&&str[j] <= '9')
{
sum += (str[j] - '0')*power[j];
}
else
{
cout << str << endl;
sign = 0;
flag = 0;
break;
}
}
if (str[17] != check[sum % 11]&&sign)
{
cout << str << endl;
flag = 0;
}
}
if (flag)
cout << "All passed";
return 0;
}
L1-017 到底有多二 (15 分)
#include
#include
using namespace std;
int main()
{
string num;
int n,i,count=0,flag=0;
double x;
cin >> num;
if (num[0] == '-')
{
n = num.size() - 1;
for (i = 1; i <= n; i++)
{
if (num[i] == '2')
count++;
}
if ((num[num.size() - 1] -'0')% 2 == 0)flag = 2;
else flag = 1;
x = count*1.0 / n*1.5*flag*100;
printf("%.2f%%", x);
}
else
{
n = num.size();
for (i = 0; i < n; i++)
{
if (num[i] == '2')
count++;
}
if ((num[num.size() - 1] - '0') % 2 == 0)flag = 2;
else flag = 1;
x = count * 1.0 / n * 1*flag * 100;
printf("%.2f%%", x);
}
return 0;
}
L1-018 大笨钟 (10 分)
#include
#include
#include
using namespace std;
int main()
{
int hh, mm,count=0,i;
char c = ':';
cin >> hh >> c >> mm;
if (hh < 12)
printf("Only %02d:%02d. Too early to Dang.", hh, mm);
else if (hh > 12)
{
if (mm != 0)
count = hh + 1-12;
else count = hh-12;
for (i = 0; i < count; i++)
cout << "Dang";
}
else if (hh == 12)
{
if(mm==0)printf("Only %02d:%02d. Too early to Dang.", hh, mm);
else if (mm != 0)printf("Dang");
}
}
L1-019 谁先倒 (15 分)
#include
using namespace std;
int main()
{
int x, y,c,d;
cin >> x >> y;
c=x;
d=y;
int N;
cin >> N;
int a1, a2, b1, b2;
while (N--)
{
cin >> a1 >> a2 >> b1 >> b2;
if (a2 == a1 + b1 && b2 != b1 + a1)
x--;
if (b2 == b1 + a1 && a2 != a1 + b1)
y--;
if (x < 0)
{
cout << 'A' << endl;
cout << d-y;
break;
}
else if (y <0)
{
cout << 'B' << endl;
cout << c-x;
break;
}
}
}
L1-020 帅到没朋友 (20 分)
#include
using namespace std;
int main()
{
int N;
cin >> N;
int a[122555] = { 0 };
int i,j,x,num ;
for (i = 0; i < N; i++)
{
cin >> x;
for (j = 0; j < x; j++)
{
cin >> num;
if(x!=1)
a[num] = 1;
}
}
int n,flag=0,sign=0;
cin >> n;
for (i = 0; i < n; i++)
{
cin >> num;
if (a[num] == 0)
{
if(flag)
cout << " ";
flag++;
printf("%05d",num);
a[num] = 2;
sign = 1;
}
}
if (sign == 0)
cout << "No one is handsome";
return 0;
}
L1-021 重要的话说三遍 (5 分)
#include
int main()
{
printf("I'm gonna WIN!\n");
printf("I'm gonna WIN!\n");
printf("I'm gonna WIN!\n");
return 0;
}
L1-022 奇偶分家 (10 分)
#include
int main()
{
int n,i,k=0,l=0;
scanf("%d\n",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{if(a[i]%2==0) k++;
else l++;
}
printf("%d %d",l,k);
return 0;
}
L1-023 输出GPLT (20 分)
#include
#include
using namespace std;
int main()
{
string s;
cin >> s;
int i, j,g=0,p=0,l=0,t=0;
for (i = 0; i < s.size(); i++)
{
if (s[i] == 'G' || s[i] == 'g')
g++;
if (s[i] == 'P' || s[i] == 'p')
p++;
if (s[i] == 'L' || s[i] == 'l')
l++;
if (s[i] == 'T' || s[i] == 't')
t++;
}
int n = g + p + l + t;
while (n--)
{
if (g > 0)
{
cout << 'G';
g--;
}
if (p > 0)
{
cout << 'P';
p--;
}
if (l > 0)
{
cout << 'L';
l--;
}
if (t > 0)
{
cout << 'T';
t--;
}
}
}
L1-024 后天 (5 分)
#include
int main()
{
int D,h;
scanf("%d",&D);
h=D+2;
if(h>7)
printf("%d",h%7);
else
printf("%d",h);
return 0;
}
L1-025 正整数A+B (15 分)
#include
#include
using namespace std;
int main()
{
string a;
string b;
int m = 0, n = 0;
cin >> a;
getline(cin, b);
int i, flag = 0, sign = 0;
for (i = 0; i < a.size(); i++)
{
if (a[i] < '0' || a[i] >'9')
{
flag = 1;
}
}
for (i = 1; i < b.size() - 1; i++)
{
if (b[i] < '0' || b[i] >'9')
{
sign = 1;
}
}
for (i = 0; i < a.size(); i++)
{
m = m * 10 + a[i] - '0';
}
for (i = 1; i < b.size(); i++)
{
n = n * 10 + b[i] - '0';
}
if (m > 1000||a[0]=='0')flag = 1;
if (n > 1000||b[1]=='0')sign = 1;
if (flag) a = "?";
if (sign) b = " ?";
if (flag == 0 && sign == 0)
{
cout << a << " " << '+' << b << " " << '=' << " " << m + n;
}
else if (flag || sign)
{
cout << a << " " << '+' << b << " " << '=' << " " << '?';
}
}
L1-026 I Love GPLT (5 分)
#include"stdio.h"
#include"stdlib.h"
int main()
{
char a[12]={"I Love GPLT"};
int i;
for(i=0;i<=11;i++)
printf("%c\n",a[i]);
return 0;
}
L1-027 出租 (20 分)
#include
#include
#include
int main()
{
char a[12];
char id[12];
char c[12];
int i,j,max,temp,sign=0,flag=0,k=0;
scanf("%s",id);
strcpy(a,id);
for(i=0;i<10;i++)
{
max=i;
for(j=i;j<11;j++)
{
if(id[max]<id[j])
max=j;
}
if(max!=i)
{
temp=id[i];
id[i]=id[max];
id[max]=temp;
}
}
for(i=0;i<11-sign;i++)
{
if(id[i]==id[i+1])
{
for(j=i;j<11-sign;j++)
id[j]=id[j+1];
sign++;
i--;
}
}
printf("int[] arr = new int[]{");
for(i=0;i<strlen(id);i++)
{
printf("%c",id[i]);
flag++;
if(flag<strlen(id))
printf(",");
}
printf("};\n");
for(i=0;i<11;i++)
{
for(j=0;j<strlen(id);j++)
{
if(a[i]==id[j])
c[k++]=j;
}
}
flag=0;
printf("int[] index = new int[]{");
for(i=0;i<k;i++)
{
printf("%d",c[i]);
flag++;
if(flag<k)
printf(",");
}
printf("};");
return 0;
}
L1-028 判断素数 (10 分)
#include
#include
int main()
{
int N,i,j,sign=0;
scanf("%d\n",&N);
long long int a[N];
for(i=0;i<N;i++)
scanf("%lld",&a[i]);
for(i=0;i<N;i++)
{
sign=0;
if(a[i]==2)
printf("Yes\n");
if(a[i]==1)
printf("No\n");
else if(a[i]>2)
{
for(j=2;j<=sqrt(a[i]);++j)
{
if(a[i]%j==0)
{
sign=1;
}
}
if(sign==0)
printf("Yes\n");
if(sign)
printf("No\n");
}
}
return 0;
}
L1-029 是不是太胖了 (5 分)
#include
int main()
{
int H;
double T;
scanf("%d",&H);
T=((H-100)*0.9)*2;
printf("%.1f",T);
return 0;
}
L1-030 一帮一 (15 分)
#include
#include
using namespace std;
struct person
{
int sex;
string name;
int flag = 0;
}a[55];
int main()
{
int N;
cin >> N;
int i,j;
for (i = 0; i < N; i++)
{
cin >> a[i].sex >> a[i].name;
}
for (i = 0; i < N; i++)
{
for (j=N-1; j >= 0; j--)
{
if (a[i].sex != a[j].sex&&a[j].flag == 0)
{
cout << a[i].name << " " << a[j].name << endl;
a[j].flag = 1;
a[i].flag = 1;
break;
}
}
}
}
L1-031 到底是不是太胖了 (10 分)
#include
#include
int main() {
double s,x,a,b;
int n; scanf("%d",&n); while(n--) {
scanf("%lf%lf",&a,&b);
s=(a-100.0)*0.9;
x=b/2.0;
if(x>(s-s*0.1)&&x<(s+s*0.1))
printf("You are wan mei!\n");
if(x>=(s+s*0.1))
printf("You are tai pang le!\n");
if(x<=(s-s*0.1))
printf("You are tai shou le!\n");
}
return 0; }
L1-032 Left-pad (20 分)
#include
#include
using namespace std;
int main()
{
string a;
char c;
int N,i;
cin >> N >> c;
getchar();
getline(cin, a);
if (a.size() > N)
{
for(i=a.size()-N;i<a.size();i++)
cout<<a[i];
}
else
{
a.insert(0,N-a.size(), c);
cout << a;
}
return 0;
}
L1-033 出生年 (15 分)
#include
#include
#include
using namespace std;
int main()
{
int year, N,i;
cin >> year >> N;
int year1 = year;
int year2 = year;
while (1)
{
year1 = year;
set<int>s;
for (i = 0; i < 4; i++)
{
s.insert(year1 % 10);
year1 /= 10;
}
if (N == s.size())
{
printf("%d %04d", year - year2, year);
break;
}
else year++;
}
}
L1-034 点赞 (20 分)
#include
#include
using namespace std;
int main()
{
int N,d,x;
cin >> N;
int i, j;
int a[1001] = { 0 };
for (i = 0; i < N; i++)
{
cin >> d;
for (j = 0; j < d; j++)
{
cin >> x;
a[x]++;
}
}
int max=0;
for (j = 0; j < 1001; j++)
{
if (a[max] <= a[j])
{
a[max] = a[j];
max = j;
}
}
cout << max << " " << a[max];
}
L1-035 情人节 (15 分)
#include
#include
using namespace std;
struct person {
string name;
}a[1001];
int main()
{
int n = 0, i;
char c = 'c';
int sign = 0;
while (1)
{
cin >> a[n].name;
if (n < 14 && n >= 2)sign = 1;
else if (n >= 14)sign = 2;
if (a[n].name == ".")break;
n++;
}
if (sign == 0) cout << "Momo... No one is for you ...";
if (sign == 1)
cout << a[1].name << " is the only one for you...";
if (sign == 2)
cout << a[1].name << " and " << a[13].name << " are inviting you to dinner...";
}
L1-036 A乘以B (5 分)
#include
int main()
{
int A,B,i,sum=1;
scanf("%d%d",&A,&B);
sum=A*B;
printf("%d",sum);
return 0;
}
L1-037 A除以B (10 分)
#include
int main()
{
double A,B;
scanf("%lf%lf",&A,&B);
if(B<0)
printf("%.0f/(%.0f)=%.2f",A,B,A/B);
if(B>0)
printf("%.0f/%.0f=%.2f",A,B,A/B);
if(B==0)
printf("%.0f/0=Error",A);
return 0;
}
L1-038 新世界 (5 分)
#include
int main()
{
printf("Hello World\n");
printf("Hello New World");
return 0;
}
L1-039 古风排版 (20 分)
#include
#include
using namespace std;
int main()
{
int n;
string s;
cin >> n;
getchar();
getline(cin, s);
int i, j, k = 0;
char a[1001][1001];
int lie = 0;
if (s.size() % n) lie = s.size() / n + 1;
else lie = s.size() / n;
for (i = lie - 1; i >= 0; i--)
{
for (j = 0; j < n; j++)
{
if(k<s.size())
a[j][i] = s[k++];
else a[j][i] = ' ';
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < lie; j++)
{
cout << a[i][j];
}
cout << endl;
}
}
L1-040 最佳情侣身高差 (10 分)
#include
struct person{
char sex;
double high;
};
int main()
{
int n;
double h;
scanf("%d\n",&n);
struct person a[99];
for(int i=0;i<n;i++)
scanf("%c %lf\n",&a[i].sex,&a[i].high);
for(int i=0;i<n;i++)
{
if(a[i].sex=='M')
h=a[i].high/1.09;
if(a[i].sex=='F')
h=a[i].high*1.09;
printf("%.2f\n",h);
}
return 0;
}
L1-041 寻找250 (10 分)
#include
int main()
{
int a[9999],i=0;
while(1)
{
scanf("%d",&a[i]);
if(a[i]==250) break;
i++;
}
printf("%d\n",i+1);
return 0;
}
L1-042 日期格式化 (5 分)
#include
int main()
{
int m,d,y;
scanf("%d-%d-%d",&m,&d,&y);
printf("%04d-%02d-%02d",y,m,d);
return 0;
}
L1-043 阅览室 (20 分)
#include
#include
typedef struct n
{
int hour;
int m;
}time;
int solve(time a,int b,int c)
{
return (b-a.hour)*60+c-a.m;
}
main()
{
int n,num,a,b,flag[1002],sum,count;
char c;
time d[1005];
scanf("%d",&n);
for(int i=0;i<n;)
{ sum=0;
count =0;
for(int j=0;j<1002;j++)flag[j]=0;
for(;scanf("%d %c %d:%d",&num,&c,&a,&b)&&num!=0;)
{
if(c=='E')
{
if(flag[num]==1)
{
sum+=solve(d[num],a,b);
count++;
flag[num]=0;
}
}
else {
flag[num]=1;
d[num].hour=a;
d[num].m=b;
}
}
i++;
if(count!=0)
printf("%d %d\n",count,(int)(1.0*sum/count+0.5));
else printf("0 0\n");
}
}
L1-044 稳赢 (15 分)
#include
#include
int main()
{
char str[10];
char s[]="Bu";
char z[]="JianDao";
char w[]="ChuiZi";
int k,n=0;
scanf("%d",&k);
while(n<(k+1))
{ scanf("%s",str);
if(strcmp(str,"End")==0)
break;
else
{
if(n<k)
{
{ if(str[0]=='C')
{puts(s);
}
else
if(str[0]=='J')
{puts(w);
}
else
if(str[0]=='B')
{ puts(z);
}
}
n++;
}
else
{
puts(str);
n=0;
}
}
}
return 0;
}
L1-045 宇宙无敌大招呼 (5 分)
#include
int main()
{
char s[10];
scanf("%s",s);
printf("Hello %s",s);
return 0;
}
L1-046 整除光棍 (20 分)
#include
int main()
{
int x,s=0,n=0;
scanf("%d",&x);
while(s<x)
{
s=s*10+1;
n++;
}
while(1)
{
printf("%d",s/x);
s%=x;
if(s==0) break;
s=s*10+1;
n++ ;
}
printf(" %d\n",n);
return 0;
}
L1-047 装睡 (10 分)
#include
struct person{
char name[10];
int huxi;
int maibo;
int flag;
};
int main()
{
struct person a[11];
int N,i;
scanf("%d\n",&N);
for(i=0;i<N;i++)
a[i].flag=0;
for(i=0;i<N;i++)
{
scanf("%s%d%d",a[i].name,&a[i].huxi,&a[i].maibo);
if(a[i].huxi<15||a[i].huxi>20||a[i].maibo<50||a[i].maibo>70)
a[i].flag=1;
}
for(i=0;i<N;i++)
if(a[i].flag==1) printf("%s\n",a[i].name);
return 0;
}
L1-048 矩阵A乘以B (15 分)
#include
using namespace std;
int main()
{
int x1, y1;
cin >> x1 >> y1;
int num1[100][100];
int num2[100][100];
int i, j,k;
int sum[100][100] = {0};
for (i = 0; i < x1; i++)
for (j = 0; j < y1; j++)
cin >> num1[i][j];
int x2, y2;
cin >> x2 >> y2;
int flag=0;
for (i = 0; i < x2; i++)
for (j = 0; j < y2; j++)
cin >> num2[i][j];
if (y1 != x2)printf("Error: %d != %d",y1,x2);
else
{
cout << x1 << " " << y2 << endl;
for (k = 0; k < x1; k++)
{
flag=0;
for (i = 0; i < y2; i++)
{
sum[k][i] = 0;
for (j = 0; j < x2; j++)
{
sum[k][i] += num1[k][j] * num2[j][i];
}
if(flag)
cout<<" ";
cout<<sum[k][i];
flag++;
}
cout << endl;
}
}
}