1069. The Black Hole of Numbers

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in this manner we will soon end up at the number 6174 -- the "black hole" of 4-digit numbers. This number is named Kaprekar Constant.

For example, start from 6767, we'll get:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...

Given any 4-digit number, you are supposed to illustrate the way it gets into the black hole.

Input Specification:

Each input file contains one test case which gives a positive integer N in the range (0, 10000).

Output Specification:

If all the 4 digits of N are the same, print in one line the equation "N - N = 0000". Else print each step of calculation in a line until 6174 comes out as the difference. All the numbers must be printed as 4-digit numbers.


这题跟B19是一样的

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int Sort1(int a1,int a2,int a3,int a4);
int Sort2(int a1,int a2,int a3,int a4);
int main ()
{
    int a1=0,a2=0,a3=0,a4=0,i=0;
    int num;
    scanf("%d",&num);
    a1=num/1000;
        a2=(num/100)%10;
        a3=(num%100)/10;
        a4=num%10;
    
    if(a1==a2 && a2==a3 && a3==a4)
    {
        printf("%d%d%d%d - %d%d%d%d = 0000\n",a1,a1,a1,a1,a1,a1,a1,a1);
        return 0;
        }
    int result=0,first=0,second=0;
    while(1)
    {
        first=Sort2(a1,a2,a3,a4);
        second=Sort1(a1,a2,a3,a4);
        result=first-second;
        a1=result/1000;
        a2=(result/100)%10;
        a3=(result%100)/10;
        a4=result%10;
        printf("%04d - %04d = %04d\n",first,second,result);
        if(result==6174||result==0) break;
        }
    
    system("pause");
    return 0;
    }
    
int Sort1(int a1,int a2,int a3,int a4)
{
     int a[4]={a1,a2,a3,a4};
     sort(a,a+4);
     return (a[0]*1000+a[1]*100+a[2]*10+a[3]);
     }
bool cmp(int a,int b)
{
     return a>b;
     }
int Sort2(int a1,int a2,int a3,int a4)
{
     int a[4]={a1,a2,a3,a4};
     sort(a,a+4,cmp);
     return (a[0]*1000+a[1]*100+a[2]*10+a[3]);
     }


To plot C versus S for 0 ≤ S ≤ 200, we can use the following MatLab code: ```matlab E = 100; % Exercise price T = 1; % Expiry date r = 0.05; % Risk-free interest rate sigma = 0.3; % Volatility S = linspace(0, 200, 1000); C = zeros(size(S)); for i = 1:numel(S) C(i) = EuropeanCall(S(i), E, r, sigma, T); end plot(S, C); xlabel('S'); ylabel('C'); title('European Call Option Price'); ``` This code first defines the parameters of the option, and then generates a range of values for S between 0 and 200 using the `linspace` function. For each value of S, the code calculates the option price using the `EuropeanCall` function (which can be obtained from various sources). Finally, the code plots the option price as a function of S using the `plot` function. To plot the value of C(S, t) at different values of t between t = 0 and t = T, we can modify the above code as follows: ```matlab E = 100; % Exercise price T = 1; % Expiry date r = 0.05; % Risk-free interest rate sigma = 0.3; % Volatility S = linspace(0, 200, 1000); t = linspace(0, T, 1000); C = zeros(numel(S), numel(t)); for i = 1:numel(S) for j = 1:numel(t) C(i, j) = EuropeanCall(S(i), E, r, sigma, T - t(j)); end end surf(S, t, C); xlabel('S'); ylabel('t'); zlabel('C'); title('European Call Option Price'); ``` This code generates a grid of values for S and t using `linspace`, and then calculates the option price for each combination of S and t using a nested loop. The option price is stored in a matrix `C`, and is plotted as a surface using the `surf` function. As t approaches T, the option price converges to the intrinsic value of the option, which is max(S - E, 0) for a call option. This is because as the expiry date approaches, the option has less time to move in the money, and its time value decreases.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值