7-23 冠军魔术
2018年FISM(世界魔术大会)近景总冠军简纶廷的表演中有一个情节:以桌面上一根带子为界,当他将纸牌从带子的一边推到另一边时,纸牌会变成硬币;把硬币推回另一边会变成纸牌。
这里我们假设纸牌会变成等量的硬币,而硬币变成纸牌时,纸牌的数量会加倍。那么给定纸牌的初始数量,当他来回推了 N 次(来/回各算一次)后,手里拿的是纸牌还是硬币?数量是多少?
输入格式:
输入在一行里给出两个正整数,分别是纸牌的初始数量和魔术师推送的次数。这里假设初始状态下魔术师手里全是纸牌。
输出格式:
如果最后魔术师手里是纸牌,输出 0 和纸牌数量;如果是硬币,则输出 1 和硬币数量。数字间须有 1 个空格。题目保证结果数值不超出整型范围(即 231−1)。
输入样例 1:
3 7
结尾无空行
输出样例 1:
1 24
结尾无空行
输入样例 2:
8 4
结尾无空行
输出样例 2:
0 32
结尾无空行
#include<stdio.h>
#include<math.h>
int main()
{
int a,N,x,sum;
scanf("%d %d",&a,&N);
if(N%2 == 0)
{
x=0;
}
if(N%2==1)
{
x=1;
}
sum=a*pow(2,N/2);
printf("%d %d",x,sum);
return 0;
}
Sum Problem
Problem Description
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
Input
The input will consist of a series of integers n, one integer per line.
Output
For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.
Sample Input
1 100
Sample Output
1 5050
#include<stdio.h>
int sum(int n)
{
return (int)((1.0+n)*n/2+0.5);
}
int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
printf("%d\n\n", sum(n));
}
return 0;
}