程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> hdu5373 The shortest problem(模擬,數學)

hdu5373 The shortest problem(模擬,數學)

編輯:關於C++

題目:

 

The shortest problem

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1479 Accepted Submission(s): 682



Problem Description In this problem, we should solve an interesting game. At first, we have an integer n, then we begin to make some funny change. We sum up every digit of the n, then insert it to the tail of the number n, then let the new number be the interesting number n. repeat it for t times. When n=123 and t=3 then we can get 123->1236->123612->12361215.
Input Multiple input.
We have two integer n (0<=n<=104 ) , t(0<=t<=105) in each row.
When n==-1 and t==-1 mean the end of input.

Output For each input , if the final number are divisible by 11, output “Yes”, else output ”No”. without quote.
Sample Input
35 2
35 1
-1 -1

Sample Output
Case #1: Yes
Case #2: No 

Author UESTC

 

 

題意:給一個整數n,每次操作是把n的每位的數字加起來的和連接到n的後面,構成一個n,問經過t次操作後結果能不能被11整除。

 

思路:能夠被11整除的數的特點是奇數位的和減去偶數位的和的差能被11整除,可以枚舉每次操作,每次操作就把前一個數字各位的和作為當前處理的數,把它的每一位求出來,然後更新奇數位的和和偶數為的和,最後看兩者的差能不能被11整除。

 

代碼:

 

#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf(%d, &(X))
#define RII(X, Y) scanf(%d%d, &(X), &(Y))
#define RIII(X, Y, Z) scanf(%d%d%d, &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf(%d, &X)
#define DRII(X, Y) int X, Y; scanf(%d%d, &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf(%d%d%d, &X, &Y, &Z)
#define OI(X) printf(%d,X);
#define RS(X) scanf(%s, (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen(in.txt,r,stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector VI;
typedef vector VS;
typedef vector VD;
typedef long long LL;
typedef pair PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判斷正負
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的數
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/

void work(int n,long long int &sum,int *a,int &len)
{if(n==0)
{
    //sum=0;
    len=1;
    a[0]=0;
    return;
}
while(n)
{
    int t=n%10;
    n=n/10;
    sum+=t;
    a[len++]=t;
}

}

long long int ji;
long long int ou;
int a[100000+5];
int len;
int main()
{int n,t;
int T=0;
while(RII(n,t)!=EOF)
{
    if(n==-1&&t==-1)
        break;
    printf(Case #%d: ,++T);
    int tot=0;
    ji=ou=0;
    len=0;
    MS0(a);
    long long int sum=0;
    int temp=n;
    work(n,sum,a,len);
    tot+=len;
    int flag=1;
    for(int i=len-1;i>=0;i--)
    {if(flag)
    ji+=a[i];
    else
    ou+=a[i];
    flag=!flag;

    }
    for(int i=0;i=0;i--)
    {if(flag)
    ji+=a[i];
    else
    ou+=a[i];
    flag=!flag;

    }

    }
    if((ji-ou)%11==0||(ou-ji)%11==0)
    {
        printf(Yes
);
    }
    else
    {
        printf(No
);
    }
}



        return 0;
}



 

 

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