[URI Online Judge] – 1095 – Sequence IJ 1

Do a program that print the sequence like following example.


This problem has no input.


Print the sequence like example below.

Sample Input Sample Output
I=1 J=60
I=4 J=55
I=7 J=50

I=? J=0

My solution:

#include <cstdio>
int main(){
 int i = 1, j = 60;
 while (j != 0){
 printf("I=%d J=%d\n", i, j);
 i += 3;
 j -= 5;
 printf("I=%d J=%d\n", i, j);
 return 0;

[URI Online Judge] – 1094 – Experiments

Maria has just started as graduate student in a medical school and she’s needing your help to organize a laboratory experiment which she is responsible about. She wants to know, at the end of the year, how many animals were used in this laboratory and the percentage of each type of animal is used at all.

This laboratory uses in particular three types of animals: frogs, rats and rabbits. To obtain this information, it knows exactly the number of experiments that were performed, the type and quantity of each animal is used in each experiment.


The first line of input contains an integer N indicating the number of test cases that follows. Each test case contains an integer Amount (1 ≤ Amount ≤ 15) which represents the amount of animal used and a character Type (‘C‘, ‘R‘ or ‘S‘), indicating the type of animal:
C: Coelho (rabbit in portuguese)
R: Rato (rat  in portuguese)
S: Sapo (frog in portuguese)


Print the total of animals used, the total of each type of animal and the percentual of each one in relation of the total of animals used. The percentual must be printed with 2 digits after the decimal point.

Sample Input Sample Output
10 C
6 R
15 S
5 C
14 R
9 C
6 R
8 S
5 C
14 R
Total: 92 cobaias
Total de coelhos: 29
Total de ratos: 40
Total de sapos: 23
Percentual de coelhos: 31.52 %
Percentual de ratos: 43.48 %
Percentual de sapos: 25.00 %

My solution:

#include <cstdio>
int main(){
 char t;
 int n, c = 0, r = 0, s = 0, soma = 0, e;
scanf("%d", &n);
 scanf("%d %c", &e, &t);
 soma += e;
 if (t == 'C'){
 c += e;
 } else if (t == 'R'){
 r += e;
 } else if (t == 'S'){
 s += e;
printf("Total: %d cobaias\n", soma);
 printf("Total de coelhos: %d\n", c);
 printf("Total de ratos: %d\n", r);
 printf("Total de sapos: %d\n", s);
 printf("Percentual de coelhos: %.2f %\n", (float)c * 100.0 / soma);
 printf("Percentual de ratos: %.2f %\n", (float)r * 100.0 / soma);
 printf("Percentual de sapos: %.2f %\n", (float)s * 100.0 / soma);

The fastfood chain McBurger owns several restaurants along a highway. Recently, they have decided to build several depots along the highway, each one located at a restaurent and supplying several of the restaurants with the needed ingredients. Naturally, these depots should be placed so that the average distance between a restaurant and its assigned depot is minimized. You are to write a program that computes the optimal positions and assignments of the depots.
To make this more precise, the management of McBurger has issued the following specification: You will be given the positions of nrestaurants along the highway as n integers $d_1 < d_2 < \dots < d_n$ (these are the distances measured from the company’s headquarter, which happens to be at the same highway). Furthermore, a number $k (k \leŸ n)$ will be given, the number of depots to be built.

The k depots will be built at the locations of k different restaurants. Each restaurant will be assigned to the closest depot, from which it will then receive its supplies. To minimize shipping costs, the total distance sum, defined as

\begin{displaymath}\sum_{i=1}^n \mid d_i - (\mbox{position of depot serving restaurant }i) \mid

must be as small as possible.

Write a program that computes the positions of the k depots, such that the total distance sum is minimized.


The input file contains several descriptions of fastfood chains. Each description starts with a line containing the two integers n and k. n and kwill satisfy $1 \leŸ n
\leŸ 200$, $1 \leŸ k Ÿ\le 30$, $k \le n$. Following this will n lines containing one integer each, giving the positions di of the restaurants, ordered increasingly.

The input file will end with a case starting with n = k = 0. This case should not be processed.


For each chain, first output the number of the chain. Then output an optimal placement of the depots as follows: for each depot output a line containing its position and the range of restaurants it serves. If there is more than one optimal solution, output any of them. After the depot descriptions output a line containing the total distance sum, as defined in the problem text.
Output a blank line after each test case.

Sample Input 

6 3
0 0

Sample Output 

Chain 1
Depot 1 at restaurant 2 serves restaurants 1 to 3
Depot 2 at restaurant 4 serves restaurants 4 to 5
Depot 3 at restaurant 6 serves restaurant 6
Total distance sum = 8
#define mem(name,value) memset(name,value,sizeof(name))
#define FOR(i,n) for(int i=1;i<=n;i++)
using namespace std;
const int maxn = 200+10;
const int maxk = 30+10;
const int inf = 0x3f3f3f3f;
int d[maxk][maxn],des[maxn],a1[maxk];
typedef pair<int,int>pii;
pii p[maxk][maxn],p1[maxk];
int dp(int cnt,int n){
    if(d[cnt][n]!=-1) return d[cnt][n];
    int &ans = d[cnt][n];
    if(cnt==0) return ans = (n ? inf : 0);
    ans = inf;
    for(int i=cnt;i<=n;i++){
        int len = (n-i)/2, v = i+len;
        int s = 0;
        for(int j=v+1;j<=n;j++) s += des[j] - des[v];
        for(int j=v-1;j>=i;j--) s += des[v] - des[j];
        int tmp = dp(cnt-1,i-1) + s;
        if(tmp < ans){
            ans = tmp;
            p[cnt][n] = make_pair(i,v);
    return ans;
void print_ans(int cnt,int n){
    if(cnt==0) return ;
    a1[cnt] = p[cnt][n].second;
    p1[cnt] = make_pair(p[cnt][n].first,n);
int main(){
    int n,k,kase=0;
    while(~scanf("%d%d",&n,&k) && n){
        printf("Chain %d\n",++kase);
        des[0] = 0;
        for(int i=1;i<=n;i++){
        int solve = dp(k,n);
        for(int i=1;i<=k;i++){
            printf("Depot %d at restaurant %d serves ",i,a1[i]);
            if(p1[i].first==p1[i].second) printf("restaurant %d\n",p1[i].first);
            else printf("restaurants %d to %d\n",p1[i].first,p1[i].second);
        printf("Total distance sum = %d\n\n",solve);
    return 0;

How to drag your form when “formBorderStyle” was set “none”?

just use 3-M …

Add 3 events MouseDown – MouseUp & MouseMove.

declare :

Boolean flag;
int x, y;

MouseDown function:

flag = true;
x = e.X;
y = e.Y;

MouseUp function:

flag = false;

MouseMove function:

if (flag)
this.SetDesktopLocation(Cursor.Position.X – x, Cursor.Position.Y – y);

or another solution:

using System.Runtime.InteropServices;

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

private static extern int SendMessage(IntPtr hWnd,
int Msg, int wParam, int lParam);
private static extern bool ReleaseCapture();

public Form1()

private void menuStrip1_MouseDown(object sender, MouseEventArgs e)
if (e.Button == MouseButtons.Left)

Factorial Problems 1


Factorial of an integer is defined by the following function

f(0) = 1

f(n) = f(n – 1) * n, if(n > 0)

So, factorial of 5 is 120. But in different bases, the factorial may be different. For example, factorial of 5 in base 8 is 170.

In this problem, you have to find the number of digit(s) of the factorial of an integer in a certain base.


Input starts with an integer T (≤ 50000), denoting the number of test cases.

Each case begins with two integers n (0 ≤ n ≤ 106) and base (2 ≤ base ≤ 1000). Both of these integers will be given in decimal.


For each case of input you have to print the case number and the digit(s) of factorial n in the given base.

Sample Input:


5 10

8 10

22 3

1000000 2

0 100

Sample Output:

Case 1: 3

Case 2: 5

Case 3: 45

Case 4: 18488885

Case 5: 1


my solution:

#include <stdio.h>
#include <iostream>
#include <math.h>

using namespace std;

double a[1000005] = {0};
void cal () {
    for (int i = 1; i <= 1000005; i ++) {
        a[i] = a[i - 1] + log10(1.0 * i);
int main () {
    int cas;
    int n, base;
    scanf("%d", &cas);
    for (int t = 1; t <= cas; t ++) {
        scanf("%d%d", &n, &base);
        printf("Case %d: %d\n", t, (int)(a[n] / log10(base * 1.0) + 1));
    return 0;

[URI Online Judge] – 1087 – Queen

The game of Chess has several pieces with curious movements. One of them is the Queen, which can move any number of squares in any direction: in the same line, in the same column or in any of the diagonals, as illustrated by the figure below (black dots represent positions the queen may reach in one move):

The great Chess Master Kary Gasparov invented a new type of chess problem: given the position of a queen in an empty standard chess board (that is, an 8 x 8 board) how many moves are needed so that she reaches another given square in the board?

Kary found the solution for some of those problems, but is having a difficult time to solve some others, and therefore he has asked that you write a program to solve this type of problem.


The input contains several test cases. The only line of each test case contains four integers X1, Y1, X2 andY2 (1 ≤ X1, Y1, X2, Y2 ≤ 8). The queen starts in the square with coordinates (X1, Y1), and must finish at the square with coordinates (X2, Y2). In the chessboard, columns are numbered from 1 to 8, from left ro right; lines are also numbered from 1 to 8, from top to bottom. The coordinates of a square in line X and columnY are (X, Y).

The end of input is indicated by a line containing four zeros, separated by spaces.


For each test case in the input your program must print a single line, containing an integer, indicating the smallest number of moves needed for the queen to reach the new position.

Sample Input Sample Output
4 4 6 2
3 5 3 5
5 5 4 3
0 0 0 0

solution here:

#include <cstdio>
#include <cmath>
using namespace std;
int main(){
 int x1, y1, x2, y2, d_x, d_y;
 while(scanf("%d %d %d %d", &x1, &y1, &x2, &y2) && x1 != 0 && y1 != 0 && x2 != 0 && y2 != 0){
 if (x1 > x2) d_x = x1 - x2;
 else d_x = x2 - x1;
if (y1 > y2) d_y = y1 - y2;
 else d_y = y2 - y1;
if (x1 == x2 && y1 == y2)
 else if (x1 == x2 || y1 == y2 || d_x == d_y)
 return 0;

Biểu diễn Danh sách kề sang Ma trận kề

// thêm -1 ở cuối mỗi dòng chỉ là thủ thuật nhỏ mà thôi, nếu muốn giữ nguyên input (không có -1) thì bạn phải chuyển dữ liệu nhập vào sang kiểu char rồi xử lý.

Cho danh sách kề như sau, biểu diễn đồ thị có 5 đỉnh (vertex)

v1 connect to 2, 3;

v2 -> 1, 3, 5

v3 -> 1, 2, 5

v4 -> 5

v5 -> 2, 3, 4

2 3 -1
1 3 5 -1
1 2 5 -1
5 -1
2 3 4 -1

…….. solution:

#include <fstream>
#include <vector>
#define MAX 1000
#define FI "DanhSachKe.INP"
#define FO "GRAPH.out"
using namespace std;
int A[MAX][MAX];//Ma tran ke
vector<vector<int>> matrix;
int N;//so dinh
ifstream fi;//doc file
ofstream fo;//xuat file
vector<int> vec;
void Input(){
 fi >> N;
 for(int i=0; i<N; i++){
 int ver;
 fi >> ver;
 if(ver != -1){
 } while (ver != -1);
void setMatrix(int x){
void Xuat(){
 // matrix.resize(N+1);
 for(int i=0; i<matrix.size(); i++){
 for(int j=0; j<matrix[i].size(); j++){
 cout << matrix[i][j] << " ";
 cout << endl;
void DSK2MTK(){
 for(int i=0; i<N; i++){
 for(int j=0; j<N; j++){
 A[i][j] = 0;
 for(int i=0; i<N; i++){
 for(int j=0; j<matrix[i].size(); j++){
 A[i][matrix[i][j]-1] = 1;
void xuatMTK(){
 for(int i=0; i<N; i++){
 for(int j=0; j<N; j++){
 cout << A[i][j] << " ";
 cout << endl;
int main(){
 // nhapDSK();
 return 0;