簡單寫一個程式來算看看吧!
也提供大家參考~
🍼 泡奶溫度計算器
計算中...
Python.Java.C.C++.Android 閱讀筆記及心得
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n, k, charge, actual = 0;
scanf("%d %d\n", &n, &k);
for(int i = 0; i < n; i++){
int c;
scanf("%d ", &c);
if(i != k){
actual += c;
}
}
actual /= 2;
scanf("%d", &charge);
if(charge > actual)
printf("%d", charge - actual);
else
printf("Bon Appetit");
return 0;
}
6 7 3
.......
...O...
....O..
.......
OO.....
OO.....
範例輸出:OOO.OOO
OO...OO
OOO...O
..OO.OO
...OOOO
...OOOO
/*
Solve by Desolve Lin, 2016/09/15
Please help yourself take it for reference,
but kindly have a link to my blog if you use it at other website,
thanks a lot!
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void printAns(int r, int c, char grid[][201]){
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
printf("%c", grid[i][j]);
}
printf("\n");
}
}
void printFull(int row, int column){
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
printf("O");
}
printf("\n");
}
}
// t3 result
void expAndRev(int row, int column, char grid[][201]){
// Expand to find detonate area
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
if(grid[i][j] == 'O'){
// Those who are detonated areas are not real bomb and can't be expanded again.
if(i - 1 >= 0)
if(grid[i-1][j] == '.')
grid[i-1][j] = 'E';
if(j - 1 >= 0)
if(grid[i][j-1] == '.')
grid[i][j-1] = 'E';
if(i + 1 < row)
if(grid[i+1][j] == '.')
grid[i+1][j] = 'E';
if(j + 1 < column)
if(grid[i][j+1] == '.')
grid[i][j+1] = 'E';
}
}
}
// Reverse to find t3 bomb state
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
if(grid[i][j] != '.'){
grid[i][j] = '.';
}else{
grid[i][j] = 'O';
}
}
}
}
int main() {
int row, column, n;
scanf("%d %d %d\n", &row, &column, &n);
char grid[201][201];
for(int i = 0; i < row; i++){
char tmp;
for(int j = 0; j < column; j++){
scanf("%c", &grid[i][j]);
}
scanf("%c", &tmp); // remove linefeed
}
if(n == 1)
printAns(row, column, grid);
else if(n % 2 == 0)
printFull(row, column);
else{
// t3 state => expand and reverse once : t3, 7, 11, 15
// t5 state => expand and reverse twice: t5, 9, 13, 17
expAndRev(row, column, grid);
if(n % 4 == 1)
expAndRev(row, column, grid);
printAns(row, column, grid);
}
return 0;
}
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int n;
scanf("%d",&n);
int *A = malloc(sizeof(int) * n);
for(int i = 0; i < n; i++){
scanf("%d",&A[i]);
}
int d = -1;
for(int i = 0; i < n-1; i++)
for(int j = i + 1; j < n; j++){
if(A[i] == A[j])
if(d < 0) d = j - i;
else if(j - i < d) d = j - i;
}
printf("%d", d);
return 0;
}
3
2 1
3 0
3 2
範例輸出:2 1
1 2 3
-1
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
// swap: make element i, j of the array arr[] swap with each other.
void swap(int *arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/* permute: calculate absolute permutation of k, if NOT possible then return -1.
/ case: k==0
Array itself is the answer, no need to swap anything.
case: other
Suppose there's an absolute permutation of the array, then element i must swap with element i+k,
then the difference will be -k, +k.
Since then, a section will be 2*k.
Ex: 0, 1, ..., k-1 -> k+0, k+1, ... k+k-1, and next round should start from 0+2k.
We could start from i=0, each time adding 2*k to find if there's a pair to swap,
that is, from 0, k -> 2k, 3k -> 4k, 5k ...(if still < n)
(Notice that if we find a single num that can't be paired, we have to return -1.)
Then i=1, i=2, i=3 ... ...
And we could get the array done with absolute permutation of k.
*/
int permute(int *arr, int k, int n){
for(int i = 0; i < k; i++){
for(int j = i; j < n; j += k*2){
if(j + k >= n)
return -1;
else
swap(arr, j, j+k);
}
}
return 0;
}
int main(){
int t;
scanf("%d",&t);
for(int a0 = 0; a0 < t; a0++){
int n, k;
scanf("%d %d",&n,&k);
int arr[n];
for(int i = 0; i < n; i++)
arr[i] = i + 1;
if(k == 0){
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
}else{
if(permute(arr, k, n) == -1)
printf("-1");
else
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
}
printf("\n");
}
return 0;
}
5 3
4 2 6 1 10
輸出4
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n, k;
scanf("%d %d\n", &n, &k);
int t[n];
for(int i=0; i<n; i++)
scanf("%d ", &t[i]);
int special_num = 0, page = 1;
for(int chap=1; chap<=n; chap++)
{
for(int pb_cnt=1; pb_cnt<=t[chap-1]; pb_cnt++)
{
if(pb_cnt == page) special_num++;
if(pb_cnt % k == 0 && pb_cnt < t[chap-1]) page++;
}
page++;
}
printf("%d", special_num);
return 0;
}
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
char* s = (char *)malloc(512000 * sizeof(char));
scanf("%s",s);
long n;
scanf("%ld",&n);
if(strlen(s) == 1)
{
if(s[0] == 'a')
printf("%ld", n);
else
printf("0");
}else {
int leng = strlen(s);
long quotient = n / leng, remainder = n % leng;
int a_total = 0, a_remain = 0, i = 0;
while(i < remainder){
if(s[i] == 'a'){
a_total++;
a_remain++;
}
i++;
}
while(i < leng){
if(s[i] == 'a'){
a_total++;
}
i++;
}
printf("%ld", quotient * a_total + a_remain);
}
return 0;
}
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
long long int t;
scanf("%lli",&t);
long long int n = 1;
long long int cycle = 3;
while(t > cycle){
t -= cycle;
n++;
cycle *= (long long int) 2;
}
printf("%lli", (long long int)cycle - t + 1);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int t;
scanf("%d\n", &t);
for(int i=0; i<t; i++)
{
int n, m, s, result;
scanf("%d %d %d\n", &n, &m, &s);
result = (m % n) + (s-1) % n;
if (result > n) result %= n;
if (result == 0) result = n;
printf("%d\n", result);
}
return 0;
}
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int compare(const void *arg1, const void *arg2){
return (*(int *)arg1 - *(int *)arg2);
}
int main(){
int n;
int m;
scanf("%d %d",&n,&m);
if(n == m)
printf("0");
else{
int *c = malloc(sizeof(int) * m);
int max = 0, temp = 0;
for(int i = 0; i < m; i++){
scanf("%d",&c[i]);
}
qsort((void *)c, m, sizeof(int), compare);
max = c[0];
for(int i = 1; i < m; i++){
temp = (c[i]-c[i-1])/2;
max = (max > temp)? max : temp;
}
temp = n-1 - c[m-1];
max = (max > temp)? max : temp;
printf("%d", max);
}
return 0;
}
2
5
2 1 5 3 4
5
2 5 1 3 4
輸出3
Too chaotic
1 2 3 -> 3 1 2
編號3的人賄賂了2次,#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int T;
scanf("%d",&T);
for(int a0 = 0; a0 < T; a0++){
int n, bribe_total = 0, chao = 0;
scanf("%d",&n);
int *q = malloc(sizeof(int) * n);
int q_now[n], pos_now[n];
for(int q_i = 0; q_i < n; q_i++){
scanf("%d",&q[q_i]);
q_now[q_i] = q_i + 1;
pos_now[q_i] = q_i;
}
for(int i = 0; i < n; i++){
if(pos_now[q[i]-1] - i > 2)
{
chao = 1;
break;
}
}
if(chao){
printf("Too chaotic\n");
}else{
for(int i = 0; i < n-1; i++)
{
int temp = q[i], diff = pos_now[q[i]-1] - i;
if( diff == 2 )
{
bribe_total +=2;
pos_now[q_now[i+1]-1]++;
pos_now[q_now[i]-1]++;
pos_now[q_now[i+2]-1] -= 2;
q_now[i+2] = q_now[i+1];
q_now[i+1] = q_now[i];
q_now[i] = temp;
} else if( diff == 1 ){
bribe_total++;
pos_now[q_now[i]-1]++;
pos_now[q_now[i+1]-1]--;
q_now[i+1] = q_now[i];
q_now[i] = temp;
}
}
printf("%d\n", bribe_total);
}
}
return 0;
}