Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

submit #145

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions src/apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,57 @@
int n;
int k;
int A[100000];
int mid=0; //global変数
int j=0;
int bag_num=0;

int bag_judge(int m){
for(j=0;j<n;j++)
{
scanf("%d", &A[j]);
bag_num += (A[j]+ m - 1) / m; //mがリンゴを何個入れるか
//printf("%d\n",A[j]);
}
if(bag_num <= k)
{
return 1;
}
else
{
return 0;
}
}

int main(){
int i, lb, ub;

int main(void){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コンパイルエラー出てますよ.

int i,max,ub,lb;
scanf("%d%d", &n, &k);
for(i = 0; i < n; i++){

for(i = 0; i < n; i++)
{
scanf("%d", &A[i]);
if(max < A[i])
{
max = A[i];
ub = A[i];
}
}



lb = 0; //maxは上で定義してある


while(ub - lb > 1)
{
mid = (ub + lb)/2;
if(bag_judge(mid) == 1)
{
ub = mid;
}
else if(bag_judge(mid) == 0)
{
lb = mid;
}

printf("%d\n",lb);
return 0;
}
34 changes: 28 additions & 6 deletions src/array.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
#include <stdio.h>
#include <stdio.h> //配列の二分探索

int n;
int k;
int A[100000];

int mid=0; //global変数

int main(){
int i, lb, ub;
int i,ub, lb;
scanf("%d%d", &n, &k);

for(i = 0; i < n; i++){
scanf("%d", &A[i]);
}

lb=0,ub=n;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

初期値が不適切です.


if(A[n-1]<k)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この条件分岐も資料を参考に無くせます.

{
mid=n;
}



while(ub - lb > 1)
{
mid = (ub + lb)/2;
if(A[mid]>k)
{
ub = mid;
//printf("%d\n",mid);
}
else
{
lb = mid;
}
//printf("%d\n",mid);
}
printf("%d\n",ub);
//printf("%d,%d",lb,ub);
return 0;
}
}
66 changes: 61 additions & 5 deletions src/spear.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,71 @@
int n;
int k;
int A[100000];
int mid=0; //global変数
int j=0;
int tree_num = 0;
int tmp=0;

int tree_judge(int m){
for(j=0;j<n;j++)
{
if(A[j] >= m)
{
tree_num += A[j] / m; //切り上げないように
}
}
tmp=tree_num;
tree_num=0;

//printf("mid=%d,tmp=%d\n",m,tmp);
if(tmp < k)
{
return 1;
}
else
{
return 0;
}
//printf("tree_num=%d\n",tree_num);
}

int main(){
int i, lb, ub;

int main(void){
int i,max,ub,lb,sum_length = 0;
scanf("%d%d", &n, &k);
for(i = 0; i < n; i++){

for(i = 0; i < n; i++)
{
scanf("%d", &A[i]);
sum_length = sum_length + A[i];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最悪桁あふれします.

//printf("%d\n",A[i]);

}



ub = sum_length / k;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

初期値が不適切です.

lb = 0;


while(ub - lb > 1)
{
mid = (ub + lb) / 2;
//printf("mid=%d\n",mid);
if(tree_judge(mid) == 1)
{
ub = mid;
//printf("lb=%d,ub=%d\n",lb,ub);
}
if(tree_judge(mid) == 0)
{
lb = mid;
// printf("lb=%d,ub=%d\n",lb,ub);
}
if(k==0)
{
lb = 0;
}
}
//printf("%d\n",lb);
printf("%d",lb);
return 0;
}