程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[daily practice of Blue Bridge Cup c++ and python] brush questions every day day2: output in the grid, nine array fractions, addition and multiplication

編輯:Python

Hello, Hello, everyone. I'm a self-motivated kid Vegetable pig

Gou huaisifangzhi , Where you can swim , Let's make progress together !

One , Output from the grid

label : Fill in the blank , 2015, Provincial competition

Title Description

This topic is code completion and filling in the blanks , Please complete the source code given in the title , And copy it to the code box on the right , Select the corresponding compilation language (C/Java) Submit after . If the source language given in the title is not unique , Then you only need to select one of them to complete the submission . After copying, delete the underline in the blank part of the source code , Fill in your answer . If it fails to pass after submission , In addition to considering the mistakes in filling in the blanks , You should also pay attention to whether there are errors due to changes in the non blank parts after copying .

StringInGrid The function prints the specified string in a specified size grid .

The string is required to be horizontal 、 It's centered in both directions .

If the string is too long , Just cut off .

If you can't just Center , It can be a little bit left or a little bit up .

The following program implements this logic , Please fill in the missing code in the underlined section .

Source code

#include <stdio.h>
#include <string.h>
void StringInGrid(int width, int height, const char* s)
{
int i,k;
char buf[1000];
strcpy(buf, s);
if(strlen(s)>width-2) buf[width-2]=0;
printf("+");
for(i=0;i<width-2;i++) printf("-");
printf("+\n");
for(k=1; k<(height-1)/2;k++){
printf("|");
for(i=0;i<width-2;i++) printf(".");
printf("|\n");
}
printf("|");
printf("%*s%s%*s",__________________);
printf("|\n");
for(k=(height-1)/2+1; k<height-1; k++){
printf("|");
for(i=0;i<width-2;i++) printf(".");
printf("|\n");
}
printf("+");
for(i=0;i<width-2;i++) printf("-");
printf("+\n");
}
int main()
{
StringInGrid(10,4,"abcd123");
return 0;
}

c++ solution :

#include <stdio.h>
#include <string.h>
void StringInGrid(int width, int height, const char* s)
{
int i,k;
char buf[1000];
strcpy(buf, s);
if(strlen(s)>width-2) buf[width-2]=0;
printf("+");
for(i=0;i<width-2;i++) printf("-");
printf("+\n");
for(k=1; k<(height-1)/2;k++){
printf("|");
for(i=0;i<width-2;i++) printf(".");
printf("|\n");
}
printf("|");
printf("%*s%s%*s",0,"",s,1,"");
printf("|\n");
for(k=(height-1)/2+1; k<height-1; k++){
printf("|");
for(i=0;i<width-2;i++) printf(".");
printf("|\n");
}
printf("+");
for(i=0;i<width-2;i++) printf("-");
printf("+\n");
}
int main()
{
StringInGrid(10,4,"abcd123");
return 0;
}

Summary of knowledge points :

1. stay printf In the sentence * Represents a more flexible control domain If written printf("%6d", 123); %6s Also domain width ;

2.printf("%*s",3,"") Represents the output of three spaces .

3. Use %s, Indicates that the specific field width here is determined by the following arguments , Such as printf("%s",6, “abc”) Is to put "abc" Put in a field with a width of 6 Right aligned in the space of .

Two , Nine array fractions

label : Fill in the blank , 2015, Provincial competition

Title Description

This topic is code completion and filling in the blanks , Please complete the source code given in the title , And copy it to the code box on the right , Select the corresponding compilation language (C/Java) Submit after . If the source language given in the title is not unique , Then you only need to select one of them to complete the submission . After copying, delete the underline in the blank part of the source code , Fill in your answer . If it fails to pass after submission , In addition to considering the mistakes in filling in the blanks , You should also pay attention to whether there are errors due to changes in the non blank parts after copying .

1,2,3...9 These nine numbers make up a fraction , The value is exactly 1/3, How to organize ?

The following program implements this function , Please fill in the missing code in the underlined part .

Source code :

#include <stdio.h>
void test(int x[])
{
int a = x[0]*1000 + x[1]*100 + x[2]*10 + x[3];
int b = x[4]*10000 + x[5]*1000 + x[6]*100 + x[7]*10 + x[8];
if(a*3==b) printf("%d/%d\n", a, b);
}
void f(int x[], int k)
{
int i,t;
if(k>=9){
test(x);
return;
}
for(i=k; i<9; i++){
{t=x[k]; x[k]=x[i]; x[i]=t;}
f(x,k+1);
_________________
}
}
int main()
{
int x[] = {1,2,3,4,5,6,7,8,9};
f(x,0);
return 0;
}

analysis :

Insert picture description here

for(i = k; i < 9; i++){t = xk;xk = xi;xi = t; to flash back ;}

c++ solution :

#include <stdio.h>
void test(int x[])
{
int a = x[0]*1000 + x[1]*100 + x[2]*10 + x[3];
int b = x[4]*10000 + x[5]*1000 + x[6]*100 + x[7]*10 + x[8];
if(a*3==b) printf("%d/%d\n", a, b);
}
void f(int x[], int k)
{
int i,t;
if(k>=9){
test(x);
return;
}
for(i=k; i<9; i++)
{
{t=x[k]; x[k]=x[i]; x[i]=t;}
f(x,k+1);
{t=x[k]; x[k]=x[i]; x[i]=t;}
}
}
int main()
{
int x[] = {1,2,3,4,5,6,7,8,9};
f(x,0);
return 0;
}

3、 ... and , Multiplication and addition

label : Fill in the blanks , 2015, Provincial competition

We all know :1+2+3+ ... + 49 = 12251+2+3+...+49=1225

Now you are required to change two of the nonadjacent plus signs into multipliers , Make the result 20152015

such as :

1+2+3+...+1011+12+...+2728+29+...+49 = 2015

1+2+3+...+10∗11+12+...+27∗28+29+...+49=2015

It's the answer that meets the requirements .

Please look for another possible answer , And submit the number to the left of the multiplier in front of you ( For example , Is to submit 1010).

Be careful : What you need to submit is an integer , Don't fill in any superfluous information .

answer :

16

c++ solution :

#include<bits/stdc++.h>
using namespace std;
int main()
{
for(int i=1;i<47;i++)
{
for(int j=i+2;j<49;j++)
{
if((i*(i+1)-(i+i+1)+(j*(j+1)-(j+j+1))==2015-1225))
{
cout<<i<<" "<<j<<endl;
}
}
}
}
Insert picture description here

python practice :

for i in range(1,47):
for j in range(i+2,49):
if(i*(i+1)-(i+i+1)+j*(j+1)-(j+j+1)==2015-1225):
print(i,j)

Ideas :

1+2+3+ ... + 49 = 12251+2+3+...+49=1225

1+2+3+...+10∗11+12+...+27∗28+29+...+49=2015

The sum of the remaining items remains unchanged , Enumerating the sum of multiplication products equals to the difference between the answers .

Four ,END

If there is a better solution and its ideas , Welcome to discuss .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved